-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtest_scipy2r.R
78 lines (64 loc) · 2.68 KB
/
test_scipy2r.R
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
## py2r.R: Conversion of SciPy sparse matrix to R
##
## Copyright (C) 2017 - 2022 Binxiang Ni and Dirk Eddelbuettel
##
## This file is part of RcppArmadillo.
##
## RcppArmadillo is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 2 of the License, or
## (at your option) any later version.
##
## RcppArmadillo is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with RcppArmadillo. If not, see <http://www.gnu.org/licenses/>.
## Reference: https://docs.scipy.org/doc/scipy-0.19.1/reference/sparse.html
#exit_file("Skip this test for now.")
## It now (Apr 2020) appears to fail on 32-bit Windows
.onWindows <- .Platform$OS.type == "windows"
.is32bit <- .Platform$r_arch == "i386"
#if (.onWindows && .is32bit) exit_file("Do not bother on 32-bit Windows")
if (.onWindows) exit_file("Do not bother on Windows")
if (!requireNamespace("Matrix", quietly=TRUE)) exit_file("Package Matrix missing")
if (!requireNamespace("reticulate", quietly=TRUE)) exit_file("Package reticulate missing")
if (!packageVersion("reticulate") >= package_version("1.14")) exit_file("SciPy not needed on newer reticulate")
suppressMessages({
library(Matrix)
library(reticulate)
})
## SciPy implies NumPy too
if (! py_module_available("scipy")) exit_file("Module scipy missing")
np <- import("numpy")
mat <- np$array(list(list(1, 0, 4), list(0, 0, 5), list(2, 3, 6)))
sp <- import("scipy.sparse")
mtxt <- c("1 0 4",
"0 0 5",
"2 3 6")
M <- as.matrix(read.table(text=mtxt))
dimnames(M) <- NULL
## Since 'reticulate' automatically converts CSC matrix to dgCMatrix,
## no need to convert it in RcppArmadillo
#test.csc2dgc <- function() {
csc <- sp$csc_matrix(mat)
dgC <- methods::as(M, "CsparseMatrix")
expect_equal(dgC, csc, info="csc2dgc")
#test.coo2dgt <- function() {
coo <- sp$coo_matrix(mat)
dgT <- new("dgTMatrix",
i = c(0L, 0L, 1L, 2L, 2L, 2L),
j = c(0L, 2L, 2L, 0L, 1L, 2L),
x = c(1, 4, 5, 2, 3, 6),
Dim = c(3L, 3L))
expect_equal(dgT, coo, info="coo2dgt") #RcppArmadillo:::.SciPy2R(coo))
#test.csr2dgr <- function() {
csr <- sp$csr_matrix(mat)
dgR <- methods::as(M, "RsparseMatrix")
expect_equal(dgR, csr, info="csr2dgr") #RcppArmadillo:::.SciPy2R(csr))
#test.other <- function() {
#bsr <- sp$bsr_matrix(list(3, 4))
#expect_error(RcppArmadillo:::.SciPy2R(bsr))
#expect_error(sp$bsr_matrix(list(3, 4)))