forked from napari/napari
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell_tracking.py
More file actions
75 lines (59 loc) · 2.12 KB
/
Copy pathcell_tracking.py
File metadata and controls
75 lines (59 loc) · 2.12 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
"""
Cell tracking example
=====================
This example demonstrates how the track layer is used
for visualizing cell tracking data, by displaying 2D + time dataset
of cells with colored properties for track-id.
Thanks to Dr. Alessia Ruggieri and Philipp Klein, Centre for Integrative Infectious
Disease Research (CIID), University Hospital Heidelberg, Germany for the data.
You can find the data on: https://doi.org/10.5281/zenodo.15597019
.. tags:: visualization-advanced, colored 2D tracks
"""
from pathlib import Path
import numpy as np
import pooch
import tifffile
from trackastra.model import Trackastra
from trackastra.tracking import ctc_to_napari_tracks, graph_to_ctc
import napari
# Create temporary directory
tmp_dir = Path(pooch.os_cache('napari-cell-tracking-example'))
tmp_dir.mkdir(parents=True, exist_ok=True)
# Extract silver truth data for trackastra
st_url = "doi:10.5281/zenodo.15852284/masks_pred.npz"
st_path = pooch.retrieve(
url=st_url,
fname="masks_pred.npz",
known_hash=None,
path=pooch.os_cache("napari cell tracking example")
)
# Extract raw tif files for trackastra
tif_url = "doi:10.5281/zenodo.17643282/01(1).zip"
tif_file = pooch.retrieve(
url=tif_url,
fname="01(1).zip",
known_hash=None,
path=pooch.os_cache("napari_cell_tracking_example"),
processor=pooch.Unzip(),
)
# Load the downloaded masks
masks_npz = np.load(st_path)
masks = masks_npz['masks']
# Sort through images and stack them
imgs = sorted(tif_file)
images = np.stack([
tifffile.imread(fn) for fn in imgs
])
# Initiate trackastra model
model = Trackastra.from_pretrained("general_2d", device='cpu')
# Generate tracks
graph, *_ = model.track(imgs=images, masks=masks, mode='greedy')
tracks_df, tracked_masks = graph_to_ctc(graph=graph, masks_original=masks)
napari_tracks, napari_graph = ctc_to_napari_tracks(segmentation=tracked_masks, man_track=tracks_df)
# Add Napari viewer
viewer = napari.Viewer()
viewer.add_image(images, name='Images')
viewer.add_labels(masks, name='Predicted Masks')
viewer.add_tracks(napari_tracks, graph=napari_graph, name="Tracks")
if __name__ == "__main__":
napari.run()