33
44import networkx as nx
55
6- from ..model import DbrefGeoNode , Edge , GeoNode , Node , Signal , Topology , Wgs84GeoNode , SignalDirection
6+ from ..model import (
7+ DbrefGeoNode ,
8+ Edge ,
9+ GeoNode ,
10+ Node ,
11+ Signal ,
12+ SignalDirection ,
13+ Topology ,
14+ Wgs84GeoNode ,
15+ )
716
817
918class CompareMatching :
@@ -35,7 +44,7 @@ def compare(
3544 topology_b : Topology ,
3645 compare_mode : CompareMode ,
3746 given_node_matching : Dict [Node , Node ] | None = None ,
38- exclude_ends_in_calculation : bool = False
47+ exclude_ends_in_calculation : bool = False ,
3948 ) -> CompareResult :
4049 if given_node_matching is None :
4150 given_node_matching = {}
@@ -53,9 +62,15 @@ def compare(
5362 )
5463 Compare ._calc_isomorphic_matching (result , topology_a , topology_b , given_node_matching )
5564
56- result .node_distance = Compare ._calc_distance_for_matching (result .node_matching , exclude_ends_in_calculation )
57- result .edge_length_difference = Compare ._calc_distance_for_matching (result .edge_matching , exclude_ends_in_calculation , element_type = "edge" )
58- result .signal_distance = Compare ._calc_distance_for_matching (result .signal_matching , exclude_ends_in_calculation , element_type = "signal" )
65+ result .node_distance = Compare ._calc_distance_for_matching (
66+ result .node_matching , exclude_ends_in_calculation
67+ )
68+ result .edge_length_difference = Compare ._calc_distance_for_matching (
69+ result .edge_matching , exclude_ends_in_calculation , element_type = "edge"
70+ )
71+ result .signal_distance = Compare ._calc_distance_for_matching (
72+ result .signal_matching , exclude_ends_in_calculation , element_type = "signal"
73+ )
5974 return result
6075
6176 @staticmethod
@@ -132,13 +147,17 @@ def __add_edges_to_matching(__edge_a: Edge, __edge_b: Edge):
132147 __add_edges_to_matching (node_a .connected_edge_on_head , node_b .connected_edge_on_head )
133148 if node_a .is_point ():
134149 __add_to_open_nodes (node_a .connected_on_left , node_b .connected_on_left )
135- __add_edges_to_matching (node_a .connected_edge_on_left , node_b .connected_edge_on_left )
136- __add_to_open_nodes (
137- node_a .connected_on_right , node_b .connected_on_right
150+ __add_edges_to_matching (
151+ node_a .connected_edge_on_left , node_b .connected_edge_on_left
152+ )
153+ __add_to_open_nodes (node_a .connected_on_right , node_b .connected_on_right )
154+ __add_edges_to_matching (
155+ node_a .connected_edge_on_right , node_b .connected_edge_on_right
138156 )
139- __add_edges_to_matching (node_a .connected_edge_on_right , node_b .connected_edge_on_right )
140157
141- def __add_signal_lists_to_matching (signal_list_a : List [Signal ], signal_list_b : List [Signal ]):
158+ def __add_signal_lists_to_matching (
159+ signal_list_a : List [Signal ], signal_list_b : List [Signal ]
160+ ):
142161 for i in range (0 , len (signal_list_a )):
143162 signal_a = signal_list_a [i ]
144163 signal_b = signal_list_b [i ]
@@ -151,21 +170,35 @@ def __add_signal_lists_to_matching(signal_list_a: List[Signal], signal_list_b: L
151170 continue
152171 if not edge_a .signals or not edge_b .signals :
153172 raise ValueError (f"Signals on edges { edge_a .uuid } and { edge_b .uuid } does not match" )
154- in_direction_a = [signal for signal in edge_a .signals if signal .direction == SignalDirection .IN ]
173+ in_direction_a = [
174+ signal for signal in edge_a .signals if signal .direction == SignalDirection .IN
175+ ]
155176 in_direction_a .sort (key = lambda signal : signal .distance_edge )
156- other_direction_a = [signal for signal in edge_a .signals if signal .direction == SignalDirection .GEGEN ]
177+ other_direction_a = [
178+ signal for signal in edge_a .signals if signal .direction == SignalDirection .GEGEN
179+ ]
157180 other_direction_a .sort (key = lambda signal : signal .distance_edge )
158- in_direction_b = [signal for signal in edge_b .signals if signal .direction == SignalDirection .IN ]
181+ in_direction_b = [
182+ signal for signal in edge_b .signals if signal .direction == SignalDirection .IN
183+ ]
159184 in_direction_b .sort (key = lambda signal : signal .distance_edge )
160- other_direction_b = [signal for signal in edge_b .signals if signal .direction == SignalDirection .GEGEN ]
185+ other_direction_b = [
186+ signal for signal in edge_b .signals if signal .direction == SignalDirection .GEGEN
187+ ]
161188 other_direction_b .sort (key = lambda signal : signal .distance_edge )
162189
163190 if result .node_matching .element_matching [edge_a .node_a ] != edge_b .node_a :
164191 # edge b is reversed, so switch lists
165- in_direction_b , other_direction_b = list (reversed (other_direction_b )), list (reversed (in_direction_b ))
192+ in_direction_b , other_direction_b = list (reversed (other_direction_b )), list (
193+ reversed (in_direction_b )
194+ )
166195
167- if len (in_direction_a ) != len (in_direction_b ) or len (other_direction_a ) != len (other_direction_b ):
168- raise ValueError (f"Number of signals on edges { edge_a .uuid } and { edge_b .uuid } differs (per direction)" )
196+ if len (in_direction_a ) != len (in_direction_b ) or len (other_direction_a ) != len (
197+ other_direction_b
198+ ):
199+ raise ValueError (
200+ f"Number of signals on edges { edge_a .uuid } and { edge_b .uuid } differs (per direction)"
201+ )
169202 __add_signal_lists_to_matching (in_direction_a , in_direction_b )
170203 __add_signal_lists_to_matching (other_direction_a , other_direction_b )
171204
@@ -181,7 +214,9 @@ def _are_topologies_isomorphic(topology_a: Topology, topology_b: Topology):
181214 return nx .is_isomorphic (graph_a , graph_b )
182215
183216 @staticmethod
184- def _calc_distance_for_matching (matching : CompareMatching , exclude_ends_in_calculation , element_type : str = "node" ):
217+ def _calc_distance_for_matching (
218+ matching : CompareMatching , exclude_ends_in_calculation , element_type : str = "node"
219+ ):
185220 if not matching .element_matching :
186221 return - 1.0
187222
@@ -196,12 +231,16 @@ def _calc_distance_for_matching(matching: CompareMatching, exclude_ends_in_calcu
196231 geo_node_b : GeoNode = element_b .geo_node
197232 distance_sum += geo_node_a .get_distance_to_other_geo_node (geo_node_b )
198233 elif element_type == "edge" :
199- if exclude_ends_in_calculation and (not element_a .node_a .is_point () or not element_a .node_b .is_point ()):
234+ if exclude_ends_in_calculation and (
235+ not element_a .node_a .is_point () or not element_a .node_b .is_point ()
236+ ):
200237 continue
201238 distance_sum += abs (element_a .length - element_b .length )
202239 elif element_type == "signal" :
203240 x_a , y_a = element_a .get_calculated_coordinates ()
204241 x_b , y_b = element_b .get_calculated_coordinates ()
205- distance_sum += DbrefGeoNode (x_a , y_a ).get_distance_to_other_geo_node (DbrefGeoNode (x_b , y_b ))
242+ distance_sum += DbrefGeoNode (x_a , y_a ).get_distance_to_other_geo_node (
243+ DbrefGeoNode (x_b , y_b )
244+ )
206245
207246 return distance_sum
0 commit comments