Skip to content

Commit 2dae133

Browse files
jeromekellehermergify[bot]
authored andcommitted
Add check for non-single sample sets and disable tests
1 parent d651881 commit 2dae133

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

python/tests/test_divmat.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,12 +1347,19 @@ def test_bad_arg_types(self, arg):
13471347

13481348

13491349
class TestGeneticRelatednessMatrix:
1350-
def check(self, ts, mode, *, windows=None, span_normalise=True):
1350+
def check(self, ts, mode, *, sample_sets=None, windows=None, span_normalise=True):
13511351
G1 = stats_api_genetic_relatedness_matrix(
1352-
ts, mode=mode, windows=windows, span_normalise=span_normalise
1352+
ts,
1353+
mode=mode,
1354+
sample_sets=sample_sets,
1355+
windows=windows,
1356+
span_normalise=span_normalise,
13531357
)
13541358
G2 = ts.genetic_relatedness_matrix(
1355-
mode=mode, windows=windows, span_normalise=span_normalise
1359+
mode=mode,
1360+
sample_sets=sample_sets,
1361+
windows=windows,
1362+
span_normalise=span_normalise,
13561363
)
13571364
np.testing.assert_array_almost_equal(G1, G2)
13581365

@@ -1368,6 +1375,33 @@ def test_single_tree(self, mode):
13681375
ts = tsutil.insert_branch_sites(ts)
13691376
self.check(ts, mode)
13701377

1378+
@pytest.mark.parametrize("mode", DIVMAT_MODES)
1379+
def test_single_tree_sample_sets(self, mode):
1380+
# 2.00┊ 6 ┊
1381+
# ┊ ┏━┻━┓ ┊
1382+
# 1.00┊ 4 5 ┊
1383+
# ┊ ┏┻┓ ┏┻┓ ┊
1384+
# 0.00┊ 0 1 2 3 ┊
1385+
# 0 1
1386+
ts = tskit.Tree.generate_balanced(4).tree_sequence
1387+
ts = tsutil.insert_branch_sites(ts)
1388+
with pytest.raises(ValueError, match="2888"):
1389+
self.check(ts, mode, sample_sets=[[0, 1], [2, 3]])
1390+
1391+
@pytest.mark.parametrize("mode", DIVMAT_MODES)
1392+
def test_single_tree_single_samples(self, mode):
1393+
# 2.00┊ 6 ┊
1394+
# ┊ ┏━┻━┓ ┊
1395+
# 1.00┊ 4 5 ┊
1396+
# ┊ ┏┻┓ ┏┻┓ ┊
1397+
# 0.00┊ 0 1 2 3 ┊
1398+
# 0 1
1399+
ts = tskit.Tree.generate_balanced(4).tree_sequence
1400+
ts = tsutil.insert_branch_sites(ts)
1401+
self.check(ts, mode, sample_sets=[[0], [1]])
1402+
self.check(ts, mode, sample_sets=[[0], [2]])
1403+
self.check(ts, mode, sample_sets=[[0], [1], [2]])
1404+
13711405
@pytest.mark.parametrize("mode", DIVMAT_MODES)
13721406
def test_single_tree_windows(self, mode):
13731407
# 2.00┊ 6 ┊
@@ -1390,3 +1424,12 @@ def test_suite_defaults(self, ts, mode):
13901424
@pytest.mark.parametrize("span_normalise", [True, False])
13911425
def test_suite_span_normalise(self, ts, mode, span_normalise):
13921426
self.check(ts, mode=mode, span_normalise=span_normalise)
1427+
1428+
@pytest.mark.skip("fix sample sets #2888")
1429+
@pytest.mark.parametrize("ts", get_example_tree_sequences())
1430+
@pytest.mark.parametrize("mode", DIVMAT_MODES)
1431+
@pytest.mark.parametrize("num_sets", [2]) # [[2, 3, 4, 5])
1432+
def test_suite_sample_sets(self, ts, mode, num_sets):
1433+
if ts.num_samples >= num_sets:
1434+
sample_sets = np.array_split(ts.samples(), num_sets)
1435+
self.check(ts, sample_sets=sample_sets, mode=mode)

python/tskit/trees.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2018-2023 Tskit Developers
3+
# Copyright (c) 2018-2024 Tskit Developers
44
# Copyright (c) 2015-2018 University of Oxford
55
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -8139,14 +8139,24 @@ def genetic_relatedness_matrix(
81398139
span_normalise=span_normalise,
81408140
)
81418141

8142+
# FIXME remove this when sample sets bug has been fixed.
8143+
# https://github.com/tskit-dev/tskit/issues/2888
8144+
if sample_sets is not None:
8145+
if any(len(ss) > 1 for ss in sample_sets):
8146+
raise ValueError(
8147+
"Only single entry sample sets allowed for now."
8148+
" See https://github.com/tskit-dev/tskit/issues/2888"
8149+
)
8150+
81428151
def _normalise(B):
81438152
if len(B) == 0:
81448153
return B
81458154
K = B + np.mean(B)
81468155
y = np.mean(B, axis=0)
81478156
X = y[:, np.newaxis] + y[np.newaxis, :]
81488157
K -= X
8149-
# FIXME I don't know what this factor of -2 is about
8158+
# FIXME this factor of 2 works for single-sample sample-sets, but not
8159+
# otherwise. https://github.com/tskit-dev/tskit/issues/2888
81508160
return K / -2
81518161

81528162
if windows is None:

0 commit comments

Comments
 (0)