Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[r] Push attrnames down to C++ #3121

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apis/r/R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ c_dimnames <- function(uri, ctxxp) {
.Call(`_tiledbsoma_c_dimnames`, uri, ctxxp)
}

c_attrnames <- function(uri, ctxxp) {
.Call(`_tiledbsoma_c_attrnames`, uri, ctxxp)
}

resize <- function(uri, new_shape, ctxxp) {
invisible(.Call(`_tiledbsoma_resize`, uri, new_shape, ctxxp))
}
Expand Down
8 changes: 4 additions & 4 deletions apis/r/R/SOMADenseNDArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ SOMADenseNDArray <- R6::R6Class(
) {
private$check_open_for_read()

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

stopifnot("Array must have two dimensions" = length(dims) == 2,
"Array must contain column 'soma_data'" = all.equal("soma_data", names(attr)))
stopifnot("Array must have two dimensions" = ndim == 2,
"Array must contain column 'soma_data'" = all.equal("soma_data", attrnames))

if (is.null(coords)) {
ned <- self$non_empty_domain()
Expand Down
7 changes: 1 addition & 6 deletions apis/r/R/TileDBArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,7 @@ TileDBArray <- R6::R6Class(
#' @description Retrieve attribute names (lifecycle: maturing)
#' @return A character vector with the array's attribute names
attrnames = function() {
vapply(
self$attributes(),
FUN = tiledb::name,
FUN.VALUE = vector("character", 1L),
USE.NAMES = FALSE
)
c_attrnames(self$uri, private$.soma_context)
},

#' @description Retrieve the names of all columns, including dimensions and
Expand Down
13 changes: 13 additions & 0 deletions apis/r/src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,18 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// c_attrnames
Rcpp::CharacterVector c_attrnames(const std::string& uri, Rcpp::XPtr<somactx_wrap_t> ctxxp);
RcppExport SEXP _tiledbsoma_c_attrnames(SEXP uriSEXP, SEXP ctxxpSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const std::string& >::type uri(uriSEXP);
Rcpp::traits::input_parameter< Rcpp::XPtr<somactx_wrap_t> >::type ctxxp(ctxxpSEXP);
rcpp_result_gen = Rcpp::wrap(c_attrnames(uri, ctxxp));
return rcpp_result_gen;
END_RCPP
}
// resize
void resize(const std::string& uri, Rcpp::NumericVector new_shape, Rcpp::XPtr<somactx_wrap_t> ctxxp);
RcppExport SEXP _tiledbsoma_resize(SEXP uriSEXP, SEXP new_shapeSEXP, SEXP ctxxpSEXP) {
Expand Down Expand Up @@ -734,6 +746,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_tiledbsoma_has_current_domain", (DL_FUNC) &_tiledbsoma_has_current_domain, 2},
{"_tiledbsoma_ndim", (DL_FUNC) &_tiledbsoma_ndim, 2},
{"_tiledbsoma_c_dimnames", (DL_FUNC) &_tiledbsoma_c_dimnames, 2},
{"_tiledbsoma_c_attrnames", (DL_FUNC) &_tiledbsoma_c_attrnames, 2},
{"_tiledbsoma_resize", (DL_FUNC) &_tiledbsoma_resize, 3},
{"_tiledbsoma_resize_soma_joinid", (DL_FUNC) &_tiledbsoma_resize_soma_joinid, 3},
{"_tiledbsoma_tiledbsoma_upgrade_shape", (DL_FUNC) &_tiledbsoma_tiledbsoma_upgrade_shape, 3},
Expand Down
15 changes: 15 additions & 0 deletions apis/r/src/rinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,21 @@ Rcpp::CharacterVector c_dimnames(
return retval;
}

// [[Rcpp::export]]
Rcpp::CharacterVector c_attrnames(
const std::string& uri, Rcpp::XPtr<somactx_wrap_t> ctxxp) {
auto sr = tdbs::SOMAArray::open(OpenMode::read, uri, ctxxp->ctxptr);
auto lib_retval = sr->attribute_names();
sr->close();

size_t n = lib_retval.size();
Rcpp::CharacterVector retval(n);
for (size_t i = 0; i < n; i++) {
retval[i] = lib_retval[i];
}
return retval;
}

// [[Rcpp::export]]
void resize(
const std::string& uri,
Expand Down
10 changes: 10 additions & 0 deletions libtiledbsoma/src/soma/soma_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,16 @@ bool SOMAArray::has_dimension_name(const std::string& name) const {
return false;
}

std::vector<std::string> SOMAArray::attribute_names() const {
std::vector<std::string> result;
auto schema = tiledb_schema();
unsigned n = schema->attribute_num();
for (unsigned i = 0; i < n; i++) {
result.push_back(schema->attribute(i).name());
}
return result;
}

void SOMAArray::write(bool sort_coords) {
if (mq_->query_type() != TILEDB_WRITE) {
throw TileDBSOMAError("[SOMAArray] array must be opened in write mode");
Expand Down
11 changes: 9 additions & 2 deletions libtiledbsoma/src/soma/soma_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ class SOMAArray : public SOMAObject {
uint64_t ndim() const;

/**
* @brief Get the name of each dimensions.
* @brief Get the name of each dimension.
*
* @return std::vector<std::string> Name of each dimensions.
* @return std::vector<std::string> Name of each dimension.
*/
std::vector<std::string> dimension_names() const;

Expand All @@ -297,6 +297,13 @@ class SOMAArray : public SOMAObject {
*/
bool has_dimension_name(const std::string& name) const;

/**
* @brief Get the name of each attribute.
*
* @return std::vector<std::string> Name of each attribute.
*/
std::vector<std::string> attribute_names() const;

/**
* @brief Set the dimension slice using one point
*
Expand Down
Loading