File: test-python-scipy-sparse-matrix.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 (297 lines) | stat: -rw-r--r-- 7,506 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
context("scipy sparse matrix")

library(methods)
library(Matrix)

# https://github.com/r-lib/testthat/issues/1556
if (!inherits("t", "standardGeneric"))
  setGeneric("t")

check_matrix_conversion <- function(r_matrix, python_matrix) {
  # check that the conversion to python works
  expect_true(all(py_to_r(python_matrix$toarray()) == as.matrix(r_matrix)))
  # check that the conversion to r works
  expect_true(all(py_to_r(python_matrix) == r_matrix))
  # check that S3 methods work
  expect_equal(dim(python_matrix), dim(r_matrix))
  expect_equal(length(python_matrix), length(r_matrix))
}

test_that("Conversion to scipy sparse matrix S3 methods behave with null pointers", {
  skip_on_cran()
  skip_if_no_scipy()

  N <- 1000
  x <- sparseMatrix(
    i = sample(N, N),
    j = sample(N, N),
    x = runif(N),
    dims = c(N, N))
  result <- r_to_py(x)
  temp_file <- file.path(tempdir(), "sparse_matrix.rds")
  saveRDS(result, temp_file)
  result <- readRDS(temp_file)

  # check that S3 methods behave with null pointers
  expect_true(is(result, "scipy.sparse.csc.csc_matrix") || is(result, "scipy.sparse._csc.csc_matrix"))
  expect_true(is.null(dim(result)))
  expect_true(length(result) == 0L)
  file.remove(temp_file)
})

test_that("Conversion between Matrix::dgCMatrix and Scipy sparse CSC matrix works", {
  skip_on_cran()
  skip_if_no_scipy()

  N <- 1000
  x <- sparseMatrix(
    i = sample(N, N),
    j = sample(N, N),
    x = runif(N),
    dims = c(N, N))
  result <- r_to_py(x)

  # check that we are testing the right classes
  expect_true(is(result, "scipy.sparse.csc.csc_matrix") || is(result, "scipy.sparse._csc.csc_matrix"))
  expect_true(is(py_to_r(result), "dgCMatrix"))
  check_matrix_conversion(x, result)
})

test_that("Conversion between a small Matrix::dgCMatrix and Scipy sparse CSC matrix works", {
  skip_on_cran()
  skip_if_no_scipy()

  N <- 1
  x <- sparseMatrix(
    i = sample(N, N),
    j = sample(N, N),
    x = runif(N),
    dims = c(N, N))
  result <- r_to_py(x)

  # check that we are testing the right classes
  expect_true(is(result, "scipy.sparse.csc.csc_matrix") || is(result, "scipy.sparse._csc.csc_matrix"))
  expect_true(is(py_to_r(result), "dgCMatrix"))
  check_matrix_conversion(x, result)
})

test_that("Conversion between Matrix::dgRMatrix and Scipy sparse CSR matrix works", {
  skip_on_cran()
  skip_if_no_scipy()

  N <- 1000
  x <- sparseMatrix(
    i = sample(N, N),
    j = sample(N, N),
    x = runif(N),
    dims = c(N, N))
  x <- as(x, "RsparseMatrix")
  result <- r_to_py(x)

  # check that we are testing the right classes
  expect_true(is(result, "scipy.sparse.csr.csr_matrix") || is(result, "scipy.sparse._csr.csr_matrix"))
  expect_true(is(py_to_r(result), "dgRMatrix"))
  check_matrix_conversion(x, result)
})

test_that("Conversion between a small Matrix::dgRMatrix and Scipy sparse CSR matrix works", {
  skip_on_cran()
  skip_if_no_scipy()

  N <- 1
  x <- sparseMatrix(
    i = sample(N, N),
    j = sample(N, N),
    x = runif(N),
    dims = c(N, N))
  x <- as(x, "RsparseMatrix")
  result <- r_to_py(x)

  # check that we are testing the right classes
  expect_true(is(result, "scipy.sparse.csr.csr_matrix") || is(result, "scipy.sparse._csr.csr_matrix"))
  expect_true(is(py_to_r(result), "dgRMatrix"))
  check_matrix_conversion(x, result)
})

test_that("Conversion between Matrix::dgTMatrix and Scipy sparse COO matrix works", {
  skip_on_cran()
  skip_if_no_scipy()

  N <- 1000
  x <- sparseMatrix(
    i = sample(N, N),
    j = sample(N, N),
    x = runif(N),
    dims = c(N, N))
  x <- as(x, "TsparseMatrix")
  result <- r_to_py(x)

  # check that we are testing the right classes
  expect_true(is(result, "scipy.sparse.coo.coo_matrix") || is(result, "scipy.sparse._coo.coo_matrix"))
  expect_true(is(py_to_r(result), "dgTMatrix"))
  check_matrix_conversion(x, result)
})

