File: test-python-base-r-generics.R

package info (click to toggle)
r-cran-reticulate 1.41.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,088 kB
  • sloc: cpp: 5,154; python: 620; sh: 13; makefile: 2
file content (171 lines) | stat: -rw-r--r-- 6,805 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
test_that("Ops group generics dispatch correctly for Python objects", {

  numpy <- import("numpy", convert = FALSE)

  py_obj1 <- numpy$array(c(1, 2, 3))
  py_obj2 <- numpy$array(c(4, 5, 6))

  # Test arithmetic operations
  expect_equal(py_to_r(py_obj1 + py_obj2), array(c(5, 7, 9)))
  expect_equal(py_to_r(py_obj1 - py_obj2), array(c(-3, -3, -3)))
  expect_equal(py_to_r(py_obj1 * py_obj2), array(c(4, 10, 18)))
  expect_equal(py_to_r(py_obj1 / py_obj2), array(c(1/4, 2/5, 3/6)))
  expect_equal(py_to_r(py_obj1 ^ 2), array(c(1, 4, 9)))
  expect_equal(py_to_r(py_obj1 %% 2), array(c(1, 0, 1)))
  expect_equal(py_to_r(py_obj1 %/% 2), array(c(0, 1, 1)))

  # Test logical operations
  py_bool1 <- numpy$array(c(TRUE, FALSE, TRUE))
  py_bool2 <- numpy$array(c(FALSE, TRUE, FALSE))

  expect_equal(py_to_r(py_bool1 & py_bool2), array(c(FALSE, FALSE, FALSE)))
  expect_equal(py_to_r(py_bool1 | py_bool2), array(c(TRUE, TRUE, TRUE)))
  expect_equal(py_to_r(!py_bool1), array(c(FALSE, TRUE, FALSE)))

  # Test comparison operations
  expect_equal(py_to_r(py_obj1 == py_obj2), array(c(FALSE, FALSE, FALSE)))
  expect_equal(py_to_r(py_obj1 != py_obj2), array(c(TRUE, TRUE, TRUE)))
  expect_equal(py_to_r(py_obj1 < py_obj2), array(c(TRUE, TRUE, TRUE)))
  expect_equal(py_to_r(py_obj1 <= py_obj2), array(c(TRUE, TRUE, TRUE)))
  expect_equal(py_to_r(py_obj1 >= py_obj2), array(c(FALSE, FALSE, FALSE)))
  expect_equal(py_to_r(py_obj1 > py_obj2), array(c(FALSE, FALSE, FALSE)))
})


test_that("Ops group generics dispatch correctly when only one argument is a Python object", {
  numpy <- import("numpy", convert = FALSE)
  py_obj <- numpy$array(c(1, 2, 3))
  r_obj <- array(c(4, 5, 6))

  # Test arithmetic operations
  expect_equal(py_to_r(py_obj + r_obj), array(c(5, 7, 9)))
  expect_equal(py_to_r(r_obj + py_obj), array(c(5, 7, 9)))

  expect_equal(py_to_r(py_obj - r_obj), array(c(-3, -3, -3)))
  expect_equal(py_to_r(r_obj - py_obj), array(c(3, 3, 3)))

  expect_equal(py_to_r(py_obj * r_obj), array(c(4, 10, 18)))
  expect_equal(py_to_r(r_obj * py_obj), array(c(4, 10, 18)))

  expect_equal(py_to_r(py_obj / r_obj), array(c(1/4, 2/5, 3/6)))
  expect_equal(py_to_r(r_obj / py_obj), array(c(4, 5/2, 2)))

  expect_equal(py_to_r(py_obj ^ 2), array(c(1, 4, 9)))
  expect_equal(py_to_r(r_obj ^ py_obj), array(c(4, 25, 216)))

  expect_equal(py_to_r(py_obj %% 2), array(c(1, 0, 1)))
  expect_equal(py_to_r(r_obj %% py_obj), array(c(0, 1, 0)))

  expect_equal(py_to_r(py_obj %/% 2), array(c(0, 1, 1)))
  expect_equal(py_to_r(r_obj %/% py_obj), array(c(4, 2, 2)))

  # Test logical operations
  py_bool <- numpy$array(array(c(TRUE, FALSE, TRUE)))
  r_bool <- array(c(FALSE, TRUE, FALSE))

  expect_equal(py_to_r(py_bool & r_bool), array(c(FALSE, FALSE, FALSE)))
  expect_equal(py_to_r(r_bool & py_bool), array(c(FALSE, FALSE, FALSE)))

  expect_equal(py_to_r(py_bool | r_bool), array(c(TRUE, TRUE, TRUE)))
  expect_equal(py_to_r(r_bool | py_bool), array(c(TRUE, TRUE, TRUE)))

  expect_equal(py_to_r(!py_bool), array(c(FALSE, TRUE, FALSE)))

  # Test comparison operations
  expect_equal(py_to_r(py_obj == r_obj), array(c(FALSE, FALSE, FALSE)))
  expect_equal(py_to_r(r_obj == py_obj), array(c(FALSE, FALSE, FALSE)))

  expect_equal(py_to_r(py_obj != r_obj), array(c(TRUE, TRUE, TRUE)))
  expect_equal(py_to_r(r_obj != py_obj), array(c(TRUE, TRUE, TRUE)))

  expect_equal(py_to_r(py_obj < r_obj), array(c(TRUE, TRUE, TRUE)))
  expect_equal(py_to_r(r_obj < py_obj), array(c(FALSE, FALSE, FALSE)))

  expect_equal(py_to_r(py_obj <= r_obj), array(c(TRUE, TRUE, TRUE)))
  expect_equal(py_to_r(r_obj <= py_obj), array(c(FALSE, FALSE, FALSE)))

  expect_equal(py_to_r(py_obj >= r_obj), array(c(FALSE, FALSE, FALSE)))
  expect_equal(py_to_r(r_obj >= py_obj), array(c(TRUE, TRUE, TRUE)))

  expect_equal(py_to_r(py_obj > r_obj), array(c(FALSE, FALSE, FALSE)))
  expect_equal(py_to_r(r_obj > py_obj), array(c(TRUE, TRUE, TRUE)))
})

