/
test_io_elementwise.py
331 lines (265 loc) · 9.93 KB
/
test_io_elementwise.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""
Tests that each element in an anndata is written correctly
"""
from __future__ import annotations
import re
import h5py
import numpy as np
import pandas as pd
import pytest
import zarr
from scipy import sparse
import anndata as ad
from anndata._io.specs import _REGISTRY, IOSpec, get_spec, read_elem, write_elem
from anndata._io.specs.registry import IORegistryError
from anndata.compat import H5Group, ZarrGroup, _read_attr
from anndata.tests.helpers import (
as_cupy_type,
assert_equal,
gen_adata,
pytest_8_raises,
)
@pytest.fixture(params=["h5ad", "zarr"])
def diskfmt(request):
return request.param
@pytest.fixture(scope="function", params=["h5", "zarr"])
def store(request, tmp_path) -> H5Group | ZarrGroup:
if request.param == "h5":
file = h5py.File(tmp_path / "test.h5", "w")
store = file["/"]
elif request.param == "zarr":
store = zarr.open(tmp_path / "test.zarr", "w")
else:
assert False
try:
yield store
finally:
if request.param == "h5":
file.close()
@pytest.mark.parametrize(
"value,encoding_type",
[
("hello world", "string"),
(np.str_("hello world"), "string"),
(np.array([1, 2, 3]), "array"),
(np.array(["hello", "world"], dtype=object), "string-array"),
(1, "numeric-scalar"),
(True, "numeric-scalar"),
(1.0, "numeric-scalar"),
({"a": 1}, "dict"),
(gen_adata((3, 2)), "anndata"),
(sparse.random(5, 3, format="csr", density=0.5), "csr_matrix"),
(sparse.random(5, 3, format="csc", density=0.5), "csc_matrix"),
(pd.DataFrame({"a": [1, 2, 3]}), "dataframe"),
(pd.Categorical(list("aabccedd")), "categorical"),
(pd.Categorical(list("aabccedd"), ordered=True), "categorical"),
(pd.Categorical([1, 2, 1, 3], ordered=True), "categorical"),
(
pd.arrays.IntegerArray(
np.ones(5, dtype=int), mask=np.array([True, False, True, False, True])
),
"nullable-integer",
),
(pd.array([1, 2, 3]), "nullable-integer"),
(
pd.arrays.BooleanArray(
np.random.randint(0, 2, size=5, dtype=bool),
mask=np.random.randint(0, 2, size=5, dtype=bool),
),
"nullable-boolean",
),
(pd.array([True, False, True, True]), "nullable-boolean"),
# (bytes, b"some bytes", "bytes"), # Does not work for zarr
# TODO consider how specific encodings should be. Should we be fully describing the written type?
# Currently the info we add is: "what you wouldn't be able to figure out yourself"
# but that's not really a solid rule.
# (bool, True, "bool"),
# (bool, np.bool_(False), "bool"),
],
)
def test_io_spec(store, value, encoding_type):
key = f"key_for_{encoding_type}"
write_elem(store, key, value, dataset_kwargs={})
assert encoding_type == _read_attr(store[key].attrs, "encoding-type")
from_disk = read_elem(store[key])
assert_equal(value, from_disk)
assert get_spec(store[key]) == _REGISTRY.get_spec(value)
# Can't instantiate cupy types at the top level, so converting them within the test
@pytest.mark.gpu
@pytest.mark.parametrize(
"value,encoding_type",
[
(np.array([1, 2, 3]), "array"),
(np.arange(12).reshape(4, 3), "array"),
(sparse.random(5, 3, format="csr", density=0.5), "csr_matrix"),
(sparse.random(5, 3, format="csc", density=0.5), "csc_matrix"),
],
)
def test_io_spec_cupy(store, value, encoding_type):
"""Tests that"""
key = f"key_for_{encoding_type}"
print(type(value))
value = as_cupy_type(value)
print(type(value))
write_elem(store, key, value, dataset_kwargs={})
assert encoding_type == _read_attr(store[key].attrs, "encoding-type")
from_disk = as_cupy_type(read_elem(store[key]))
assert_equal(value, from_disk)
assert get_spec(store[key]) == _REGISTRY.get_spec(value)
@pytest.mark.parametrize("sparse_format", ["csr", "csc"])
def test_dask_write_sparse(store, sparse_format):
import dask.array as da
X = sparse.random(
1000,
1000,
format=sparse_format,
density=0.01,
random_state=np.random.default_rng(),
)
X_dask = da.from_array(X, chunks=(100, 100))
write_elem(store, "X", X)
write_elem(store, "X_dask", X_dask)
X_from_disk = read_elem(store["X"])
X_dask_from_disk = read_elem(store["X_dask"])
assert_equal(X_from_disk, X_dask_from_disk)
assert_equal(dict(store["X"].attrs), dict(store["X_dask"].attrs))
def test_io_spec_raw(store):
adata = gen_adata((3, 2))
adata.raw = adata
write_elem(store, "adata", adata)
assert "raw" == _read_attr(store["adata/raw"].attrs, "encoding-type")
from_disk = read_elem(store["adata"])
assert_equal(from_disk.raw, adata.raw)
def test_write_anndata_to_root(store):
adata = gen_adata((3, 2))
write_elem(store, "/", adata)
from_disk = read_elem(store)
assert "anndata" == _read_attr(store.attrs, "encoding-type")
assert_equal(from_disk, adata)
@pytest.mark.parametrize(
["attribute", "value"],
[
("encoding-type", "floob"),
("encoding-version", "10000.0"),
],
)
def test_read_iospec_not_found(store, attribute, value):
adata = gen_adata((3, 2))
write_elem(store, "/", adata)
store["obs"].attrs.update({attribute: value})
with pytest.raises(IORegistryError) as exc_info:
read_elem(store)
msg = str(exc_info.value)
assert "No read method registered for IOSpec" in msg
assert f"{attribute.replace('-', '_')}='{value}'" in msg
@pytest.mark.parametrize(
["obj"],
[(b"x",)],
)
def test_write_io_error(store, obj):
full_pattern = re.compile(
rf"No method registered for writing {type(obj)} into .*Group"
)
with pytest_8_raises(IORegistryError, match=r"while writing key '/el'") as exc_info:
write_elem(store, "/el", obj)
msg = str(exc_info.value)
assert re.search(full_pattern, msg)
def test_categorical_order_type(store):
# https://github.com/scverse/anndata/issues/853
cat = pd.Categorical([0, 1], ordered=True)
write_elem(store, "ordered", cat)
write_elem(store, "unordered", cat.set_ordered(False))
assert isinstance(read_elem(store["ordered"]).ordered, bool)
assert read_elem(store["ordered"]).ordered is True
assert isinstance(read_elem(store["unordered"]).ordered, bool)
assert read_elem(store["unordered"]).ordered is False
def test_override_specification():
"""
Test that trying to overwrite an existing encoding raises an error.
"""
from copy import deepcopy
registry = deepcopy(_REGISTRY)
with pytest.raises(TypeError):
@registry.register_write(
ZarrGroup, ad.AnnData, IOSpec("some new type", "0.1.0")
)
def _(store, key, adata):
pass
@pytest.mark.parametrize(
"value",
[
pytest.param({"a": 1}, id="dict"),
pytest.param(gen_adata((3, 2)), id="anndata"),
pytest.param(sparse.random(5, 3, format="csr", density=0.5), id="csr_matrix"),
pytest.param(sparse.random(5, 3, format="csc", density=0.5), id="csc_matrix"),
pytest.param(pd.DataFrame({"a": [1, 2, 3]}), id="dataframe"),
pytest.param(pd.Categorical(list("aabccedd")), id="categorical"),
pytest.param(
pd.Categorical(list("aabccedd"), ordered=True), id="categorical-ordered"
),
pytest.param(
pd.Categorical([1, 2, 1, 3], ordered=True), id="categorical-numeric"
),
pytest.param(
pd.arrays.IntegerArray(
np.ones(5, dtype=int), mask=np.array([True, False, True, False, True])
),
id="nullable-integer",
),
pytest.param(pd.array([1, 2, 3]), id="nullable-integer-no-nulls"),
pytest.param(
pd.arrays.BooleanArray(
np.random.randint(0, 2, size=5, dtype=bool),
mask=np.random.randint(0, 2, size=5, dtype=bool),
),
id="nullable-boolean",
),
pytest.param(
pd.array([True, False, True, True]), id="nullable-boolean-no-nulls"
),
],
)
def test_write_to_root(store, value):
"""
Test that elements which are written as groups can we written to the root group.
"""
write_elem(store, "/", value)
result = read_elem(store)
assert_equal(result, value)
@pytest.mark.parametrize("consolidated", [True, False])
def test_read_zarr_from_group(tmp_path, consolidated):
# https://github.com/scverse/anndata/issues/1056
pth = tmp_path / "test.zarr"
adata = gen_adata((3, 2))
with zarr.open(pth, mode="w") as z:
write_elem(z, "table/table", adata)
if consolidated:
zarr.convenience.consolidate_metadata(z.store)
if consolidated:
read_func = zarr.open_consolidated
else:
read_func = zarr.open
with read_func(pth) as z:
expected = ad.read_zarr(z["table/table"])
assert_equal(adata, expected)
def test_dataframe_column_uniqueness(store):
repeated_cols = pd.DataFrame(np.ones((3, 2)), columns=["a", "a"])
with pytest_8_raises(
ValueError,
match=r"Found repeated column names: \['a'\]\. Column names must be unique\.",
):
write_elem(store, "repeated_cols", repeated_cols)
index_shares_col_name = pd.DataFrame(
{"col_name": [1, 2, 3]}, index=pd.Index([1, 3, 2], name="col_name")
)
with pytest_8_raises(
ValueError,
match=r"DataFrame\.index\.name \('col_name'\) is also used by a column whose values are different\.",
):
write_elem(store, "index_shares_col_name", index_shares_col_name)
index_shared_okay = pd.DataFrame(
{"col_name": [1, 2, 3]}, index=pd.Index([1, 2, 3], name="col_name")
)
write_elem(store, "index_shared_okay", index_shared_okay)
result = read_elem(store["index_shared_okay"])
assert_equal(result, index_shared_okay)