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

Reticulate backend #48

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(py_to_r,collections.abc.Mapping)
S3method(py_to_r,pandas.core.indexes.base.Index)
export(AnnData)
export(InMemoryAnnData)
export(from_Seurat)
Expand All @@ -8,6 +10,7 @@ export(generate_dataset)
export(read_h5ad)
export(to_HDF5AnnData)
export(to_InMemoryAnnData)
export(to_ReticulateAnnData)
export(to_Seurat)
export(to_SingleCellExperiment)
export(write_h5ad)
Expand Down
7 changes: 6 additions & 1 deletion R/AbstractAnnData.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,14 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint
to_Seurat = function() {
to_Seurat(self)
},
#' @description Convert to an InMemory AnnData
#' @description Convert to an InMemoryAnnData
to_InMemoryAnnData = function() {
to_InMemoryAnnData(self)
},
#' @description Convert to a ReticulateAnnData
to_ReticulateAnnData = function() {
to_ReticulateAnnData(self)
},
#' @description Convert to an HDF5 Backed AnnData
#' @param path The path to the HDF5 file
to_HDF5AnnData = function(path) {
Expand All @@ -178,6 +182,7 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint
# @param mat A matrix to validate
# @param label Must be `"X"` or `"layer[[...]]"` where `...` is
# the name of a layer.

# @param shape Expected dimensions of matrix
# @param expected_rownames
# @param excepted_colnames
Expand Down
52 changes: 52 additions & 0 deletions R/Reticulate-Read.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

# copied from https://github.com/dynverse/anndata/blob/main/R/reticulate_conversions.R#L1/
py_to_r_ifneedbe <- function(x) {
requireNamespace("reticulate")
if (inherits(x, "python.builtin.object")) {
reticulate::py_to_r(x)
} else {
x
}
}

#' Convert between Python and R objects
#'
#' @param x A Python object.
#' @param name A name
#' @param value A value
#'
#' @return An \R object, as converted from the Python object.
#'
#' @name r-py-conversion
#' @export
# copied from https://github.com/dynverse/anndata/blob/main/R/reticulate_conversions.R#L61
py_to_r.pandas.core.indexes.base.Index <- function(x) { # nolint
requireNamespace("reticulate")
python_builtins <- reticulate::import_builtins()
out <- python_builtins$list(x)
attr(out, "name") <- py_to_r_ifneedbe(x$name)
out
}

#' @name r-py-conversion
#' @export
# copied from https://github.com/dynverse/anndata/blob/main/R/reticulate_conversions.R#LL77C38-L90C2
py_to_r.collections.abc.Mapping <- function(x) { # nolint
requireNamespace("reticulate")
python_builtins <- reticulate::import_builtins()

x_list <- python_builtins$dict(x)

# convert members of x_list if need be
for (i in seq_along(x_list)) {
if (inherits(x_list[[i]], "python.builtin.object")) {
x_list[[i]] <- py_to_r_ifneedbe(x_list[[i]])
}
}

x_list
}

# `py_to_r.collections.abc.Set` ?
# `py_to_r.collections.abc.KeysView` ?
# `py_to_r.scipy.sparse.csc.csc_matrix` ?
Loading
Loading