diff --git a/src/funtracks/candidate_graph/utils.py b/src/funtracks/candidate_graph/utils.py index 57b30a5..7147de5 100644 --- a/src/funtracks/candidate_graph/utils.py +++ b/src/funtracks/candidate_graph/utils.py @@ -65,6 +65,8 @@ def nodes_from_segmentation( attrs[NodeAttr.SEG_ID.value] = regionprop.label centroid = regionprop.centroid # [z,] y, x attrs[NodeAttr.POS.value] = centroid + if node_id in cand_graph.nodes: + raise ValueError("Duplicate values found among nodes") cand_graph.add_node(node_id, **attrs) nodes_in_frame.append(node_id) if nodes_in_frame: diff --git a/tests/candidate_graph/test_compute_graph.py b/tests/candidate_graph/test_compute_graph.py index df35afa..0e7f049 100644 --- a/tests/candidate_graph/test_compute_graph.py +++ b/tests/candidate_graph/test_compute_graph.py @@ -50,6 +50,15 @@ def test_graph_from_segmentation_3d(segmentation_3d, graph_3d): assert pytest.approx(cand_graph.edges[edge], abs=0.01) == graph_3d.edges[edge] +def test_graph_from_segmentation_with_duplicate_nodes(segmentation_2d): + segmentation_2d[1][20:25, 20:25] = 1 # add a duplicate label to another time point + with pytest.raises(ValueError, match="Duplicate values found among nodes"): + compute_graph_from_seg( + segmentation=segmentation_2d, + max_edge_distance=100, + ) + + def test_graph_from_points_list(): points_list = np.array( [