|
19 | 19 | """ |
20 | 20 | Test cases for demographic events in msprime. |
21 | 21 | """ |
| 22 | +import importlib |
22 | 23 | import io |
23 | 24 | import itertools |
24 | 25 | import json |
@@ -4292,6 +4293,35 @@ def test_duplicate_population_name(self): |
4292 | 4293 | [msprime.Population(10, name="A"), msprime.Population(11, name="A")] |
4293 | 4294 | ) |
4294 | 4295 |
|
| 4296 | + def test_init_with_pops(self): |
| 4297 | + pop1 = msprime.Population(10) |
| 4298 | + pop2 = msprime.Population(10) |
| 4299 | + demography = msprime.Demography([pop1, pop2]) |
| 4300 | + assert len(demography.populations) == 2 |
| 4301 | + assert demography.populations[0].id == 0 |
| 4302 | + assert demography.populations[1].id == 1 |
| 4303 | + |
| 4304 | + def test_init_with_matrix(self): |
| 4305 | + pop1 = msprime.Population(10) |
| 4306 | + pop2 = msprime.Population(10) |
| 4307 | + # Test passing in as a list of lists, not a numpy array |
| 4308 | + demography = msprime.Demography([pop1, pop2], migration_matrix=[[1, 1], [1, 1]]) |
| 4309 | + assert demography.migration_matrix.shape == (2, 2) |
| 4310 | + assert np.all(demography.migration_matrix == 1) |
| 4311 | + |
| 4312 | + def test_init_with_events(self): |
| 4313 | + pop = msprime.Population(10) |
| 4314 | + event = msprime.PopulationParametersChange(1, initial_size=1) |
| 4315 | + demography = msprime.Demography([pop], events=[event]) |
| 4316 | + assert len(demography.events) == 1 |
| 4317 | + assert np.all(demography.migration_matrix == 0) |
| 4318 | + assert demography.events[0].demography == demography |
| 4319 | + |
| 4320 | + def test_bad_init_with_events(self): |
| 4321 | + pop = msprime.Population(10) |
| 4322 | + with pytest.raises(TypeError, match="instances of DemographicEvent"): |
| 4323 | + msprime.Demography([pop], events=[None]) |
| 4324 | + |
4295 | 4325 | def test_duplicate_populations(self): |
4296 | 4326 | pop = msprime.Population(10) |
4297 | 4327 | with pytest.raises(ValueError, match="must be distinct"): |
@@ -4562,6 +4592,45 @@ def test_validate_resolves_defaults(self): |
4562 | 4592 | assert validated["B"].initially_active |
4563 | 4593 | assert not validated["C"].initially_active |
4564 | 4594 |
|
| 4595 | + def test_population_asdict(self): |
| 4596 | + # Test that we can instantiate the components of a demography object |
| 4597 | + demography = msprime.Demography() |
| 4598 | + demography.add_population( |
| 4599 | + name="A", |
| 4600 | + initial_size=1234, |
| 4601 | + growth_rate=0.234, |
| 4602 | + description="ASDF", |
| 4603 | + extra_metadata={"a": "B", "c": 1234}, |
| 4604 | + default_sampling_time=0.2, |
| 4605 | + initially_active=True, |
| 4606 | + ) |
| 4607 | + popdict = demography.populations[0].asdict() |
| 4608 | + module, classname = popdict.pop("__class__").rsplit(".", 1) |
| 4609 | + popdict.pop("id", None) # can't create a population with ID set |
| 4610 | + cls = getattr(importlib.import_module(module), classname) |
| 4611 | + obj = cls(**popdict) |
| 4612 | + assert isinstance(obj, msprime.demography.Population) |
| 4613 | + assert obj.name == "A" |
| 4614 | + assert obj.initial_size == 1234 |
| 4615 | + |
| 4616 | + def test_event_asdict(self): |
| 4617 | + demography = msprime.Demography( |
| 4618 | + [msprime.Population(1234), msprime.Population(4321)], |
| 4619 | + events=[ |
| 4620 | + msprime.PopulationParametersChange(2, initial_size=5, population_id=1) |
| 4621 | + ], |
| 4622 | + ) |
| 4623 | + assert len(demography.events) == 1 |
| 4624 | + eventdict = demography.events[0].asdict() |
| 4625 | + assert "population_id" not in eventdict # deprecated param |
| 4626 | + assert eventdict["population"] == 1 |
| 4627 | + module, classname = eventdict.pop("__class__").rsplit(".", 1) |
| 4628 | + cls = getattr(importlib.import_module(module), classname) |
| 4629 | + obj = cls(**eventdict) |
| 4630 | + assert isinstance(obj, msprime.demography.PopulationParametersChange) |
| 4631 | + assert obj.time == 2 |
| 4632 | + assert obj.initial_size == 5 |
| 4633 | + |
4565 | 4634 |
|
4566 | 4635 | class TestDemographyCopy: |
4567 | 4636 | def test_empty(self): |
|
0 commit comments