Skip to content

Commit

Permalink
update the uns rountrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Dec 12, 2024
1 parent 5812783 commit e92b671
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 71 deletions.
120 changes: 120 additions & 0 deletions inst/known_issues.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,123 @@ known_issues:
proposed_solution: The input checking function for obsm and varm should allow the object to be a vector of the correct length instead of only a matrix or a data frame.
to_investigate: True
to_fix: True
- backend: HDF5AnnData
slot:
- uns
dtype:
- empty
- none
process: [read]
error_message: |
Error: names(adata_r$uns) (`actual`) not equal to reticulate::py_to_r(adata_py$uns) (`expected`).
`actual` is NULL
`expected` is a list
description: None's are being dropped from uns
proposed_solution: Debug and fix
to_investigate: True
to_fix: True
- backend: HDF5AnnData
slot:
- uns
dtype:
- boolean_array
process: [reticulate]
error_message: |
adata_r$uns[[name]] (`actual`) not equal to reticulate::py_to_r(adata_py$uns[[name]]) (`expected`).
`dim(actual)` is absent
`dim(expected)` is an integer vector (10)
description: The python object has a dimension while the R object does not.
proposed_solution: Think about whether this is a problem or not. If it isn't, fix the unit test.
to_investigate: True
to_fix: True
- backend: HDF5AnnData
slot:
- uns
dtype:
- boolean_array
- dense_array
- integer_array
- string_array
process: [reticulate]
error_message: |
adata_r$uns[[name]] (`actual`) not equal to reticulate::py_to_r(adata_py$uns[[name]]) (`expected`).
`dim(actual)` is absent
`dim(expected)` is an integer vector (10)
description: The python object has a dimension while the R object does not.
proposed_solution: Think about whether this is a problem or not. If it isn't, fix the unit test.
to_investigate: True
to_fix: True
- backend: HDF5AnnData
slot:
- uns
dtype:
- categorical
- categorical_missing_values
- categorical_ordered
- categorical_ordered_missing_values
process: [reticulate]
error_message: |
<python.builtin.AttributeError/python.builtin.Exception/python.builtin.BaseException/python.builtin.object/error/condition>
Error in `py_get_attr(x, name)`: AttributeError: 'Categorical' object has no attribute 'get_values'. Did you mean: 'sort_values'?
Run `reticulate::py_last_error()` for details.
Backtrace:
1. ├─testthat::expect_equal(adata_r$uns[[name]], reticulate::py_to_r(adata_py$uns[[name]])) at test-roundtrip-uns.R:80:5
2. │ └─testthat::quasi_label(enquo(expected), expected.label, arg = "expected")
3. │ └─rlang::eval_bare(expr, quo_get_env(quo))
4. ├─reticulate::py_to_r(adata_py$uns[[name]])
5. └─reticulate:::py_to_r.pandas.core.arrays.categorical.Categorical(adata_py$uns[[name]])
6. ├─reticulate::py_to_r(x$get_values())
7. │ ├─reticulate::is_py_object(x <- py_to_r_cpp(x))
8. │ └─reticulate:::py_to_r_cpp(x)
9. ├─x$get_values
10. └─reticulate:::`$.python.builtin.object`(x, "get_values")
11. └─reticulate:::py_get_attr_or_item(x, name, TRUE)
12. └─reticulate::py_get_attr(x, name)
description: Python object is not being converted correctly.
proposed_solution: Debug and fix
to_investigate: True
to_fix: True
- backend: HDF5AnnData
slot:
- uns
dtype:
- nullable_boolean_array
- nullable_integer_array
process: [reticulate]
error_message: |
adata_r$uns[[name]] (`actual`) not equal to reticulate::py_to_r(adata_py$uns[[name]]) (`expected`).
`actual` is a logical vector (NA, FALSE, TRUE, FALSE, TRUE, ...)
`expected` is an S3 object of class <pandas.core.arrays.boolean.BooleanArray/pandas.core.arrays.masked.BaseMaskedArray/pandas.core.arraylike.OpsMixin/pandas.core.arrays.base.ExtensionArray/python.builtin.object>, an environment
description: Python object is not being converted correctly.
proposed_solution: Debug and fix
to_investigate: True
to_fix: True
- backend: HDF5AnnData
slot:
- uns
dtype:
- boolean
- char
- float
- integer
- nan
- string
process: [write]
error_message: |
bi$type(a) (`actual`) not equal to bi$type(b) (`expected`).
`attr(actual, 'py_object')$pyobj` is <pointer: 0x7f4af9694d00>
`attr(expected, 'py_object')$pyobj` is <pointer: 0x7f4af9f5eca0>
Backtrace:
1. └─anndataR:::expect_equal_py(...) at test-roundtrip-uns.R:109:5
2. └─testthat::expect_equal(bi$type(a), bi$type(b)) at tests/testthat/helper-expect_equal_py.R:7:3
description: The data type is different after the roundtrip test.
proposed_solution: Debug and fix
to_investigate: True
to_fix: True
160 changes: 89 additions & 71 deletions tests/testthat/test-roundtrip-uns.R
Original file line number Diff line number Diff line change
@@ -1,95 +1,113 @@
skip_if_no_anndata()
skip_if_not_installed("hdf5r")

data <- generate_dataset(10L, 20L)

