Skip to content

Commit

Permalink
Test writer for v0.4 with various axes
Browse files Browse the repository at this point in the history
  • Loading branch information
will-moore committed Oct 28, 2021
1 parent c8b80bc commit e16de4d
Showing 1 changed file with 80 additions and 12 deletions.
92 changes: 80 additions & 12 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import pytest
import zarr

from ome_zarr.format import FormatV01, FormatV02, FormatV03
from ome_zarr.format import FormatV01, FormatV02, FormatV03, FormatV04
from ome_zarr.io import parse_url
from ome_zarr.reader import Multiscales, Reader
from ome_zarr.scale import Scaler
from ome_zarr.writer import _validate_axes_names, write_image
from ome_zarr.writer import _validate_axes, write_image


class TestWriter:
Expand Down Expand Up @@ -51,6 +51,7 @@ def scaler(self, request):
),
pytest.param(FormatV02, id="V02"),
pytest.param(FormatV03, id="V03"),
pytest.param(FormatV04, id="V04"),
),
)
def test_writer(self, shape, scaler, format_version):
Expand Down Expand Up @@ -84,21 +85,21 @@ def test_dim_names(self):

# v0.3 MUST specify axes for 3D or 4D data
with pytest.raises(ValueError):
_validate_axes_names(3, axes=None, fmt=v03)
_validate_axes(3, axes=None, fmt=v03)

# ndims must match axes length
with pytest.raises(ValueError):
_validate_axes_names(3, axes="yx", fmt=v03)
_validate_axes(3, axes="yx", fmt=v03)

# axes must be ordered tczyx
with pytest.raises(ValueError):
_validate_axes_names(3, axes="yxt", fmt=v03)
_validate_axes(3, axes="yxt", fmt=v03)
with pytest.raises(ValueError):
_validate_axes_names(2, axes=["x", "y"], fmt=v03)
_validate_axes(2, axes=["x", "y"], fmt=v03)

# valid axes - no change, converted to list
assert _validate_axes_names(2, axes=["y", "x"], fmt=v03) == ["y", "x"]
assert _validate_axes_names(5, axes="tczyx", fmt=v03) == [
assert _validate_axes(2, axes=["y", "x"], fmt=v03) == ["y", "x"]
assert _validate_axes(5, axes="tczyx", fmt=v03) == [
"t",
"c",
"z",
Expand All @@ -107,12 +108,12 @@ def test_dim_names(self):
]

# if 2D or 5D, axes can be assigned automatically
assert _validate_axes_names(2, axes=None, fmt=v03) == ["y", "x"]
assert _validate_axes_names(5, axes=None, fmt=v03) == ["t", "c", "z", "y", "x"]
assert _validate_axes(2, axes=None, fmt=v03) == ["y", "x"]
assert _validate_axes(5, axes=None, fmt=v03) == ["t", "c", "z", "y", "x"]

# for v0.1 or v0.2, axes should be None
assert _validate_axes_names(2, axes=["y", "x"], fmt=FormatV01()) is None
assert _validate_axes_names(2, axes=["y", "x"], fmt=FormatV02()) is None
assert _validate_axes(2, axes=["y", "x"], fmt=FormatV01()) is None
assert _validate_axes(2, axes=["y", "x"], fmt=FormatV02()) is None

# check that write_image is checking axes
data = self.create_data((125, 125))
Expand All @@ -123,3 +124,70 @@ def test_dim_names(self):
fmt=v03,
axes="xyz",
)

def test_axes_dicts(self):

v04 = FormatV04()

# ALL axes must specify 'name'
with pytest.raises(ValueError):
_validate_axes(2, axes=[{"name": "y"}, {}], fmt=v04)

all_dims = [
{"name": "t", "type": "time"},
{"name": "c", "type": "channel"},
{"name": "z", "type": "space"},
{"name": "y", "type": "space"},
{"name": "x", "type": "space"},
]

# auto axes for 2D, 5D, converted to dict for v0.4
assert _validate_axes(2, axes=None, fmt=v04) == all_dims[-2:]
assert _validate_axes(5, axes=None, fmt=v04) == all_dims

# convert from list or string
assert _validate_axes(3, axes=["z", "y", "x"], fmt=v04) == all_dims[-3:]
assert _validate_axes(3, axes="czyx", fmt=v04) == all_dims[-4:]

# invalid based on ordering of types
with pytest.raises(ValueError):
assert _validate_axes(3, axes=["y", "c", "x"], fmt=v04)
with pytest.raises(ValueError):
assert _validate_axes(4, axes="ctyx", fmt=v04)

# custom types
assert _validate_axes(3, axes=["foo", "y", "x"], fmt=v04) == [
{"name": "foo"},
all_dims[-2],
all_dims[-1],
]

# space types can be in ANY order
assert _validate_axes(3, axes=["x", "z", "y"], fmt=v04) == [
all_dims[-1],
all_dims[-3],
all_dims[-2],
]

# Not allowed multiple custom types
with pytest.raises(ValueError):
_validate_axes(4, axes=["foo", "bar", "y", "x"], fmt=v04)

# unconventional naming is allowed
strange_axes = [
{"name": "duration", "type": "time"},
{"name": "rotation", "type": "angle"},
{"name": "dz", "type": "space"},
{"name": "WIDTH", "type": "space"},
]
assert _validate_axes(4, axes=strange_axes, fmt=v04) == strange_axes

# check that write_image is checking axes
data = self.create_data((125, 125))
with pytest.raises(ValueError):
write_image(
image=data,
group=self.group,
fmt=v04,
axes="xt",
)

0 comments on commit e16de4d

Please sign in to comment.