Skip to content

Commit a325919

Browse files
Add flag skip signals
1 parent b24641f commit a325919

File tree

1 file changed

+61
-52
lines changed

1 file changed

+61
-52
lines changed

yaramo/operations/compare.py

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def compare(
4545
compare_mode: CompareMode,
4646
given_node_matching: Dict[Node, Node] | None = None,
4747
exclude_ends_in_calculation: bool = False,
48+
skip_signals: bool = False,
4849
) -> CompareResult:
4950
if given_node_matching is None:
5051
given_node_matching = {}
@@ -60,17 +61,18 @@ def compare(
6061
raise ValueError(
6162
"For isomorphic topologies, at least one mathing node needs to be given."
6263
)
63-
Compare._calc_isomorphic_matching(result, topology_a, topology_b, given_node_matching)
64+
Compare._calc_isomorphic_matching(result, topology_a, topology_b, given_node_matching, skip_signals)
6465

6566
result.node_distance = Compare._calc_distance_for_matching(
66-
result.node_matching, exclude_ends_in_calculation
67+
result.node_matching, exclude_ends_in_calculation, start_node_a=list(given_node_matching.keys())[0], start_node_b=list(given_node_matching.values())[0]
6768
)
6869
result.edge_length_difference = Compare._calc_distance_for_matching(
6970
result.edge_matching, exclude_ends_in_calculation, element_type="edge"
7071
)
71-
result.signal_distance = Compare._calc_distance_for_matching(
72-
result.signal_matching, exclude_ends_in_calculation, element_type="signal"
73-
)
72+
if not skip_signals:
73+
result.signal_distance = Compare._calc_distance_for_matching(
74+
result.signal_matching, exclude_ends_in_calculation, element_type="signal"
75+
)
7476
return result
7577

7678
@staticmethod
@@ -103,6 +105,7 @@ def _calc_isomorphic_matching(
103105
topology_a: Topology,
104106
topology_b: Topology,
105107
given_node_matching: Dict[Node, Node],
108+
skip_signals: bool,
106109
):
107110
open_nodes: List[Tuple[Node, Node]] = []
108111

@@ -155,52 +158,53 @@ def __add_edges_to_matching(__edge_a: Edge, __edge_b: Edge):
155158
node_a.connected_edge_on_right, node_b.connected_edge_on_right
156159
)
157160

