-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_segmentation.py
More file actions
450 lines (384 loc) · 13.3 KB
/
Copy pathbenchmark_segmentation.py
File metadata and controls
450 lines (384 loc) · 13.3 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
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
import os
from pathlib import Path
import loguru
import numpy as np
import spatialdata as sd
from numpy.typing import NDArray
logger = loguru.logger
def harpy_segment(
sdata: sd.SpatialData,
chunksize: int,
img_layer: str,
workers: int | None = None,
threads: int | None = None,
):
import harpy as hp
from dask.distributed import Client, LocalCluster
from instanseg import InstanSeg
logger.info(f"Running on dataset {sdata}")
if workers is not None and threads is not None:
cluster = LocalCluster(
n_workers=workers,
threads_per_worker=threads,
memory_limit="500GB", # prevent spilling to disk
)
client = Client(cluster)
logger.info(client.dashboard_link)
else:
logger.info(
"Workers or threads not specified, running segmentation without a client."
)
logger.info("Start segmentation.")
_ = InstanSeg("fluorescence_nuclei_and_cells", verbosity=1, device="cpu")
path_model = os.path.join(
os.environ.get("INSTANSEG_BIOIMAGEIO_PATH"),
"fluorescence_nuclei_and_cells/0.1.0/instanseg.pt",
)
logger.info(f"Path to instanseg model: {path_model}.")
sdata = hp.im.segment(
sdata,
img_layer=img_layer,
output_labels_layer=["labels_nuclei_harpy", "labels_cells_harpy"],
output_shapes_layer=None,
labels_layer_align=None,
chunks=(chunksize, chunksize),
depth=50,
model=hp.im.instanseg_callable,
pixel_size=0.17,
# parameters passed to hp.im.instanseg_callable
output="all_outputs",
device="cpu",
instanseg_model=path_model, # load it in every worker, because torchscript model is not serializable
iou=True,
trim=False,
overwrite=True,
)
def harpy_cellpose_segment(
sdata: sd.SpatialData,
chunksize: int,
img_layer: str,
workers: int | None = None,
threads: int | None = None,
):
import harpy as hp
from dask.distributed import Client, LocalCluster
logger.info(f"Running on dataset {sdata}")
if workers is not None and threads is not None:
cluster = LocalCluster(
n_workers=workers,
threads_per_worker=threads,
memory_limit="500GB", # prevent spilling to disk
)
client = Client(cluster)
logger.info(client.dashboard_link)
else:
logger.info(
"Workers or threads not specified, running segmentation without a client."
)
logger.info("Start segmentation.")
sdata = hp.im.segment(
sdata,
img_layer=img_layer,
model=hp.im.cellpose_callable,
device="cpu",
model_type="cyto3",
output_labels_layer="labels_cells_harpy",
diameter=30,
depth=50,
channels=[2, 1],
chunks=(chunksize, chunksize),
)
logger.info("End segmentation.")
def instanseg_segment(
sdata: sd.SpatialData,
chunksize: int,
img_layer: str,
workers: int | None = None,
threads: int | None = None,
):
import torch
from instanseg import InstanSeg
_ = InstanSeg("fluorescence_nuclei_and_cells", verbosity=1, device="cpu")
path_model = os.path.join(
os.environ.get("INSTANSEG_BIOIMAGEIO_PATH"),
"fluorescence_nuclei_and_cells/0.1.0/instanseg.pt",
)
batch_size = 1
if workers is not None and threads is not None:
batch_size = max(workers, threads)
elif workers is not None:
batch_size = workers
elif threads is not None:
batch_size = threads
instanseg_model = torch.load(path_model)
instanseg_model = InstanSeg(model_type=instanseg_model, device="cpu")
image_array = sdata[img_layer].data.compute()
labeled_output, _ = instanseg_model.eval_medium_image(
image_array,
tile_size=chunksize,
batch_size=batch_size,
resolve_cell_and_nucleus=True,
cleanup_fragments=True,
target="all_outputs",
pixel_size=0.17,
) # "all_outputs", "nuclei", or "cells".
nuclei, cells = labeled_output.squeeze(0).numpy().astype(np.uint32)
sdata["labels_nuclei_instanseg"] = sd.models.Labels2DModel.parse(
nuclei, dims=("y", "x")
)
sdata.write_element("labels_nuclei_instanseg")
sdata["labels_cells_instanseg"] = sd.models.Labels2DModel.parse(
cells, dims=("y", "x")
)
sdata.write_element("labels_cells_instanseg")
def sopa_segment(
sdata,
chunksize: int,
img_layer: str,
workers: int,
threads: int,
):
import sopa
sopa.settings.parallelization_backend = "dask"
sopa.settings.dask_client_kwargs["n_workers"] = workers
sopa.settings.dask_client_kwargs["threads_per_worker"] = threads
sopa.make_image_patches(
sdata,
patch_width=chunksize,
image_key=img_layer,
patch_overlap=50,
)
output_layer = "shapes_sopa"
sopa.segmentation.custom_staining_based(
sdata,
instanseg_callable_sopa,
channels=sdata[img_layer].c.data.tolist(),
image_key=img_layer,
key_added=output_layer,
# does not support model parameters, so we rely on the pixel_size default of 0.17 in instanseg_callable_sopa
)
nr_of_shapes_found = len(sdata[output_layer])
logger.info(f"Sopa segmentation finished. Found {nr_of_shapes_found} shapes.")
def squidpy_segment(
sdata,
img_layer: str,
workers: int,
threads: int,
):
import squidpy as sq
from dask.distributed import Client, LocalCluster
logger.info(f"Running on dataset {sdata}")
if workers is not None and threads is not None:
cluster = LocalCluster(
n_workers=workers,
threads_per_worker=threads,
memory_limit="500GB", # prevent spilling to disk
)
client = Client(cluster)
logger.info(client.dashboard_link)
else:
logger.info(
"Workers or threads not specified, running segmentation without a client."
)
logger.info("Start segmentation.")
arr = sdata[img_layer].data
arr = arr[:, None, ...].transpose(2, 3, 1, 0) # (y,x,z,c)
ic = sq.im.ImageContainer(arr, layer=img_layer, lazy=True)
sq.im.segment(
img=ic,
layer=img_layer,
layer_added="labels_cells_instanseg",
method=instanseg_callable_squidpy,
lazy=True,
channel=ic[img_layer].channels.data.tolist(),
depth=(50, 50, 0, 0),
pixel_size=0.17,
)
sdata["labels_cells_instanseg"] = sd.models.Labels2DModel.parse(
ic["labels_cells_instanseg"].data.squeeze(), dims=("y", "x")
)
sdata.write_element("labels_cells_instanseg")
logger.info("Segmentation finished.")
def instanseg_callable_sopa(
img: NDArray,
device: str | None = "cpu",
dtype: type = np.uint32,
pixel_size: float = 0.17,
**kwargs, # kwargs passed to .eval_small_image
) -> NDArray:
# input is c,y,x
# output is y,x
from instanseg import InstanSeg
import torch
_ = InstanSeg("fluorescence_nuclei_and_cells", verbosity=1, device="cpu")
path_model = os.path.join(
os.environ.get("INSTANSEG_BIOIMAGEIO_PATH"),
"fluorescence_nuclei_and_cells/0.1.0/instanseg.pt",
)
instanseg_model = torch.load(path_model, weights_only=False)
instanseg_model = InstanSeg(model_type=instanseg_model, device=device)
labeled_output, _ = instanseg_model.eval_small_image(
img,
pixel_size=pixel_size,
resolve_cell_and_nucleus=True,
cleanup_fragments=True,
target="cells",
**kwargs,
)
# we want the c dimension to be the last dimension and the output to be in numpy format
labeled_output = labeled_output.permute([0, 2, 3, 1]).cpu().numpy().astype(dtype)
# already has a trivial z dimension (batch) at 0
# dimension 1 is (nucleus mask (0) and whole cell mask (1))
return labeled_output.squeeze()
def instanseg_callable_squidpy(
img: NDArray,
device: str | None = "cpu",
dtype: type = np.uint32,
pixel_size: float = 0.17,
**kwargs, # kwargs passed to .eval_small_image
) -> NDArray:
# input is y,x,c. z is ignored by squidpy
img = img.transpose(2, 0, 1) # this is c,y,x
# output is y,x
from instanseg import InstanSeg
import torch
_ = InstanSeg("fluorescence_nuclei_and_cells", verbosity=1, device="cpu")
path_model = os.path.join(
os.environ.get("INSTANSEG_BIOIMAGEIO_PATH"),
"fluorescence_nuclei_and_cells/0.1.0/instanseg.pt",
)
instanseg_model = torch.load(path_model, weights_only=False)
instanseg_model = InstanSeg(model_type=instanseg_model, device=device)
labeled_output, _ = instanseg_model.eval_small_image(
img,
pixel_size=pixel_size,
resolve_cell_and_nucleus=True,
cleanup_fragments=True,
target="cells",
**kwargs,
)
labeled_output = labeled_output.permute([0, 2, 3, 1]).cpu().numpy().astype(dtype)
labeled_output = labeled_output.squeeze()
return labeled_output
def zarr_file(value):
path = Path(value).resolve()
if path.suffix != ".zarr":
raise argparse.ArgumentTypeError("Dataset must be a .zarr file.")
return path
if __name__ == "__main__":
import argparse
# get path from argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type=zarr_file,
help="Path where a multi channel artificial dataset will be created.",
)
parser.add_argument(
"--c_dim",
type=int,
default=10,
help="The target number of channels (first axis). Default is 10. Ignored if 'cellpose' in 'method'.",
)
parser.add_argument(
"--y_dim",
type=int,
default=10000,
help="The target size along the y-axis (second axis). Default is 10000.",
)
parser.add_argument(
"--x_dim",
type=int,
default=10000,
help="The target size along the x-axis (third axis). Default is 10000.",
)
parser.add_argument(
"--chunksize",
type=int,
default=4096,
help="Chunksize in y and x. Chunksize in c is c_dim.",
)
parser.add_argument(
"--img_layer",
type=str,
default="image_tiled",
help="Name of the image layer in spatialdata object to run benchmark on.",
)
parser.add_argument("--method", help="Method to use", default="harpy")
parser.add_argument("--threads", help="Threads per worker", default=None, type=int)
parser.add_argument("--workers", help="Workers to use", default=None, type=int)
args = parser.parse_args()
d = Path(args.dataset).resolve()
# p_profile: Path = Path(args.profile).resolve()
# if not p_profile.parent.exists():
# p_profile.parent.mkdir(parents=True)
if not d.exists():
logger.info(f"Dataset {d} does not exist. Creating dataset at {args.dataset}")
if "cellpose" not in args.method:
from prep_multi_channel_dataset import create_multi_channel_macsima_dataset
sdata = create_multi_channel_macsima_dataset(
path=args.dataset,
c_dim=args.c_dim,
y_dim=args.y_dim,
x_dim=args.x_dim,
chunksize=args.chunksize,
img_layer=args.img_layer,
dtype=np.uint32 if args.method == "sopa" else np.float32, # sopa only accepts np.uint
)
else:
from prep_cellpose_dataset import create_cellpose_dataset
sdata = create_cellpose_dataset(
path=args.dataset,
y_dim=args.y_dim,
x_dim=args.x_dim,
chunksize=args.chunksize,
img_layer=args.img_layer,
)
else:
# raise FileExistsError(
# f"A dataset already exists at {d}. To create a new dataset, "
# "please specify a different path or remove the existing dataset."
# )
logger.info(f"Dataset already exists. Reading dataset at {d}")
sdata = sd.read_zarr(d)
# sdata needs to be backed, otherwise we persist mask in memory
# sdata.path = None
if args.method == "harpy":
harpy_segment(
sdata,
chunksize=args.chunksize,
img_layer=args.img_layer,
workers=args.workers,
threads=args.threads,
)
if args.method == "harpy_cellpose":
harpy_cellpose_segment(
sdata,
chunksize=args.chunksize,
img_layer=args.img_layer,
workers=args.workers,
threads=args.threads,
)
if args.method == "instanseg":
instanseg_segment(
sdata,
chunksize=args.chunksize,
img_layer=args.img_layer,
workers=args.workers,
threads=args.threads,
)
if args.method == "sopa":
sopa_segment(
sdata,
chunksize=args.chunksize,
img_layer=args.img_layer,
workers=args.workers,
threads=args.threads,
)
if args.method == "squidpy":
squidpy_segment(
sdata,
img_layer=args.img_layer,
workers=args.workers,
threads=args.threads,
)