Skip to content

Commit 7948807

Browse files
committed
expand tests to cover incomplete metadata
1 parent 69ca410 commit 7948807

File tree

7 files changed

+114
-27
lines changed

7 files changed

+114
-27
lines changed

tests/fixtures/metadatas/empty.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"axis_names": [
3+
"c0^",
4+
"c1^",
5+
"d0",
6+
"d1",
7+
"d2"
8+
]
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"units": [
3+
"",
4+
"",
5+
""
6+
]
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"offset": [
3+
0,
4+
0,
5+
0
6+
],
7+
"voxel_size": [
8+
1,
9+
1,
10+
1
11+
],
12+
"axis_names": [
13+
"c0^",
14+
"c1^",
15+
"d0",
16+
"d1",
17+
"d2"
18+
]
19+
}

tests/test_array.py

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ def test_constructor():
1717
Array(data, offset, (2, 2, 2))
1818

1919
# dims don't match
20-
with pytest.raises(AssertionError):
20+
with pytest.raises(ValueError):
2121
Array(data, offset, (1, 1))
2222

2323
Array(data, offset, (1, 1, 3))
2424
Array(data, offset, (1, 1, 4))
2525

2626
Array(data, (1, 1, 1), (1, 1, 2))
2727

28+
2829
def test_dtype():
2930
for dtype in [np.float32, np.uint8, np.uint64]:
3031
assert Array(np.zeros((1,), dtype=dtype), (0,), (1,)).dtype == dtype
3132

33+
3234
def test_getitem():
3335
a = Array(np.arange(0, 10).reshape(2, 5), (0, 0), (1, 1))
3436

@@ -62,6 +64,7 @@ def test_getitem():
6264
b = a[Roi((1, 1), (1, 4))]
6365
np.testing.assert_array_equal(b, [[6, 7, 8, 9]])
6466

67+
6568
def test_setitem():
6669
# set entirely with numpy array
6770

@@ -136,6 +139,7 @@ def test_setitem():
136139
assert a[Coordinate((1, 1))] == 1
137140
assert a[Coordinate((1, 2))] == 2
138141

142+
139143
def test_to_ndarray():
140144
a = Array(np.arange(0, 10).reshape(2, 5), (0, 0), (1, 1))
141145

@@ -163,8 +167,11 @@ def test_to_ndarray():
163167
)
164168
np.testing.assert_array_equal(b, compare)
165169

170+
166171
def test_to_ndarray_with_slices():
167-
a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=slice(0, 1))
172+
a = Array(
173+
np.arange(0, 10 * 10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=slice(0, 1)
174+
)
168175

169176
b = a.to_ndarray(Roi((0, 0), (1, 5)))
170177
compare = np.array([[[0, 1, 2, 3, 4]]])
@@ -180,32 +187,43 @@ def test_to_ndarray_with_slices():
180187

181188
b = a.to_ndarray(Roi((0, 0), (5, 5)), fill_value=1)
182189
compare = np.array(
183-
[[
184-
[0, 1, 2, 3, 4],
185-
[5, 6, 7, 8, 9],
186-
[1, 1, 1, 1, 1],
187-
[1, 1, 1, 1, 1],
188-
[1, 1, 1, 1, 1],
189-
]]
190+
[
191+
[
192+
[0, 1, 2, 3, 4],
193+
[5, 6, 7, 8, 9],
194+
[1, 1, 1, 1, 1],
195+
[1, 1, 1, 1, 1],
196+
[1, 1, 1, 1, 1],
197+
]
198+
]
190199
)
191200
np.testing.assert_array_equal(b, compare)
192201

202+
193203
def test_adapters():
194-
a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1))
204+
a = Array(np.arange(0, 10 * 10).reshape(10, 2, 5), (0, 0), (1, 1))
195205
assert a.dtype == int
196206

197-
a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=lambda x: x > 2)
207+
a = Array(
208+
np.arange(0, 10 * 10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=lambda x: x > 2
209+
)
198210
assert a.dtype == bool
199211

200-
a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=lambda x: x + 0.5)
212+
a = Array(
213+
np.arange(0, 10 * 10).reshape(10, 2, 5),
214+
(0, 0),
215+
(1, 1),
216+
adapter=lambda x: x + 0.5,
217+
)
201218
assert a.dtype == float
202219

220+
203221
def test_slicing():
204-
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))
205-
222+
a = Array(np.arange(0, 4 * 4).reshape(4, 2, 2), (0, 0), (1, 1))
223+
206224
a.adapt(np.s_[0:3, 1, :])
207225
assert a.shape == (3, 2)
208-
assert a.axis_names == ["c0^", "d1"]
226+
assert a.axis_names == ["c0^", "d1"], a.axis_names
209227
assert a.units == [""]
210228

211229
a.adapt(np.s_[2, :])
@@ -217,11 +235,10 @@ def test_slicing():
217235

218236
assert all([x == 42 for x in a._source_data[2, 1, :]]), a._source_data[2, 1, :]
219237

220-
221238
# test with list indexing
222-
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))
239+
a = Array(np.arange(0, 4 * 4).reshape(4, 2, 2), (0, 0), (1, 1))
223240

224-
a.adapt(np.s_[[0,1,2], 1, :])
241+
a.adapt(np.s_[[0, 1, 2], 1, :])
225242
assert a.shape == (3, 2)
226243
assert a.axis_names == ["c0^", "d1"]
227244
assert a.units == [""]
@@ -235,26 +252,24 @@ def test_slicing():
235252

236253
assert all([x == 42 for x in a._source_data[2, 1, :]]), a._source_data[:]
237254

238-
239255
# test weird case
240-
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))
256+
a = Array(np.arange(0, 4 * 4).reshape(4, 2, 2), (0, 0), (1, 1))
241257

242-
a.adapt(np.s_[[2,2,2], 1, :])
258+
a.adapt(np.s_[[2, 2, 2], 1, :])
243259
assert a.shape == (3, 2)
244260
assert a.axis_names == ["c0^", "d1"]
245261
assert a.units == [""]
246262

247263
a[:, :] = np.array([42, 43, 44]).reshape(3, 1)
248264
assert all([x == 44 for x in a._source_data[2, 1, :]]), a._source_data[2, 1, :]
249265

250-
251266
# test_bool_indexing
252-
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))
267+
a = Array(np.arange(0, 4 * 4).reshape(4, 2, 2), (0, 0), (1, 1))
253268

254269
a.adapt(np.s_[np.array([True, True, True, False]), 1, :])
255270
assert a.shape == (3, 2)
256271
assert a.axis_names == ["c0^", "d1"]
257272
assert a.units == [""]
258-
273+
259274
with pytest.raises(RuntimeError):
260275
a[:, :] = np.array([42, 43, 44]).reshape(3, 1)

tests/test_datasets.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
@pytest.mark.parametrize("store", stores.keys())
2121
@pytest.mark.parametrize("dtype", [np.float32, np.uint8, np.uint64])
2222
def test_helpers(tmpdir, store, dtype):
23+
shape = Coordinate(1, 1, 10, 20, 30)
24+
chunk_shape = Coordinate(2, 3, 10, 10, 10)
2325
store = tmpdir / store
2426
metadata = MetaDataFormat().parse(
27+
shape,
2528
{
2629
"offset": [100, 200, 400],
2730
"voxel_size": [1, 2, 3],
2831
"axis_names": ["sample^", "channel^", "z", "y", "x"],
2932
"units": ["nm", "nm", "nm"],
3033
}
3134
)
32-
shape = Coordinate(1, 1, 10, 20, 30)
33-
chunk_shape = Coordinate(2, 3, 10, 10, 10)
3435

3536
# test prepare_ds fails if array does not exist and mode is read
3637
with pytest.raises(ArrayNotFoundError):

tests/test_metadata.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88

99
fixtures_dir = Path(__file__).parent / "fixtures"
10+
incomplete_metadatas = {
11+
"incomplete1": "metadatas/incomplete1.json",
12+
"incomplete2": "metadatas/incomplete2.json",
13+
"incomplete3": "metadatas/incomplete3.json",
14+
}
15+
incomplete_metadata_formats = {
16+
"incomplete1": MetaDataFormat(),
17+
"incomplete2": MetaDataFormat(),
18+
"incomplete3": MetaDataFormat(),
19+
}
1020
metadata_jsons = {
1121
"default": "metadatas/default.json",
1222
"simple": "metadatas/simple.json",
@@ -27,10 +37,12 @@
2737
),
2838
}
2939

40+
3041
@pytest.fixture(params=metadata_jsons.keys())
3142
def metadata(request):
3243
return metadata_formats[request.param].parse(
33-
json.loads(open(fixtures_dir / metadata_jsons[request.param]).read())
44+
(10, 2, 100, 100, 100),
45+
json.loads(open(fixtures_dir / metadata_jsons[request.param]).read()),
3446
)
3547

3648

@@ -39,3 +51,26 @@ def test_parse_metadata(metadata):
3951
assert metadata.voxel_size == Coordinate(1, 2, 3)
4052
assert metadata.axis_names == ["sample^", "channel^", "z", "y", "x"]
4153
assert metadata.units == ["nm", "nm", "nm"]
54+
55+
56+
@pytest.fixture(params=incomplete_metadatas.keys())
57+
def incomplete_metadata(request):
58+
return incomplete_metadata_formats[request.param].parse(
59+
(10, 2, 100, 100, 100),
60+
json.loads(open(fixtures_dir / incomplete_metadatas[request.param]).read()),
61+
)
62+
63+
64+
def test_parse_incomplete_metadata(incomplete_metadata):
65+
assert incomplete_metadata.offset == Coordinate(0, 0, 0)
66+
assert incomplete_metadata.voxel_size == Coordinate(1, 1, 1)
67+
assert incomplete_metadata.axis_names == ["c0^", "c1^", "d0", "d1", "d2"]
68+
assert incomplete_metadata.units == ["", "", ""]
69+
70+
71+
def test_empty_metadata():
72+
metadata = MetaDataFormat().parse((10, 2, 100, 100, 100), {})
73+
assert metadata.offset == Coordinate(0, 0, 0, 0, 0)
74+
assert metadata.voxel_size == Coordinate(1, 1, 1, 1, 1)
75+
assert metadata.axis_names == ["d0", "d1", "d2", "d3", "d4"]
76+
assert metadata.units == ["", "", "", "", ""]

0 commit comments

Comments
 (0)