Skip to content

Commit

Permalink
Resync with SparseArray 1.5.22 and DelayedArray 0.31.8. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpages authored Jul 19, 2024
1 parent 3d04935 commit 747c17c
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 98 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: alabaster.matrix
Title: Load and Save Artifacts from File
Version: 1.5.4
Date: 2024-06-21
Version: 1.5.5
Date: 2024-07-16
Authors@R: person("Aaron", "Lun", role=c("aut", "cre"), email="[email protected]")
License: MIT + file LICENSE
Description:
Expand All @@ -15,9 +15,9 @@ Imports:
methods,
BiocGenerics,
S4Vectors,
DelayedArray (>= 0.27.2),
DelayedArray (>= 0.31.8),
S4Arrays,
SparseArray,
SparseArray (>= 1.5.22),
rhdf5 (>= 2.47.1),
HDF5Array,
Matrix,
Expand All @@ -31,7 +31,7 @@ Suggests:
ResidualMatrix
LinkingTo: Rcpp
VignetteBuilder: knitr
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
biocViews:
DataImport,
DataRepresentation
21 changes: 8 additions & 13 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ exportClasses(ReloadedMatrix)
exportClasses(WrapperArray)
exportClasses(WrapperArraySeed)
exportMethods(DelayedArray)
exportMethods(OLD_extract_sparse_array)
exportMethods(chunkdim)
exportMethods(dim)
exportMethods(dimnames)
Expand All @@ -54,32 +53,23 @@ importClassesFrom(Matrix,Matrix)
importClassesFrom(Matrix,dgCMatrix)
importClassesFrom(Matrix,dsparseMatrix)
importClassesFrom(SparseArray,SVT_SparseMatrix)
importFrom(BiocGenerics,"type<-")
importFrom(BiocGenerics,path)
importFrom(BiocGenerics,start)
importFrom(DelayedArray,"type<-")
importFrom(BiocGenerics,type)
importFrom(DelayedArray,DelayedArray)
importFrom(DelayedArray,OLD_extract_sparse_array)
importFrom(DelayedArray,acbind)
importFrom(DelayedArray,arbind)
importFrom(DelayedArray,blockApply)
importFrom(DelayedArray,chunkdim)
importFrom(DelayedArray,colAutoGrid)
importFrom(DelayedArray,currentViewport)
importFrom(DelayedArray,defaultSinkAutoGrid)
importFrom(DelayedArray,extract_array)
importFrom(DelayedArray,getAutoBlockLength)
importFrom(DelayedArray,getAutoBlockSize)
importFrom(DelayedArray,isPristine)
importFrom(DelayedArray,is_sparse)
importFrom(DelayedArray,matrixClass)
importFrom(DelayedArray,new_DelayedArray)
importFrom(DelayedArray,nzdata)
importFrom(DelayedArray,nzindex)
importFrom(DelayedArray,path)
importFrom(DelayedArray,read_sparse_block)
importFrom(DelayedArray,rowAutoGrid)
importFrom(DelayedArray,seed)
importFrom(DelayedArray,type)
importFrom(HDF5Array,H5SparseMatrixSeed)
importFrom(HDF5Array,HDF5Array)
importFrom(HDF5Array,HDF5ArraySeed)
Expand All @@ -89,10 +79,15 @@ importFrom(HDF5Array,getHDF5DumpCompressionLevel)
importFrom(HDF5Array,writeHDF5Array)
importFrom(Matrix,t)
importFrom(Rcpp,sourceCpp)
importFrom(S4Arrays,acbind)
importFrom(S4Arrays,arbind)
importFrom(S4Arrays,extract_array)
importFrom(S4Arrays,is_sparse)
importFrom(S4Vectors,new2)
importFrom(SparseArray,extract_sparse_array)
importFrom(SparseArray,nzdata)
importFrom(SparseArray,nzvals)
importFrom(SparseArray,nzwhich)
importFrom(SparseArray,read_block_as_sparse)
importFrom(alabaster.base,.loadObject)
importFrom(alabaster.base,.stageObject)
importFrom(alabaster.base,.writeMetadata)
Expand Down
2 changes: 1 addition & 1 deletion R/AmalgamatedArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
NULL

