py-ctcmetrics 0.1.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ctc_metrics/__init__.py +2 -0
- ctc_metrics/metrics/__init__.py +24 -0
- ctc_metrics/metrics/biological/__init__.py +0 -0
- ctc_metrics/metrics/biological/bc.py +169 -0
- ctc_metrics/metrics/biological/bio.py +41 -0
- ctc_metrics/metrics/biological/cca.py +75 -0
- ctc_metrics/metrics/biological/ct.py +65 -0
- ctc_metrics/metrics/biological/op_clb.py +19 -0
- ctc_metrics/metrics/biological/tf.py +107 -0
- ctc_metrics/metrics/clearmot/__init__.py +0 -0
- ctc_metrics/metrics/clearmot/mota.py +67 -0
- ctc_metrics/metrics/hota/__init__.py +0 -0
- ctc_metrics/metrics/hota/chota.py +125 -0
- ctc_metrics/metrics/hota/hota.py +63 -0
- ctc_metrics/metrics/identity_metrics/__init__.py +0 -0
- ctc_metrics/metrics/identity_metrics/idf1.py +117 -0
- ctc_metrics/metrics/others/__init__.py +0 -0
- ctc_metrics/metrics/others/faf.py +34 -0
- ctc_metrics/metrics/others/mt_ml.py +39 -0
- ctc_metrics/metrics/technical/__init__.py +0 -0
- ctc_metrics/metrics/technical/det.py +37 -0
- ctc_metrics/metrics/technical/lnk.py +37 -0
- ctc_metrics/metrics/technical/op_csb.py +19 -0
- ctc_metrics/metrics/technical/op_ctb.py +19 -0
- ctc_metrics/metrics/technical/seg.py +27 -0
- ctc_metrics/metrics/technical/tra.py +48 -0
- ctc_metrics/metrics/validation/__init__.py +0 -0
- ctc_metrics/metrics/validation/valid.py +238 -0
- ctc_metrics/scripts/__init__.py +0 -0
- ctc_metrics/scripts/evaluate.py +410 -0
- ctc_metrics/scripts/noise.py +692 -0
- ctc_metrics/scripts/validate.py +96 -0
- ctc_metrics/scripts/visualize.py +322 -0
- ctc_metrics/utils/__init__.py +0 -0
- ctc_metrics/utils/filesystem.py +106 -0
- ctc_metrics/utils/handle_results.py +55 -0
- ctc_metrics/utils/representations.py +456 -0
- py_ctcmetrics-0.1.4.dist-info/LICENSE.txt +24 -0
- py_ctcmetrics-0.1.4.dist-info/METADATA +16 -0
- py_ctcmetrics-0.1.4.dist-info/RECORD +47 -0
- py_ctcmetrics-0.1.4.dist-info/WHEEL +5 -0
- py_ctcmetrics-0.1.4.dist-info/entry_points.txt +5 -0
- py_ctcmetrics-0.1.4.dist-info/top_level.txt +2 -0
- test/__init__.py +0 -0
- test/test_metrics.py +72 -0
- test/test_validate.py +7 -0
- test/utils.py +17 -0
ctc_metrics/__init__.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from ctc_metrics.metrics.validation.valid import valid
|
|
2
|
+
from ctc_metrics.metrics.biological.bc import bc
|
|
3
|
+
from ctc_metrics.metrics.biological.ct import ct
|
|
4
|
+
from ctc_metrics.metrics.biological.cca import cca
|
|
5
|
+
from ctc_metrics.metrics.biological.tf import tf
|
|
6
|
+
from ctc_metrics.metrics.technical.seg import seg
|
|
7
|
+
from ctc_metrics.metrics.technical.tra import tra
|
|
8
|
+
from ctc_metrics.metrics.technical.det import det
|
|
9
|
+
from ctc_metrics.metrics.clearmot.mota import mota
|
|
10
|
+
from ctc_metrics.metrics.hota.hota import hota
|
|
11
|
+
from ctc_metrics.metrics.hota.chota import chota
|
|
12
|
+
from ctc_metrics.metrics.identity_metrics.idf1 import idf1
|
|
13
|
+
from ctc_metrics.metrics.others.mt_ml import mtml
|
|
14
|
+
from ctc_metrics.metrics.others.faf import faf
|
|
15
|
+
from ctc_metrics.metrics.technical.op_ctb import op_ctb
|
|
16
|
+
from ctc_metrics.metrics.technical.op_csb import op_csb
|
|
17
|
+
from ctc_metrics.metrics.biological.bio import bio
|
|
18
|
+
from ctc_metrics.metrics.biological.op_clb import op_clb
|
|
19
|
+
from ctc_metrics.metrics.technical.lnk import lnk
|
|
20
|
+
|
|
21
|
+
ALL_METRICS = [
|
|
22
|
+
"Valid", "CHOTA", "BC", "CT", "CCA", "TF", "SEG", "TRA", "DET", "MOTA",
|
|
23
|
+
"HOTA", "IDF1", "MTML", "FAF", "LNK", "OP_CTB", "OP_CSB", "BIO", "OP_CLB"
|
|
24
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def get_ids_that_ends_with_split(
|
|
5
|
+
tracks: np.ndarray
|
|
6
|
+
):
|
|
7
|
+
"""
|
|
8
|
+
Extracts the ids of tracks that end with a cell split.
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
tracks: The tracks to check. A numpy nd array with columns:
|
|
12
|
+
- label
|
|
13
|
+
- birth frame
|
|
14
|
+
- end frame
|
|
15
|
+
- parent
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
The ids of tracks that end with a cell split stored in a numpy.ndarray.
|
|
19
|
+
"""
|
|
20
|
+
parents, counts = np.unique(tracks[:, 3], return_counts=True)
|
|
21
|
+
counts = counts[parents > 0]
|
|
22
|
+
parents = parents[parents > 0]
|
|
23
|
+
ends_with_split = parents[counts > 1]
|
|
24
|
+
return ends_with_split
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def calculate_f1_score(
|
|
28
|
+
tp: int,
|
|
29
|
+
fp: int,
|
|
30
|
+
fn: int
|
|
31
|
+
):
|
|
32
|
+
"""
|
|
33
|
+
Calculates the f1 score.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
tp: The number of true positives.
|
|
37
|
+
fp: The number of false positives.
|
|
38
|
+
fn: The number of false negatives.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
The f1 score.
|
|
42
|
+
"""
|
|
43
|
+
precision = tp / max((tp + fp), 1)
|
|
44
|
+
recall = tp / max((tp + fn), 1)
|
|
45
|
+
f1_score = 2 * (precision * recall) / max((precision + recall), 0.0001)
|
|
46
|
+
return f1_score
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def is_matching(
|
|
50
|
+
id_comp: int,
|
|
51
|
+
id_ref: int,
|
|
52
|
+
mapped_ref: list,
|
|
53
|
+
mapped_comp: list,
|
|
54
|
+
ref_children: np.ndarray,
|
|
55
|
+
comp_children: np.ndarray,
|
|
56
|
+
tr: int,
|
|
57
|
+
tc: int
|
|
58
|
+
):
|
|
59
|
+
"""
|
|
60
|
+
Checks if the reference and the computed track match.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
id_comp: The computed track id.
|
|
64
|
+
id_ref: The reference track id.
|
|
65
|
+
mapped_ref: The matched labels of the ground truth masks.
|
|
66
|
+
mapped_comp: The matched labels of the result masks.
|
|
67
|
+
ref_children: The children ids of the reference track.
|
|
68
|
+
comp_children: The children ids of the computed track.
|
|
69
|
+
tr: The frame of the reference track end.
|
|
70
|
+
tc: The frame of the computed track end.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
True if the reference and the computed track match, False otherwise.
|
|
74
|
+
"""
|
|
75
|
+
# Check if the number of children is the same
|
|
76
|
+
if len(ref_children) != len(comp_children):
|
|
77
|
+
return False
|
|
78
|
+
# Compare parents
|
|
79
|
+
t1, t2 = min(tr, tc), max(tr, tc)
|
|
80
|
+
mr, mc = mapped_ref[t1], mapped_comp[t1]
|
|
81
|
+
if np.sum(mc == id_comp) < 1 or np.sum(mr == id_ref) != 1:
|
|
82
|
+
return False
|
|
83
|
+
ind = np.argwhere(mr == id_ref).squeeze()
|
|
84
|
+
if mc[ind] != id_comp:
|
|
85
|
+
return False
|
|
86
|
+
# Compare children
|
|
87
|
+
mr, mc = np.asarray(mapped_ref[t2 + 1]), np.asarray(mapped_comp[t2 + 1])
|
|
88
|
+
if not np.all(np.isin(comp_children, mc)):
|
|
89
|
+
return False
|
|
90
|
+
if not np.all(np.isin(mr[np.isin(mc, comp_children)], ref_children)):
|
|
91
|
+
return False
|
|
92
|
+
return True
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def bc(
|
|
96
|
+
comp_tracks: np.ndarray,
|
|
97
|
+
ref_tracks: np.ndarray,
|
|
98
|
+
mapped_ref: list,
|
|
99
|
+
mapped_comp: list,
|
|
100
|
+
i: int
|
|
101
|
+
):
|
|
102
|
+
"""
|
|
103
|
+
Computes the branching correctness metric. As described in the paper,
|
|
104
|
+
"An objective comparison of cell-tracking algorithms."
|
|
105
|
+
- Vladimir Ulman et al., Nature methods 2017
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
comp_tracks: The result tracks. A (n,4) numpy ndarray with columns:
|
|
109
|
+
- label
|
|
110
|
+
- birth frame
|
|
111
|
+
- end frame
|
|
112
|
+
- parent
|
|
113
|
+
ref_tracks: The ground truth tracks. A (n,4) numpy ndarray with columns:
|
|
114
|
+
- label
|
|
115
|
+
- birth frame
|
|
116
|
+
- end frame
|
|
117
|
+
- parent
|
|
118
|
+
mapped_ref: The matched labels of the ground truth masks. A list of
|
|
119
|
+
length equal to the number of frames. Each element is a list with
|
|
120
|
+
the matched labels of the ground truth masks in the respective
|
|
121
|
+
frame. The elements are in the same order as the corresponding
|
|
122
|
+
elements in mapped_comp.
|
|
123
|
+
mapped_comp: The matched labels of the result masks. A list of length
|
|
124
|
+
equal to the number of frames. Each element is a list with the
|
|
125
|
+
matched labels of the result masks in the respective frame. The
|
|
126
|
+
elements are in the same order as the corresponding elements in
|
|
127
|
+
mapped_ref.
|
|
128
|
+
i: The maximal allowed error in frames.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
The branching correctness metric.
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
# Extract relevant tracks with children in reference
|
|
135
|
+
ends_with_split_ref = get_ids_that_ends_with_split(ref_tracks)
|
|
136
|
+
t_ref = np.array([ref_tracks[ref_tracks[:, 0] == ref][0, 2]
|
|
137
|
+
for ref in ends_with_split_ref])
|
|
138
|
+
if len(ends_with_split_ref) == 0:
|
|
139
|
+
return None
|
|
140
|
+
# Extract relevant tracks with children in computed result
|
|
141
|
+
ends_with_split_comp = get_ids_that_ends_with_split(comp_tracks)
|
|
142
|
+
t_comp = np.asarray([comp_tracks[comp_tracks[:, 0] == comp][0, 2]
|
|
143
|
+
for comp in ends_with_split_comp])
|
|
144
|
+
if len(ends_with_split_comp) == 0:
|
|
145
|
+
return 0
|
|
146
|
+
# Find all matches between reference and computed branching events (mitosis)
|
|
147
|
+
matches = []
|
|
148
|
+
for comp, tc in zip(ends_with_split_comp, t_comp):
|
|
149
|
+
# Find potential matches
|
|
150
|
+
pot_matches = np.abs(t_ref - tc) <= i
|
|
151
|
+
if len(pot_matches) == 0:
|
|
152
|
+
continue
|
|
153
|
+
comp_children = comp_tracks[comp_tracks[:, 3] == comp][:, 0]
|
|
154
|
+
# Evaluate potential matches
|
|
155
|
+
for ref, tr in zip(
|
|
156
|
+
ends_with_split_ref[pot_matches],
|
|
157
|
+
t_ref[pot_matches]
|
|
158
|
+
):
|
|
159
|
+
ref_children = ref_tracks[ref_tracks[:, 3] == ref][:, 0]
|
|
160
|
+
if is_matching(
|
|
161
|
+
comp, ref, mapped_ref, mapped_comp, ref_children,
|
|
162
|
+
comp_children, tr, tc
|
|
163
|
+
):
|
|
164
|
+
matches.append((ref, comp))
|
|
165
|
+
# Calculate BC(i)
|
|
166
|
+
return calculate_f1_score(
|
|
167
|
+
len(matches),
|
|
168
|
+
len(ends_with_split_comp) - len(matches),
|
|
169
|
+
len(ends_with_split_ref) - len(matches))
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
def bio(
|
|
3
|
+
ct: float,
|
|
4
|
+
tf: float,
|
|
5
|
+
bc: float,
|
|
6
|
+
cca: float,
|
|
7
|
+
):
|
|
8
|
+
"""
|
|
9
|
+
Computes the BIO. As described by
|
|
10
|
+
[celltrackingchallenge](http://celltrackingchallenge.net/).
|
|
11
|
+
It is the average of the CT, TF, BC, and CCA metrics. If a metric is not
|
|
12
|
+
available, it is not considered in the average.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
ct: The complete tracking metric.
|
|
16
|
+
tf: The track fractions metric.
|
|
17
|
+
bc: The branching correctness metric.
|
|
18
|
+
cca: The cell cycle accuracy metric.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
The BIO metric.
|
|
22
|
+
"""
|
|
23
|
+
total_metrics = 0
|
|
24
|
+
if ct is not None:
|
|
25
|
+
total_metrics += 1
|
|
26
|
+
else:
|
|
27
|
+
ct = 0
|
|
28
|
+
if tf is not None:
|
|
29
|
+
total_metrics += 1
|
|
30
|
+
else:
|
|
31
|
+
tf = 0
|
|
32
|
+
if bc is not None:
|
|
33
|
+
total_metrics += 1
|
|
34
|
+
else:
|
|
35
|
+
bc = 0
|
|
36
|
+
if cca is not None:
|
|
37
|
+
total_metrics += 1
|
|
38
|
+
else:
|
|
39
|
+
cca = 0
|
|
40
|
+
bio_score = (ct + tf + bc + cca) / total_metrics
|
|
41
|
+
return bio_score
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from ctc_metrics.metrics.biological.bc import get_ids_that_ends_with_split
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def is_valid_track(
|
|
7
|
+
tracks: np.ndarray,
|
|
8
|
+
):
|
|
9
|
+
"""
|
|
10
|
+
Extracts the boolean indices of valid tracks that start and end with a
|
|
11
|
+
cell split.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
tracks: The tracks to check.
|
|
15
|
+
|
|
16
|
+
Returns:
|
|
17
|
+
The boolean indices of valid tracks that start and end with a cell
|
|
18
|
+
split.
|
|
19
|
+
"""
|
|
20
|
+
ends_with_split = get_ids_that_ends_with_split(tracks)
|
|
21
|
+
is_parent = np.isin(tracks[:, 0], ends_with_split)
|
|
22
|
+
is_child = np.isin(tracks[:, 3], ends_with_split)
|
|
23
|
+
valid = np.logical_and(is_parent, is_child)
|
|
24
|
+
return valid
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def cca(
|
|
28
|
+
comp_tracks: np.ndarray,
|
|
29
|
+
ref_tracks: np.ndarray
|
|
30
|
+
):
|
|
31
|
+
"""
|
|
32
|
+
Computes the cell cycle accuracy. As described in the paper,
|
|
33
|
+
"An objective comparison of cell-tracking algorithms."
|
|
34
|
+
- Vladimir Ulman et al., Nature methods 2017
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
comp_tracks: The result tracks. A (n,4) numpy ndarray with columns:
|
|
38
|
+
- label
|
|
39
|
+
- birth frame
|
|
40
|
+
- end frame
|
|
41
|
+
- parent
|
|
42
|
+
ref_tracks: The ground truth tracks. A (n,4) numpy ndarray with columns:
|
|
43
|
+
- label
|
|
44
|
+
- birth frame
|
|
45
|
+
- end frame
|
|
46
|
+
- parent
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
The cell cycle accuracy metric.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
# Extract relevant tracks with parents and children in reference
|
|
53
|
+
valid_ref = is_valid_track(ref_tracks)
|
|
54
|
+
if np.sum(valid_ref) == 0:
|
|
55
|
+
return None
|
|
56
|
+
track_lengths_ref = ref_tracks[valid_ref, 2] - ref_tracks[valid_ref, 1]
|
|
57
|
+
# Extract relevant tracks with parents and children in computed result
|
|
58
|
+
valid_comp = is_valid_track(comp_tracks)
|
|
59
|
+
if np.sum(valid_comp) == 0:
|
|
60
|
+
return 0
|
|
61
|
+
track_lengths_comp = comp_tracks[valid_comp, 2] - comp_tracks[valid_comp, 1]
|
|
62
|
+
# Calculate CCA
|
|
63
|
+
max_track_length = np.max(
|
|
64
|
+
[np.max(track_lengths_ref), np.max(track_lengths_comp)])
|
|
65
|
+
hist_ref = np.zeros(np.max(max_track_length) + 1)
|
|
66
|
+
for i in track_lengths_ref:
|
|
67
|
+
hist_ref[i] += 1
|
|
68
|
+
hist_ref = hist_ref / np.sum(hist_ref)
|
|
69
|
+
cum_hist_ref = np.cumsum(hist_ref)
|
|
70
|
+
hist_comp = np.zeros(np.max(max_track_length) + 1)
|
|
71
|
+
for i in track_lengths_comp:
|
|
72
|
+
hist_comp[i] += 1
|
|
73
|
+
hist_comp = hist_comp / np.sum(hist_comp)
|
|
74
|
+
cum_hist_comp = np.cumsum(hist_comp)
|
|
75
|
+
return float(1 - np.max(np.abs(cum_hist_ref - cum_hist_comp)))
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from ctc_metrics.utils.representations import assign_comp_to_ref
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def ct(
|
|
7
|
+
comp_tracks: np.ndarray,
|
|
8
|
+
ref_tracks: np.ndarray,
|
|
9
|
+
labels_ref: list,
|
|
10
|
+
mapped_ref: list,
|
|
11
|
+
mapped_comp: list
|
|
12
|
+
):
|
|
13
|
+
"""
|
|
14
|
+
Computes the complete tracks metric. As described in the paper,
|
|
15
|
+
"An objective comparison of cell-tracking algorithms."
|
|
16
|
+
- Vladimir Ulman et al., Nature methods 2017
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
comp_tracks: The result tracks. A (n,4) numpy ndarray with columns:
|
|
20
|
+
- label
|
|
21
|
+
- birth frame
|
|
22
|
+
- end frame
|
|
23
|
+
- parent
|
|
24
|
+
ref_tracks: The ground truth tracks. A (n,4) numpy ndarray with columns:
|
|
25
|
+
- label
|
|
26
|
+
- birth frame
|
|
27
|
+
- end frame
|
|
28
|
+
- parent
|
|
29
|
+
labels_ref: The labels of the ground truth masks. A list of length
|
|
30
|
+
equal to the number of frames. Each element is a list with the
|
|
31
|
+
labels of the ground truth masks in the respective frame.
|
|
32
|
+
mapped_ref: The matched labels of the ground truth masks. A list of
|
|
33
|
+
length equal to the number of frames. Each element is a list with
|
|
34
|
+
the matched labels of the ground truth masks in the respective
|
|
35
|
+
frame. The elements are in the same order as the corresponding
|
|
36
|
+
elements in mapped_comp.
|
|
37
|
+
mapped_comp: The matched labels of the result masks. A list of length
|
|
38
|
+
equal to the number of frames. Each element is a list with the
|
|
39
|
+
matched labels of the result masks in the respective frame. The
|
|
40
|
+
elements are in the same order as the corresponding elements in
|
|
41
|
+
mapped_ref.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
The complete tracks metric.
|
|
45
|
+
"""
|
|
46
|
+
track_assignments = assign_comp_to_ref(labels_ref, mapped_ref, mapped_comp)
|
|
47
|
+
correct_tracks = 0
|
|
48
|
+
for k, v in track_assignments.items():
|
|
49
|
+
start_ref = ref_tracks[ref_tracks[:, 0] == k][0, 1]
|
|
50
|
+
end_ref = ref_tracks[ref_tracks[:, 0] == k][0, 2]
|
|
51
|
+
assigned_labels = np.unique(v[~np.isnan(v)])
|
|
52
|
+
if len(assigned_labels) > 1:
|
|
53
|
+
continue
|
|
54
|
+
if assigned_labels[0] == 0:
|
|
55
|
+
continue
|
|
56
|
+
assignee = assigned_labels[0]
|
|
57
|
+
start_comp = comp_tracks[comp_tracks[:, 0] == assignee][0, 1]
|
|
58
|
+
end_comp = comp_tracks[comp_tracks[:, 0] == assignee][0, 2]
|
|
59
|
+
if start_ref == start_comp and end_ref == end_comp:
|
|
60
|
+
correct_tracks += 1
|
|
61
|
+
T_rc = correct_tracks
|
|
62
|
+
T_r = len(ref_tracks)
|
|
63
|
+
T_c = len(comp_tracks)
|
|
64
|
+
return float(2 * T_rc / (T_c + T_r))
|
|
65
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
def op_clb(
|
|
3
|
+
lnk: float,
|
|
4
|
+
bio: float,
|
|
5
|
+
):
|
|
6
|
+
"""
|
|
7
|
+
Computes the OP_CLB metric. As described by
|
|
8
|
+
[celltrackingchallenge](http://celltrackingchallenge.net/).
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
lnk: The linking metric.
|
|
12
|
+
bio: The biological metric.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
The OP_CLB metric.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
op = 0.5 * lnk + 0.5 * bio
|
|
19
|
+
return op
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from ctc_metrics.utils.representations import assign_comp_to_ref
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def calculate_fractions_fo_computed_tracks(
|
|
7
|
+
ref_tracks: np.ndarray,
|
|
8
|
+
labels_ref: list,
|
|
9
|
+
mapped_ref: list,
|
|
10
|
+
mapped_comp: list,
|
|
11
|
+
):
|
|
12
|
+
"""
|
|
13
|
+
Returns a dictionary with the fractions of overlap for each computed track.
|
|
14
|
+
Each entry in the dictionary is a dictionary with the computed track id as
|
|
15
|
+
key and a new dictionary as value. In the sub-dictionary, the reference
|
|
16
|
+
track id is the key and the fraction of overlap is the value.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
ref_tracks: The ground truth tracks.
|
|
20
|
+
labels_ref: The labels of the ground truth masks.
|
|
21
|
+
mapped_ref: The matched labels of the ground truth masks.
|
|
22
|
+
mapped_comp: The matched labels of the result masks.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
A dictionary with the fractions of overlap for each computed track.
|
|
26
|
+
"""
|
|
27
|
+
track_assignments = assign_comp_to_ref(labels_ref, mapped_ref, mapped_comp)
|
|
28
|
+
comp_fractions = {}
|
|
29
|
+
for k, v in track_assignments.items():
|
|
30
|
+
start_ref = ref_tracks[ref_tracks[:, 0] == k][0, 1]
|
|
31
|
+
end_ref = ref_tracks[ref_tracks[:, 0] == k][0, 2]
|
|
32
|
+
length = int(end_ref - start_ref + 1)
|
|
33
|
+
array = v[start_ref:end_ref + 1]
|
|
34
|
+
assigned_labels = np.unique(array)
|
|
35
|
+
if len(assigned_labels) == 1 and assigned_labels[0] == 0:
|
|
36
|
+
continue
|
|
37
|
+
assignments = 0
|
|
38
|
+
last_i = 0
|
|
39
|
+
for i in array:
|
|
40
|
+
if last_i != i or i == 0:
|
|
41
|
+
assignments = 0
|
|
42
|
+
if i > 0:
|
|
43
|
+
assignments += 1
|
|
44
|
+
if i not in comp_fractions:
|
|
45
|
+
comp_fractions[i] = {}
|
|
46
|
+
comp_fractions[i][k] = max(
|
|
47
|
+
comp_fractions[i].get(k, 0), assignments / length)
|
|
48
|
+
last_i = i
|
|
49
|
+
return comp_fractions
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def tf(
|
|
53
|
+
ref_tracks: np.ndarray,
|
|
54
|
+
labels_ref: list,
|
|
55
|
+
mapped_ref: list,
|
|
56
|
+
mapped_comp: list
|
|
57
|
+
):
|
|
58
|
+
"""
|
|
59
|
+
Computes the track fractions metric. As described in the paper,
|
|
60
|
+
"An objective comparison of cell-tracking algorithms."
|
|
61
|
+
- Vladimir Ulman et al., Nature methods 2017
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
ref_tracks: The ground truth tracks. A (n,4) numpy ndarray with columns:
|
|
65
|
+
- label
|
|
66
|
+
- birth frame
|
|
67
|
+
- end frame
|
|
68
|
+
- parent
|
|
69
|
+
labels_ref: The labels of the ground truth masks. A list of length
|
|
70
|
+
mapped_ref: The matched labels of the ground truth masks. A list of
|
|
71
|
+
length equal to the number of frames. Each element is a list with
|
|
72
|
+
the matched labels of the ground truth masks in the respective
|
|
73
|
+
frame. The elements are in the same order as the corresponding
|
|
74
|
+
elements in mapped_comp.
|
|
75
|
+
mapped_comp: The matched labels of the result masks. A list of length
|
|
76
|
+
equal to the number of frames. Each element is a list with the
|
|
77
|
+
matched labels of the result masks in the respective frame. The
|
|
78
|
+
elements are in the same order as the corresponding elements in
|
|
79
|
+
mapped_ref.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
The track fractions metric.
|
|
83
|
+
"""
|
|
84
|
+
comp_fractions = calculate_fractions_fo_computed_tracks(
|
|
85
|
+
ref_tracks, labels_ref, mapped_ref, mapped_comp)
|
|
86
|
+
# Calculate the track fractions with respect to the reference tracks
|
|
87
|
+
tfs = {k: 0 for k in ref_tracks[:, 0]}
|
|
88
|
+
for _, v in sorted(comp_fractions.items()):
|
|
89
|
+
for k2, v2 in sorted(v.items()):
|
|
90
|
+
if tfs[k2] == 1:
|
|
91
|
+
continue
|
|
92
|
+
if v2 > tfs[k2]:
|
|
93
|
+
tfs[k2] = v2
|
|
94
|
+
# ###
|
|
95
|
+
# The next lines do not make sense, because it causes instabilities.
|
|
96
|
+
# Exactly same results with different order of labels can lead to
|
|
97
|
+
# different metrics. They are kept, because they are in the original
|
|
98
|
+
# codebase for the CTC. Remove the break to solve the issue.
|
|
99
|
+
# Note: Does not make real differences in most cases.
|
|
100
|
+
# ###
|
|
101
|
+
if v2 == 1:
|
|
102
|
+
break
|
|
103
|
+
# Filter out undetected tracks. They should not be counted.
|
|
104
|
+
tfs = [x for k, x in tfs.items() if x > 0]
|
|
105
|
+
if len(tfs) == 0:
|
|
106
|
+
return 0
|
|
107
|
+
return np.mean(np.asarray(tfs))
|
|
File without changes
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def mota(
|
|
5
|
+
labels_ref: list,
|
|
6
|
+
labels_comp: list,
|
|
7
|
+
mapped_ref: list,
|
|
8
|
+
mapped_comp: list
|
|
9
|
+
):
|
|
10
|
+
"""
|
|
11
|
+
Computes the MOTA metric. As described in the paper,
|
|
12
|
+
"Evaluating Multiple Object Tracking Performance:
|
|
13
|
+
The CLEAR MOT Metrics."
|
|
14
|
+
- Keni Bernardin and Rainer Stiefelhagen, EURASIP 2008
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
labels_comp: The labels of the computed masks. A list of length equal
|
|
18
|
+
to the number of frames. Each element is a list with the labels of
|
|
19
|
+
the computed masks in the respective frame.
|
|
20
|
+
labels_ref: The labels of the ground truth masks. A list of length
|
|
21
|
+
equal to the number of frames. Each element is a list with the
|
|
22
|
+
labels of the ground truth masks in the respective frame.
|
|
23
|
+
mapped_ref: The matched labels of the ground truth masks. A list of
|
|
24
|
+
length equal to the number of frames. Each element is a list with
|
|
25
|
+
the matched labels of the ground truth masks in the respective
|
|
26
|
+
frame. The elements are in the same order as the corresponding
|
|
27
|
+
elements in mapped_comp.
|
|
28
|
+
mapped_comp: The matched labels of the result masks. A list of length
|
|
29
|
+
equal to the number of frames. Each element is a list with the
|
|
30
|
+
matched labels of the result masks in the respective frame. The
|
|
31
|
+
elements are in the same order as the corresponding elements in
|
|
32
|
+
mapped_ref.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
The MOTA tracks metric.
|
|
36
|
+
"""
|
|
37
|
+
tp, fp, fn, idsw, multi_assignments = 0, 0, 0, 0, 0
|
|
38
|
+
|
|
39
|
+
max_label_gt = int(np.max(np.concatenate(labels_ref)))
|
|
40
|
+
matches = np.zeros(max_label_gt + 1)
|
|
41
|
+
for ref, comp, m_ref, m_comp in zip(
|
|
42
|
+
labels_ref, labels_comp, mapped_ref, mapped_comp):
|
|
43
|
+
# Calculate metrics
|
|
44
|
+
_, counts = np.unique(m_comp, return_counts=True)
|
|
45
|
+
tp += len(m_ref)
|
|
46
|
+
fn += len(ref) - len(m_ref)
|
|
47
|
+
fp += len(comp) - len(m_comp) + np.sum(counts[counts > 1] - 1)
|
|
48
|
+
multi_assignments += np.sum(counts[counts > 1] - 1)
|
|
49
|
+
idsw += np.sum((matches[m_ref] != m_comp) & (matches[m_ref] != 0))
|
|
50
|
+
# Update the match cache
|
|
51
|
+
matches[m_ref] = m_comp
|
|
52
|
+
|
|
53
|
+
mota_score = 1 - (fn + fp + idsw + multi_assignments) / (tp + fn)
|
|
54
|
+
precision = tp / (tp + fp)
|
|
55
|
+
recall = tp / (tp + fn)
|
|
56
|
+
|
|
57
|
+
res = {
|
|
58
|
+
"MOTA": mota_score,
|
|
59
|
+
"TP": tp,
|
|
60
|
+
"FP": fp,
|
|
61
|
+
"FN": fn,
|
|
62
|
+
"IDSW": idsw,
|
|
63
|
+
"MULTI-ASSIGNMENTS": multi_assignments,
|
|
64
|
+
"Precision": precision,
|
|
65
|
+
"Recall": recall
|
|
66
|
+
}
|
|
67
|
+
return res
|
|
File without changes
|