-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest-roundtrip-uns.R
More file actions
122 lines (101 loc) · 2.88 KB
/
test-roundtrip-uns.R
File metadata and controls
122 lines (101 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
skip_if_no_anndata()
skip_if_no_dummy_anndata()
library(reticulate)
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) {
# 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()
)
# 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")
)
# write to file
adata_py$write_h5ad(file_py)
# Read it back in to get the version as read from disk
adata_py <- ad$read_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)
adata_r <- read_h5ad(file_py, as = "HDF5AnnData")
expect_equal(
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)
})
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)
adata_r <- read_h5ad(file_py, as = "HDF5AnnData")
py_value <- convert_py_value(adata_py$uns[[name]], name)
expect_equal(
adata_r$uns[[name]],
py_value
)
}
)
gc()
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
)
skip_if(!is.null(msg), message = msg)
adata_r <- read_h5ad(file_py, as = "InMemoryAnnData")
write_h5ad(adata_r, file_r)
# read from file
adata_py2 <- ad$read_h5ad(file_r)
# 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)
)
})
}