Skip to content

Commit 0ce4eac

Browse files
jeromekellehermergify[bot]
authored andcommitted
Add support for string window-specs to divmat
Closes #2791
1 parent 9533a76 commit 0ce4eac

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

python/tests/test_divmat.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def span_normalise_windows(D, windows):
163163

164164
def branch_divergence_matrix(ts, windows=None, samples=None, span_normalise=True):
165165
windows_specified = windows is not None
166-
windows = [0, ts.sequence_length] if windows is None else windows
166+
windows = ts.parse_windows(windows)
167167
num_windows = len(windows) - 1
168168
samples = ts.samples() if samples is None else samples
169169

@@ -296,7 +296,7 @@ def group_alleles(genotypes, num_alleles):
296296

297297
def site_divergence_matrix(ts, windows=None, samples=None, span_normalise=True):
298298
windows_specified = windows is not None
299-
windows = [0, ts.sequence_length] if windows is None else windows
299+
windows = ts.parse_windows(windows)
300300
num_windows = len(windows) - 1
301301
samples = ts.samples() if samples is None else samples
302302

@@ -954,6 +954,8 @@ def check(self, ts, num_threads, *, windows, samples=None, mode=None):
954954
([5, 7, 9, 20],),
955955
([5.1, 5.2, 5.3, 5.5, 6],),
956956
([5.1, 5.2, 6.5],),
957+
("trees",),
958+
("sites",),
957959
],
958960
)
959961
@pytest.mark.parametrize("mode", DIVMAT_MODES)
@@ -968,6 +970,8 @@ def test_all_trees(self, num_threads, windows, mode):
968970
[
969971
([0, 26],),
970972
(None,),
973+
("trees",),
974+
("sites",),
971975
],
972976
)
973977
@pytest.mark.parametrize("mode", DIVMAT_MODES)
@@ -984,6 +988,8 @@ def test_all_trees_samples(self, samples, windows, mode):
984988
([50, 75, 95, 100],),
985989
([0, 50, 75, 95],),
986990
(list(range(100)),),
991+
("trees",),
992+
("sites",),
987993
],
988994
)
989995
@pytest.mark.parametrize("mode", DIVMAT_MODES)

python/tests/test_lowlevel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,8 @@ def test_divergence_matrix(self):
15371537
assert D.shape == (1, n, n)
15381538
D = ts.divergence_matrix(windows, samples=[0, 1])
15391539
assert D.shape == (1, 2, 2)
1540+
D = ts.divergence_matrix(windows, samples=[0, 1], span_normalise=True)
1541+
assert D.shape == (1, 2, 2)
15401542
with pytest.raises(TypeError, match="str"):
15411543
ts.divergence_matrix(windows, span_normalise="xdf")
15421544
with pytest.raises(TypeError):

python/tskit/trees.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7855,6 +7855,12 @@ def worker(sub_windows):
78557855
# return A
78567856
# NOTE: see older definition of divmat here, which may be useful when documenting
78577857
# this function. See https://github.com/tskit-dev/tskit/issues/2781
7858+
7859+
# NOTE for documentation of num_threads. Need to explain that the
7860+
# its best to think of as the number of background *worker* threads.
7861+
# default is to run without any worker threads. If you want to run
7862+
# with all the cores on the machine, use num_threads=os.cpu_count().
7863+
78587864
def divergence_matrix(
78597865
self,
78607866
*,
@@ -7865,12 +7871,11 @@ def divergence_matrix(
78657871
span_normalise=True,
78667872
):
78677873
windows_specified = windows is not None
7868-
windows = [0, self.sequence_length] if windows is None else windows
7869-
7874+
windows = self.parse_windows(windows)
78707875
mode = "site" if mode is None else mode
78717876

7872-
# NOTE: maybe we want to use a different default for num_threads here, just
7873-
# following the approach in GNN
7877+
# FIXME this logic should be merged into __run_windowed_stat if
7878+
# we generalise the num_threads argument to all stats.
78747879
if num_threads <= 0:
78757880
D = self._ll_tree_sequence.divergence_matrix(
78767881
windows,

0 commit comments

Comments
 (0)