|
7 | 7 | PanSegImage, |
8 | 8 | SemanticType, |
9 | 9 | ) |
| 10 | +from panseg.functionals.dataprocessing.dataprocessing import normalize_01 |
10 | 11 | from panseg.io.voxelsize import VoxelSize |
11 | 12 | from panseg.tasks.io_tasks import ( |
12 | 13 | export_image_task, |
|
17 | 18 |
|
18 | 19 |
|
19 | 20 | @pytest.mark.parametrize( |
20 | | - "shape, layout, export_format", |
| 21 | + "shape,layout,export_format", |
| 22 | + [ |
| 23 | + ((2, 64, 64), ImageLayout.CYX, "tiff"), |
| 24 | + ((2, 64, 64), ImageLayout.CYX, "h5"), |
| 25 | + ((2, 64, 64), ImageLayout.CYX, "zarr"), |
| 26 | + ((2, 64, 32, 32), ImageLayout.CZYX, "h5"), |
| 27 | + ((2, 64, 32, 32), ImageLayout.CZYX, "tiff"), |
| 28 | + ((2, 64, 32, 32), ImageLayout.CZYX, "zarr"), |
| 29 | + # ((64, 64), ImageLayout.YX, "tiff"), |
| 30 | + # ((64, 64), ImageLayout.YX, "h5"), |
| 31 | + # ((64, 64), ImageLayout.YX, "zarr"), |
| 32 | + ], |
| 33 | +) |
| 34 | +def test_image_io_round_trip_multichannel(tmp_path, shape, layout, export_format): |
| 35 | + mock_data = normalize_01(np.random.rand(*shape).astype("float32")) |
| 36 | + |
| 37 | + property = ImageProperties( |
| 38 | + name="test", |
| 39 | + voxel_size=VoxelSize(voxels_size=(1.0, 1.0, 1.0), unit="um"), |
| 40 | + semantic_type=SemanticType.RAW, |
| 41 | + image_layout=layout, |
| 42 | + original_voxel_size=VoxelSize(voxels_size=(1.0, 1.0, 1.0), unit="um"), |
| 43 | + source_file_name="test", |
| 44 | + ) |
| 45 | + image = PanSegImage(data=mock_data, properties=property) |
| 46 | + |
| 47 | + export_image_task( |
| 48 | + image=image, |
| 49 | + export_directory=tmp_path, |
| 50 | + name_pattern="test", |
| 51 | + key="raw", |
| 52 | + export_format=export_format, |
| 53 | + data_type="float32", |
| 54 | + ) |
| 55 | + |
| 56 | + if export_format == "tiff": |
| 57 | + file_path = tmp_path / "test.tiff" |
| 58 | + key = None |
| 59 | + # tiff alwayes saved as ZCYX |
| 60 | + if layout == ImageLayout.CZYX: |
| 61 | + layout = ImageLayout.ZCYX |
| 62 | + |
| 63 | + elif export_format == "h5": |
| 64 | + file_path = tmp_path / "test.h5" |
| 65 | + key = "raw" |
| 66 | + else: |
| 67 | + file_path = tmp_path / "test.zarr" |
| 68 | + key = "raw" |
| 69 | + |
| 70 | + imported_image = import_image_task( |
| 71 | + input_path=file_path, |
| 72 | + key=key, |
| 73 | + image_name="test_import", |
| 74 | + semantic_type="raw", |
| 75 | + stack_layout=layout.name, |
| 76 | + m_slicing=None, |
| 77 | + ) |
| 78 | + assert isinstance(imported_image, list) |
| 79 | + |
| 80 | + for i in [0, 1]: |
| 81 | + original_data = image.get_data()[i] |
| 82 | + imported_data = imported_image[i].get_data() |
| 83 | + |
| 84 | + assert np.allclose(original_data, imported_data) |
| 85 | + assert original_data.max() <= 1.0 # check if the normalization is applied |
| 86 | + assert imported_data.max() <= 1.0 |
| 87 | + |
| 88 | + assert image.voxel_size == imported_image[i].voxel_size |
| 89 | + assert image.semantic_type == imported_image[i].semantic_type |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize( |
| 93 | + "shape,layout,export_format", |
21 | 94 | [ |
22 | 95 | ((32, 64, 64), ImageLayout.ZYX, "tiff"), |
23 | 96 | ((32, 64, 64), ImageLayout.ZYX, "h5"), |
@@ -69,7 +142,8 @@ def test_image_io_round_trip(tmp_path, shape, layout, export_format): |
69 | 142 | ) |
70 | 143 | assert isinstance(imported_image, PanSegImage) |
71 | 144 |
|
72 | | - original_data = image.get_data() |
| 145 | + # would be normalized during import |
| 146 | + original_data = image.get_data(normalize_01=True) |
73 | 147 | imported_data = imported_image.get_data() |
74 | 148 |
|
75 | 149 | assert np.allclose(original_data, imported_data) |
@@ -133,7 +207,7 @@ def test_label_io_round_trip(tmp_path, shape, layout, export_format): |
133 | 207 | imported_image = import_image_task( |
134 | 208 | input_path=file_path, |
135 | 209 | key=key, |
136 | | - image_name="tesi_import", |
| 210 | + image_name="test_import", |
137 | 211 | semantic_type="segmentation", |
138 | 212 | stack_layout=layout.name, |
139 | 213 | m_slicing=None, |
|
0 commit comments