Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions python/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
--------------------
[1.0.x] - YYYY-MM-DD
--------------------

**Bugfixes**

- ``ts.samples(population=...)`` now raises a ``ValueError`` if the population
ID is e.g. a population name, rather than silently returning no samples.
(:user:`hyanwong`, :pr:`3344`)

--------------------
[1.0.0] - 2025-11-27
--------------------
Expand Down
11 changes: 11 additions & 0 deletions python/tests/test_highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,17 @@ def test_samples(self):
]
assert total == ts.num_samples

@pytest.mark.parametrize("pop", ["string", "", "0", np.arange(2), 0.0, 0.5, np.nan])
def test_bad_samples(self, pop):
ts = tskit.Tree.generate_balanced(4).tree_sequence
with pytest.raises(ValueError, match="must be an integer ID"):
ts.samples(population=pop)

@pytest.mark.parametrize("pop", [0, np.int32(0), np.int64(0), np.uint32(0)])
def test_good_samples(self, pop):
ts = msprime.sim_ancestry(2)
assert np.array_equiv(ts.samples(population=pop), ts.samples())

@pytest.mark.parametrize("time", [0, 0.1, 1 / 3, 1 / 4, 5 / 7])
def test_samples_time(self, time):
ts = self.get_tree_sequence(num_demes=2, n=20, times=[time, 0.2, 1, 15])
Expand Down
3 changes: 3 additions & 0 deletions python/tskit/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -6533,6 +6533,9 @@ def samples(self, population=None, *, population_id=None, time=None):
samples = self._ll_tree_sequence.get_samples()
keep = np.full(shape=samples.shape, fill_value=True)
if population is not None:
if not isinstance(population, numbers.Integral):
raise ValueError("`population` must be an integer ID")
population = int(population)
sample_population = self.nodes_population[samples]
keep = np.logical_and(keep, sample_population == population)
if time is not None:
Expand Down