Skip to content

Commit 87c9ec5

Browse files
committed
Fix tonotopic mapping of IHC segmentation
1 parent 301e018 commit 87c9ec5

File tree

3 files changed

+77
-12
lines changed

3 files changed

+77
-12
lines changed

flamingo_tools/segmentation/cochlea_mapping.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def measure_run_length_sgns(centroids: np.ndarray, scale_factor=10):
146146
return total_distance, path, path_dict
147147

148148

149-
def measure_run_length_ihcs(centroids):
149+
def measure_run_length_ihcs(centroids, max_edge_distance=50):
150150
"""Measure the run lengths of the IHC segmentation
151151
by finding the shortest path between the most distant nodes in a Steiner Tree.
152152
@@ -159,15 +159,25 @@ def measure_run_length_ihcs(centroids):
159159
A dictionary containing the position and the length fraction of each point in the path.
160160
"""
161161
graph = nx.Graph()
162-
for num, pos in enumerate(centroids):
162+
coords = {}
163+
labels = [int(i) for i in range(len(centroids))]
164+
for index, element in zip(labels, centroids):
165+
coords[index] = element
166+
167+
for num, pos in coords.items():
163168
graph.add_node(num, pos=pos)
164-
# approximate Steiner tree and find shortest path between the two most distant nodes
165-
terminals = set(graph.nodes()) # All nodes are required
166-
# Approximate Steiner Tree over all nodes
167-
T = steiner_tree(graph, terminals)
168-
u, v = find_most_distant_nodes(T)
169-
path = nx.shortest_path(T, source=u, target=v)
170-
total_distance = nx.path_weight(T, path, weight="weight")
169+
170+
# create edges between points whose distance is less than threshold max_edge_distance
171+
for num_i, pos_i in coords.items():
172+
for num_j, pos_j in coords.items():
173+
if num_i < num_j:
174+
dist = math.dist(pos_i, pos_j)
175+
if dist <= max_edge_distance:
176+
graph.add_edge(num_i, num_j, weight=dist)
177+
178+
u, v = find_most_distant_nodes(graph)
179+
path = nx.shortest_path(graph, source=u, target=v)
180+
total_distance = nx.path_weight(graph, path, weight="weight")
171181

172182
# assign relative distance to points on path
173183
path_dict = {}
@@ -180,6 +190,9 @@ def measure_run_length_ihcs(centroids):
180190
path_dict[num + 1] = {"pos": graph.nodes[p]["pos"], "length_fraction": rel_dist}
181191
path_dict[len(path)] = {"pos": graph.nodes[path[-1]]["pos"], "length_fraction": 1}
182192

193+
path_pos = np.array([graph.nodes[p]["pos"] for p in path])
194+
path = moving_average_3d(path_pos, window=5)
195+
183196
return total_distance, path, path_dict
184197

185198

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[
2+
{
3+
"cochlea": "M_AMD_OTOF1_L",
4+
"image_channel": [
5+
"Apha",
6+
"Vglut3",
7+
"IHC_v4"
8+
],
9+
"segmentation_channel": "IHC_v4",
10+
"type": "ihc",
11+
"n_blocks": 6,
12+
"component_list": [
13+
5,
14+
9
15+
],
16+
"halo_size": [
17+
256,
18+
256,
19+
50
20+
],
21+
"crop_centers": [
22+
[
23+
915,
24+
1358,
25+
870
26+
],
27+
[
28+
1307,
29+
776,
30+
764
31+
],
32+
[
33+
687,
34+
828,
35+
362
36+
],
37+
[
38+
598,
39+
1142,
40+
1014
41+
],
42+
[
43+
1011,
44+
469,
45+
1118
46+
],
47+
[
48+
710,
49+
35,
50+
534
51+
]
52+
]
53+
}
54+
]

reproducibility/block_extraction/repro_equidistant_centers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def repro_equidistant_centers(
3232

3333
for dic in param_dicts:
3434
cochlea = dic["cochlea"]
35-
img_channel = dic["image_channel"]
3635
seg_channel = dic["segmentation_channel"]
3736

3837
s3_path = os.path.join(f"{cochlea}", "tables", f"{seg_channel}", "default.tsv")
@@ -50,8 +49,7 @@ def repro_equidistant_centers(
5049

5150
centers = equidistant_centers(table, component_label=component_list, cell_type=cell_type, n_blocks=n_blocks)
5251
centers = [[int(c) for c in center] for center in centers]
53-
ddict = {"cochlea": cochlea}
54-
ddict["image_channel"] = img_channel
52+
ddict = dic.copy()
5553
ddict["crop_centers"] = centers
5654
ddict["halo_size"] = halo_size
5755
out_dict.append(ddict)

0 commit comments

Comments
 (0)