158-
def __add_signal_lists_to_matching(
159-
signal_list_a: List[Signal], signal_list_b: List[Signal]
160-
):
161-
for i in range(0, len(signal_list_a)):
162-
signal_a = signal_list_a[i]
163-
signal_b = signal_list_b[i]
164-
result.signal_matching.element_matching[signal_a] = signal_b
165-
166-
# signal matching
167-
for edge_a in topology_a.edges.values():
168-
edge_b = result.edge_matching.element_matching[edge_a]
169-
if not edge_a.signals and not edge_b.signals:
170-
continue
171-
if not edge_a.signals or not edge_b.signals:
172-
raise ValueError(f"Signals on edges {edge_a.uuid} and {edge_b.uuid} does not match")
173-
in_direction_a = [
174-
signal for signal in edge_a.signals if signal.direction == SignalDirection.IN
175-
]
176-
in_direction_a.sort(key=lambda signal: signal.distance_edge)
177-
other_direction_a = [
178-
signal for signal in edge_a.signals if signal.direction == SignalDirection.GEGEN
179-
]
180-
other_direction_a.sort(key=lambda signal: signal.distance_edge)
181-
in_direction_b = [
182-
signal for signal in edge_b.signals if signal.direction == SignalDirection.IN
183-
]
184-
in_direction_b.sort(key=lambda signal: signal.distance_edge)
185-
other_direction_b = [
186-
signal for signal in edge_b.signals if signal.direction == SignalDirection.GEGEN
187-
]
188-
other_direction_b.sort(key=lambda signal: signal.distance_edge)
189-
190-
if result.node_matching.element_matching[edge_a.node_a] != edge_b.node_a:
191-
# edge b is reversed, so switch lists
192-
in_direction_b, other_direction_b = list(reversed(other_direction_b)), list(
193-
reversed(in_direction_b)
194-
)
195-
196-
if len(in_direction_a) != len(in_direction_b) or len(other_direction_a) != len(
197-
other_direction_b
161+
if not skip_signals:
162+
def __add_signal_lists_to_matching(
163+
signal_list_a: List[Signal], signal_list_b: List[Signal]
198164
):
199-
raise ValueError(
200-
f"Number of signals on edges {edge_a.uuid} and {edge_b.uuid} differs (per direction)"
201-
)
202-
__add_signal_lists_to_matching(in_direction_a, in_direction_b)
203-
__add_signal_lists_to_matching(other_direction_a, other_direction_b)
165+
for i in range(0, len(signal_list_a)):
166+
signal_a = signal_list_a[i]
167+
signal_b = signal_list_b[i]
168+
result.signal_matching.element_matching[signal_a] = signal_b
169+
170+
# signal matching
171+
for edge_a in topology_a.edges.values():
172+
edge_b = result.edge_matching.element_matching[edge_a]
173+
if not edge_a.signals and not edge_b.signals:
174+
continue
175+
if not edge_a.signals or not edge_b.signals:
176+
raise ValueError(f"Signals on edges {edge_a.uuid} and {edge_b.uuid} does not match")
177+
in_direction_a = [
178+
signal for signal in edge_a.signals if signal.direction == SignalDirection.IN
179+
]
180+
in_direction_a.sort(key=lambda signal: signal.distance_edge)
181+
other_direction_a = [
182+
signal for signal in edge_a.signals if signal.direction == SignalDirection.GEGEN
183+
]
184+
other_direction_a.sort(key=lambda signal: signal.distance_edge)
185+
in_direction_b = [
186+
signal for signal in edge_b.signals if signal.direction == SignalDirection.IN
187+
]
188+
in_direction_b.sort(key=lambda signal: signal.distance_edge)
189+
other_direction_b = [
190+
signal for signal in edge_b.signals if signal.direction == SignalDirection.GEGEN
191+
]
192+
other_direction_b.sort(key=lambda signal: signal.distance_edge)
193+
194+
if result.node_matching.element_matching[edge_a.node_a] != edge_b.node_a:
195+
# edge b is reversed, so switch lists
196+
in_direction_b, other_direction_b = list(reversed(other_direction_b)), list(
197+
reversed(in_direction_b)
198+
)
199+
200+
if len(in_direction_a) != len(in_direction_b) or len(other_direction_a) != len(
201+
other_direction_b
202+
):
203+
raise ValueError(
204+
f"Number of signals on edges {edge_a.uuid} and {edge_b.uuid} differs (per direction)"
205+
)
206+
__add_signal_lists_to_matching(in_direction_a, in_direction_b)
207+
__add_signal_lists_to_matching(other_direction_a, other_direction_b)
204208

205209
@staticmethod
206210
def _are_topologies_isomorphic(topology_a: Topology, topology_b: Topology):
@@ -215,7 +219,7 @@ def _are_topologies_isomorphic(topology_a: Topology, topology_b: Topology):
215219

216220
@staticmethod
217221
def _calc_distance_for_matching(
218-
matching: CompareMatching, exclude_ends_in_calculation, element_type: str = "node"
222+
matching: CompareMatching, exclude_ends_in_calculation, element_type: str = "node", start_node_a: Node = None, start_node_b: Node = None,
219223
):
220224
if not matching.element_matching:
221225
return -1.0
@@ -227,14 +231,19 @@ def _calc_distance_for_matching(
227231
if element_type == "node":
228232
if exclude_ends_in_calculation and not element_a.is_point():
229233
continue
234+
start_geo_node_a = start_node_a.geo_node
235+
start_geo_node_b = start_node_b.geo_node
230236
geo_node_a: GeoNode = element_a.geo_node
231237
geo_node_b: GeoNode = element_b.geo_node
232-
distance_sum += geo_node_a.get_distance_to_other_geo_node(geo_node_b)
238+
distance = abs(start_geo_node_a.get_distance_to_other_geo_node(geo_node_a) - start_geo_node_b.get_distance_to_other_geo_node(geo_node_b))
239+
print(f"From {element_a.uuid} to {element_b.uuid}: {distance}")
240+
distance_sum += distance
233241
elif element_type == "edge":
234242
if exclude_ends_in_calculation and (
235243
not element_a.node_a.is_point() or not element_a.node_b.is_point()
236244
):
237245
continue
246+
print(f"Edge {element_a.uuid} compared to {element_b.uuid}: {abs(element_a.length - element_b.length)}")
238247
distance_sum += abs(element_a.length - element_b.length)
239248
elif element_type == "signal":
240249
x_a, y_a = element_a.get_calculated_coordinates()

0 commit comments

Comments
 (0)