Skip to content

Commit 0e84471

Browse files
hyanwongjeromekelleher
authored andcommitted
Work around to allow Demography init from provenance with preset population IDs
1 parent 82c4161 commit 0e84471

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Support for simulations after local mrca,
1414
({issue}`2157`, {pr}`2396`, {user}`jeromekelleher`, {user}`hossam26644`).
1515
- Add wheels on Windows ({pr}`2414`, {issue}`2200`,{user}`benjeffery`)
16+
- Demography objects can now be created from provenance entries ({pr}`{2369}`, {user}`hyanwong`)
1617

1718
**Performance improvements**
1819

msprime/demography.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ def __post_init__(self):
292292

293293
# Assign the IDs and default names, if needed.
294294
for j, population in enumerate(self.populations):
295-
if population.id is not None:
295+
if population.id is not None and population.id != j:
296296
raise ValueError(
297-
"Population ID should not be set before using to create "
298-
"a Demography"
297+
"Population ID should be unset or set to its index within"
298+
f" the Demography (expected None or {j}, got {population.id})"
299299
)
300300
population.id = j
301301
if population.name is None:

tests/test_demography.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4312,9 +4312,11 @@ def test_duplicate_populations(self):
43124312
with pytest.raises(ValueError, match="must be distinct"):
43134313
msprime.Demography([pop] * 2)
43144314

4315-
def test_population_ids_set_on_init(self):
4316-
pop = msprime.Population(10, id=0)
4317-
with pytest.raises(ValueError, match="ID should not be set"):
4315+
def test_population_ids_bad_on_init(self):
4316+
pop = msprime.Population(10, id=1)
4317+
with pytest.raises(
4318+
ValueError, match="ID should be unset or set to its index.*got 1"
4319+
):
43184320
msprime.Demography([pop])
43194321

43204322
def test_add_population_error(self):

tests/test_provenance.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,39 @@ def test_current_ts(self):
244244
assert command == "sim_mutations"
245245
assert prov["tree_sequence"] == ts1
246246

247+
def test_demography(self):
248+
demography = msprime.Demography.island_model([1, 1], 1 / 3)
249+
ts = msprime.sim_ancestry(
250+
demography=demography,
251+
samples=[
252+
msprime.SampleSet(1, population=0),
253+
msprime.SampleSet(1, population=1),
254+
],
255+
random_seed=3,
256+
)
257+
command, prov = msprime.provenance.parse_provenance(ts.provenance(-1), ts)
258+
assert command == "sim_ancestry"
259+
assert prov["demography"] == demography
260+
261+
def test_bad_demography(self):
262+
demography = msprime.Demography.island_model([1, 1], 1 / 3)
263+
ts = msprime.sim_ancestry(
264+
demography=demography,
265+
samples=[
266+
msprime.SampleSet(1, population=0),
267+
msprime.SampleSet(1, population=1),
268+
],
269+
random_seed=3,
270+
)
271+
prov = ts.provenance(-1)
272+
record = json.loads(prov.record)
273+
# Corrupt the provenance record
274+
assert len(record["parameters"]["demography"]["migration_matrix"]) == 2
275+
record["parameters"]["demography"]["migration_matrix"] = [1, 0, 0]
276+
prov.record = json.dumps(record)
277+
with pytest.raises(ValueError, match="Migration matrix must be square"):
278+
msprime.provenance.parse_provenance(prov, ts)
279+
247280

248281
class TestRoundTrip:
249282
"""

0 commit comments

Comments
 (0)