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

respect matrix_type when coerced into matrix #77

Merged
merged 4 commits into from
Mar 12, 2024
Merged

Conversation

Yunuuuu
Copy link
Contributor

@Yunuuuu Yunuuuu commented Mar 2, 2024

library(BPCells)
mock_matrix <- function(ngenes, ncells) {
    cell.means <- 2^stats::runif(ngenes, 2, 10)
    cell.disp <- 100 / cell.means + 0.5
    cell.data <- matrix(stats::rnbinom(ngenes * ncells,
        mu = cell.means,
        size = 1 / cell.disp
    ), ncol = ncells)
    rownames(cell.data) <- sprintf("Gene_%s", formatC(seq_len(ngenes),
        width = 4, flag = 0
    ))
    colnames(cell.data) <- sprintf("Cell_%s", formatC(seq_len(ncells),
        width = 3, flag = 0
    ))
    cell.data
}
tmpdir <- tempdir()
mat <- mock_matrix(30, 20)
path <- normalizePath(tempfile(tmpdir = tmpdir), mustWork = FALSE)
obj <- write_matrix_dir(mat = as(mat, "dgCMatrix"), dir = path)
#> Warning: Matrix compression performs poorly with non-integers.
#> • Consider calling convert_matrix_type if a compressed integer matrix is intended.
#> This message is displayed once every 8 hours.
storage.mode(as.matrix(obj))
#> Warning: Converting to a dense matrix may use excessive memory
#> This message is displayed once every 8 hours.
#> [1] "double"
obj <- convert_matrix_type(obj, "uint32_t")
storage.mode(as.matrix(obj))
#> [1] "integer"

Created on 2024-03-02 with reprex v2.0.2
~
~
~
~

R/matrix.R Outdated
if (all(matrix <= .Machine$integer.max)) {
storage.mode(matrix) <- "integer"
} else {
cli::cli_warn(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we change this to a call to rlang::warn? I don't think cli is in the dependency list right now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's well to do that, I missed it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My working computer is not at hand, I'll do it tonight.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No rush, I just wanted to get you back comments in a semi-timely manner :)

@bnprks
Copy link
Owner

bnprks commented Mar 3, 2024

Thanks @Yunuuuu, this looks like a nice quality-of-life improvement. One additional request would be if you can add a basic test in test-matrix_utils.R that just confirms that the typing works properly on demo 3x5 matrices? (The generate_dense_matrix helper function will probably be useful to you there). The cases I'd want covered are:

  • basic double matrix
  • basic integer matrix
  • uint matrix that exceeds the int range

Then confirming everything is the same with expect_identical which is type-sensitive

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Mar 4, 2024

Hi, @bnprks. I have added the unit test. Another opinion, do you think should we keep the matrix data type when coercing a dense matrix into the IterableMatrix by adding a new method instead of coercing into dgCMatrix firstly?

@bnprks
Copy link
Owner

bnprks commented Mar 12, 2024

Hi @Yunuuuu, sorry for the delay getting back to this. I've just looked through your changes, made a couple of final edits to tests, and merged back into main.

Your idea about adding a specific method to convert directly from a dense matrix to IterableMatrix is good. I'm not sure how commonly BPCells users will want to do that, but I'd be happy to review a separate pull request if you want to implement it.

@bnprks bnprks merged commit 6ca7ebb into bnprks:main Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants