Skip to content

Commit

Permalink
expand tests to cover incomplete metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
pattonw committed Jul 31, 2024
1 parent 69ca410 commit 7948807
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 27 deletions.
1 change: 1 addition & 0 deletions tests/fixtures/metadatas/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions tests/fixtures/metadatas/incomplete1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"axis_names": [
"c0^",
"c1^",
"d0",
"d1",
"d2"
]
}
7 changes: 7 additions & 0 deletions tests/fixtures/metadatas/incomplete2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"units": [
"",
"",
""
]
}
19 changes: 19 additions & 0 deletions tests/fixtures/metadatas/incomplete3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"offset": [
0,
0,
0
],
"voxel_size": [
1,
1,
1
],
"axis_names": [
"c0^",
"c1^",
"d0",
"d1",
"d2"
]
}
63 changes: 39 additions & 24 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ def test_constructor():
Array(data, offset, (2, 2, 2))

# dims don't match
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
Array(data, offset, (1, 1))

Array(data, offset, (1, 1, 3))
Array(data, offset, (1, 1, 4))

Array(data, (1, 1, 1), (1, 1, 2))


def test_dtype():
for dtype in [np.float32, np.uint8, np.uint64]:
assert Array(np.zeros((1,), dtype=dtype), (0,), (1,)).dtype == dtype


def test_getitem():
a = Array(np.arange(0, 10).reshape(2, 5), (0, 0), (1, 1))

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


def test_setitem():
# set entirely with numpy array

Expand Down Expand Up @@ -136,6 +139,7 @@ def test_setitem():
assert a[Coordinate((1, 1))] == 1
assert a[Coordinate((1, 2))] == 2


def test_to_ndarray():
a = Array(np.arange(0, 10).reshape(2, 5), (0, 0), (1, 1))

Expand Down Expand Up @@ -163,8 +167,11 @@ def test_to_ndarray():
)
np.testing.assert_array_equal(b, compare)


def test_to_ndarray_with_slices():
a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=slice(0, 1))
a = Array(
np.arange(0, 10 * 10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=slice(0, 1)
)

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

b = a.to_ndarray(Roi((0, 0), (5, 5)), fill_value=1)
compare = np.array(
[[
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
]]
[
[
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
]
]
)
np.testing.assert_array_equal(b, compare)


def test_adapters():
a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1))
a = Array(np.arange(0, 10 * 10).reshape(10, 2, 5), (0, 0), (1, 1))
assert a.dtype == int

a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=lambda x: x > 2)
a = Array(
np.arange(0, 10 * 10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=lambda x: x > 2
)
assert a.dtype == bool

a = Array(np.arange(0, 10*10).reshape(10, 2, 5), (0, 0), (1, 1), adapter=lambda x: x + 0.5)
a = Array(
np.arange(0, 10 * 10).reshape(10, 2, 5),
(0, 0),
(1, 1),
adapter=lambda x: x + 0.5,
)
assert a.dtype == float


def test_slicing():
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))
a = Array(np.arange(0, 4 * 4).reshape(4, 2, 2), (0, 0), (1, 1))

a.adapt(np.s_[0:3, 1, :])
assert a.shape == (3, 2)
assert a.axis_names == ["c0^", "d1"]
assert a.axis_names == ["c0^", "d1"], a.axis_names
assert a.units == [""]

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

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


# test with list indexing
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))
a = Array(np.arange(0, 4 * 4).reshape(4, 2, 2), (0, 0), (1, 1))

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

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


# test weird case
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))
a = Array(np.arange(0, 4 * 4).reshape(4, 2, 2), (0, 0), (1, 1))

a.adapt(np.s_[[2,2,2], 1, :])
a.adapt(np.s_[[2, 2, 2], 1, :])
assert a.shape == (3, 2)
assert a.axis_names == ["c0^", "d1"]
assert a.units == [""]

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


# test_bool_indexing
a = Array(np.arange(0, 4*4).reshape(4, 2, 2), (0, 0), (1, 1))
a = Array(np.arange(0, 4 * 4).reshape(4, 2, 2), (0, 0), (1, 1))

a.adapt(np.s_[np.array([True, True, True, False]), 1, :])
assert a.shape == (3, 2)
assert a.axis_names == ["c0^", "d1"]
assert a.units == [""]

with pytest.raises(RuntimeError):
a[:, :] = np.array([42, 43, 44]).reshape(3, 1)
5 changes: 3 additions & 2 deletions tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
@pytest.mark.parametrize("store", stores.keys())
@pytest.mark.parametrize("dtype", [np.float32, np.uint8, np.uint64])
def test_helpers(tmpdir, store, dtype):
shape = Coordinate(1, 1, 10, 20, 30)
chunk_shape = Coordinate(2, 3, 10, 10, 10)
store = tmpdir / store
metadata = MetaDataFormat().parse(
shape,
{
"offset": [100, 200, 400],
"voxel_size": [1, 2, 3],
"axis_names": ["sample^", "channel^", "z", "y", "x"],
"units": ["nm", "nm", "nm"],
}
)
shape = Coordinate(1, 1, 10, 20, 30)
chunk_shape = Coordinate(2, 3, 10, 10, 10)

# test prepare_ds fails if array does not exist and mode is read
with pytest.raises(ArrayNotFoundError):
Expand Down
37 changes: 36 additions & 1 deletion tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@


fixtures_dir = Path(__file__).parent / "fixtures"
incomplete_metadatas = {
"incomplete1": "metadatas/incomplete1.json",
"incomplete2": "metadatas/incomplete2.json",
"incomplete3": "metadatas/incomplete3.json",
}
incomplete_metadata_formats = {
"incomplete1": MetaDataFormat(),
"incomplete2": MetaDataFormat(),
"incomplete3": MetaDataFormat(),
}
metadata_jsons = {
"default": "metadatas/default.json",
"simple": "metadatas/simple.json",
Expand All @@ -27,10 +37,12 @@
),
}


@pytest.fixture(params=metadata_jsons.keys())
def metadata(request):
return metadata_formats[request.param].parse(
json.loads(open(fixtures_dir / metadata_jsons[request.param]).read())
(10, 2, 100, 100, 100),
json.loads(open(fixtures_dir / metadata_jsons[request.param]).read()),
)


Expand All @@ -39,3 +51,26 @@ def test_parse_metadata(metadata):
assert metadata.voxel_size == Coordinate(1, 2, 3)
assert metadata.axis_names == ["sample^", "channel^", "z", "y", "x"]
assert metadata.units == ["nm", "nm", "nm"]


@pytest.fixture(params=incomplete_metadatas.keys())
def incomplete_metadata(request):
return incomplete_metadata_formats[request.param].parse(
(10, 2, 100, 100, 100),
json.loads(open(fixtures_dir / incomplete_metadatas[request.param]).read()),
)


def test_parse_incomplete_metadata(incomplete_metadata):
assert incomplete_metadata.offset == Coordinate(0, 0, 0)
assert incomplete_metadata.voxel_size == Coordinate(1, 1, 1)
assert incomplete_metadata.axis_names == ["c0^", "c1^", "d0", "d1", "d2"]
assert incomplete_metadata.units == ["", "", ""]


def test_empty_metadata():
metadata = MetaDataFormat().parse((10, 2, 100, 100, 100), {})
assert metadata.offset == Coordinate(0, 0, 0, 0, 0)
assert metadata.voxel_size == Coordinate(1, 1, 1, 1, 1)
assert metadata.axis_names == ["d0", "d1", "d2", "d3", "d4"]
assert metadata.units == ["", "", "", "", ""]

0 comments on commit 7948807

Please sign in to comment.