Skip to content

Commit

Permalink
also include arrays in obsmvarm
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Nov 14, 2024
1 parent 9dc494f commit e08932c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 68 deletions.
50 changes: 50 additions & 0 deletions inst/known_issues.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,53 @@ known_issues:
proposed_solution: Debug and fix
to_investigate: True
to_fix: True
- backend: HDF5AnnData
slot:
- obsm
- varm
dtype:
- boolean_array
- categorical
- categorical_missing_values
- categorical_ordered
- categorical_ordered_missing_values
- dense_array
- integer_array
- nullable_boolean_array
- nullable_integer_array
- string_array
process: [reticulate]
error_message: |
adata_r$varm[[name]] (`actual`) not equal to py_to_r(py_get_item(adata_py$varm, name)) (`expected`).
`dim(actual)` is absent
`dim(expected)` is an integer vector (20)
description: Python nd.arrays have a dimension while R vectors do not.
proposed_solution: Debug and fix
to_investigate: True
to_fix: True
- backend: HDF5AnnData
slot:
- obsm
- varm
dtype:
- boolean_array
- categorical
- categorical_missing_values
- categorical_ordered
- categorical_ordered_missing_values
- dense_array
- integer_array
- nullable_boolean_array
- nullable_integer_array
- string_array
process: [write]
error_message: |
Error in `if (found_dim != expected_dim) {
stop("dim(", label, ")[", i, "] should have shape: ", expected_dim,
", found: ", found_dim, ".")
}`: argument is of length zero
description: R vectors don't have a dimension.
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
57 changes: 57 additions & 0 deletions tests/testthat/helper-expect_equal_py.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
expect_equal_py <- function(a, b) {
requireNamespace("testthat")
requireNamespace("reticulate")

bi <- reticulate::import_builtins()

testthat::expect_equal(bi$type(a), bi$type(b)) # does this always work?

if (inherits(a, "pandas.core.frame.DataFrame")) {
pd <- reticulate::import("pandas")
testthat::expect_null(
pd$testing$assert_frame_equal(
a,
b,
check_dtype = FALSE,
check_exact = FALSE
)
)
} else if (inherits(a, "np.ndarray") || inherits(a, "scipy.sparse.base.spmatrix")) {
scipy <- reticulate::import("scipy")
np <- reticulate::import("numpy")

testthat::expect_equal(a$dtype, b$dtype)

testthat::expect_equal(
py_to_r_ifneedbe(a$shape),
py_to_r_ifneedbe(b$shape)
)

a_dense <-
if (scipy$sparse$issparse(a)) {
a$toarray()
} else {
a
}
b_dense <-
if (scipy$sparse$issparse(b)) {
b$toarray()
} else {
b
}

testthat::expect_null(
np$testing$assert_allclose(a_dense, b_dense)
)

}
}

py_to_r_ifneedbe <- function(x) {
if (inherits(x, "python.builtin.object")) {
requireNamespace("reticulate")
reticulate::py_to_r(x)
} else {
x
}
}

Check warning on line 57 in tests/testthat/helper-expect_equal_py.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-expect_equal_py.R,line=57,col=2,[trailing_blank_lines_linter] Missing terminal newline.
18 changes: 0 additions & 18 deletions tests/testthat/helper-expect_py_df_equal.R

This file was deleted.

31 changes: 0 additions & 31 deletions tests/testthat/helper-expect_py_matrix_equal.R

This file was deleted.

2 changes: 1 addition & 1 deletion tests/testthat/test-roundtrip-X.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ for (name in test_names) {
adata_py2 <- ad$read_h5ad(file_r)

# expect that the objects are the same
expect_py_matrix_equal(
expect_equal_py(
adata_py2$X,
adata_py$X
)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-roundtrip-layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ for (name in test_names) {
)
expect_equal(
adata_r$layers_keys(),
py_to_r(adata_py$layers$keys())
bi$list(adata_py$layers$keys())
)

# check that the print output is the same
Expand Down Expand Up @@ -100,7 +100,7 @@ for (name in test_names) {
adata_py2 <- ad$read_h5ad(file_r)

# expect that the objects are the same
expect_py_matrix_equal(
expect_equal_py(
py_get_item(adata_py2$layers, name),
py_get_item(adata_py$layers, name)
)
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-roundtrip-obsmvarm.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ testthat::skip_if_not(
message = "Python dummy_anndata module not available for testing"
)

# TODO: make sure data frames in obsm / varm are also tested?
# TODO: create a github issue for this

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

known_issues <- read_known_issues()

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

for (name in test_names) {
# first generate a python h5ad
Expand Down Expand Up @@ -57,11 +57,11 @@ for (name in test_names) {
)
expect_equal(
adata_r$obsm_keys(),
py_to_r(adata_py$obsm$keys())
bi$list(adata_py$obsm$keys())
)
expect_equal(
adata_r$varm_keys(),
py_to_r(adata_py$varm$keys())
bi$list(adata_py$varm$keys())
)

# check that the print output is the same
Expand Down Expand Up @@ -112,11 +112,11 @@ for (name in test_names) {
adata_py2 <- ad$read_h5ad(file_r)

# expect that the objects are the same
expect_py_matrix_equal(
expect_equal_py(
py_get_item(adata_py2$obsm, name),
py_get_item(adata_py$obsm, name)
)
expect_py_matrix_equal(
expect_equal_py(
py_get_item(adata_py2$varm, name),
py_get_item(adata_py$varm, name)
)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-roundtrip-obspvarp.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ for (name in test_names) {
)
expect_equal(
adata_r$obsp_keys(),
py_to_r(adata_py$obsp$keys())
bi$list(adata_py$obsp$keys())
)
expect_equal(
adata_r$varp_keys(),
py_to_r(adata_py$varp$keys())
bi$list(adata_py$varp$keys())
)

# check that the print output is the same
Expand Down Expand Up @@ -109,11 +109,11 @@ for (name in test_names) {
adata_py2 <- ad$read_h5ad(file_r)

# expect that the objects are the same
expect_py_matrix_equal(
expect_equal_py(
py_get_item(adata_py2$obsp, name),
py_get_item(adata_py$obsp, name)
)
expect_py_matrix_equal(
expect_equal_py(
py_get_item(adata_py2$varp, name),
py_get_item(adata_py$varp, name)
)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-roundtrip-obsvar.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ for (name in test_names) {
)
expect_equal(
adata_r$obs_keys(),
py_to_r(adata_py$obs_keys())
bi$list(adata_py$obs_keys())
)
expect_equal(
adata_r$var_keys(),
py_to_r(adata_py$var_keys())
bi$list(adata_py$var_keys())
)

# check that the print output is the same
Expand Down Expand Up @@ -108,7 +108,7 @@ for (name in test_names) {
adata_py2 <- ad$read_h5ad(file_r)

# expect that the objects are the same
expect_py_df_equal(adata_py2$obs, adata_py$obs)
expect_py_df_equal(adata_py2$var, adata_py$var)
expect_equal_py(adata_py2$obs, adata_py$obs)
expect_equal_py(adata_py2$var, adata_py$var)
})
}

0 comments on commit e08932c

Please sign in to comment.