Skip to content

Commit cd830f7

Browse files
committed
fix: use fixture for self comparison test
1 parent c91169a commit cd830f7

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

python/tests/test_tree_stats.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,16 +2311,29 @@ def test_shapes(self, proportion):
23112311
class TestBranchGeneticRelatedness(TestGeneticRelatedness, TopologyExamplesMixin):
23122312
mode = "branch"
23132313

2314-
def test_single_sample_set_self_comparison(self):
2314+
def test_single_sample_set_self_comparison(self, ts_12_highrecomb_fixture):
23152315
# Test for issue #3055 - self-comparisons with single sample set
2316-
ts = msprime.simulate(sample_size=10, Ne=10000, length=1000, random_seed=42)
2316+
ts = ts_12_highrecomb_fixture
23172317
# Single sample set with self-comparison
23182318
result = ts.genetic_relatedness([[0]], indexes=[(0, 0)], mode="branch")
23192319
assert result.shape == (1,)
23202320
# Should work for multiple samples in single set too
23212321
result = ts.genetic_relatedness([[0, 1, 2]], indexes=[(0, 0)], mode="branch")
23222322
assert result.shape == (1,)
23232323

2324+
def test_single_sample_set_invalid_indexes(self, ts_12_highrecomb_fixture):
2325+
# Test that invalid indexes raise ValueError with single sample set
2326+
ts = ts_12_highrecomb_fixture
2327+
# Index out of bounds (only have 1 sample set, but trying to access index 1)
2328+
with pytest.raises(ValueError, match="Index out of bounds"):
2329+
ts.genetic_relatedness([[0]], indexes=[(0, 1)], mode="branch")
2330+
# Negative index
2331+
with pytest.raises(ValueError, match="Index out of bounds"):
2332+
ts.genetic_relatedness([[0]], indexes=[(-1, 0)], mode="branch")
2333+
# Both indexes out of bounds
2334+
with pytest.raises(ValueError, match="Index out of bounds"):
2335+
ts.genetic_relatedness([[0, 1]], indexes=[(2, 2)], mode="branch")
2336+
23242337
@pytest.mark.parametrize("polarised", [True, False])
23252338
def test_simple_tree_noncentred(self, polarised):
23262339
# 2.00┊ 4 ┊
@@ -2385,6 +2398,19 @@ def test_single_sample_set_self_comparison(self, ts_12_highrecomb_fixture):
23852398
result = ts.genetic_relatedness([[0, 1, 2]], indexes=[(0, 0)], mode="node")
23862399
assert result.shape == (ts.num_nodes, 1)
23872400

2401+
def test_single_sample_set_invalid_indexes(self, ts_12_highrecomb_fixture):
2402+
# Test that invalid indexes raise ValueError with single sample set
2403+
ts = ts_12_highrecomb_fixture
2404+
# Index out of bounds (only have 1 sample set, but trying to access index 1)
2405+
with pytest.raises(ValueError, match="Index out of bounds"):
2406+
ts.genetic_relatedness([[0]], indexes=[(0, 1)], mode="node")
2407+
# Negative index
2408+
with pytest.raises(ValueError, match="Index out of bounds"):
2409+
ts.genetic_relatedness([[0]], indexes=[(-1, 0)], mode="node")
2410+
# Both indexes out of bounds
2411+
with pytest.raises(ValueError, match="Index out of bounds"):
2412+
ts.genetic_relatedness([[0, 1]], indexes=[(2, 2)], mode="node")
2413+
23882414

23892415
class TestSiteGeneticRelatedness(TestGeneticRelatedness, MutatedTopologyExamplesMixin):
23902416
mode = "site"
@@ -2404,6 +2430,19 @@ def test_single_sample_set_self_comparison(self, ts_12_highrecomb_fixture):
24042430
)
24052431
assert result.shape == (2,)
24062432

2433+
def test_single_sample_set_invalid_indexes(self, ts_12_highrecomb_fixture):
2434+
# Test that invalid indexes raise ValueError with single sample set
2435+
ts = ts_12_highrecomb_fixture
2436+
# Index out of bounds (only have 1 sample set, but trying to access index 1)
2437+
with pytest.raises(ValueError, match="Index out of bounds"):
2438+
ts.genetic_relatedness([[0]], indexes=[(0, 1)], mode="site")
2439+
# Negative index
2440+
with pytest.raises(ValueError, match="Index out of bounds"):
2441+
ts.genetic_relatedness([[0]], indexes=[(-1, 0)], mode="site")
2442+
# Both indexes out of bounds
2443+
with pytest.raises(ValueError, match="Index out of bounds"):
2444+
ts.genetic_relatedness([[0, 1]], indexes=[(2, 2)], mode="site")
2445+
24072446
def test_match_K_c0(self):
24082447
# This test checks that ts.genetic_relatedness() matches K_c0
24092448
# from Speed & Balding (2014) https://www.nature.com/articles/nrg3821

0 commit comments

Comments
 (0)