Skip to content

Commit

Permalink
Update to use extract_sparse_array, COO_SparseArray.
Browse files Browse the repository at this point in the history
  • Loading branch information
hpages authored and LTLA committed Jul 15, 2024
1 parent 81f0708 commit 19d67d2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: TileDBArray
Version: 1.15.0
Date: 2023-05-03
Version: 1.15.1
Date: 2024-07-13
Title: Using TileDB as a DelayedArray Backend
Description: Implements a DelayedArray backend for reading and
writing dense or sparse arrays in the TileDB format. The
Expand All @@ -13,7 +13,8 @@ Authors@R:
)
License: MIT + file LICENSE
Depends:
DelayedArray (>= 0.27.2)
SparseArray (>= 1.5.20),
DelayedArray (>= 0.31.7)
Imports:
methods,
Rcpp,
Expand All @@ -31,5 +32,5 @@ biocViews: DataRepresentation, Infrastructure, Software
VignetteBuilder: knitr
BugReports: https://github.com/LTLA/TileDBArray
URL: https://github.com/LTLA/TileDBArray
RoxygenNote: 7.1.1
RoxygenNote: 7.3.2
Encoding: UTF-8
8 changes: 7 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ exportClasses(TileDBArraySeed)
exportClasses(TileDBMatrix)
exportClasses(TileDBRealizationSink)
exportMethods(DelayedArray)
exportMethods(OLD_extract_sparse_array)
exportMethods(chunkdim)
exportMethods(extract_array)
exportMethods(extract_sparse_array)
exportMethods(is_sparse)
exportMethods(matrixClass)
exportMethods(path)
Expand All @@ -35,11 +35,17 @@ exportMethods(write_block)
import(DelayedArray)
import(methods)
import(tiledb)
importClassesFrom(SparseArray,COO_SparseArray)
importFrom(DelayedArray,path)
importFrom(DelayedArray,start)
importFrom(DelayedArray,width)
importFrom(Rcpp,sourceCpp)
importFrom(S4Vectors,isSingleString)
importFrom(S4Vectors,setValidity2)
importFrom(SparseArray,COO_SparseArray)
importFrom(SparseArray,extract_sparse_array)
importFrom(SparseArray,nzcoo)
importFrom(SparseArray,nzdata)
importFrom(SparseArray,nzwhich)
importFrom(methods,show)
useDynLib(TileDBArray)
14 changes: 7 additions & 7 deletions R/TileDBArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#' The latter should be a list of length equal to the number of dimensions in \code{x},
#' where each entry is an integer vector or \code{NULL} (in which case the entirety of the dimension is used).
#'
#' \code{\link{OLD_extract_sparse_array}(x, index)} will return a \linkS4class{SparseArraySeed}
#' containing the indices of non-zero entries in \code{x}, subsetted to the indices in \code{index}.
#' \code{\link{extract_sparse_array}(x, index)} will return a \linkS4class{COO_SparseArray}
#' representing the subset of \code{x} corresponding to the indices in \code{index}.
#' The latter should be a list of the same structure as described for \code{extract_array}.
#'
#' \code{\link{type}(x)} will return a string containing the type of the TileDBArraySeed object \code{x}.
Expand Down Expand Up @@ -61,7 +61,7 @@
#' is_sparse,TileDBArraySeed-method
#' type,TileDBArraySeed-method
#' extract_array,TileDBArraySeed-method
#' OLD_extract_sparse_array,TileDBArraySeed-method
#' extract_sparse_array,TileDBArraySeed-method
#' DelayedArray,TileDBArraySeed-method
#' path,TileDBArraySeed-method
#' chunkdim,TileDBArraySeed-method
Expand Down Expand Up @@ -215,18 +215,18 @@ setMethod("extract_array", "TileDBArraySeed", function(x, index) {
}

#' @export
setMethod("OLD_extract_sparse_array", "TileDBArraySeed", function(x, index) {
#' @importFrom SparseArray extract_sparse_array COO_SparseArray
setMethod("extract_sparse_array", "TileDBArraySeed", function(x, index) {
d2 <- .get_block_dims(x, index)
if (any(d2==0L)) {
fill <- switch(type(x), double=0, integer=0L, logical=FALSE)
return(SparseArraySeed(d2, nzindex=matrix(0L, 0, length(index)), nzdata=fill[0]))
return(COO_SparseArray(d2, nzdata=vector(type(x))))
}

obj <- tiledb_array(path(x), attrs=x@attr, query_type="READ")
on.exit(tiledb_array_close(obj))

df <- .extract_values(obj, index)
SparseArraySeed(d2, nzindex=df$indices, nzdata=as(df$values, type(x)))
COO_SparseArray(d2, nzcoo=df$indices, nzdata=as(df$values, type(x)))
})

#' @export
Expand Down
15 changes: 9 additions & 6 deletions R/TileDBRealizationSink.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,24 @@ setValidity2("TileDBRealizationSink", function(object) {
})

#' @export
#' @importClassesFrom SparseArray COO_SparseArray
#' @importFrom SparseArray nzcoo nzdata nzwhich
#' @importFrom DelayedArray start width
setMethod("write_block", "TileDBRealizationSink", function(sink, viewport, block) {
starts <- start(viewport) - 1L
obj <- tiledb_array(sink@path, attrs=sink@attr, query_type="WRITE")
on.exit(tiledb_array_close(obj))

if (sink@sparse) {
# Need this because SparseArraySeed doesn't follow a matrix abstraction.
if (is(block, "SparseArraySeed")) {
# Need this because COO_SparseArray doesn't support [.
if (is(block, "COO_SparseArray")) {
store <- data.frame(
d1=nzindex(block)[,1] + starts[1],
d2=nzindex(block)[,2] + starts[2],
d1=nzcoo(block)[,1] + starts[1],
d2=nzcoo(block)[,2] + starts[2],
sink=nzdata(block)
)
} else {
idx <- which(block!=0, arr.ind=TRUE)
idx <- nzwhich(block, arr.ind=TRUE)
store <- data.frame(
d1=idx[,1] + starts[1],
d2=idx[,2] + starts[2],
Expand All @@ -207,7 +209,8 @@ setMethod("write_block", "TileDBRealizationSink", function(sink, viewport, block
args <- lapply(width(viewport), seq_len)
args <- mapply(FUN="+", starts, args, SIMPLIFY=FALSE)

# Need to coerce the block, because it could be a SparseArraySeed.
# Need to coerce the block, because it could be a SparseArray
# derivative.
args <- c(list(sink=obj), args, list(value=as.array(block)))
do.call("[<-", args)
}
Expand Down
6 changes: 3 additions & 3 deletions man/TileDBArray.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

using namespace Rcpp;

#ifdef RCPP_USE_GLOBAL_ROSTREAM
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// remap_indices
Rcpp::List remap_indices(Rcpp::List extracted, Rcpp::List remapping);
RcppExport SEXP _TileDBArray_remap_indices(SEXP extractedSEXP, SEXP remappingSEXP) {
Expand Down

0 comments on commit 19d67d2

Please sign in to comment.