if (getRversion() >= "4.3.0")
test_that("matrixOps group generics dispatch", {

  r_obj1 <- array(1:9, c(3, 3))
  r_obj2 <- t(r_obj1) + 10

  py_obj1 <- r_to_py(r_obj1)
  py_obj2 <- r_to_py(r_obj2)

  expect_equal(py_to_r(py_obj1 %*% py_obj2), r_obj1 %*% r_obj2)
  expect_equal(py_to_r(py_obj2 %*% py_obj1), r_obj2 %*% r_obj1)

  expect_equal(py_to_r(py_obj1 %*% r_obj2), r_obj1 %*% r_obj2)
  expect_equal(py_to_r(py_obj2 %*% r_obj1), r_obj2 %*% r_obj1)

  expect_equal(py_to_r(r_obj1 %*% py_obj2), r_obj1 %*% r_obj2)
  expect_equal(py_to_r(r_obj2 %*% py_obj1), r_obj2 %*% r_obj1)

})


test_that("[ can infer slices, multiple args", {

  x <- r_to_py(array(1:64, c(4, 4, 4)))
  py$x <- x

  expect_identical(py_eval("x[0]"), py_to_r(x[0]))
  expect_identical(py_eval("x[:, 0]"), py_to_r(x[, 0]))
  expect_identical(py_eval("x[:, :, 0]"), py_to_r(x[, , 0]))

  expect_identical(py_eval("x[:2]"), py_to_r(x[`:2`]))
  expect_identical(py_eval("x[:2]"), py_to_r(x[NULL:2]))
  expect_identical(py_eval("x[:2]"), py_to_r(x[NA:2]))

  expect_identical(py_eval("x[1:2]"), py_to_r(x[1:2]))
  expect_identical(py_eval("x[1:2]"), py_to_r(x[`1:2`]))

  expect_identical(py_eval("x[2:]"), py_to_r(x[2:NA]))
  expect_identical(py_eval("x[2:]"), py_to_r(x[`2:`]))
  expect_identical(py_eval("x[2:]"), py_to_r(x[2:NULL]))

  expect_identical(py_eval("x[1:3:2]"), py_to_r(x[1:3:2]))
  expect_identical(py_eval("x[1:3:2]"), py_to_r(x[`1:3:2`]))

  expect_identical(py_eval("x[::2]"), py_to_r(x[`::2`]))
  expect_identical(py_eval("x[::2]"), py_to_r(x[NULL:NULL:2]))
  expect_identical(py_eval("x[::2]"), py_to_r(x[NA:NA:2]))

  expect_identical(py_eval("x[:, :2]"), py_to_r(x[, `:2`]))
  expect_identical(py_eval("x[:, :2]"), py_to_r(x[, NULL:2]))
  expect_identical(py_eval("x[:, :2]"), py_to_r(x[, NA:2]))

  expect_identical(py_eval("x[:, ::2]"), py_to_r(x[, `::2`]))
  expect_identical(py_eval("x[:, ::2]"), py_to_r(x[, NULL:NULL:2]))
  expect_identical(py_eval("x[:, ::2]"), py_to_r(x[, NA:NA:2]))

  expect_identical(py_eval("x[:, :2, :]"), py_to_r(x[, `:2`]))
  expect_identical(py_eval("x[:, :2, :]"), py_to_r(x[, NULL:2]))
  expect_identical(py_eval("x[:, :2, :]"), py_to_r(x[, NA:2]))

  expect_identical(py_eval("x[:, ::2, :]"), py_to_r(x[, `::2`, ]))
  expect_identical(py_eval("x[:, ::2, :]"), py_to_r(x[, NULL:NULL:2, ]))
  expect_identical(py_eval("x[:, ::2, :]"), py_to_r(x[, NA:NA:2, ]))

  # test the test is actually comparing R arrays
  py$x <- x <- np_array(x, dtype = "float64") # https://github.com/rstudio/reticulate/issues/1473
  expect_identical(py_to_r(x[, , 0]), array(as.double(1:16), c(4, 4)))
  expect_identical(py_eval("x[:, :, 0]"), array(as.double(1:16), c(4, 4)))

  # copy `x` to make it writeable
  py_run_string("import numpy as np; x = np.array(x)")
  x <- np_array(x)

  py_run_string("x[:, 2, :] = 99")
  x[, 2, ] <- 99L
  expect_identical(py_eval("x"), py_to_r(x))

})