Skip to content

Commit 536ac29

Browse files
committed
Fix best matrix selection when matrix is sparse
1 parent fb2f70c commit 536ac29

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

R/RcppExports.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
22
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
33

4-
n_zeros_mat <- function(mat) {
5-
.Call(`_SeuratIntegrate_n_zeros_mat`, mat)
4+
n_zeros_dense_mat <- function(mat) {
5+
.Call(`_SeuratIntegrate_n_zeros_dense_mat`, mat)
6+
}
7+
8+
n_zeros_sparse_mat <- function(mat) {
9+
.Call(`_SeuratIntegrate_n_zeros_sparse_mat`, mat)
610
}
711

R/utils.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,14 @@ could.be.connectivity.Matrix <- function(object, check.symmetry = T) {
743743
}
744744
#' @keywords internal
745745
#' @noRd
746+
n_zeros_mat <- function(mat) {
747+
n <- if(is.matrix(mat)) n_zeros_dense_mat(mat)
748+
else if (inherits(mat, what = "sparseMatrix")) n_zeros_sparse_mat(mat)
749+
else tryCatch(sum(mat == 0, na.rm = TRUE), error = function(e) NA)
750+
return(n)
751+
}
752+
#' @keywords internal
753+
#' @noRd
746754
choose_matrix_format <- function(mat) {
747755
max_int_32bit <- 2^31 - 1
748756
if (all(c(ncol(mat), nrow(mat)) <= max_int_32bit)) { # should always be TRUE

src/RcppExports.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,32 @@ Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
1111
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
1212
#endif
1313

14-
// n_zeros_mat
15-
int64_t n_zeros_mat(const arma::mat& mat);
16-
RcppExport SEXP _SeuratIntegrate_n_zeros_mat(SEXP matSEXP) {
14+
// n_zeros_dense_mat
15+
int64_t n_zeros_dense_mat(const arma::mat& mat);
16+
RcppExport SEXP _SeuratIntegrate_n_zeros_dense_mat(SEXP matSEXP) {
1717
BEGIN_RCPP
1818
Rcpp::RObject rcpp_result_gen;
1919
Rcpp::RNGScope rcpp_rngScope_gen;
2020
Rcpp::traits::input_parameter< const arma::mat& >::type mat(matSEXP);
21-
rcpp_result_gen = Rcpp::wrap(n_zeros_mat(mat));
21+
rcpp_result_gen = Rcpp::wrap(n_zeros_dense_mat(mat));
22+
return rcpp_result_gen;
23+
END_RCPP
24+
}
25+
// n_zeros_sparse_mat
26+
int64_t n_zeros_sparse_mat(const arma::sp_mat& mat);
27+
RcppExport SEXP _SeuratIntegrate_n_zeros_sparse_mat(SEXP matSEXP) {
28+
BEGIN_RCPP
29+
Rcpp::RObject rcpp_result_gen;
30+
Rcpp::RNGScope rcpp_rngScope_gen;
31+
Rcpp::traits::input_parameter< const arma::sp_mat& >::type mat(matSEXP);
32+
rcpp_result_gen = Rcpp::wrap(n_zeros_sparse_mat(mat));
2233
return rcpp_result_gen;
2334
END_RCPP
2435
}
2536

2637
static const R_CallMethodDef CallEntries[] = {
27-
{"_SeuratIntegrate_n_zeros_mat", (DL_FUNC) &_SeuratIntegrate_n_zeros_mat, 1},
38+
{"_SeuratIntegrate_n_zeros_dense_mat", (DL_FUNC) &_SeuratIntegrate_n_zeros_dense_mat, 1},
39+
{"_SeuratIntegrate_n_zeros_sparse_mat", (DL_FUNC) &_SeuratIntegrate_n_zeros_sparse_mat, 1},
2840
{NULL, NULL, 0}
2941
};
3042

src/n_zeros_matrix.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
using namespace Rcpp;
33

44
// [[Rcpp::export]]
5-
int64_t n_zeros_mat(const arma::mat &mat) {
5+
int64_t n_zeros_dense_mat(const arma::mat &mat) {
66
int64_t count = arma::accu(mat == 0);
77
return count;
88
}
99

10+
// [[Rcpp::export]]
11+
int64_t n_zeros_sparse_mat(const arma::sp_mat &mat) {
12+
int64_t count = mat.n_elem - mat.n_nonzero;
13+
return count;
14+
}
15+

0 commit comments

Comments
 (0)