-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_workflows.py
463 lines (401 loc) · 13.9 KB
/
test_workflows.py
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""
Copyright 2022 (C) Friedrich Miescher Institute for Biomedical Research and
University of Zurich
Original authors:
Marco Franzon <marco.franzon@exact-lab.it>
Tommaso Comparin <tommaso.comparin@exact-lab.it>
This file is part of Fractal and was originally developed by eXact lab S.r.l.
<exact-lab.it> under contract with Liberali Lab from the Friedrich Miescher
Institute for Biomedical Research and Pelkmans Lab from the University of
Zurich.
"""
import logging
import shutil
from pathlib import Path
import pytest
import zarr
from devtools import debug
from ._validation import check_file_number
from ._validation import validate_schema
from fractal_tasks_core.tasks.cellvoyager_to_ome_zarr_compute import (
cellvoyager_to_ome_zarr_compute,
)
from fractal_tasks_core.tasks.cellvoyager_to_ome_zarr_init import (
cellvoyager_to_ome_zarr_init,
)
from fractal_tasks_core.tasks.copy_ome_zarr_hcs_plate import (
copy_ome_zarr_hcs_plate,
)
from fractal_tasks_core.tasks.illumination_correction import (
illumination_correction,
)
from fractal_tasks_core.tasks.maximum_intensity_projection import (
maximum_intensity_projection,
)
from fractal_tasks_core.zarr_utils import OverwriteNotAllowedError
allowed_channels = [
{
"label": "DAPI",
"wavelength_id": "A01_C01",
"color": "00FFFF",
"window": {"start": 0, "end": 700},
},
{
"wavelength_id": "A01_C02",
"label": "nanog",
"color": "FF00FF",
"window": {"start": 0, "end": 180},
},
{
"wavelength_id": "A02_C03",
"label": "Lamin B1",
"color": "FFFF00",
"window": {"start": 0, "end": 1500},
},
]
num_levels = 6
coarsening_xy = 2
@pytest.mark.xfail(reason="This would fail for a dataset with N>1 channels")
def test_create_ome_zarr_fail(tmp_path: Path, zenodo_images: str):
tmp_allowed_channels = [
{"label": "repeated label", "wavelength_id": "A01_C01"},
{"label": "repeated label", "wavelength_id": "A01_C02"},
{"label": "repeated label", "wavelength_id": "A02_C03"},
]
# Init
image_dir = zenodo_images
zarr_dir = str(tmp_path / "tmp_out/")
# Create zarr structure
with pytest.raises(ValueError):
_ = cellvoyager_to_ome_zarr_init(
zarr_urls=[],
zarr_dir=zarr_dir,
image_dirs=[image_dir],
allowed_channels=tmp_allowed_channels,
num_levels=num_levels,
coarsening_xy=coarsening_xy,
metadata_table_file=None,
)
def test_create_ome_zarr_no_images(
tmp_path: Path,
zenodo_images: str,
testdata_path: Path,
):
"""
For invalid image_extension or image_glob_patterns arguments,
create_ome_zarr must fail.
"""
with pytest.raises(ValueError):
cellvoyager_to_ome_zarr_init(
zarr_urls=[],
zarr_dir=str(tmp_path / "output"),
image_dirs=[zenodo_images],
allowed_channels=allowed_channels,
num_levels=num_levels,
coarsening_xy=coarsening_xy,
metadata_table_file=None,
image_extension="xyz",
)
with pytest.raises(ValueError):
cellvoyager_to_ome_zarr_init(
zarr_urls=[],
zarr_dir=str(tmp_path / "output"),
image_dirs=[zenodo_images],
allowed_channels=allowed_channels,
num_levels=num_levels,
coarsening_xy=coarsening_xy,
metadata_table_file=None,
image_extension="png",
image_glob_patterns=["*asdasd*"],
)
metadata_inputs = ["use_mrf_mlf_files", "use_existing_csv_files"]
@pytest.mark.parametrize("metadata_input", metadata_inputs)
def test_yokogawa_to_ome_zarr(
tmp_path: Path,
zenodo_images: str,
testdata_path: Path,
metadata_input: str,
):
# Select the kind of metadata_table_file input
if metadata_input == "use_mrf_mlf_files":
metadata_table_file = None
if metadata_input == "use_existing_csv_files":
testdata_str = testdata_path.as_posix()
metadata_table_file = (
f"{testdata_str}/metadata_files/"
+ "corrected_site_metadata_tiny_test.csv"
)
debug(metadata_table_file)
# Init
img_path = Path(zenodo_images)
output_path = tmp_path / "output"
# Create zarr structure
parallelization_list = cellvoyager_to_ome_zarr_init(
zarr_urls=[],
zarr_dir=str(output_path),
image_dirs=[str(img_path)],
allowed_channels=allowed_channels,
num_levels=num_levels,
coarsening_xy=coarsening_xy,
metadata_table_file=metadata_table_file,
image_extension="png",
)["parallelization_list"]
debug(parallelization_list)
image_list_updates = []
# Yokogawa to zarr
for image in parallelization_list:
image_list_updates += cellvoyager_to_ome_zarr_compute(
zarr_url=image["zarr_url"],
init_args=image["init_args"],
)["image_list_updates"]
debug(image_list_updates)
# Validate image_list_updates contents
expected_image_list_update = {
"zarr_url": (
f"{output_path}/20200812-CardiomyocyteDifferentiation14"
"-Cycle1.zarr/B/03/0/"
),
"attributes": {
"plate": "20200812-CardiomyocyteDifferentiation14-Cycle1.zarr",
"well": "B03",
},
"types": {
"is_3D": True,
},
}
assert image_list_updates[0] == expected_image_list_update
# OME-NGFF JSON validation
image_zarr = Path(parallelization_list[0]["zarr_url"])
well_zarr = image_zarr.parent
plate_zarr = image_zarr.parents[2]
validate_schema(path=str(image_zarr), type="image")
validate_schema(path=str(well_zarr), type="well")
validate_schema(path=str(plate_zarr), type="plate")
check_file_number(zarr_path=image_zarr)
# Test presence and attributes of FOV/well ROI tables
for table_name in ["FOV_ROI_table", "well_ROI_table"]:
table_attrs = zarr.open_group(
image_zarr / f"tables/{table_name}", mode="r"
).attrs.asdict()
assert table_attrs["type"] == "roi_table"
assert table_attrs["fractal_table_version"] == "1"
# Re-run (with overwrite=True for the init task)
parallelization_list = cellvoyager_to_ome_zarr_init(
zarr_urls=[],
zarr_dir=str(output_path),
image_dirs=[str(img_path)],
allowed_channels=allowed_channels,
num_levels=num_levels,
coarsening_xy=coarsening_xy,
metadata_table_file=metadata_table_file,
image_extension="png",
overwrite=True,
)["parallelization_list"]
for image in parallelization_list:
cellvoyager_to_ome_zarr_compute(
zarr_url=image["zarr_url"],
init_args=image["init_args"],
)
# Re-run (with overwrite=False for the init task) and fail
with pytest.raises(OverwriteNotAllowedError):
cellvoyager_to_ome_zarr_init(
zarr_urls=[],
zarr_dir=str(output_path),
image_dirs=[str(img_path)],
allowed_channels=allowed_channels,
num_levels=num_levels,
coarsening_xy=coarsening_xy,
metadata_table_file=metadata_table_file,
image_extension="png",
overwrite=False,
)
def test_MIP(
tmp_path: Path,
zenodo_zarr: list[str],
):
# Init
zarr_path = tmp_path / "tmp_out/"
# Load zarr array from zenodo
zenodo_zarr_3D, zenodo_zarr_2D = zenodo_zarr[:]
shutil.copytree(zenodo_zarr_3D, str(zarr_path / Path(zenodo_zarr_3D).name))
zarr_urls = []
zarr_dir = "/".join(zenodo_zarr_3D.split("/")[:-1])
zarr_urls = [Path(zarr_dir, "plate.zarr/B/03/0").as_posix()]
parallelization_list = copy_ome_zarr_hcs_plate(
zarr_urls=zarr_urls,
zarr_dir="tmp_out",
overwrite=True,
)["parallelization_list"]
debug(parallelization_list)
# Run again, with overwrite=True
parallelization_list_2 = copy_ome_zarr_hcs_plate(
zarr_urls=zarr_urls,
zarr_dir="tmp_out",
overwrite=True,
)["parallelization_list"]
assert parallelization_list_2 == parallelization_list
# Run again, with overwrite=False
with pytest.raises(OverwriteNotAllowedError):
_ = copy_ome_zarr_hcs_plate(
zarr_urls=zarr_urls,
zarr_dir="tmp_out",
overwrite=False,
)
# MIP
image_list_updates = []
for image in parallelization_list:
image_list_updates += maximum_intensity_projection(
zarr_url=image["zarr_url"],
init_args=image["init_args"],
overwrite=True,
)["image_list_updates"]
debug(image_list_updates[0])
expected_image_list_updates = {
"zarr_url": (parallelization_list[0]["zarr_url"]),
"origin": f"{zarr_dir}/plate.zarr/B/03/0",
"types": {
"is_3D": False,
},
}
debug(expected_image_list_updates)
assert image_list_updates[0] == expected_image_list_updates
# Re-run with overwrite=True
for image in parallelization_list:
maximum_intensity_projection(
zarr_url=image["zarr_url"],
init_args=image["init_args"],
overwrite=True,
)
# Re-run with overwrite=False
with pytest.raises(OverwriteNotAllowedError):
for image in parallelization_list:
maximum_intensity_projection(
zarr_url=image["zarr_url"],
init_args=image["init_args"],
overwrite=False,
)
# OME-NGFF JSON validation
image_zarr = Path(parallelization_list[0]["zarr_url"])
debug(image_zarr)
well_zarr = image_zarr.parent
plate_zarr = image_zarr.parents[2]
validate_schema(path=str(image_zarr), type="image")
validate_schema(path=str(well_zarr), type="well")
validate_schema(path=str(plate_zarr), type="plate")
# Test presence and attributes of FOV/well ROI tables
for table_name in ["FOV_ROI_table", "well_ROI_table"]:
table_attrs = zarr.open_group(
image_zarr / f"tables/{table_name}", mode="r"
).attrs.asdict()
assert table_attrs["type"] == "roi_table"
assert table_attrs["fractal_table_version"] == "1"
def test_MIP_subset_of_images(
tmp_path: Path,
zenodo_images: str,
):
"""
Run a full image-parsing + MIP workflow on a subset of the images (i.e. a
single field of view).
"""
# Init
zarr_dir = tmp_path / "tmp_out/"
# Create zarr structure
parallelization_list = cellvoyager_to_ome_zarr_init(
zarr_urls=[],
zarr_dir=str(zarr_dir),
image_dirs=[zenodo_images],
allowed_channels=allowed_channels,
num_levels=num_levels,
coarsening_xy=coarsening_xy,
metadata_table_file=None,
image_extension="png",
image_glob_patterns=["*F001*"],
)["parallelization_list"]
debug(parallelization_list)
# Yokogawa to zarr
image_list_updates = []
# Yokogawa to zarr
for image in parallelization_list:
image_list_updates += cellvoyager_to_ome_zarr_compute(
zarr_url=image["zarr_url"],
init_args=image["init_args"],
)["image_list_updates"]
debug(image_list_updates)
zarr_urls = [a["zarr_url"] for a in image_list_updates]
debug(zarr_urls)
# Replicate
parallelization_list = copy_ome_zarr_hcs_plate(
zarr_urls=zarr_urls,
zarr_dir="tmp_out",
overwrite=True,
)["parallelization_list"]
debug(parallelization_list)
# MIP
for image in parallelization_list:
maximum_intensity_projection(
zarr_url=image["zarr_url"],
init_args=image["init_args"],
overwrite=True,
)
# OME-NGFF JSON validation
image_zarr = Path(parallelization_list[0]["zarr_url"])
debug(image_zarr)
well_zarr = image_zarr.parent
plate_zarr = image_zarr.parents[2]
validate_schema(path=str(image_zarr), type="image")
validate_schema(path=str(well_zarr), type="well")
validate_schema(path=str(plate_zarr), type="plate")
def test_illumination_correction(
tmp_path: Path,
testdata_path: Path,
zenodo_images: str,
caplog: pytest.LogCaptureFixture,
):
# Setup caplog fixture, see
# https://docs.pytest.org/en/stable/how-to/logging.html#caplog-fixture
caplog.set_level(logging.INFO)
# Init
img_path = Path(zenodo_images)
zarr_dir = tmp_path / "tmp_out"
testdata_str = testdata_path.as_posix()
illum_params = {"A01_C01": "illum_corr_matrix.png"}
illumination_profiles_folder = f"{testdata_str}/illumination_correction/"
# Create zarr structure
parallelization_list = cellvoyager_to_ome_zarr_init(
zarr_urls=[],
zarr_dir=str(zarr_dir),
image_dirs=[str(img_path)],
image_extension="png",
allowed_channels=allowed_channels,
num_levels=num_levels,
coarsening_xy=coarsening_xy,
metadata_table_file=None,
)["parallelization_list"]
print(caplog.text)
caplog.clear()
# Yokogawa to zarr
for image in parallelization_list:
cellvoyager_to_ome_zarr_compute(
zarr_url=image["zarr_url"],
init_args=image["init_args"],
)
print(caplog.text)
caplog.clear()
# Illumination correction
for image in parallelization_list:
illumination_correction(
zarr_url=image["zarr_url"],
overwrite_input=True,
illumination_profiles_folder=illumination_profiles_folder,
illumination_profiles=illum_params,
)
print(caplog.text)
caplog.clear()
# OME-NGFF JSON validation
image_zarr = Path(parallelization_list[0]["zarr_url"])
well_zarr = image_zarr.parent
plate_zarr = image_zarr.parents[2]
validate_schema(path=str(image_zarr), type="image")
validate_schema(path=str(well_zarr), type="well")
validate_schema(path=str(plate_zarr), type="plate")
check_file_number(zarr_path=image_zarr)