This repository was archived by the owner on Jun 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_image.py
More file actions
167 lines (144 loc) · 4.61 KB
/
Copy pathtest_image.py
File metadata and controls
167 lines (144 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Tests for the image module.
"""
import pathlib
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import Dict, List, Optional, Tuple
import pytest
import tifffile as tiff
import zarr
from nviz.image import (
image_set_to_arrays,
tiff_to_ometiff,
tiff_to_zarr,
)
from tests.utils import (
example_data_for_image_tests,
)
@pytest.mark.parametrize(
(
"image_dir, label_dir, output_path, channel_map, "
"scaling_values, ignore, expected_labels"
),
example_data_for_image_tests,
)
def test_image_set_to_arrays(
image_dir: str,
label_dir: Optional[str],
output_path: str,
channel_map: Dict[str, str],
scaling_values: Tuple[int, int, int],
ignore: Optional[List[str]],
expected_labels: List[str],
):
# Call the function
result = image_set_to_arrays(
image_dir=image_dir, label_dir=label_dir, channel_map=channel_map, ignore=ignore
)
# check that we have all keys
if ignore is None:
all(channel not in result["images"] for channel in channel_map.values())
# check that we ignored what we should have
elif ignore is not None:
assert all(ignored not in result["images"] for ignored in ignore)
@pytest.mark.parametrize(
(
"image_dir, label_dir, output_path, channel_map, "
"scaling_values, ignore, expected_labels"
),
example_data_for_image_tests,
)
def test_tiff_to_zarr(
image_dir: str,
label_dir: Optional[str],
output_path: str,
channel_map: Dict[str, str],
scaling_values: Tuple[int, int, int],
ignore: Optional[List[str]],
expected_labels: List[str],
tmp_path: pathlib.Path,
):
"""
Tests the tiff_to_zarr function.
"""
output_path = tiff_to_zarr(
image_dir=image_dir,
label_dir=label_dir,
output_path=f"{tmp_path}/{output_path}",
channel_map=channel_map,
scaling_values=scaling_values,
ignore=ignore,
)
# Check if the output path exists
assert Path(output_path).exists()
# Check if the Zarr structure is correct
zarr_root = zarr.open(output_path, mode="r")
assert "images" in list(zarr_root.keys())
# check if we have labels if we supplied them
if label_dir is not None:
assert "labels" in zarr_root
for channel in channel_map.values():
if ignore is not None and channel not in [
channel_map[ignored] for ignored in ignore
]:
assert channel in list(zarr_root["images"])
# check if we have labels if we supplied them
if label_dir is not None:
assert all(
expected_label in list(zarr_root["labels"].keys())
for expected_label in expected_labels
)
@pytest.mark.parametrize(
(
"image_dir, label_dir, output_path, channel_map, "
"scaling_values, ignore, expected_labels"
),
example_data_for_image_tests,
)
def test_tiff_to_ometiff(
image_dir: str,
label_dir: Optional[str],
output_path: str,
channel_map: Dict[str, str],
scaling_values: Tuple[int, int, int],
ignore: Optional[List[str]],
expected_labels: List[str],
tmp_path: pathlib.Path,
):
"""
Tests the tiff_to_ometiff function.
"""
output_path = tiff_to_ometiff(
image_dir=image_dir,
label_dir=label_dir,
output_path=f"{tmp_path}/{output_path}",
channel_map=channel_map,
scaling_values=scaling_values,
ignore=ignore,
)
# Check if the output path exists
assert Path(output_path).exists()
# Read the OME-TIFF file and check its contents
with tiff.TiffFile(output_path) as tif:
assert len(tif.pages) > 0
metadata = tif.ome_metadata
assert metadata is not None
# Parse the OME-XML metadata
root = ET.fromstring(metadata)
channels = root.find(
".//{http://www.openmicroscopy.org/Schemas/OME/2016-06}Pixels"
).findall("{http://www.openmicroscopy.org/Schemas/OME/2016-06}Channel")
# Check the metadata for channels
for channel in channel_map.values():
if ignore is not None and channel not in [
channel_map[ignored] for ignored in ignore
]:
assert any(channel == ch.get("Name") for ch in channels)
# Check the metadata for physical sizes
pixels = root.find(
".//{http://www.openmicroscopy.org/Schemas/OME/2016-06}Pixels"
)
assert pixels.get("PhysicalSizeX") == str(scaling_values[2])
assert pixels.get("PhysicalSizeY") == str(scaling_values[1])
assert pixels.get("PhysicalSizeZ") == str(scaling_values[0])