Bioconductor / HDF5Array

HDF5 backend for DelayedArray objects
https://bioconductor.org/packages/HDF5Array
9 stars 13 forks source link

Loss of sparsity when diving by vector #44

Closed const-ae closed 3 years ago

const-ae commented 3 years ago

Hi,

first thanks for adding sparsity support for HDF5 matrices.

I noticed that, unlike dgCMatrix objects, sparse HDF5 matrices lose their sparsity when I divide by a vector. Is this the expected behavior?

library(Matrix)
mat <- matrix(0, nrow = 5, ncol = 3)
mat[c(1,5, 14)] <- 1:3
sp_mat <- as(mat, "dgCMatrix")
sp_mat_disk <- HDF5Array::writeHDF5Array(sp_mat)
sp_mat
#> 5 x 3 sparse Matrix of class "dgCMatrix"
#>           
#> [1,] 1 . .
#> [2,] . . .
#> [3,] . . .
#> [4,] . . 3
#> [5,] 2 . .

sp_mat / 5
#> 5 x 3 sparse Matrix of class "dgCMatrix"
#>               
#> [1,] 0.2 . .  
#> [2,] .   . .  
#> [3,] .   . .  
#> [4,] .   . 0.6
#> [5,] 0.4 . .
sp_mat / 1:5
#> 5 x 3 sparse Matrix of class "dgCMatrix"
#>                
#> [1,] 1.0 . .   
#> [2,] .   . .   
#> [3,] .   . .   
#> [4,] .   . 0.75
#> [5,] 0.4 . .

tmp <- sp_mat_disk / 5
HDF5Array::is_sparse(tmp)
#> [1] TRUE

tmp <- sp_mat_disk / 1:5
HDF5Array::is_sparse(tmp)
#> [1] FALSE

Created on 2021-06-18 by the reprex package (v0.3.0)

Best, Constantin

LTLA commented 3 years ago

Sounds like you probably have an older version of the stack. The final statement evaluates to TRUE for me.

const-ae commented 3 years ago

Ah, true. I am still on the previous Bioconductor version. Sorry about that then 🙈