Skip to content

Commit e9a9a0c

Browse files
committed
Updated the way nodes are sorted and prevented lateral rendering of areas around the brain such as the pons and medulla.
1 parent d575e12 commit e9a9a0c

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

mapmaker/routing/__init__.py

+33-19
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ def pairwise(iterable):
8888

8989
#===============================================================================
9090

91+
NOT_LATERAL_NODES = [
92+
'UBERON:0001896',
93+
'UBERON:0000988',
94+
'UBERON:0001891'
95+
]
96+
97+
#=============================================================
98+
9199
def expand_centreline_graph(graph: nx.MultiGraph) -> nx.Graph:
92100
#=============================================================
93101
G = nx.Graph()
@@ -957,8 +965,11 @@ def add_route_edges_from_graph(G, used_nodes):
957965
if neighbour_dict['type'] == 'feature':
958966
# if the node is a no-segment and neighbour is a terminal, connect to all neighbour features
959967
# else connect to one closest no_segment point and neighbour feature.
960-
features = (neighbour_dict['features'] if len(neighbour_dict['features']) <= 2
961-
else sorted(neighbour_dict['features'], key=lambda f: f.id)[0:1])
968+
features = (
969+
neighbour_dict['features']
970+
if len(neighbour_dict['features']) <= 2 and all(item not in {neighbour_dict['node'][0], *neighbour_dict['node'][1]} for item in NOT_LATERAL_NODES)
971+
else sorted(neighbour_dict['features'], key=lambda f: f.id)[:1]
972+
)
962973
for feature in features:
963974
neighbouring_ids.update([feature.id])
964975
closest_feature_id = None
@@ -985,7 +996,7 @@ def add_route_edges_from_graph(G, used_nodes):
985996
if n_id in segment_graph:
986997
updated_neighbouring_ids.update([n_id])
987998
else:
988-
if (f:=self.__map_feature(n_id)) is not None:
999+
if self.__map_feature(n_id) is not None:
9891000
if (closest_feature_id:=closest_feature_dict.get(n_id)) is not None:
9901001
updated_neighbouring_ids.update([closest_feature_id])
9911002
new_direct_edges.update([(n_id, closest_feature_id)])
@@ -1190,26 +1201,27 @@ def get_node_feature(node_dict, neighbour_features, used_features) -> Feature:
11901201
pseudo_terminals += list(subgraph.nodes)[0:1]
11911202

11921203
# sorting nodes with priority -> terminal, number of features (2 than 1, than any size), distance to neighbours
1193-
terminal_one_feature = {}
1194-
terminal_one_feature = {
1195-
n: min(
1196-
(features[0].geometry.centroid.distance(nf.geometry.centroid)),
1197-
terminal_one_feature.get(n, float('inf'))
1198-
)
1204+
one_feature_terminals = {
1205+
n: min([
1206+
features[0].geometry.centroid.distance(nf.geometry.centroid)
1207+
for neighbour in connectivity_graph.neighbors(n)
1208+
for nf in (
1209+
connectivity_graph.nodes[neighbour].get("features", set()) |
1210+
{self.__flatmap.get_feature(f_id) for f_id in connectivity_graph.nodes[neighbour].get("used", set())}
1211+
)
1212+
])
11991213
for n, n_dict in connectivity_graph.nodes(data=True)
12001214
if connectivity_graph.degree(n) == 1 and len(features := list(n_dict.get("features", []))) == 1
1201-
for neighbour in connectivity_graph.neighbors(n)
1202-
for nf in connectivity_graph.nodes[neighbour].get("features", [])
12031215
}
1204-
terminal_one_feature = dict(sorted(terminal_one_feature.items(), key=lambda item: item[1]))
1205-
terminal_two_features = [
1216+
one_feature_terminals = dict(sorted(one_feature_terminals.items(), key=lambda item: item[1]))
1217+
two_feature_terminals = [
12061218
n for n, n_dict in connectivity_graph.nodes(data=True)
12071219
if len(n_dict.get("features", [])) == 2 and connectivity_graph.degree(n) == 1
12081220
]
12091221
sorted_nodes = (
1210-
terminal_two_features +
1211-
list(terminal_one_feature.keys()) +
1212-
[n for n in connectivity_graph if n not in terminal_two_features and n not in terminal_one_feature]
1222+
two_feature_terminals +
1223+
list(one_feature_terminals.keys()) +
1224+
[n for n in connectivity_graph if n not in two_feature_terminals and n not in one_feature_terminals]
12131225
)
12141226

12151227
terminal_graphs: dict[tuple, nx.Graph] = {}
@@ -1255,14 +1267,15 @@ def add_paths_to_neighbours(node, node_dict):
12551267
for neighbour in (neighbours - visited):
12561268
neighbour_dict = connectivity_graph.nodes[neighbour]
12571269
degree = connectivity_graph.degree(neighbour)
1270+
neighbour_features = neighbour_dict.get('features', set()) | {self.__flatmap.get_feature(f_id) for f_id in neighbour_dict.get('used', set())}
12581271
node_features = (
1259-
[get_node_feature(node_dict, neighbour_dict.get('features', []), used_features)]
1272+
[get_node_feature(node_dict, neighbour_features, used_features)]
12601273
if len(node_dict['features']) != 2
12611274
else used_features.get(node, set())
12621275
if connectivity_graph.degree(node) > 1 and len(used_features.get(node, set())) in [1, 2]
12631276
else set(node_dict['features'])
1264-
if connectivity_graph.degree(node) == 1
1265-
else [get_node_feature(node_dict, neighbour_dict.get('features', []), used_features)]
1277+
if connectivity_graph.degree(node) == 1 and all(item not in {node_dict['node'][0], *node_dict['node'][1]} for item in NOT_LATERAL_NODES)
1278+
else [get_node_feature(node_dict, neighbour_features, used_features)]
12661279
)
12671280
for node_feature in node_features:
12681281
used_features.setdefault(node, set()).add(node_feature)
@@ -1289,6 +1302,7 @@ def add_paths_to_neighbours(node, node_dict):
12891302
neighbour_features = (
12901303
neighbour_dict.get('features', [])
12911304
if len(neighbour_dict.get('features', [])) <= 2 and degree == 1 and len(node_features) == 1
1305+
and all(item not in {neighbour_dict['node'][0], *neighbour_dict['node'][1]} for item in NOT_LATERAL_NODES)
12921306
else [get_node_feature(neighbour_dict, [node_feature], used_features)]
12931307
if len(neighbour_terminal_laterals) > 0 and len(used_features.get(neighbour, set())) == 0
12941308
else [get_node_feature(neighbour_dict, [node_feature], used_features)]

0 commit comments

Comments
 (0)