From 44b8c28d5f4315cab58e6190c6bb0e0fc338ca3f Mon Sep 17 00:00:00 2001 From: John Kerl Date: Wed, 17 Apr 2024 16:26:59 +0000 Subject: [PATCH] Adapt for R --- apis/r/R/SOMADenseNDArray.R | 28 +++++++++++++------ apis/r/R/SOMAExperimentAxisQuery.R | 16 +++++------ apis/r/tests/testthat/test-SOMADenseNDArray.R | 2 +- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/apis/r/R/SOMADenseNDArray.R b/apis/r/R/SOMADenseNDArray.R index 464685de55..bfa32c23e3 100644 --- a/apis/r/R/SOMADenseNDArray.R +++ b/apis/r/R/SOMADenseNDArray.R @@ -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) @@ -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") }, diff --git a/apis/r/R/SOMAExperimentAxisQuery.R b/apis/r/R/SOMAExperimentAxisQuery.R index c4a73d3630..d44bb80a83 100644 --- a/apis/r/R/SOMAExperimentAxisQuery.R +++ b/apis/r/R/SOMAExperimentAxisQuery.R @@ -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 ) ) diff --git a/apis/r/tests/testthat/test-SOMADenseNDArray.R b/apis/r/tests/testthat/test-SOMADenseNDArray.R index 53f9048952..1eadc8eb01 100644 --- a/apis/r/tests/testthat/test-SOMADenseNDArray.R +++ b/apis/r/tests/testthat/test-SOMADenseNDArray.R @@ -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")),