Skip to content

Commit d2048c3

Browse files
authored
Merge pull request #995 from benjeffery/fix-overflow
Error on cycles in found ancestor deps
2 parents fbff408 + 2a681ab commit d2048c3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

tests/test_ancestors.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,25 @@ def test_grouping_random_cases(self, seed):
274274
assert group_ids[anc_a] > group_ids[anc_b]
275275
else:
276276
assert group_ids[anc_a] < group_ids[anc_b]
277+
278+
def test_find_groups_cycle(self):
279+
"""
280+
Test find_groups detects cycles in the dependency graph using a minimal example
281+
"""
282+
# Create a direct cycle between two nodes where:
283+
# Node 0 depends on node 1 (has incoming edge from 1)
284+
# Node 1 depends on node 0 (has incoming edge from 0)
285+
children_data = np.array(
286+
[1, 0], dtype=np.int32
287+
) # Node 0 has child 1, node 1 has child 0
288+
children_indices = np.array(
289+
[0, 1, 2], dtype=np.int32
290+
) # Each node has one child
291+
incoming_edge_count = np.array(
292+
[1, 1], dtype=np.int32
293+
) # Each node has one incoming edge
294+
295+
with pytest.raises(
296+
ValueError, match="Erroneous cycle in ancestor dependancies.*"
297+
):
298+
ancestors.find_groups(children_data, children_indices, incoming_edge_count)

tsinfer/ancestors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ def find_groups(children_data, children_indices, incoming_edge_count):
133133
# Add them to the group
134134
group_id[no_incoming] = current_group
135135
current_group += 1
136+
137+
# Check for unassigned nodes (cycles in dependency graph)
138+
if np.any(group_id == -1):
139+
raise ValueError(
140+
"Erroneous cycle in ancestor dependancies, this is often "
141+
"caused by too many unique site times. This fixed by discretising "
142+
"the site times, for example rounding times to the nearest 0.1."
143+
)
136144
return group_id
137145

138146

0 commit comments

Comments
 (0)