From 2059859e6d411c0053b4d53434a392b2520f4ac0 Mon Sep 17 00:00:00 2001 From: Nate Pope Date: Thu, 19 Sep 2024 10:27:37 -0700 Subject: [PATCH] Catch case when windows do not span entire sequence Check for _tskit.LibraryError --- c/tests/test_stats.c | 12 ++++++------ c/tskit/trees.c | 2 +- python/tests/test_lowlevel.py | 19 ++++++++++++------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/c/tests/test_stats.c b/c/tests/test_stats.c index 186c9b00f8..0e5c2e17a1 100644 --- a/c/tests/test_stats.c +++ b/c/tests/test_stats.c @@ -373,34 +373,34 @@ verify_pair_coalescence_counts(tsk_treeseq_t *ts, tsk_flags_t options) index_tuples[0] = (tsk_id_t) P; ret = tsk_treeseq_pair_coalescence_counts(ts, P, sample_set_sizes, sample_sets, I, - index_tuples, 1, breakpoints, N, node_bin_map, options, C); + index_tuples, T, breakpoints, N, node_bin_map, options, C); CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_SAMPLE_SET_INDEX); index_tuples[0] = 0; tsk_size_t tmp = sample_set_sizes[0]; sample_set_sizes[0] = 0; ret = tsk_treeseq_pair_coalescence_counts(ts, P, sample_set_sizes, sample_sets, I, - index_tuples, 1, breakpoints, N, node_bin_map, options, C); + index_tuples, T, breakpoints, N, node_bin_map, options, C); CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_EMPTY_SAMPLE_SET); sample_set_sizes[0] = tmp; sample_sets[1] = 0; ret = tsk_treeseq_pair_coalescence_counts(ts, P, sample_set_sizes, sample_sets, I, - index_tuples, 1, breakpoints, N, node_bin_map, options, C); + index_tuples, T, breakpoints, N, node_bin_map, options, C); CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_DUPLICATE_SAMPLE); sample_sets[1] = 1; ret = tsk_treeseq_pair_coalescence_counts(ts, P, sample_set_sizes, sample_sets, I, - index_tuples, 1, breakpoints, N - 1, node_bin_map, options, C); + index_tuples, T, breakpoints, N - 1, node_bin_map, options, C); CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_NODE_BIN_MAP_DIM); ret = tsk_treeseq_pair_coalescence_counts(ts, P, sample_set_sizes, sample_sets, I, - index_tuples, 1, breakpoints, 0, node_bin_map, options, C); + index_tuples, T, breakpoints, 0, node_bin_map, options, C); CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_NODE_BIN_MAP_DIM); node_bin_map[0] = -2; ret = tsk_treeseq_pair_coalescence_counts(ts, P, sample_set_sizes, sample_sets, I, - index_tuples, 1, breakpoints, N, node_bin_map, options, C); + index_tuples, T, breakpoints, N, node_bin_map, options, C); CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_NODE_BIN_MAP); node_bin_map[0] = 0; } diff --git a/c/tskit/trees.c b/c/tskit/trees.c index d214f1dce0..6119081a78 100644 --- a/c/tskit/trees.c +++ b/c/tskit/trees.c @@ -9371,7 +9371,7 @@ tsk_treeseq_pair_coalescence_stat(const tsk_treeseq_t *self, tsk_size_t num_samp tsk_memset(&tree_pos, 0, sizeof(tree_pos)); /* check inputs */ - ret = tsk_treeseq_check_windows(self, num_windows, windows, options); + ret = tsk_treeseq_check_windows(self, num_windows, windows, TSK_REQUIRE_FULL_SPAN); if (ret != 0) { goto out; } diff --git a/python/tests/test_lowlevel.py b/python/tests/test_lowlevel.py index dc0120fb8a..89d38cf5e1 100644 --- a/python/tests/test_lowlevel.py +++ b/python/tests/test_lowlevel.py @@ -4402,8 +4402,8 @@ def test_c_tsk_err_node_out_of_bounds(self, bad_node): def test_c_tsk_err_bad_windows(self): ts = self.example_ts() L = ts.get_sequence_length() - with pytest.raises(_tskit.LibraryError, match="BAD_WINDOWS"): - self.pair_coalescence_counts(ts, windows=[-1.0, L]) + with pytest.raises(_tskit.LibraryError, match="TSK_ERR_BAD_WINDOWS"): + self.pair_coalescence_counts(ts, windows=[1.0, L]) def test_c_tsk_err_bad_node_bin_map(self): ts = self.example_ts() @@ -4526,6 +4526,11 @@ def test_c_tsk_err_unsorted_times(self): with pytest.raises(_tskit.LibraryError, match="TSK_ERR_UNSORTED_TIMES"): self.pair_coalescence_quantiles(ts, node_bin_map=node_bin_map) + def test_c_tsk_err_bad_windows(self): + ts = self.example_ts() + with pytest.raises(_tskit.LibraryError, match="TSK_ERR_BAD_WINDOWS"): + self.pair_coalescence_quantiles(ts, windows=[1.0, ts.get_sequence_length()]) + @pytest.mark.parametrize("bad_ss_size", [-1, 1000]) def test_cpy_bad_sample_sets(self, bad_ss_size): ts = self.example_ts() @@ -4654,6 +4659,11 @@ def test_c_tsk_err_bad_sample_pair_times(self): with pytest.raises(_tskit.LibraryError, match="TSK_ERR_BAD_SAMPLE_PAIR_TIMES"): self.pair_coalescence_rates(ts, time_windows=np.array([-1.0, np.inf])) + def test_c_tsk_err_bad_windows(self): + ts = self.example_ts() + with pytest.raises(_tskit.LibraryError, match="TSK_ERR_BAD_WINDOWS"): + self.pair_coalescence_rates(ts, windows=[1.0, ts.get_sequence_length()]) + @pytest.mark.parametrize("bad_ss_size", [-1, 1000]) def test_cpy_bad_sample_sets(self, bad_ss_size): ts = self.example_ts() @@ -4665,11 +4675,6 @@ def test_cpy_bad_sample_sets(self, bad_ss_size): ts, sample_set_sizes=[bad_ss_size, ts.get_num_samples()] ) - def test_cpy_bad_windows(self): - ts = self.example_ts() - with pytest.raises(ValueError, match="at least 2"): - self.pair_coalescence_rates(ts, windows=[0.0]) - @pytest.mark.parametrize("indexes", [[(0, 0, 0)], np.zeros((0, 2), dtype=np.int32)]) def test_cpy_bad_indexes(self, indexes): ts = self.example_ts()