Skip to content

Commit

Permalink
Adapt for R
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Apr 18, 2024
1 parent c807cb2 commit 44b8c28
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
28 changes: 20 additions & 8 deletions apis/r/R/SOMADenseNDArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ SOMADenseNDArray <- R6::R6Class(

result_order <- map_query_layout(match_query_layout(result_order))

if (!is.null(coords)) {
coords <- private$.convert_coords(coords)
if (is.null(coords)) {
# These are 0-up: add 1 for R use
ned <- self$non_empty_domain()
coords <- lapply(X=as.integer(ned), FUN=function(x){0:x})
}
coords <- private$.convert_coords(coords)

cfg <- as.character(tiledb::config(self$tiledbsoma_ctx$context()))
rl <- soma_array_reader(uri = uri,
dim_points = coords, # NULL dealt with by soma_array_reader()
dim_points = coords,
result_order = result_order,
loglevel = log_level, # idem
loglevel = log_level, # NULL dealt with by soma_array_reader()
config = cfg)

soma_array_to_arrow_table(rl)
Expand All @@ -74,15 +77,24 @@ SOMADenseNDArray <- R6::R6Class(

dims <- self$dimensions()
attr <- self$attributes()

stopifnot("Array must have two dimensions" = length(dims) == 2,
"Array must contain columns 'soma_dim_0' and 'soma_dim_1'" =
all.equal(c("soma_dim_0", "soma_dim_1"), names(dims)),
"Array must contain column 'soma_data'" = all.equal("soma_data", names(attr)))

if (is.null(coords)) {
ned <- self$non_empty_domain()
# These are 0-up: add 1 for R use
nrow <- as.numeric(ned[[1]]) + 1
ncol <- as.numeric(ned[[2]]) + 1
} else {
nrow <- length(unique(as.numeric(coords[[1]])))
ncol <- length(unique(as.numeric(coords[[2]])))
}

tbl <- self$read_arrow_table(coords = coords, result_order = result_order, log_level = log_level)
m <- matrix(as.numeric(tbl$GetColumnByName("soma_data")),
nrow = length(unique(as.numeric(tbl$GetColumnByName("soma_dim_0")))),
ncol = length(unique(as.numeric(tbl$GetColumnByName("soma_dim_1")))),
nrow = nrow,
ncol = ncol,
byrow = result_order == "ROW_MAJOR")

},
Expand Down
16 changes: 8 additions & 8 deletions apis/r/R/SOMAExperimentAxisQuery.R
Original file line number Diff line number Diff line change
Expand Up @@ -425,24 +425,24 @@ SOMAExperimentAxisQuery <- R6::R6Class(
# reindex the coordinates.
mat_coords <- switch(collection,
X = list(
i = self$indexer$by_obs(tbl$soma_dim_0),
j = self$indexer$by_var(tbl$soma_dim_1)
i = self$indexer$by_obs(tbl$soma_dim_0), # XXX UPDATE HERE FOR DENSE
j = self$indexer$by_var(tbl$soma_dim_1) # XXX UPDATE HERE FOR DENSE
),
obsm = list(
i = self$indexer$by_obs(tbl$soma_dim_0),
i = self$indexer$by_obs(tbl$soma_dim_0), # XXX UPDATE HERE FOR DENSE
j = tbl$soma_dim_1
),
varm = list(
i = self$indexer$by_var(tbl$soma_dim_0),
i = self$indexer$by_var(tbl$soma_dim_0), # XXX UPDATE HERE FOR DENSE
j = tbl$soma_dim_1
),
obsp = list(
i = self$indexer$by_obs(tbl$soma_dim_0),
j = self$indexer$by_obs(tbl$soma_dim_1)
i = self$indexer$by_obs(tbl$soma_dim_0), # XXX UPDATE HERE FOR DENSE
j = self$indexer$by_obs(tbl$soma_dim_1) # XXX UPDATE HERE FOR DENSE
),
varp = list(
i = self$indexer$by_var(tbl$soma_dim_0),
j = self$indexer$by_var(tbl$soma_dim_1)
i = self$indexer$by_var(tbl$soma_dim_0), # XXX UPDATE HERE FOR DENSE
j = self$indexer$by_var(tbl$soma_dim_1) # XXX UPDATE HERE FOR DENSE
)
)

Expand Down
2 changes: 1 addition & 1 deletion apis/r/tests/testthat/test-SOMADenseNDArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_that("SOMADenseNDArray creation", {
ndarray <- SOMADenseNDArrayOpen(uri)
tbl <- ndarray$read_arrow_table(result_order = "COL_MAJOR")
expect_true(is_arrow_table(tbl))
expect_equal(tbl$ColumnNames(), c("soma_dim_0", "soma_dim_1", "soma_data"))
expect_equal(tbl$ColumnNames(), c("soma_data"))

expect_identical(
as.numeric(tbl$GetColumnByName("soma_data")),
Expand Down

0 comments on commit 44b8c28

Please sign in to comment.