test_names <- names(data$uns)
# TODO: re-enable these tests
test_names <- test_names[!grepl("_with_nas", test_names)]
# TODO: re-enable these tests
test_names <- test_names[!grepl("_na$", test_names)]
# TODO: re-enable these tests
test_names <- test_names[!grepl("mat_", test_names)]
# TODO: re-enable these tests
test_names <- test_names[!test_names %in% c("vec_factor", "vec_factor_ordered", "vec_logical")]
# TODO: re-enable these tests
test_names <- test_names[test_names != "list"]
# TODO: re-enable these tests
test_names <- test_names[!test_names %in% c("scalar_factor", "scalar_factor_ordered", "scalar_logical")]
skip_if_not_installed("reticulate")

library(reticulate)
testthat::skip_if_not(
reticulate::py_module_available("dummy_anndata"),
message = "Python dummy_anndata module not available for testing"
)

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

known_issues <- read_known_issues()

test_names <- c(
names(da$matrix_generators),
names(da$vector_generators),
names(da$scalar_generators)
)

for (name in test_names) {
test_that(paste0("roundtrip with uns '", name, "'"), {
# create anndata
ad <- AnnData(
obs = data$obs[, c(), drop = FALSE],
var = data$var[, c(), drop = FALSE],
uns = data$uns[name]
)
# first generate a python h5ad
adata_py <- da$generate_dataset(
x_type = NULL,
obs_types = list(),
var_types = list(),
layer_types = list(),
obsm_types = list(),
varm_types = list(),
obsp_types = list(),
varp_types = list(),
uns_types = list(name),
nested_uns_types = list()
)

# write to file
filename <- withr::local_file(tempfile(fileext = ".h5ad"))
write_h5ad(ad, filename)
# create a couple of paths
file_py <- withr::local_file(tempfile(paste0("anndata_py_", name), fileext = ".h5ad"))
file_r <- withr::local_file(tempfile(paste0("anndata_r_", name), fileext = ".h5ad"))

# read from file
ad_new <- read_h5ad(filename, to = "HDF5AnnData")
# write to file
adata_py$write_h5ad(file_py)

test_that(paste0("Reading an AnnData with uns '", name, "' works"), {
msg <- message_if_known(
backend = "HDF5AnnData",
slot = c("uns"),
dtype = name,
process = "read",
known_issues = known_issues
)
skip_if(!is.null(msg), message = msg)

# expect slots are unchanged
adata_r <- read_h5ad(file_py, to = "HDF5AnnData")
expect_equal(
ad_new$uns,
data$uns[name],
ignore_attr = TRUE,
tolerance = 1e-6
names(adata_r$uns),
bi$list(adata_py$uns$keys())
)

# check that the print output is the same
str_r <- capture.output(print(adata_r))
str_py <- capture.output(print(adata_py))
expect_equal(str_r, str_py)
})
}

for (name in test_names) {
test_that(paste0("reticulate->hdf5 with uns '", name, "'"), {
# create anndata
ad <- anndata::AnnData(
obs = data$obs[, c(), drop = FALSE],
var = data$var[, c(), drop = FALSE],
uns = data$uns[name]
# maybe this test simply shouldn't be run if there is a known issue with reticulate
test_that(paste0("Comparing an anndata with uns '", name, "' with reticulate works"), {
msg <- message_if_known(
backend = "HDF5AnnData",
slot = c("uns"),
dtype = name,
process = c("read", "reticulate"),
known_issues = known_issues
)
skip_if(!is.null(msg), message = msg)

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

# read from file
ad_new <- HDF5AnnData$new(filename)
adata_r <- read_h5ad(file_py, to = "HDF5AnnData")

# expect slots are unchanged
expect_equal(
ad_new$uns,
data$uns[name],
ignore_attr = TRUE,
tolerance = 1e-6
adata_r$uns[[name]],
reticulate::py_to_r(adata_py$uns[[name]])
)
})
}

for (name in test_names) {
test_that(paste0("hdf5->reticulate with uns '", name, "'"), {
# write to file
filename <- withr::local_file(tempfile(fileext = ".h5ad"))

# make anndata
ad <- AnnData(
obs = data$obs[, c(), drop = FALSE],
var = data$var[, c(), drop = FALSE],
uns = data$uns[name]
test_that(paste0("Writing an AnnData with uns '", name, "' works"), {
msg <- message_if_known(
backend = "HDF5AnnData",
slot = c("uns"),
dtype = name,
process = c("read", "write"),
known_issues = known_issues
)
write_h5ad(ad, filename)
skip_if(!is.null(msg), message = msg)

adata_r <- read_h5ad(file_py, to = "InMemoryAnnData")
write_h5ad(adata_r, file_r)

# read from file
ad_new <- anndata::read_h5ad(filename)
adata_py2 <- ad$read_h5ad(file_r)

expect_equal(
ad_new$uns,
data$uns[name],
ignore_attr = TRUE,
tolerance = 1e-6
# expect name is one of the keys
expect_contains(
bi$list(adata_py2$uns$keys()),
name
)

# expect that the objects are the same
expect_equal_py(
py_get_item(adata_py2$uns, name),
py_get_item(adata_py$uns, name)
)
})
}

0 comments on commit e92b671

Please sign in to comment.