-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhelper-expect_equal_py.R
More file actions
59 lines (51 loc) · 1.25 KB
/
helper-expect_equal_py.R
File metadata and controls
59 lines (51 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
expect_equal_py <- function(a, b) {
requireNamespace("testthat")
requireNamespace("reticulate")
bi <- reticulate::import_builtins()
# ↓ does this always work?
testthat::expect_equal(bi$str(bi$type(a)), bi$str(bi$type(b)))
if (inherits(a, "pandas.core.frame.DataFrame")) {
pd <- reticulate::import("pandas")
testthat::expect_null(
pd$testing$assert_frame_equal(
a,
b,
check_dtype = FALSE,
check_exact = FALSE
)
)
} else if (
inherits(a, "np.ndarray") || inherits(a, "scipy.sparse.base.spmatrix")
) {
scipy <- reticulate::import("scipy")
np <- reticulate::import("numpy")
testthat::expect_equal(a$dtype, b$dtype)
testthat::expect_equal(
py_to_r_ifneedbe(a$shape),
py_to_r_ifneedbe(b$shape)
)
a_dense <-
if (scipy$sparse$issparse(a)) {
a$toarray()
} else {
a
}
b_dense <-
if (scipy$sparse$issparse(b)) {
b$toarray()
} else {
b
}
testthat::expect_null(
np$testing$assert_allclose(a_dense, b_dense)
)
}
}
py_to_r_ifneedbe <- function(x) {
if (inherits(x, "python.builtin.object")) {
requireNamespace("reticulate")
reticulate::py_to_r(x)
} else {
x
}
}