Skip to content

Commit

Permalink
avoid reticulate conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Nov 9, 2024
1 parent 39dbb15 commit cd2f80f
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions tests/testthat/test-roundtrip-obsvar.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ testthat::skip_if_not(
message = "Python dummy_anndata module not available for testing"
)

ad <- reticulate::import("anndata")
da <- reticulate::import("dummy_anndata")
ad <- reticulate::import("anndata", convert = FALSE)
da <- reticulate::import("dummy_anndata", convert = FALSE)
pd <- reticulate::import("pandas", convert = FALSE)
bi <- reticulate::import_builtins()

test_names <- names(da$vector_generators)

Expand All @@ -25,29 +27,30 @@ for (name in test_names) {
uns_types = list()
)
# remove uns
adata_py$uns <- NULL
adata_py$uns <- bi$dict()
# TODO: remove X

# write to file
filename <- withr::local_file(tempfile(fileext = ".h5ad"))
adata_py$write_h5ad(filename)

# read from file
adata_r <- read_h5ad(filename, to = "HDF5AnnData")
adata_r <- read_h5ad(filename, to = "InMemoryAnnData")

# expect slots are unchanged
expect_equal(
adata_r$obs[[name]],
adata_py$obs[[name]],
ignore_attr = TRUE,
tolerance = 1e-6
)
expect_equal(
adata_r$var[[name]],
adata_py$var[[name]],
ignore_attr = TRUE,
tolerance = 1e-6
)
# if we would test the objects at this stage,
# we're also testing reticulate's conversion
# nolint start
# expect_equal(
# adata_r$obs[[name]],
# py_to_r(adata_py$obs[[name]]),
# tolerance = 1e-6
# )
# expect_equal(
# adata_r$var[[name]],
# py_to_r(adata_py$var[[name]]),
# tolerance = 1e-6
# )
# nolint end

# write back to file
filename2 <- withr::local_file(tempfile(fileext = ".h5ad"))
Expand All @@ -56,19 +59,25 @@ for (name in test_names) {
# read from file
adata_py2 <- ad$read_h5ad(filename2)

# expect slots are unchanged
expect_equal(
adata_py2$obs[[name]],
adata_py$obs[[name]],
ignore_attr = TRUE,
tolerance = 1e-6
reticulate::py_to_r(adata_py2$shape),
reticulate::py_to_r(adata_py$shape)
)

expect_equal(
adata_py2$var[[name]],
adata_py$var[[name]],
ignore_attr = TRUE,
tolerance = 1e-6
# expect slots are unchanged
zz <- pd$testing$assert_frame_equal(
adata_py$obs,
adata_py2$obs,
check_dtype = FALSE,
check_exact = FALSE
)
expect_null(reticulate::py_to_r(zz))
pd$testing$assert_frame_equal(
adata_py$var,
adata_py2$var,
check_dtype = FALSE,
check_exact = FALSE
)
expect_null(reticulate::py_to_r(zz))
})
}

0 comments on commit cd2f80f

Please sign in to comment.