@@ -88,6 +88,14 @@ def pairwise(iterable):
88
88
89
89
#===============================================================================
90
90
91
+ NOT_LATERAL_NODES = [
92
+ 'UBERON:0001896' ,
93
+ 'UBERON:0000988' ,
94
+ 'UBERON:0001891'
95
+ ]
96
+
97
+ #=============================================================
98
+
91
99
def expand_centreline_graph (graph : nx .MultiGraph ) -> nx .Graph :
92
100
#=============================================================
93
101
G = nx .Graph ()
@@ -957,8 +965,11 @@ def add_route_edges_from_graph(G, used_nodes):
957
965
if neighbour_dict ['type' ] == 'feature' :
958
966
# if the node is a no-segment and neighbour is a terminal, connect to all neighbour features
959
967
# 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
+ )
962
973
for feature in features :
963
974
neighbouring_ids .update ([feature .id ])
964
975
closest_feature_id = None
@@ -985,7 +996,7 @@ def add_route_edges_from_graph(G, used_nodes):
985
996
if n_id in segment_graph :
986
997
updated_neighbouring_ids .update ([n_id ])
987
998
else :
988
- if ( f := self .__map_feature (n_id ) ) is not None :
999
+ if self .__map_feature (n_id ) is not None :
989
1000
if (closest_feature_id := closest_feature_dict .get (n_id )) is not None :
990
1001
updated_neighbouring_ids .update ([closest_feature_id ])
991
1002
new_direct_edges .update ([(n_id , closest_feature_id )])
@@ -1190,26 +1201,27 @@ def get_node_feature(node_dict, neighbour_features, used_features) -> Feature:
1190
1201
pseudo_terminals += list (subgraph .nodes )[0 :1 ]
1191
1202
1192
1203
# 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
+ ])
1199
1213
for n , n_dict in connectivity_graph .nodes (data = True )
1200
1214
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" , [])
1203
1215
}
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 = [
1206
1218
n for n , n_dict in connectivity_graph .nodes (data = True )
1207
1219
if len (n_dict .get ("features" , [])) == 2 and connectivity_graph .degree (n ) == 1
1208
1220
]
1209
1221
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 ]
1213
1225
)
1214
1226
1215
1227
terminal_graphs : dict [tuple , nx .Graph ] = {}
@@ -1255,14 +1267,15 @@ def add_paths_to_neighbours(node, node_dict):
1255
1267
for neighbour in (neighbours - visited ):
1256
1268
neighbour_dict = connectivity_graph .nodes [neighbour ]
1257
1269
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 ())}
1258
1271
node_features = (
1259
- [get_node_feature (node_dict , neighbour_dict . get ( 'features' , []) , used_features )]
1272
+ [get_node_feature (node_dict , neighbour_features , used_features )]
1260
1273
if len (node_dict ['features' ]) != 2
1261
1274
else used_features .get (node , set ())
1262
1275
if connectivity_graph .degree (node ) > 1 and len (used_features .get (node , set ())) in [1 , 2 ]
1263
1276
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 )]
1266
1279
)
1267
1280
for node_feature in node_features :
1268
1281
used_features .setdefault (node , set ()).add (node_feature )
@@ -1289,6 +1302,7 @@ def add_paths_to_neighbours(node, node_dict):
1289
1302
neighbour_features = (
1290
1303
neighbour_dict .get ('features' , [])
1291
1304
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 )
1292
1306
else [get_node_feature (neighbour_dict , [node_feature ], used_features )]
1293
1307
if len (neighbour_terminal_laterals ) > 0 and len (used_features .get (neighbour , set ())) == 0
1294
1308
else [get_node_feature (neighbour_dict , [node_feature ], used_features )]
0 commit comments