-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from r-lib/better-coerce
Better coerce
- Loading branch information
Showing
4 changed files
with
320 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# coerce_to_sparse_matrix() errors on wrong input | ||
|
||
Code | ||
coerce_to_sparse_matrix(1:10) | ||
Condition | ||
Error in `coerce_to_sparse_matrix()`: | ||
! `x` must be a <data.frame>, not an integer vector. | ||
|
||
--- | ||
|
||
Code | ||
coerce_to_sparse_matrix(matrix(0, nrow = 10, ncol = 10)) | ||
Condition | ||
Error in `coerce_to_sparse_matrix()`: | ||
! `x` must be a <data.frame>, not a double matrix. | ||
|
||
--- | ||
|
||
Code | ||
coerce_to_sparse_matrix(iris) | ||
Condition | ||
Error in `coerce_to_sparse_matrix()`: | ||
x All columns of `x` must be numeric. | ||
i Non-numeric columns: Species. | ||
|
||
# coerce_to_sparse_matrix() materializes non-zero defaulted columns | ||
|
||
Code | ||
res <- coerce_to_sparse_matrix(sparse_df) | ||
Output | ||
sparsevctrs: Sparse vector materialized | ||
sparsevctrs: Sparse vector materialized | ||
|
||
# coerce_to_sparse_data_frame() errors with no column names | ||
|
||
Code | ||
coerce_to_sparse_data_frame(sparse_mat) | ||
Condition | ||
Error in `coerce_to_sparse_data_frame()`: | ||
! `x` must have column names. | ||
|
||
# coerce_to_sparse_data_frame() errors with wrong input | ||
|
||
Code | ||
coerce_to_sparse_data_frame(mtcars) | ||
Condition | ||
Error in `coerce_to_sparse_data_frame()`: | ||
! `x` must be a <sparseMatrix>, not a data frame. | ||
|
||
--- | ||
|
||
Code | ||
coerce_to_sparse_data_frame(1:10) | ||
Condition | ||
Error in `coerce_to_sparse_data_frame()`: | ||
! `x` must be a <sparseMatrix>, not an integer vector. | ||
|
||
# coerce_to_sparse_tibble() errors with no column names | ||
|
||
Code | ||
coerce_to_sparse_tibble(sparse_mat) | ||
Condition | ||
Error in `coerce_to_sparse_tibble()`: | ||
! `x` must have column names. | ||
|
||
# coerce_to_sparse_tibble() errors with wrong input | ||
|
||
Code | ||
coerce_to_sparse_tibble(mtcars) | ||
Condition | ||
Error in `coerce_to_sparse_tibble()`: | ||
! `x` must be a <sparseMatrix>, not a data frame. | ||
|
||
--- | ||
|
||
Code | ||
coerce_to_sparse_tibble(1:10) | ||
Condition | ||
Error in `coerce_to_sparse_tibble()`: | ||
! `x` must be a <sparseMatrix>, not an integer vector. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
### coerce_to_sparse_matrix ---------------------------------------------------- | ||
test_that("coerce_to_sparse_matrix() works", { | ||
skip_if_not_installed("Matrix") | ||
|
||
sparse_df <- lapply(1:10, function(x) sparse_double(x, x, length = 10)) | ||
names(sparse_df) <- letters[1:10] | ||
sparse_df <- as.data.frame(sparse_df) | ||
|
||
res <- coerce_to_sparse_matrix(sparse_df) | ||
expect_s4_class(res, "dgCMatrix") | ||
expect_identical(dim(res), c(10L, 10L)) | ||
|
||
exp <- Matrix::diag(1:10, 10, 10) | ||
exp <- Matrix::Matrix(exp, sparse = TRUE) | ||
exp <- as(exp, "generalMatrix") | ||
exp <- as(exp, "CsparseMatrix") | ||
colnames(exp) <- colnames(res) | ||
rownames(exp) <- rownames(res) | ||
|
||
expect_identical(res, exp) | ||
}) | ||
|
||
test_that("coerce_to_sparse_matrix() errors on wrong input", { | ||
skip_if_not_installed("Matrix") | ||
|
||
expect_snapshot( | ||
error = TRUE, | ||
coerce_to_sparse_matrix(1:10) | ||
) | ||
expect_snapshot( | ||
error = TRUE, | ||
coerce_to_sparse_matrix(matrix(0, nrow = 10, ncol = 10)) | ||
) | ||
expect_snapshot( | ||
error = TRUE, | ||
coerce_to_sparse_matrix(iris) | ||
) | ||
}) | ||
|
||
test_that("coerce_to_sparse_matrix() will divert for non-sparse data.frames", { | ||
skip_if_not_installed("Matrix") | ||
|
||
expect_identical( | ||
coerce_to_sparse_matrix(mtcars), | ||
Matrix::Matrix(as.matrix(mtcars), sparse = TRUE) | ||
) | ||
}) | ||
|
||
test_that("coerce_to_sparse_matrix() materializes non-zero defaulted columns", { | ||
skip_if_not_installed("Matrix") | ||
withr::local_options("sparsevctrs.verbose_materialize" = TRUE) | ||
|
||
sparse_df <- lapply(1:10, function(x) sparse_double(x, x, length = 10)) | ||
names(sparse_df) <- letters[1:10] | ||
sparse_df <- as.data.frame(sparse_df) | ||
sparse_df$nonzero1 <- sparse_double(1, 1, 10, default = 10) | ||
sparse_df$nonzero2 <- sparse_double(1, 1, 10, default = 20) | ||
|
||
expect_snapshot( | ||
res <- coerce_to_sparse_matrix(sparse_df) | ||
) | ||
|
||
withr::local_options("sparsevctrs.verbose_materialize" = NULL) | ||
|
||
expect_s4_class(res, "dgCMatrix") | ||
expect_identical(dim(res), c(10L, 12L)) | ||
|
||
exp <- Matrix::diag(1:10, 10, 10) | ||
exp <- Matrix::Matrix(exp, sparse = TRUE) | ||
exp <- as(exp, "generalMatrix") | ||
exp <- as(exp, "CsparseMatrix") | ||
|
||
exp <- cbind(exp, sparse_df$nonzero1) | ||
exp <- cbind(exp, sparse_df$nonzero2) | ||
|
||
colnames(exp) <- colnames(res) | ||
rownames(exp) <- rownames(res) | ||
|
||
expect_identical(res, exp) | ||
}) | ||
|
||
### coerce_to_sparse_data_frame ------------------------------------------------ | ||
|
||
test_that("coerce_to_sparse_data_frame() works", { | ||
skip_if_not_installed("Matrix") | ||
|
||
sparse_mat <- Matrix::diag(1:10, 10, 10) | ||
sparse_mat <- Matrix::Matrix(sparse_mat, sparse = TRUE) | ||
sparse_mat <- as(sparse_mat, "generalMatrix") | ||
sparse_mat <- as(sparse_mat, "CsparseMatrix") | ||
colnames(sparse_mat) <- letters[1:10] | ||
rownames(sparse_mat) <- 1:10 | ||
|
||
res <- coerce_to_sparse_data_frame(sparse_mat) | ||
|
||
exp <- lapply(1:10, function(x) sparse_double(x, x, length = 10)) | ||
names(exp) <- letters[1:10] | ||
exp <- as.data.frame(exp) | ||
|
||
expect_identical(res, exp) | ||
}) | ||
|
||
test_that("coerce_to_sparse_data_frame() works with non-dgCMatrix input", { | ||
skip_if_not_installed("Matrix") | ||
|
||
sparse_mat <- Matrix::diag(1:10, 10, 10) | ||
sparse_mat <- Matrix::Matrix(sparse_mat, sparse = TRUE) | ||
colnames(sparse_mat) <- letters[1:10] | ||
rownames(sparse_mat) <- 1:10 | ||
|
||
res <- coerce_to_sparse_data_frame(sparse_mat) | ||
|
||
exp <- lapply(1:10, function(x) sparse_double(x, x, length = 10)) | ||
names(exp) <- letters[1:10] | ||
exp <- as.data.frame(exp) | ||
|
||
expect_identical(res, exp) | ||
}) | ||
|
||
test_that("coerce_to_sparse_data_frame() errors with no column names", { | ||
skip_if_not_installed("Matrix") | ||
|
||
sparse_mat <- Matrix::diag(1:10, 10, 10) | ||
sparse_mat <- Matrix::Matrix(sparse_mat, sparse = TRUE) | ||
|
||
expect_snapshot( | ||
error = TRUE, | ||
coerce_to_sparse_data_frame(sparse_mat) | ||
) | ||
}) | ||
|
||
test_that("coerce_to_sparse_data_frame() errors with wrong input", { | ||
expect_snapshot( | ||
error = TRUE, | ||
coerce_to_sparse_data_frame(mtcars) | ||
) | ||
expect_snapshot( | ||
error = TRUE, | ||
coerce_to_sparse_data_frame(1:10) | ||
) | ||
}) | ||
|
||
### coerce_to_sparse_tibble ---------------------------------------------------- | ||
|
||
test_that("coerce_to_sparse_tibble() works", { | ||
skip_if_not_installed("Matrix") | ||
skip_if_not_installed("tibble") | ||
|
||
sparse_mat <- Matrix::diag(1:10, 10, 10) | ||
sparse_mat <- Matrix::Matrix(sparse_mat, sparse = TRUE) | ||
sparse_mat <- as(sparse_mat, "generalMatrix") | ||
sparse_mat <- as(sparse_mat, "CsparseMatrix") | ||
colnames(sparse_mat) <- letters[1:10] | ||
rownames(sparse_mat) <- 1:10 | ||
|
||
res <- coerce_to_sparse_tibble(sparse_mat) | ||
|
||
exp <- lapply(1:10, function(x) sparse_double(x, x, length = 10)) | ||
names(exp) <- letters[1:10] | ||
exp <- tibble::as_tibble(exp) | ||
|
||
expect_identical(res, exp) | ||
}) | ||
|
||
test_that("coerce_to_sparse_tibble() works with non-dgCMatrix input", { | ||
skip_if_not_installed("Matrix") | ||
skip_if_not_installed("tibble") | ||
|
||
sparse_mat <- Matrix::diag(1:10, 10, 10) | ||
sparse_mat <- Matrix::Matrix(sparse_mat, sparse = TRUE) | ||
colnames(sparse_mat) <- letters[1:10] | ||
rownames(sparse_mat) <- 1:10 | ||
|
||
res <- coerce_to_sparse_tibble(sparse_mat) | ||
|
||
exp <- lapply(1:10, function(x) sparse_double(x, x, length = 10)) | ||
names(exp) <- letters[1:10] | ||
exp <- tibble::as_tibble(exp) | ||
|
||
expect_identical(res, exp) | ||
}) | ||
|
||
test_that("coerce_to_sparse_tibble() errors with no column names", { | ||
skip_if_not_installed("Matrix") | ||
skip_if_not_installed("tibble") | ||
|
||
sparse_mat <- Matrix::diag(1:10, 10, 10) | ||
sparse_mat <- Matrix::Matrix(sparse_mat, sparse = TRUE) | ||
|
||
expect_snapshot( | ||
error = TRUE, | ||
coerce_to_sparse_tibble(sparse_mat) | ||
) | ||
}) | ||
|
||
test_that("coerce_to_sparse_tibble() errors with wrong input", { | ||
expect_snapshot( | ||
error = TRUE, | ||
coerce_to_sparse_tibble(mtcars) | ||
) | ||
expect_snapshot( | ||
error = TRUE, | ||
coerce_to_sparse_tibble(1:10) | ||
) | ||
}) |