#' @export
#' @importFrom DelayedArray arbind acbind
#' @importFrom S4Arrays arbind acbind
AmalgamatedArraySeed <- function(components, along = 1) {
sample.names <- names(components)
if (anyDuplicated(sample.names) || any(sample.names == "")) {
Expand Down
34 changes: 4 additions & 30 deletions R/DelayedMask.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#' is_sparse,DelayedMask-method
#' extract_array,DelayedMask-method
#' extract_sparse_array,DelayedMask-method
#' OLD_extract_sparse_array,DelayedMask-method
#'
#' @examples
#' original <- DelayedArray(matrix(rpois(40, lambda=2), ncol=5))
Expand Down Expand Up @@ -56,7 +55,7 @@ setMethod("dim", "DelayedMask", function(x) callGeneric(x@seed))
setMethod("dimnames", "DelayedMask", function(x) callGeneric(x@seed))

#' @export
#' @importFrom DelayedArray is_sparse
#' @importFrom S4Arrays is_sparse
setMethod("is_sparse", "DelayedMask", function(x) {
if (is.finite(x@placeholder) && x@placeholder == 0) {
return(FALSE)
Expand All @@ -66,7 +65,7 @@ setMethod("is_sparse", "DelayedMask", function(x) {
})

#' @export
#' @importFrom DelayedArray extract_array
#' @importFrom S4Arrays extract_array
setMethod("extract_array", "DelayedMask", function(x, index) {
ans <- callGeneric(x@seed, index)
.replace_missing(ans, x@placeholder)
Expand All @@ -79,19 +78,12 @@ setMethod("extract_sparse_array", "DelayedMask", function(x, index) {
if (is(ans, "COO_SparseArray")) {
ans@nzdata <- .replace_missing(ans@nzdata, x@placeholder)
} else {
ans@SVT <- .replace_missing_svt(ans@SVT, length(dim(x)) - 1L, x@placeholder)
nzidx <- nzwhich(ans)
ans[nzidx] <- .replace_missing(ans[nzidx], x@placeholder)
}
ans
})

#' @export
#' @importFrom DelayedArray OLD_extract_sparse_array
setMethod("OLD_extract_sparse_array", "DelayedMask", function(x, index) {
ans <- callGeneric(x@seed, index)
ans@nzdata <- .replace_missing(ans@nzdata, x@placeholder)
ans
})

.replace_missing <- function(vec, placeholder) {
if (is.na(placeholder)) {
if (anyNA(vec)) {
Expand All @@ -118,21 +110,3 @@ setMethod("OLD_extract_sparse_array", "DelayedMask", function(x, index) {
vec
}

.replace_missing_svt <- function(tree, dim, placeholder) {
if (dim == 1L) {
for (i in seq_along(tree)) {
current <- tree[[i]]
if (!is.null(current)) {
tree[[i]][[2]] <- .replace_missing(tree[[i]][[2]], placeholder)
}
}
} else {
for (i in seq_along(tree)) {
current <- tree[[i]]
if (!is.null(current)) {
tree[[i]] <- .replace_missing_svt(current, dim - 1L, placeholder)
}
}
}
tree
}
13 changes: 4 additions & 9 deletions R/WrapperArraySeed.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#' is_sparse,WrapperArraySeed-method
#' extract_array,WrapperArraySeed-method
#' extract_sparse_array,WrapperArraySeed-method
#' OLD_extract_sparse_array,WrapperArraySeed-method
#' WrapperArray-class
#' coerce,WrapperArray,dgCMatrix-method
#' coerce,WrapperArraySeed,dgCMatrix-method
Expand All @@ -38,7 +37,7 @@
#' dim(foo)
#' is_sparse(foo)
#' extract_array(foo, list(1:10, 1:10))
#' OLD_extract_sparse_array(foo, list(1:10, 1:10))
#' extract_sparse_array(foo, list(1:10, 1:10))
#'
#' @name WrapperArraySeed
NULL
Expand All @@ -54,25 +53,21 @@ setMethod("dimnames", "WrapperArraySeed", function(x) callGeneric(x@seed))
setMethod("chunkdim", "WrapperArraySeed", function(x) callGeneric(x@seed))

#' @export
#' @importFrom DelayedArray path
#' @importFrom BiocGenerics path
setMethod("path", "WrapperArraySeed", function(object, ...) callGeneric(object@seed))

#' @export
#' @importFrom DelayedArray is_sparse
#' @importFrom S4Arrays is_sparse
setMethod("is_sparse", "WrapperArraySeed", function(x) callGeneric(x@seed))

#' @export
#' @importFrom DelayedArray extract_array
#' @importFrom S4Arrays extract_array
setMethod("extract_array", "WrapperArraySeed", function(x, index) callGeneric(x@seed, index))

#' @export
#' @importFrom SparseArray extract_sparse_array
setMethod("extract_sparse_array", "WrapperArraySeed", function(x, index) callGeneric(x@seed, index))

#' @export
#' @importFrom DelayedArray OLD_extract_sparse_array
setMethod("OLD_extract_sparse_array", "WrapperArraySeed", function(x, index) callGeneric(x@seed, index))

##############################

# We define the coercion methods here to give us the opportunity to
Expand Down
22 changes: 13 additions & 9 deletions R/optimize_storage.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ setMethod("collect_integer_attributes", "SVT_SparseMatrix", function(x) {
})

#' @importFrom S4Arrays is_sparse
#' @importFrom SparseArray nzdata
#' @importFrom SparseArray nzvals
setMethod("collect_integer_attributes", "ANY", function(x) {
output <- list()

if (is_sparse(x)) {
collated <- blockApply(x, function(y) {
out <- .simple_integer_collector(nzdata(y))
out$non_zero <- length(nzdata(y))
y_nzvals <- nzvals(y)
out <- .simple_integer_collector(y_nzvals)
out$non_zero <- length(y_nzvals)
out
}, as.sparse=TRUE)
output$non_zero <- aggregate_sum(collated, "non_zero")
Expand Down Expand Up @@ -208,14 +209,14 @@ setMethod("collect_float_attributes", "SVT_SparseMatrix", function(x) {
})

#' @importFrom S4Arrays is_sparse
#' @importFrom SparseArray nzdata
#' @importFrom SparseArray nzvals
setMethod("collect_float_attributes", "ANY", function(x) {
output <- list()
if (is_sparse(x)) {
collated <- blockApply(x, function(y) {
nzd <- nzdata(y)
out <- collect_double_attributes(nzd)
out$non_zero <- length(nzd)
y_nzvals <- nzvals(y)
out <- collect_double_attributes(y_nzvals)
out$non_zero <- length(y_nzvals)
out
}, as.sparse=TRUE)
output$non_zero <- aggregate_sum(collated, "non_zero")
Expand Down Expand Up @@ -267,7 +268,7 @@ optimize_float_storage <- function(x) {
# Fallback that just goes through and pulls out all unique values.
if (is.null(placeholder)) {
if (is_sparse(x)) {
u <- Reduce(union, blockApply(x, function(y) unique(nzdata(y))))
u <- Reduce(union, blockApply(x, function(y) unique(nzvals(y))))
} else {
u <- Reduce(union, blockApply(x, function(y) unique(as.vector(y))))
}
Expand Down Expand Up @@ -364,7 +365,10 @@ setGeneric("collect_boolean_attributes", function(x) standardGeneric("collect_bo
setMethod("collect_boolean_attributes", "ANY", function(x) {
output <- list()
if (is_sparse(x)) {
collated <- blockApply(x, function(x) list(missing=anyNA(nzdata(x)), non_zero=length(nzdata(x))), as.sparse=TRUE)
collated <- blockApply(x, function(x) {
x_nzvals <- nzvals(x)
list(missing=anyNA(x_nzvals), non_zero=length(x_nzvals))
}, as.sparse=TRUE)
output$non_zero <- aggregate_sum(collated, "non_zero")
} else {
collated <- list(list(missing=anyNA(x)))
Expand Down
2 changes: 1 addition & 1 deletion R/readArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
#' @export
#' @aliases
#' loadArray
#' @importFrom BiocGenerics type<-
#' @importFrom HDF5Array HDF5Array
#' @importFrom DelayedArray type<-
readArray <- function(path, metadata, ...) {
fpath <- file.path(path, "array.h5")

Expand Down
2 changes: 1 addition & 1 deletion R/readSparseMatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#' readObject(dir)
#'
#' @export
#' @importFrom BiocGenerics type<-
#' @importFrom HDF5Array H5SparseMatrixSeed
#' @importFrom DelayedArray type<-
readSparseMatrix <- function(path, metadata, ...) {
fpath <- file.path(path, "matrix.h5")
name <- "compressed_sparse_matrix"
Expand Down
4 changes: 2 additions & 2 deletions R/saveArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ h5_write_array <- function(handle, name, x, type, placeholder, extract.native=NU
######### OLD STUFF ##########
##############################

#' @importFrom DelayedArray is_sparse
#' @importFrom S4Arrays is_sparse
#' @importFrom rhdf5 h5createFile
#' @importFrom HDF5Array writeHDF5Array
#' @importFrom alabaster.base transformVectorForHdf5 addMissingPlaceholderAttributeForHdf5
Expand Down Expand Up @@ -258,7 +258,7 @@ setMethod("stageObject", "DelayedArray", function(x, dir, path, child=FALSE) .st
)
}

#' @importFrom DelayedArray is_sparse
#' @importFrom S4Arrays is_sparse
.stage_any_matrix <- function(x, dir, path, child=FALSE) {
if (is_sparse(x)) {
.stage_sparse_matrix(x, dir, path, child=child)
Expand Down
6 changes: 3 additions & 3 deletions R/saveSparseMatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ setMethod("h5_write_sparse_matrix", "ANY", function(x, handle, details, ...) {
start <- 1 # don't use integers to avoid overflow
pointers <- list(0)
for (i in seq_along(grid)) {
block <- read_sparse_block(x, grid[[i]])
block <- read_block_as_sparse(x, grid[[i]])

nzdex <- nzindex(block)
nzdex <- nzwhich(block, arr.ind=TRUE)
if (layout == "CSC") {
primary <- nzdex[, 2]
secondary <- nzdex[, 1]
Expand All @@ -256,7 +256,7 @@ setMethod("h5_write_sparse_matrix", "ANY", function(x, handle, details, ...) {
secondary <- nzdex[, 2]
ndim <- nrow(block)
}
v <- nzdata(block)
v <- nzvals(block)

o <- order(primary, secondary)
primary <- primary[o]
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @importFrom DelayedArray type
#' @importFrom BiocGenerics type
to_array_type <- function(x) {
switch(type(x),
integer="integer",
Expand Down
Loading

0 comments on commit 747c17c

Please sign in to comment.