Skip to content

Commit

Permalink
Merge pull request #99 from anwai98/u-tracking-metrics
Browse files Browse the repository at this point in the history
Add scripts for traccuracy metric evaluation
  • Loading branch information
constantinpape authored May 7, 2024
2 parents 87aed16 + 892ad63 commit 3a7d489
Show file tree
Hide file tree
Showing 3 changed files with 414 additions and 0 deletions.
103 changes: 103 additions & 0 deletions scripts/get_tracking_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import os
from pathlib import Path

import numpy as np
import pandas as pd
import imageio.v3 as imageio

from deepcell_tracking.utils import load_trks


ROOT = "/scratch/projects/nim00007/sam/for_tracking"


def load_tracking_segmentation(experiment):
result_dir = os.path.join(ROOT, "results")

if experiment.startswith("vit"):
if experiment == "vit_l":
seg_path = os.path.join(result_dir, "vit_l.tif")
elif experiment == "vit_l_lm":
seg_path = os.path.join(result_dir, "vit_l_lm.tif")
elif experiment == "vit_l_specialist":
seg_path = os.path.join(result_dir, "vit_l_lm_specialist.tif")
# elif experiment == "trackmate_stardist":
# seg_path = os.path.join(result_dir, "trackmate_stardist", "every_3rd_fr_result.tif")

else:
raise ValueError(experiment)

seg = imageio.imread(seg_path)
return seg

else: # return the result directory for stardist
return os.path.join(result_dir, "trackmate_stardist", "01_RES")


def check_tracking_results(raw, labels, curr_lineages, chosen_frames):
seg_default = load_tracking_segmentation("vit_l")
seg_generalist = load_tracking_segmentation("vit_l_lm")
seg_specialist = load_tracking_segmentation("vit_l_specialist")

# let's get the tracks only for the objects present per frame
for idx in np.unique(labels)[1:]:
lineage = curr_lineages[idx]
lineage["frames"] = [frame for frame in lineage["frames"] if frame in chosen_frames]

import napari
v = napari.Viewer()
v.add_image(raw)
v.add_labels(labels)

v.add_labels(seg_default, visible=False)
v.add_labels(seg_generalist, visible=False)
v.add_labels(seg_specialist, visible=False)

napari.run()


def get_tracking_data():
data_dir = os.path.join(ROOT, "data", "DynamicNuclearNet-tracking-v1_0")
data_source = np.load(os.path.join(data_dir, "data-source.npz"), allow_pickle=True)

fname = "test.trks"
track_file = os.path.join(data_dir, fname)
split_name = Path(track_file).stem

data = load_trks(track_file)

X = data["X"]
y = data["y"]
lineages = data["lineages"]

meta = pd.DataFrame(
data_source[split_name],
columns=["filename", "experiment", "pixel_size", "screening_passed", "time_step", "specimen"]
)
# print(meta)

# let's convert the data to expected shape
X = X.squeeze(-1)
y = y.squeeze(-1)

# NOTE: chosen slice for the tracking user study.
_slice = 7
raw, labels = X[_slice, ...], y[_slice, ...]
curr_lineages = lineages[_slice]

# NOTE: let's get every third frame of data and see how it looks
chosen_frames = list(range(0, raw.shape[0], 3))
raw = np.stack([raw[frame] for frame in chosen_frames])
labels = np.stack([labels[frame] for frame in chosen_frames])

# let's create a value map
frmaps = {}
for i, frval in enumerate(chosen_frames):
frmaps[frval] = i

# let's remove frames which are not a part of our chosen frames
for k, v in curr_lineages.items():
curr_frames = v["frames"]
v["frames"] = [frmaps[frval] for frval in curr_frames if frval in chosen_frames]

return raw, labels, curr_lineages, chosen_frames
108 changes: 108 additions & 0 deletions scripts/gt_tracks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
,Cell_ID,Start,End,Parent_ID
0,1,0,23,0
1,2,0,23,0
2,3,0,23,0
3,4,0,23,0
4,5,0,20,0
5,6,0,5,0
6,7,0,23,0
7,8,0,23,0
8,9,0,23,0
9,10,0,4,0
10,11,0,22,0
11,12,0,23,0
12,13,0,3,0
13,14,0,23,0
14,15,0,3,0
15,16,0,23,0
16,17,0,23,0
17,18,0,23,0
18,19,0,3,0
19,20,0,23,0
20,21,0,23,0
21,22,0,23,0
22,23,0,22,0
23,24,0,3,0
24,25,0,23,0
25,26,0,23,0
26,27,0,4,0
27,28,0,5,0
28,29,0,23,0
29,30,0,23,0
30,31,0,20,0
31,32,0,11,0
32,33,0,23,0
33,34,0,23,0
34,35,0,7,0
35,36,0,23,0
36,37,0,18,0
37,38,0,23,0
38,39,0,23,0
39,40,0,18,0
40,41,0,1,0
41,42,0,23,0
42,43,0,2,0
43,44,0,23,0
44,45,0,23,0
45,46,0,23,0
46,47,0,23,0
47,48,0,2,0
48,49,0,13,0
49,50,0,13,0
50,51,0,23,0
51,52,0,23,0
52,53,0,3,0
53,54,0,23,0
54,55,0,23,0
55,56,0,1,0
56,57,0,3,0
57,58,0,12,0
58,59,0,23,0
59,60,0,0,0
60,61,1,23,60
61,62,1,23,0
62,63,2,23,41
63,64,2,23,41
64,65,2,23,56
65,66,2,23,56
66,67,3,23,48
67,68,3,23,48
68,69,3,23,43
69,70,3,23,43
70,109,4,23,0
71,71,4,23,13
72,72,4,23,13
73,73,4,23,57
74,74,4,23,24
75,75,4,23,24
76,76,4,23,53
77,77,4,23,53
78,78,4,4,0
79,79,5,23,10
80,80,5,23,10
81,81,5,21,27
82,82,5,9,27
83,83,6,23,6
84,84,6,23,6
85,85,6,23,28
86,86,6,23,28
87,87,7,13,0
88,88,7,21,0
89,89,8,18,0
90,90,8,23,35
91,91,8,23,35
92,92,12,14,0
93,93,12,23,32
94,94,12,23,32
95,95,13,15,58
96,96,13,23,58
97,97,14,14,50
98,98,14,23,50
99,99,14,23,49
100,100,14,23,49
101,101,18,22,0
102,102,19,23,0
103,103,23,23,23
104,104,23,23,23
105,105,23,23,11
106,106,23,23,11
Loading

0 comments on commit 3a7d489

Please sign in to comment.