11import filecmp
22import json
33import pathlib
4+ import re
45from typing import Any
56
67import dask .array as da
1617from ome_zarr_models .v05 .hcs import HCS as Models05HCS
1718from ome_zarr_models .v05 .image import Image as Models05Image
1819from ome_zarr_models .v05 .well import Well as Models05Well
20+ from skimage .data import binary_blobs
1921from zarr .abc .codec import BytesBytesCodec
2022from zarr .codecs import BloscCodec
2123
@@ -664,7 +666,9 @@ def test_multi_levels_transformations(self, fmt):
664666 # No arrays, so this is expected:
665667 with pytest .raises (
666668 ValueError ,
667- match = "Expected to find an array at /0, but no array was found there." ,
669+ match = re .escape (
670+ "Expected to find an array at 0, but no array was found there."
671+ ),
668672 ):
669673 if fmt .version == "0.4" :
670674 Models04Image .from_zarr (group )
@@ -756,7 +760,9 @@ def test_valid_transformations(self, coordinateTransformations):
756760 # No arrays, so this is expected:
757761 with pytest .raises (
758762 ValueError ,
759- match = "Expected to find an array at /0, but no array was found there." ,
763+ match = re .escape (
764+ "Expected to find an array at 0, but no array was found there."
765+ ),
760766 ):
761767 Models04Image .from_zarr (self .root )
762768
@@ -835,7 +841,8 @@ def test_omero_metadata(self, metadata: dict[str, Any] | None):
835841 datasets .append ({"path" : str (level ), "coordinateTransformations" : transf })
836842 if metadata is None :
837843 with pytest .raises (
838- KeyError , match = "If `'omero'` is present, value cannot be `None`."
844+ KeyError ,
845+ match = re .escape ("If `'omero'` is present, value cannot be `None`." ),
839846 ):
840847 write_multiscales_metadata (
841848 self .root ,
@@ -856,7 +863,7 @@ def test_omero_metadata(self, metadata: dict[str, Any] | None):
856863 )
857864 if window_metadata is not None and len (window_metadata ) < 4 :
858865 if isinstance (window_metadata , dict ):
859- with pytest .raises (KeyError , match = ".*`' window'`.* " ):
866+ with pytest .raises (KeyError , match = "window" ):
860867 write_multiscales_metadata (
861868 self .root ,
862869 datasets ,
@@ -865,7 +872,7 @@ def test_omero_metadata(self, metadata: dict[str, Any] | None):
865872 fmt = FormatV04 (),
866873 )
867874 elif isinstance (window_metadata , list ):
868- with pytest .raises (TypeError , match = ".*`' window'`.* " ):
875+ with pytest .raises (TypeError , match = "window" ):
869876 write_multiscales_metadata (
870877 self .root ,
871878 datasets ,
@@ -874,7 +881,7 @@ def test_omero_metadata(self, metadata: dict[str, Any] | None):
874881 fmt = FormatV04 (),
875882 )
876883 elif color_metadata is not None and len (color_metadata ) != 6 :
877- with pytest .raises (TypeError , match = ".*`' color'`.* " ):
884+ with pytest .raises (TypeError , match = "color" ):
878885 write_multiscales_metadata (
879886 self .root ,
880887 datasets ,
@@ -891,7 +898,9 @@ def test_omero_metadata(self, metadata: dict[str, Any] | None):
891898 # no arrays, so this is expected
892899 with pytest .raises (
893900 ValueError ,
894- match = "Expected to find an array at /0, but no array was found there." ,
901+ match = re .escape (
902+ "Expected to find an array at 0, but no array was found there."
903+ ),
895904 ):
896905 Models04Image .from_zarr (self .root )
897906
@@ -1457,6 +1466,12 @@ def verify_label_data(
14571466 name = imglabel_attrs ["multiscales" ][0 ].get ("name" , "" )
14581467 assert label_name == name
14591468
1469+ labels_paths = [
1470+ ds ["path" ] for ds in imglabel_attrs ["multiscales" ][0 ]["datasets" ]
1471+ ]
1472+ label_data = [da .from_zarr (label_group [path ]) for path in labels_paths ]
1473+ return label_data
1474+
14601475 @pytest .mark .parametrize (
14611476 "format_version" ,
14621477 (
@@ -1468,7 +1483,9 @@ def verify_label_data(
14681483 ),
14691484 )
14701485 @pytest .mark .parametrize ("array_constructor" , [np .array , da .from_array ])
1471- def test_write_labels (self , shape , scaler , format_version , array_constructor ):
1486+ @pytest .mark .parametrize ("scale_type" , ["custom" , "noop" , "default" ])
1487+ def test_write_labels (self , shape , format_version , array_constructor , scale_type ):
1488+
14721489 fmt = format_version ()
14731490 if fmt .version == "0.5" :
14741491 img_path = self .path_v3
@@ -1485,11 +1502,23 @@ def test_write_labels(self, shape, scaler, format_version, array_constructor):
14851502 transformations .append (
14861503 [{"type" : "scale" , "scale" : transf ["scale" ][- len (shape ) :]}]
14871504 )
1488- if scaler is None :
1505+ if scale_type == "noop" :
14891506 break
14901507
1491- # create the actual label data
1492- label_data = np .random .randint (0 , 1000 , size = shape )
1508+ # create the actual label data: zeros with blobs
1509+ label_data = np .zeros (shape , dtype = np .uint8 )
1510+ # add some blobs, corresponding to shape
1511+ blobs = binary_blobs (length = 256 , volume_fraction = 0.1 , n_dim = 2 ).astype ("int8" )
1512+ # we only apply blobs to the last two dimensions of label_data
1513+ slices = [slice (None )] * (len (shape ) - blobs .ndim )
1514+ slices += [slice (0 , 256 ), slice (0 , 256 )]
1515+ label_data [tuple (slices )] = 2 * blobs
1516+
1517+ print ("label_data.shape:" , label_data .shape , shape )
1518+ assert label_data .max () == 2
1519+ assert label_data .min () == 0
1520+ assert np .unique (label_data ).tolist () == [0 , 2 ]
1521+
14931522 if fmt .version in ("0.1" , "0.2" ):
14941523 # v0.1 and v0.2 require 5d
14951524 expand_dims = (np .s_ [None ],) * (5 - len (shape ))
@@ -1498,21 +1527,33 @@ def test_write_labels(self, shape, scaler, format_version, array_constructor):
14981527 label_name = "my-labels"
14991528 label_data = array_constructor (label_data )
15001529
1530+ scaler = Scaler ()
1531+ if scale_type == "noop" :
1532+ scaler = None
1533+ kwargs = {"scaler" : scaler }
1534+ if scale_type == "default" :
1535+ del kwargs ["scaler" ]
1536+
15011537 # create the root level image data
15021538 self .create_image_data (group , shape , scaler , fmt , axes , transformations )
15031539
15041540 write_labels (
15051541 label_data ,
15061542 group ,
1507- scaler = scaler ,
15081543 name = label_name ,
15091544 fmt = fmt ,
15101545 axes = axes ,
15111546 coordinate_transformations = transformations ,
1547+ ** kwargs ,
15121548 )
1513- self .verify_label_data (
1549+ label_data = self .verify_label_data (
15141550 img_path , label_name , label_data , fmt , shape , transformations
15151551 )
1552+
1553+ for level in label_data :
1554+ if scale_type == "default" :
1555+ assert np .unique (level .compute ()).tolist () == [0 , 2 ]
1556+
15161557 if fmt .version == "0.4" :
15171558 test_root = zarr .open (self .path )
15181559 Models04Labels .from_zarr (test_root ["labels" ])
0 commit comments