Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert SciPy sparse matrix to R #158

Merged
merged 7 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ before_install:
- ./run.sh bootstrap

install:
- ./run.sh install_aptget r-cran-rcpp r-cran-runit r-cran-matrix r-cran-pkgkitten
- ./run.sh install_aptget r-cran-rcpp r-cran-runit r-cran-matrix r-cran-pkgkitten r-cran-jsonlite python-scipy
- ./run.sh install_r reticulate

script:
- ./run.sh run_tests
Expand Down
12 changes: 12 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
2017-08-07 Dirk Eddelbuettel <edd@debian.org>

* inst/unitTests/runit.scipy2r.R: Whitespace edits
* R/SciPy2R.R: Idem; also renamed from py2R, removed one require()

* .travis.yml (install): Also install reticulate and (r-cran-)jsonlite

2017-08-07 Binxiang Ni <binxiangni@gmail.com>

* R/py2r.R: Python to R conversion
* inst/unitTests/runit.scipy2r.R: Unit tests

2017-08-06 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Version, Date): Rolled to fictional pre-release
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Description: 'Armadillo' is a templated C++ linear algebra library (by Conrad
License: GPL (>= 2)
LazyLoad: yes
LinkingTo: Rcpp
Imports: Rcpp (>= 0.11.0), stats, utils
Imports: Rcpp (>= 0.11.0), stats, utils, methods, reticulate
Suggests: RUnit, Matrix, pkgKitten
URL: http://dirk.eddelbuettel.com/code/rcpp.armadillo.html
BugReports: https://github.com/RcppCore/RcppArmadillo/issues
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
useDynLib("RcppArmadillo", .registration=TRUE)

import("Rcpp")
import("reticulate")
importFrom("stats", "coef", "fitted", "model.frame", "model.matrix", "model.response", "printCoefmat", "pt")
importFrom("utils", "packageDescription", "package.skeleton")
importFrom("methods", "new")

export("fastLmPure",
"fastLm",
Expand Down
40 changes: 40 additions & 0 deletions R/SciPy2R.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## .sciPy2R.R: Conversion of SciPy sparse matrix to R
##
## Copyright (C) 2017 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/>.

.SciPy2R <- function(spmat) {
type <- spmat$getformat()
shape <- unlist(spmat$shape)
data <- as.vector(py_get_attr(spmat, "data"))
if (type == "csc") {
indices <- as.vector(py_get_attr(spmat, "indices"))
indptr <- as.vector(py_get_attr(spmat, "indptr"))
res <- new("dgCMatrix", i = indices, p = indptr, x = data, Dim = shape)
} else if (type == "coo") {
row <- as.vector(py_get_attr(spmat, "row"))
col <- as.vector(py_get_attr(spmat, "col"))
res <- new("dgTMatrix", i = row, j = col, x = data, Dim = shape)
} else if (type == "csr") {
indices <- as.vector(py_get_attr(spmat, "indices"))
indptr <- as.vector(py_get_attr(spmat, "indptr"))
res <- new("dgRMatrix", j = indices, p = indptr, x = data, Dim = shape)
} else {
stop("Only CSC, COO and CSR matrices from SciPy are supported.")
}
return(res)
}
59 changes: 59 additions & 0 deletions inst/unitTests/runit.scipy2r.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## py2r.R: Conversion of SciPy sparse matrix to R
### Copyright (C) 2017 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

suppressMessages(require(Matrix))
suppressMessages(require(reticulate))

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

test.csc2dgc <- function() {
csc <- sp$csc_matrix(mat)
dgC <- methods::as(M, "dgCMatrix")
checkEquals(dgC, RcppArmadillo:::.SciPy2R(csc), msg="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))
checkEquals(dgT, RcppArmadillo:::.SciPy2R(coo), msg="coo2dgt")
}

test.csr2dgr <- function() {
csr <- sp$csr_matrix(mat)
dgR <- methods::as(M, "dgRMatrix")
checkEquals(dgR, RcppArmadillo:::.SciPy2R(csr), msg="csr2dgr")
}

test.other <- function() {
bsr <- sp$bsr_matrix(list(3, 4))
checkException(RcppArmadillo:::.SciPy2R(bsr))
}