test_that("Conversion between a small Matrix::dgTMatrix and Scipy sparse COO matrix works", {
  skip_on_cran()
  skip_if_no_scipy()

  N <- 1
  x <- sparseMatrix(
    i = sample(N, N),
    j = sample(N, N),
    x = runif(N),
    dims = c(N, N))
  x <- as(x, "TsparseMatrix")
  result <- r_to_py(x)

  # check that we are testing the right classes
  expect_true(is(result, "scipy.sparse.coo.coo_matrix") || is(result, "scipy.sparse._coo.coo_matrix"))
  expect_true(is(py_to_r(result), "dgTMatrix"))
  check_matrix_conversion(x, result)
})

test_that("Conversion between Scipy sparse matrices without specific conversion functions works", {
  skip_on_cran()
  skip_if_no_scipy()

  N <- 1000
  x <- sparseMatrix(
    i = sample(N, N),
    j = sample(N, N),
    x = runif(N),
    dims = c(N, N))
  result <- r_to_py(x)$tolil()

  # check that we are testing the right classes
  expect_true(is(result, "scipy.sparse.lil.lil_matrix") || is(result, "scipy.sparse._lil.lil_matrix"))
  expect_true(is(py_to_r(result), "dgCMatrix"))
  check_matrix_conversion(x, result)
})

test_that("Conversion between R sparse matrices without specific conversion functions works", {
  skip_on_cran()
  skip_if_no_scipy()

  N <- 1000

  x <- sparseMatrix(
    i = sample(N, N),
    j = sample(N, N),
    x = runif(N),
    dims = c(N, N))
  # symmetrize
  x <- x + t(x)
  x <- as(x, "symmetricMatrix")
  result <- r_to_py(x)

  # check that we are testing the right classes
  expect_true(is(x, "dsCMatrix"))
  expect_true(is(result, "scipy.sparse.csc.csc_matrix") || is(result, "scipy.sparse._csc.csc_matrix"))
  check_matrix_conversion(x, result)
})

test_that("Conversion with unsorted values works in csc", {
  skip_on_cran()
  skip_if_no_scipy()

  sp <- import("scipy.sparse", convert = FALSE)

  # Test data
  indices <- c(1L, 0L, 2L, 1L, 0L)
  indptr <- c(0L, 3L, 5L)
  data <- c(2, 1, 3, 5, 4)

  # create csr matrix and try to convert
  mat_py <- sp$csc_matrix(
    tuple(
      np_array(data),
      np_array(indices),
      np_array(indptr),
      convert = FALSE
    ),
    shape = c(3L, 2L)
  )

  mat_py_to_r <- py_to_r(mat_py)

  mat_r <- Matrix::sparseMatrix(
    i = indices + 1,
    p = indptr,
    x = data,
    dims = c(3L, 2L)
  )

  expect_equal(as.matrix(mat_py_to_r), as.matrix(mat_r))
})

test_that("Conversion with unsorted values works in csr", {
  skip_on_cran()
  skip_if_no_scipy()

  sp <- import("scipy.sparse", convert = FALSE)

  # Test data
  indices <- c(1L, 0L, 2L, 1L, 0L)
  indptr <- c(0L, 3L, 5L)
  data <- c(2, 1, 3, 5, 4)

  # create csr matrix and try to convert
  mat_py <- sp$csr_matrix(
    tuple(
      np_array(data),
      np_array(indices),
      np_array(indptr),
      convert = FALSE
    ),
    shape = c(2L, 3L)
  )

  mat_py_to_r <- py_to_r(mat_py)

  mat_r <- Matrix::sparseMatrix(
    j = indices + 1,
    p = indptr,
    x = data,
    dims = c(2L, 3L)
  )

  expect_equal(as.matrix(mat_py_to_r), as.matrix(mat_r))
})


test_that("Conversion with unsorted values works in coo", {
  skip_on_cran()
  skip_if_no_scipy()

  sp <- import("scipy.sparse", convert = FALSE)

  # Test data
  row <- c(1L, 0L, 2L, 1L, 0L)
  col <- c(1L, 0L, 0L, 0L, 1L)
  data <- c(5, 1, 3, 2, 4)

  # create csr matrix and try to convert
  mat_py <- sp$coo_matrix(
    tuple(
      np_array(data),
      tuple(
        np_array(row),
        np_array(col)
      ),
      convert = FALSE
    ),
    shape = c(3L, 2L)
  )

  mat_py_to_r <- py_to_r(mat_py)

  mat_r <- as.matrix(Matrix::sparseMatrix(
    i = row + 1,
    j = col + 1,
    x = data,
    dims = c(3L, 2L)
  ))
  dimnames(mat_r) <- NULL
  expect_equal(as.matrix(mat_py_to_r), mat_r)
})