-
Notifications
You must be signed in to change notification settings - Fork 1
/
cellulus-motile-fluo-c3dl-mda231.py
198 lines (161 loc) · 6.66 KB
/
cellulus-motile-fluo-c3dl-mda231.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
import math
from pathlib import Path
from motile import Solver, TrackGraph
from motile.constraints import MaxChildren, MaxParents
from motile.costs import EdgeSelection, Appear
from motile.variables import NodeSelected, EdgeSelected
import networkx as nx
import toml
from loading_utils import load_cellulus_results
from tqdm import tqdm
import pprint
import time
from skimage.measure import regionprops
from traccuracy import TrackingGraph
from traccuracy.matchers import CTCMatcher
from traccuracy.metrics import CTCMetrics, DivisionMetrics
from traccuracy.loaders import load_ctc_data
import logging
from saving_utils import save_result_tifs_res_track
logging.basicConfig(
level=logging.INFO, format="%(asctime)s %(name)s %(levelname)-8s %(message)s"
)
logger = logging.getLogger(__name__)
# logger.setLevel(logging.DEBUG)
# logging.getLogger('traccuracy.matchers._ctc').setLevel(logging.DEBUG)
def get_cand_graph_from_segmentation(
segmentation, max_edge_distance, pos_labels=["y", "x"], f_a=30
):
"""_summary_
Args:
segmentation (np.array): A numpy array with shape (t, [z,], y, x)
"""
# add nodes
node_frame_dict = (
{}
) # construct a dictionary from time frame to node_id for efficiency
cand_graph = nx.DiGraph()
for t in range(len(segmentation)):
nodes_in_frame = []
props = regionprops(segmentation[t])
for i, regionprop in enumerate(props):
node_id = f"{t}_{regionprop.label}" # TODO: previously node_id= f"{t}_{i}"
attrs = {
"t": t,
"cost_appear": 0 if t == 0 else f_a,
"segmentation_id": regionprop.label,
"area": regionprop.area,
}
centroid = regionprop.centroid # [z,] y, x
for label, value in zip(pos_labels, centroid):
attrs[label] = value
cand_graph.add_node(node_id, **attrs)
nodes_in_frame.append(node_id)
node_frame_dict[t] = nodes_in_frame
print(f"Candidate nodes: {cand_graph.number_of_nodes()}")
# add edges
frames = sorted(node_frame_dict.keys())
for frame in tqdm(frames):
if frame + 1 not in node_frame_dict:
continue
next_nodes = node_frame_dict[frame + 1]
next_locs = [
get_location(cand_graph.nodes[n], loc_keys=pos_labels) for n in next_nodes
]
for node in node_frame_dict[frame]:
loc = get_location(cand_graph.nodes[node], loc_keys=pos_labels)
for next_id, next_loc in zip(next_nodes, next_locs):
dist = math.dist(next_loc, loc)
attrs = {
"dist": dist,
}
if dist < max_edge_distance:
cand_graph.add_edge(node, next_id, **attrs)
print(f"Candidate edges: {cand_graph.number_of_edges()}")
return cand_graph
def get_location(node_data, loc_keys=("z", "y", "x")):
return [node_data[k] for k in loc_keys]
def get_max_distance(graph):
max_dist = 0
for source, target in graph.edges:
source_loc = get_location(graph.nodes[source])
target_loc = get_location(graph.nodes[target])
dist = math.dist(source_loc, target_loc)
if dist > max_dist:
max_dist = dist
return max_dist
def solve_with_motile(cand_graph, w_e=1, b_e=-20, w_a=1, b_a=0):
motile_cand_graph = TrackGraph(cand_graph)
solver = Solver(motile_cand_graph)
solver.add_constraints(MaxChildren(2))
solver.add_constraints(MaxParents(1))
solver.add_costs(EdgeSelection(w_e, attribute="dist", constant=b_e))
solver.add_costs(Appear(weight=w_a, attribute="cost_appear", constant=b_a))
start_time = time.time()
solution = solver.solve()
print(f"Solution took {time.time() - start_time} seconds")
return solution, solver
def get_solution_nx_graph(solution, solver):
node_selected = solver.get_variables(NodeSelected)
edge_selected = solver.get_variables(EdgeSelected)
selected_nodes = [
node for node in cand_graph.nodes if solution[node_selected[node]] > 0.5
]
selected_edges = [
edge for edge in cand_graph.edges if solution[edge_selected[edge]] > 0.5
]
print(f"Selected nodes: {len(selected_nodes)}")
print(f"Selected edges: {len(selected_edges)}")
solution_graph = nx.edge_subgraph(cand_graph, selected_edges)
return solution_graph
def evaluate_with_traccuracy(ds_name, ctc_data_path, solution_graph, solution_seg):
gt_tracking_graph = load_ctc_data(
ctc_data_path, track_path=ctc_data_path / "man_track.txt"
)
pred_tracking_graph = TrackingGraph(solution_graph, segmentation=solution_seg)
for node in pred_tracking_graph.nodes:
assert pred_tracking_graph.nodes[node][pred_tracking_graph.label_key]
matcher = CTCMatcher()
matched = matcher.compute_mapping(gt_tracking_graph, pred_tracking_graph)
ctc_metrics = CTCMetrics().compute(matched)
pprint.pprint(ctc_metrics)
div_metrics = DivisionMetrics().compute(matched)
pprint.pprint(div_metrics)
if __name__ == "__main__":
config_file = "configs/cellulus_fluo_c3dl_mda231.toml"
config = toml.load(config_file)
cellulus_data_path = Path(config["zarr_dataset"])
cellulus_dataset_name = config["dataset_name"]
ctc_data_path = Path(config["ctc_format"])
edge_dist_threshold = config["edge_distance_threshold"]
ds_name = config["ds_name"]
output_tifs_directory = config["output_tifs_directory"]
print(f"Data path: {cellulus_data_path}")
print(f"Path exists?: {cellulus_data_path.exists()}")
images, segmentation = load_cellulus_results(
cellulus_data_path, seg_group=cellulus_dataset_name
)
print(f"Image shape: {images.shape}")
print(f"Segmentation shape: {segmentation.shape}")
# specify weights
w_e = 1
b_e = -20
w_a = 1
b_a = 0
# specify attribute for appearance
f_a = 30
# note now there would be 4 weights which would be needed (w_e, b_e, w_a=1, b_a=0)
cand_graph = get_cand_graph_from_segmentation(
segmentation, edge_dist_threshold, f_a=f_a
)
print(f"Cand graph has {cand_graph.number_of_nodes()} nodes")
solution, solver = solve_with_motile(cand_graph, w_e=w_e, b_e=b_e, w_a=w_a, b_a=b_a)
solution_nx_graph = get_solution_nx_graph(solution, solver)
# evaluate_with_traccuracy(ds_name, ctc_data_path, solution_nx_graph, segmentation)
new_mapping, res_track, new_segmentations = save_result_tifs_res_track(
solution_nx_graph, segmentation, output_tifs_directory
)
print(
f"Value of objective function after optimisation is {solver.solution.get_value()}"
)
print(f"Default solver weights are:\n{solver.weights}")