Skip to content

Commit 643a292

Browse files
Split: Validate input
1 parent 8d31473 commit 643a292

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

yaramo/operations/split.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ def split(
2424
add_missing_elements_to_topology: None | Label = None,
2525
node_label_assignments: Node | Dict[Node, Label] = None,
2626
) -> Tuple[Topology, Topology, Dict[Edge, Tuple[Node, Node]]]:
27+
if not topology.nodes:
28+
raise ValueError("Given topology is empty.")
2729
if split_edges is None:
2830
split_edges = {}
2931
else:
30-
Split._validate_split_edges(split_edges)
32+
Split._validate_split_edges(topology, split_edges)
3133
if node_label_assignments is None:
3234
node_label_assignments = {}
35+
else:
36+
Split._validate_node_label_assignment(topology, node_label_assignments)
3337

3438
if not split_edges and not node_label_assignments:
3539
raise ValueError("Either split edges or node label assignments are necessary")
@@ -83,8 +87,10 @@ def _add_elements_to_topology(
8387
return topology_a, topology_b, new_end_nodes
8488

8589
@staticmethod
86-
def _validate_split_edges(split_edges: Dict[Edge, float]):
90+
def _validate_split_edges(topology: Topology, split_edges: Dict[Edge, float]):
8791
for edge, distance_on_edge in split_edges.items():
92+
if edge not in topology:
93+
raise ValueError("Given split edge is not part of the topology")
8894
edge.update_length()
8995
if edge.length < distance_on_edge:
9096
raise ValueError(f"The edge {edge.name} is shorter than split distance.")
@@ -99,6 +105,12 @@ def _validate_split_edges(split_edges: Dict[Edge, float]):
99105
f"should be split. This is not possible."
100106
)
101107

108+
@staticmethod
109+
def _validate_node_label_assignment(topology: Topology, node_label_assignment: Dict[Node, Label]):
110+
for node in node_label_assignment:
111+
if node not in topology:
112+
raise ValueError("Node of node label assignment not in topology.")
113+
102114
@staticmethod
103115
def _split_edges(
104116
topology: Topology, split_edges: Dict[Edge, float]

0 commit comments

Comments
 (0)