Repository navigation
Expand file tree
/
Copy pathtest_metamorph.py
More file actions
367 lines (309 loc) · 19.2 KB
/
Copy pathtest_metamorph.py
File metadata and controls
367 lines (309 loc) · 19.2 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""MetaMorph ``.nd`` sets (backends/metamorph.py, docs/COMPOSITE.md): the
``.nd`` file names its TIFF / STK files by a rule; opening any of them opens
the set. Synthetic sets from ``_metamorph_writer``."""
from __future__ import annotations
import os
import warnings
import numpy as np
import pytest
import micro_reader
from micro_reader.backends import metamorph
tifffile = pytest.importorskip("tifffile")
from _metamorph_writer import member_name, write_metaseries, write_nd, write_set # noqa: E402
Y, X = 8, 12
UM = "micrometer"
def _plane(data, s, t, w, z):
return data[(s, t, w)][z]
def test_a_full_set_reads_every_plane(tmp_path):
nd, data, _ = write_set(str(tmp_path), times=3, stages=["A1", "B7"],
waves=[("DAPI", True), ("GFP", True)], nz=4, z_step=1.5)
with micro_reader.open(nd) as f:
assert (f.backend, f.format) == ("metamorph", "metamorph")
assert [im.index for im in f.images] == [{"series": 0}, {"series": 1}]
assert [im.name for im in f.images] == ["A1", "B7"] # Stage labels (MR-2)
for s, im in enumerate(f.images):
assert im.axes == "tczyx" and im.shape == (3, 2, 4, Y, X) and im.dtype == "uint16"
assert not im.is_ragged
whole = im[...]
for t in range(3):
for w in range(2):
np.testing.assert_array_equal(whole[t, w], data[(s, t, w)])
np.testing.assert_array_equal(im[2, 1, 3, 2:5, 1:9], data[(s, 2, 1)][3, 2:5, 1:9])
md = f.images[1].metadata
assert [c.name for c in md.channels] == ["DAPI", "GFP"]
assert md.pixel_sizes == {"x": micro_reader.Quantity(0.65, UM),
"y": micro_reader.Quantity(0.65, UM),
"z": micro_reader.Quantity(1.5, UM)}
assert md.position == {} # not read (module docstring)
assert len(f._member_files()) == 2 * 3 * 2
def test_names_follow_the_rule(tmp_path):
_nd, _data, paths = write_set(str(tmp_path), times=2, stages=["p"], waves=[("Cy 5", True)])
assert os.path.basename(paths[(0, 1, 0)]) == "exp_w1Cy 5_s1_t2.TIF"
lay = metamorph.layout(metamorph.read_nd(_nd), _nd)
assert metamorph.member_key(lay, "EXP_w1cy 5_s1_t2.tif") == (0, 1, 0) # any letter case
assert metamorph.member_key(lay, "exp_w1Cy 5_s1_t3.TIF") is None # t out of range
assert metamorph.member_key(lay, "exp_w2Cy 5_s1_t1.TIF") is None # no wavelength 2
assert metamorph.member_key(lay, "exp_w1GFP_s1_t1.TIF") is None # another name
def test_the_nd_file_and_its_description(tmp_path):
nd = write_nd(str(tmp_path / "d.nd"), times=2, waves=[("FRET", False)],
description="40xOil, bins4\ntimelapse: 31 points\n")
info = metamorph.read_nd(nd)
assert info["Description"] == "40xOil, bins4\ntimelapse: 31 points"
assert info["NDInfoFile"] == "Version 2.0" and info["NTimePoints"] == 2
assert info["WaveName1"] == "FRET" and info["WaveDoZ1"] is False and info["EndFile"] is None
def test_any_member_and_the_folder_open_the_set(tmp_path):
nd, data, paths = write_set(str(tmp_path), times=2, stages=["a", "b"], waves=[("w", True)])
for opened in (paths[(1, 1, 0)], str(tmp_path)):
with micro_reader.open(opened) as f:
assert f.backend == "metamorph" and os.path.samefile(f.path, nd)
assert f.opened == opened and len(f.images) == 2
np.testing.assert_array_equal(f.images[1][1, 0, 0], _plane(data, 1, 1, 0, 0))
# backend="tiff" reads the member alone
with micro_reader.open(paths[(1, 1, 0)], backend="tiff") as f:
assert f.backend == "tiff" and f.images[0].shape == (Y, X)
# the MetaSeries calibration of a TIFF opened on its own
assert f.images[0].metadata.pixel_sizes["x"] == micro_reader.Quantity(0.65, UM)
def test_a_file_outside_the_rule_opens_alone(tmp_path):
nd, _data, _paths = write_set(str(tmp_path), times=2, waves=[("w", True)])
other = str(tmp_path / "exp_w1w_t9.TIF") # timepoint 9 of 2
tifffile.imwrite(other, np.zeros((Y, X), "u2"))
thumb = str(tmp_path / "exp_w1w_thumb_t1.TIF")
tifffile.imwrite(thumb, np.zeros((4, 6), "u2"))
for path in (other, thumb):
with micro_reader.open(path) as f:
assert f.backend == "tiff"
def test_wave_not_in_file_name(tmp_path):
nd, data, paths = write_set(str(tmp_path), times=2, stages=["B2", "B3"],
waves=[("BF", True)], waves_in_name=False)
assert os.path.basename(paths[(1, 0, 0)]) == "exp_s2_t1.TIF"
with micro_reader.open(paths[(1, 0, 0)]) as f:
assert len(f.images) == 2 and f.images[0].shape == (2, 1, 1, Y, X)
assert f.images[0].metadata.channels[0].name == "BF"
np.testing.assert_array_equal(f.images[1][1, 0, 0], _plane(data, 1, 1, 0, 0))
def test_several_waves_in_one_file_are_refused(tmp_path):
nd = write_nd(str(tmp_path / "exp.nd"), times=2, waves=[("a", True), ("b", True)],
waves_in_name=False)
member = write_metaseries(str(tmp_path / "exp_t1.TIF"), np.zeros((2, Y, X), "u2"))
for path in (nd, member): # the member is claimed: it says why
with pytest.raises(micro_reader.UnsupportedFormatError, match="backend='tiff'"):
micro_reader.open(path)
with micro_reader.open(member, backend="tiff") as f:
assert f.backend == "tiff"
def test_wave_points_collected_is_refused(tmp_path):
nd = write_nd(str(tmp_path / "exp.nd"), times=3, waves=[("a", True), ("b", True)],
extra=[("WavePointsCollected2", "2, 1, 3")])
with pytest.raises(micro_reader.UnsupportedFormatError, match="WavePointsCollected"):
micro_reader.open(nd)
def test_no_wavelengths_like_the_zenodo_sample(tmp_path):
"""DoWave FALSE: no _w part; the channel's name comes from the members'
illumination setting (the .nd file names none: a gap filled)."""
nd, data, paths = write_set(str(tmp_path), times=3, stages=["B2", "B3", "C4"],
waves_in_name=False, illumination="Brightfield")
assert os.path.basename(paths[(2, 1, 0)]) == "exp_s3_t2.TIF"
with warnings.catch_warnings():
warnings.simplefilter("error")
with micro_reader.open(nd) as f:
assert [im.name for im in f.images] == ["B2", "B3", "C4"] # Stage labels (MR-2)
assert f.images[2].metadata.channels[0].name == "Brightfield"
np.testing.assert_array_equal(f.images[2][1, 0, 0], _plane(data, 2, 1, 0, 0))
def test_the_nd_names_win_over_the_members(tmp_path):
nd, _data, _paths = write_set(str(tmp_path), times=1, waves=[("GFP", True)],
illumination="FITC")
with micro_reader.open(nd) as f:
with pytest.warns(micro_reader.CompositeWarning, match="'GFP' in the .nd file, 'FITC'"):
names = [c.name for c in f.images[0].metadata.channels]
assert names == ["GFP"]
def test_a_dimension_of_one_may_leave_its_part_out(tmp_path):
"""DoTimelapse TRUE with one timepoint / DoStage TRUE with one position:
the file is found with or without its _t1 / _s1."""
write_nd(str(tmp_path / "exp.nd"), times=1, stages=["only"], waves=[("w", True)])
a = np.full((1, Y, X), 7, "u2")
write_metaseries(str(tmp_path / "exp_w1w.TIF"), a)
with micro_reader.open(str(tmp_path / "exp.nd")) as f:
np.testing.assert_array_equal(f.images[0][0, 0, 0], a[0])
# both spellings: neither is used
write_metaseries(str(tmp_path / "exp_w1w_s1_t1.TIF"), a)
with pytest.warns(micro_reader.CompositeWarning, match="more than one file"), \
pytest.warns(micro_reader.MissingImagesWarning):
with pytest.raises(micro_reader.CorruptFileError, match="none of the fields"):
micro_reader.open(str(tmp_path / "exp.nd"))
def test_missing_files_read_as_zeros(tmp_path):
with pytest.warns(micro_reader.MissingImagesWarning, match="3 image files are missing") as rec:
nd, data, _ = write_set(str(tmp_path), times=2, stages=["a", "b", "c"],
waves=[("w", True)], skip={(0, 1, 0), (2, 0, 0), (2, 1, 0)})
f = micro_reader.open(nd)
message = str(rec[0].message)
assert "1 fields miss some planes" in message and "1 fields have no files" in message
with f:
assert [im.index["series"] for im in f.images] == [0, 1] # position c left out
assert not f.images[0][1].any()
np.testing.assert_array_equal(f.images[0][0, 0, 0], _plane(data, 0, 0, 0, 0))
def test_a_ragged_set_has_parts(tmp_path):
"""A wavelength without z (WaveDoZ FALSE) next to a z-stack: placed at its
focus position (the middle slice here), zeros elsewhere."""
nd, data, _ = write_set(str(tmp_path), times=2, waves=[("DAPI", True), ("BF", False)],
nz=5, z_step=2.0)
with micro_reader.open(nd) as f:
(whole,) = f.images
assert whole.is_ragged and whole.shape == (2, 2, 5, Y, X)
stack, flat = whole.parts
assert stack.shape == (2, 1, 5, Y, X) and flat.shape == (2, 1, 1, Y, X)
assert [c.name for c in stack.metadata.channels] == ["DAPI"]
assert [c.name for c in flat.metadata.channels] == ["BF"]
assert stack.metadata.pixel_sizes["z"] == micro_reader.Quantity(2.0, UM)
assert "z" not in flat.metadata.pixel_sizes
np.testing.assert_array_equal(flat[1, 0, 0], _plane(data, 0, 1, 1, 0))
np.testing.assert_array_equal(stack[1, 0], data[(0, 1, 0)])
w = whole[...]
np.testing.assert_array_equal(w[1, 0], data[(0, 1, 0)])
np.testing.assert_array_equal(w[1, 1, 2], _plane(data, 0, 1, 1, 0))
assert not np.delete(w[:, 1], 2, axis=1).any()
def test_stk_members(tmp_path):
"""STK files (one IFD, the planes after each other; tifffile calls them T
by their times -- the .nd file says they are z)."""
nd, data, paths = write_set(str(tmp_path), times=2, stages=["a", "b"],
waves=[("DAPI", True), ("BF", False)], nz=3, z_step=0.5, stk=True)
assert paths[(1, 1, 0)].endswith("exp_w1DAPI_s2_t2.STK")
with micro_reader.open(paths[(1, 1, 1)]) as f:
assert f.backend == "metamorph" and len(f.images) == 2
whole = f.images[1]
assert whole.is_ragged and whole.shape == (2, 2, 3, Y, X)
w = whole[...]
np.testing.assert_array_equal(w[1, 0], data[(1, 1, 0)])
np.testing.assert_array_equal(w[0, 1, 1], _plane(data, 1, 0, 1, 0)) # AbsoluteZ: middle
assert whole.metadata.pixel_sizes == {"x": micro_reader.Quantity(1.25, UM),
"y": micro_reader.Quantity(1.25, UM),
"z": micro_reader.Quantity(0.5, UM)}
def test_a_member_of_the_wrong_depth_is_an_error(tmp_path):
nd, _data, paths = write_set(str(tmp_path), times=2, waves=[("w", True)], nz=3)
write_metaseries(paths[(0, 1, 0)], np.zeros((2, Y, X), "u2")) # 2 planes, not 3
with micro_reader.open(nd) as f:
f.images[0][0] # timepoint 0 is fine
with pytest.raises(micro_reader.CorruptFileError, match="expects 3"):
f.images[0][1]
with pytest.raises(micro_reader.CorruptFileError, match="1 of 2 member files"):
f.check_members()
def test_group_files(tmp_path):
nd, _data, paths = write_set(str(tmp_path), times=2, stages=["a", "b"], waves=[("w", True)])
thumb = str(tmp_path / "exp_w1w_s1_thumb.TIF")
tifffile.imwrite(thumb, np.zeros((4, 6), "u2"))
lone = str(tmp_path / "notes.tif")
tifffile.imwrite(lone, np.zeros((4, 6), "u2"))
g = micro_reader.group_files(str(tmp_path))
(group,) = g.groups
assert (group.kind, group.format, group.notes) == ("index", "metamorph", ())
assert os.path.samefile(group.main, nd)
assert {os.path.normcase(p) for p in group.members} == \
{os.path.normcase(p) for p in paths.values()}
assert [os.path.basename(p) for p in group.parts] == [os.path.basename(thumb)]
assert [os.path.basename(p) for p in g.singles] == ["notes.tif"]
# members given without their .nd file: one composite group under it
g = micro_reader.group_files(list(paths.values()))
(group,) = g.groups
assert group.kind == "composite" and os.path.samefile(group.expected, nd)
assert len(group.parts) == len(paths) and not g.singles
def test_member_name_helper():
assert member_name("x", 2, 3, 1, "GFP") == "x_w1GFP_s2_t3.TIF"
def test_stage_coordinates_as_recorded_not_a_position(tmp_path):
"""The members' stage-position-x / -y become stage_position, as recorded;
position (the first pixel along the image axes) stays unknown."""
nd, _, _ = write_set(str(tmp_path), stages=["B2", "C4"], times=2,
stage_xy=lambda s: (70360.2 - 39143.2 * s, 9839.7 - 18184.3 * s))
with micro_reader.open(nd) as f:
b2, c4 = (image.metadata for image in f.images)
assert b2.stage_position == {"x": (70360.2, "micrometer"), "y": (9839.7, "micrometer")}
assert c4.stage_position["x"] == (pytest.approx(31217.0), "micrometer")
assert c4.stage_position["y"] == (pytest.approx(-8344.6), "micrometer")
assert b2.position == {} and c4.position == {}
def test_no_stage_coordinates_recorded(tmp_path):
nd, _, _ = write_set(str(tmp_path), stages=["B2"], times=1)
with micro_reader.open(nd) as f:
assert f.images[0].metadata.stage_position == {}
def test_an_underscore_in_a_wave_name_is_a_dash_in_the_file_name(tmp_path):
"""MetaMorph writes WaveName ``iSIM1_DAPI`` as ``_w1iSIM1-DAPI`` (the Zenodo
7963332 sample); the channel keeps the ``.nd`` file's spelling."""
nd, data, paths = write_set(str(tmp_path), waves=[("iSIM1_DAPI", True), ("WF_BF", False)], nz=3)
assert os.path.basename(paths[(0, 0, 0)]) == "exp_w1iSIM1-DAPI.TIF"
assert os.path.basename(paths[(0, 0, 1)]) == "exp_w2WF-BF.TIF"
with warnings.catch_warnings():
warnings.simplefilter("error")
with micro_reader.open(nd) as f:
(im,) = f.images
assert im.is_ragged and im.shape == (1, 2, 3, Y, X)
assert [c.name for c in im.metadata.channels] == ["iSIM1_DAPI", "WF_BF"]
np.testing.assert_array_equal(im[0, 0], data[(0, 0, 0)])
np.testing.assert_array_equal(im[0, 1, 1], _plane(data, 0, 0, 1, 0))
assert len(f._member_files()) == 2
with micro_reader.open(paths[(0, 0, 1)]) as f: # a member opens the set
assert f.backend == "metamorph" and os.path.samefile(f.path, nd)
assert metamorph.wave_spellings("iSIM1_DAPI") == ["iSIM1_DAPI", "iSIM1-DAPI"]
assert metamorph.wave_spellings("GFP") == ["GFP"] and metamorph.wave_spellings(None) == [""]
lay = metamorph.layout(metamorph.read_nd(nd), nd)
assert metamorph.member_key(lay, "exp_w1iSIM1-DAPI.TIF") == (0, 0, 0)
assert metamorph.member_key(lay, "exp_w1iSIM1_DAPI.TIF") == (0, 0, 0) # as the .nd file spells it
assert metamorph.member_key(lay, "exp_w1iSIM1 DAPI.TIF") is None
def test_the_nd_spelling_of_a_wave_name_is_found_too(tmp_path):
"""A file named with the ``_`` as the ``.nd`` file spells it still reads;
both spellings there: neither is used."""
nd, data, paths = write_set(str(tmp_path), times=2, waves=[("a_b", True)], raw_wave_names=True)
assert os.path.basename(paths[(0, 1, 0)]) == "exp_w1a_b_t2.TIF"
with micro_reader.open(nd) as f:
np.testing.assert_array_equal(f.images[0][1, 0, 0], _plane(data, 0, 1, 0, 0))
write_metaseries(str(tmp_path / "exp_w1a-b_t2.TIF"), data[(0, 1, 0)])
with pytest.warns(micro_reader.CompositeWarning, match="more than one file"), \
pytest.warns(micro_reader.MissingImagesWarning):
f = micro_reader.open(nd)
with f:
np.testing.assert_array_equal(f.images[0][0, 0, 0], _plane(data, 0, 0, 0, 0))
assert not f.images[0][1, 0, 0].any()
DATA = os.environ.get("MICROSCOPY_DATA", os.path.expanduser("~/Desktop/data/microscopy"))
def test_zenodo_7963332_wave_names_with_underscores():
"""KO_2789_002.nd (CC BY 4.0): WaveName ``iSIM1_DAPI`` ... ``WF_BF``, the
files ``_w1iSIM1-DAPI`` ... ``_w4WF-BF``. The 4th wave has no z (WaveDoZ4
FALSE): one plane, placed at its focus position, the middle of the 29-plane
stacks; ZStepSize is 0, so the z step comes from the focus positions."""
folder = os.path.join(DATA, "formats", "metamorph", "zenodo-7963332")
nd = os.path.join(folder, "KO_2789_002.nd")
if not os.path.isfile(nd):
pytest.skip(f"sample not found: {nd} (set MICROSCOPY_DATA)")
w4 = os.path.join(folder, "KO_2789_002_w4WF-BF.TIF")
ref_bf = tifffile.imread(w4) # tifffile warns on its own (NumPy)
ref_gfp = tifffile.imread(os.path.join(folder, "KO_2789_002_w2iSIM2-GFP.TIF"), key=5)
with warnings.catch_warnings():
warnings.simplefilter("error")
with micro_reader.open(nd) as f:
(im,) = f.images
assert im.is_ragged and im.shape == (1, 4, 29, 974, 1402) and im.dtype == "uint16"
assert [c.name for c in im.metadata.channels] == \
["iSIM1_DAPI", "iSIM2_GFP", "iSIM3_RFP", "WF_BF"]
assert sorted(os.path.basename(p) for p in f._member_files()) == \
["KO_2789_002_w1iSIM1-DAPI.TIF", "KO_2789_002_w2iSIM2-GFP.TIF",
"KO_2789_002_w3iSIM3-RFP.TIF", "KO_2789_002_w4WF-BF.TIF"]
assert im.metadata.pixel_sizes["x"] == micro_reader.Quantity(0.065, UM)
assert im.metadata.pixel_sizes["z"] == (pytest.approx(0.2), UM)
stack, flat = im.parts
assert stack.shape == (1, 3, 29, 974, 1402) and flat.shape == (1, 1, 1, 974, 1402)
assert "z" not in flat.metadata.pixel_sizes
np.testing.assert_array_equal(flat[0, 0, 0], ref_bf)
bf = im[0, 3] # the whole: WF_BF at z 14 only
np.testing.assert_array_equal(bf[14], ref_bf)
assert not np.delete(bf, 14, axis=0).any()
np.testing.assert_array_equal(im[0, 1, 5], ref_gfp)
with micro_reader.open(w4) as f: # a member opens the set
assert f.backend == "metamorph" and os.path.samefile(f.path, nd)
def test_the_time_step_comes_from_the_planes_stamps(tmp_path):
# review cycle 1, N-62: no time step, though every plane records its acquisition time
nd, _data, _paths = write_set(str(tmp_path), times=4, waves=[("GFP", False), ("RFP", False)],
t_step=60.5)
with micro_reader.open(nd) as f:
step = f.images[0].metadata.pixel_sizes["t"]
assert (round(step.value, 3), step.unit) == (60.5, "second")
nd, _data, _paths = write_set(str(tmp_path / "plain"), times=3) # no stamps: no step
with micro_reader.open(nd) as f:
assert "t" not in f.images[0].metadata.pixel_sizes
def test_micro_manager_2_position_labels_are_read():
# review cycle 1, N-61: 2.0 writes StagePositions[k]["Label"]; it was looked for as "label"
from micro_reader.backends.micromanager import _labels
summary = {"Positions": 2, "StagePositions": [{"Label": "2-Pos000_000"}, {"Label": "2-Pos001_000"}]}
assert _labels(summary) == ["2-Pos000_000", "2-Pos001_000"]
assert _labels({"Positions": 2, "StagePositions": [{"label": "A"}, {"label": "B"}]}) == ["A", "B"]