def test_two_events(self): expected_events = [ msprime.AncestryModelChange(time=1, model=msprime.StandardCoalescent()), msprime.AncestryModelChange(time=2, model=msprime.SmcApproxCoalescent()), ] model, events = ancestry._parse_model_arg( ["dtwf", (1, "hudson"), (2, "smc")]) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events model, events = ancestry._parse_model_arg( ["dtwf", (1, None), (2, msprime.SmcApproxCoalescent())]) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events model, events = ancestry._parse_model_arg( ["dtwf", expected_events[0], (2, msprime.SmcApproxCoalescent())]) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events model, events = ancestry._parse_model_arg( ["dtwf", expected_events[0], (2, msprime.SmcApproxCoalescent())]) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events assert events[0] is not expected_events[0] model, events = ancestry._parse_model_arg(["dtwf"] + expected_events) assert model == msprime.DiscreteTimeWrightFisher() assert events == expected_events assert events[0] is not expected_events[0] assert events[1] is not expected_events[1]
def test_two_events(self): expected_events = [ msprime.SimulationModelChange(time=1, model=msprime.StandardCoalescent()), msprime.SimulationModelChange(time=2, model=msprime.SmcApproxCoalescent()), ] model, events = msprime.parse_model_arg( ["dtwf", (1, "hudson"), (2, "smc")]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) model, events = msprime.parse_model_arg( ["dtwf", (1, None), (2, msprime.SmcApproxCoalescent())]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) model, events = msprime.parse_model_arg( ["dtwf", expected_events[0], (2, msprime.SmcApproxCoalescent())]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) model, events = msprime.parse_model_arg( ["dtwf", expected_events[0], (2, msprime.SmcApproxCoalescent())]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) self.assertIsNot(events[0], expected_events[0]) model, events = msprime.parse_model_arg(["dtwf"] + expected_events) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, expected_events) self.assertIsNot(events[0], expected_events[0]) self.assertIsNot(events[1], expected_events[1])
def test_two_changes(self): models = ancestry._parse_model_arg([ msprime.DiscreteTimeWrightFisher(duration=1), msprime.StandardCoalescent(duration=2), msprime.SmcApproxCoalescent(duration=3), ]) assert models == [ msprime.DiscreteTimeWrightFisher(duration=1), msprime.StandardCoalescent(duration=2), msprime.SmcApproxCoalescent(duration=3), ]
def test_many_models(self): Ne = 10000 ts = msprime.simulate( Ne=Ne, sample_size=10, recombination_rate=0.1, model=[ "hudson", msprime.SimulationModelChange(10, msprime.StandardCoalescent()), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent()), msprime.SimulationModelChange( 30, msprime.SmcPrimeApproxCoalescent()), msprime.SimulationModelChange( 40, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange( 50, msprime.BetaCoalescent(alpha=1.1, truncation_point=1), ), msprime.SimulationModelChange(60, msprime.StandardCoalescent()), ], random_seed=10, ) for tree in ts.trees(): self.assertEqual(tree.num_roots, 1)
def test_encode_simulation_models(self): models = [ msprime.StandardCoalescent(duration=10), msprime.DiscreteTimeWrightFisher(duration=10), msprime.SmcApproxCoalescent(duration=10), msprime.StandardCoalescent(), ] ts = msprime.sim_ancestry(10, model=models, random_seed=1234) decoded = self.decode(ts.provenance(0).record) parameters = decoded.parameters assert parameters.model[0] == { "__class__": "msprime.ancestry.StandardCoalescent", "duration": 10, } assert parameters.model[1] == { "__class__": "msprime.ancestry.DiscreteTimeWrightFisher", "duration": 10, } assert parameters.model[2] == { "__class__": "msprime.ancestry.SmcApproxCoalescent", "duration": 10, } assert parameters.model[3] == { "__class__": "msprime.ancestry.StandardCoalescent", "duration": None, }
def test_model_change_old_style(self): main_model = msprime.SmcApproxCoalescent() sim = msprime.simulator_factory( Ne=100, sample_size=2, model=main_model, demographic_events=[ msprime.SimulationModelChange( 1, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange(2, None), ], ) self.assertEqual(len(sim.model_change_events), 2) self.assertEqual(sim.model_change_events[0].time, 1) # When model=None we change to the standard coalescent self.assertEqual(sim.model_change_events[1].time, 2) self.assertEqual(sim.model_change_events[1].model.name, "hudson") # This should be the same in new notation sim = msprime.simulator_factory( Ne=100, sample_size=2, model=[main_model, (1, "dtwf"), (2, None)], ) self.assertEqual(len(sim.model_change_events), 2) self.assertEqual(sim.model_change_events[0].time, 1) # When model=None we change to the standard coalescent self.assertEqual(sim.model_change_events[1].time, 2) self.assertEqual(sim.model_change_events[1].model.name, "hudson")
def test_many_models_simulate(self): Ne = 10000 ts = msprime.simulate( Ne=Ne, sample_size=10, # Use the old-style SimulationModelChange model=[ "hudson", msprime.SimulationModelChange(10, msprime.StandardCoalescent()), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent()), msprime.SimulationModelChange( 30, msprime.SmcPrimeApproxCoalescent()), msprime.SimulationModelChange( 40, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange( 50, msprime.BetaCoalescent(alpha=1.1)), msprime.SimulationModelChange(60, msprime.StandardCoalescent()), ], random_seed=10, ) for tree in ts.trees(): assert tree.num_roots == 1
def test_many_models(self): # What happens when we have loads of models models = [ msprime.StandardCoalescent(duration=0.1), msprime.SmcApproxCoalescent(duration=0.1), ] ts = msprime.sim_ancestry(10, model=models * 1000, random_seed=2) assert all(tree.num_roots == 1 for tree in ts.trees())
def test_smc_models(self): model = msprime.SmcApproxCoalescent() repr_s = "SmcApproxCoalescent()" self.assertEqual(repr(model), repr_s) self.assertEqual(str(model), repr_s) model = msprime.SmcPrimeApproxCoalescent() repr_s = "SmcPrimeApproxCoalescent()" self.assertEqual(repr(model), repr_s) self.assertEqual(str(model), repr_s)
def test_smc_models(self): model = msprime.SmcApproxCoalescent() repr_s = "SmcApproxCoalescent()" assert repr(model) == repr_s assert str(model) == repr_s model = msprime.SmcPrimeApproxCoalescent() repr_s = "SmcPrimeApproxCoalescent()" assert repr(model) == repr_s assert str(model) == repr_s
def test_encode_simulation_models(self): simple_model = ["hudson", [10, "dtwf"], [20, "smc"], [None, None]] ts = msprime.simulate(10, model=simple_model) decoded = self.decode(ts.provenance(0).record) parameters = decoded.parameters self.assertEqual(parameters.sample_size, 10) self.assertEqual(list(parameters.model), simple_model) model_instances = [ msprime.StandardCoalescent(), msprime.SimulationModelChange(10, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent()), msprime.SimulationModelChange(30, msprime.BetaCoalescent(alpha=1.1)), ] ts = msprime.simulate(10, model=model_instances) decoded = self.decode(ts.provenance(0).record) parameters = decoded.parameters self.assertEqual(parameters.sample_size, 10) self.assertEqual(parameters.model[0], {"__class__": "msprime.ancestry.StandardCoalescent"}) self.assertDictEqual( parameters.model[1], { "__class__": "msprime.ancestry.SimulationModelChange", "model": { "__class__": "msprime.ancestry.DiscreteTimeWrightFisher" }, "time": 10, }, ) self.assertDictEqual( parameters.model[2], { "__class__": "msprime.ancestry.SimulationModelChange", "model": { "__class__": "msprime.ancestry.SmcApproxCoalescent" }, "time": 20, }, ) self.assertDictEqual( parameters.model[3], { "__class__": "msprime.ancestry.SimulationModelChange", "model": { "__class__": "msprime.ancestry.BetaCoalescent", "alpha": 1.1, "truncation_point": 1.0, }, "time": 30, }, )
def test_model_change_inherits_Ne(self): K = 10 sim = msprime.simulator_factory(sample_size=2, Ne=10, demographic_events=[ msprime.SimulationModelChange( None, msprime.SmcApproxCoalescent()) for _ in range(K) ]) self.assertEqual(sim.model.reference_size, 10) self.assertEqual(len(sim.model_change_events), K) for event in sim.model_change_events: self.assertEqual(event.model.reference_size, 10) self.assertEqual(event.time, None)
def test_simulation_models(self): simple_model = ["hudson", [10, "dtwf"], [20, "smc"]] ts = msprime.simulate(10, model=simple_model) self.verify(ts) model_instances = [ msprime.StandardCoalescent(), msprime.SimulationModelChange(10, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent()), msprime.SimulationModelChange(30, msprime.BetaCoalescent(alpha=1.1)), ] ts = msprime.simulate(10, model=model_instances) self.verify(ts)
def test_many_models_sim_ancestry(self): ts = msprime.sim_ancestry( samples=10, population_size=10_000, model=[ msprime.StandardCoalescent(duration=10), msprime.SmcApproxCoalescent(duration=10), msprime.SmcPrimeApproxCoalescent(duration=10), msprime.DiscreteTimeWrightFisher(duration=10), msprime.BetaCoalescent(alpha=1.1, duration=10), msprime.StandardCoalescent(), ], random_seed=10, ) for tree in ts.trees(): assert tree.num_roots == 1
def test_model_instances(self): models = [ msprime.StandardCoalescent(100), msprime.SmcApproxCoalescent(30), msprime.SmcPrimeApproxCoalescent(2132), msprime.DiscreteTimeWrightFisher(500), msprime.SweepGenicSelection( reference_size=500, position=0.5, start_frequency=0.1, end_frequency=0.9, alpha=0.1, dt=0.01), msprime.DiracCoalescent(), msprime.BetaCoalescent(), ] for model in models: new_model = msprime.model_factory(model=model) self.assertFalse(new_model is model) self.assertEqual(new_model.__dict__, model.__dict__)
def test_model_instances(self): for bad_type in [1234, {}]: self.assertRaises(TypeError, msprime.simulator_factory, sample_size=2, model=bad_type) models = [ msprime.StandardCoalescent(), msprime.SmcApproxCoalescent(), msprime.SmcPrimeApproxCoalescent(), msprime.BetaCoalescent(), msprime.DiracCoalescent(), ] for model in models: sim = msprime.simulator_factory(sample_size=10, model=model) self.assertEqual(sim.get_model(), model)
def test_many_models(self): Ne = 10000 ts = msprime.simulate( Ne=Ne, sample_size=10, recombination_rate=0.1, demographic_events=[ msprime.SimulationModelChange(10, msprime.StandardCoalescent(Ne)), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent(Ne)), msprime.SimulationModelChange(30, msprime.SmcPrimeApproxCoalescent(Ne)), msprime.SimulationModelChange( 40, msprime.DiscreteTimeWrightFisher(100)), msprime.SimulationModelChange( 50, msprime.BetaCoalescent(reference_size=10)), msprime.SimulationModelChange(60, msprime.StandardCoalescent(0.1))], random_seed=10) for tree in ts.trees(): self.assertEqual(tree.num_roots, 1)
def verify_simulation_models(self, sim_func): simple_model = ["hudson", [10, "dtwf"], [20, "smc"], [None, None]] ts = sim_func(10, model=simple_model) decoded = self.decode(ts.provenance(0).record) parameters = decoded.parameters assert list(parameters.model) == simple_model model_instances = [ msprime.StandardCoalescent(), msprime.SimulationModelChange(10, msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange(20, msprime.SmcApproxCoalescent()), msprime.SimulationModelChange( 30, msprime.BetaCoalescent(alpha=1.1, truncation_point=1)), ] ts = sim_func(10, model=model_instances) decoded = self.decode(ts.provenance(0).record) parameters = decoded.parameters assert parameters.model[0] == { "__class__": "msprime.ancestry.StandardCoalescent" } assert parameters.model[1] == { "__class__": "msprime.ancestry.SimulationModelChange", "model": { "__class__": "msprime.ancestry.DiscreteTimeWrightFisher" }, "time": 10, } assert parameters.model[2] == { "__class__": "msprime.ancestry.SimulationModelChange", "model": { "__class__": "msprime.ancestry.SmcApproxCoalescent" }, "time": 20, } assert parameters.model[3] == { "__class__": "msprime.ancestry.SimulationModelChange", "model": { "__class__": "msprime.ancestry.BetaCoalescent", "alpha": 1.1, "truncation_point": 1.0, }, "time": 30, }
def test_model_change_no_model_inherits_model_size(self): main_model = msprime.SmcApproxCoalescent(100) sim = msprime.simulator_factory( sample_size=2, model=main_model, demographic_events=[ msprime.SimulationModelChange( 1, msprime.DiscreteTimeWrightFisher(500)), msprime.SimulationModelChange(2, None) ]) self.assertEqual(sim.model.reference_size, 100) self.assertEqual(len(sim.model_change_events), 2) self.assertEqual(sim.model_change_events[0].time, 1) self.assertEqual(sim.model_change_events[0].model.reference_size, 500) # When model=None we change to the standard coalescent using the # reference size set by the initial model. self.assertEqual(sim.model_change_events[1].time, 2) self.assertEqual(sim.model_change_events[1].model.reference_size, 100) self.assertEqual(sim.model_change_events[1].model.name, "hudson")
def test_many_models_sim_ancestry(self): ts = msprime.sim_ancestry( samples=10, population_size=10_000, model=[ "hudson", msprime.AncestryModelChange(10, msprime.StandardCoalescent()), msprime.AncestryModelChange(20, msprime.SmcApproxCoalescent()), msprime.AncestryModelChange( 30, msprime.SmcPrimeApproxCoalescent()), msprime.AncestryModelChange( 40, msprime.DiscreteTimeWrightFisher()), msprime.AncestryModelChange(50, msprime.BetaCoalescent(alpha=1.1)), msprime.AncestryModelChange(60, msprime.StandardCoalescent()), ], random_seed=10, ) for tree in ts.trees(): assert tree.num_roots == 1
def test_simulation_models(self): examples = [ msprime.StandardCoalescent(), msprime.SmcApproxCoalescent(), msprime.SmcPrimeApproxCoalescent(), msprime.DiscreteTimeWrightFisher(), msprime.WrightFisherPedigree(), msprime.BetaCoalescent(), msprime.BetaCoalescent(alpha=1, truncation_point=10), msprime.DiracCoalescent(), msprime.DiracCoalescent(psi=1234, c=56), msprime.SweepGenicSelection( position=1, start_frequency=0.5, end_frequency=0.9, alpha=1, dt=1e-4, ), ] self.assert_repr_round_trip(examples)
def test_model_instances(self): models = [ msprime.StandardCoalescent(), msprime.SmcApproxCoalescent(), msprime.SmcPrimeApproxCoalescent(), msprime.DiscreteTimeWrightFisher(), msprime.WrightFisherPedigree(), msprime.SweepGenicSelection( position=0.5, start_frequency=0.1, end_frequency=0.9, alpha=0.1, dt=0.01, ), msprime.BetaCoalescent(alpha=2), msprime.DiracCoalescent(psi=1, c=1), ] for model in models: new_model = msprime.model_factory(model=model) self.assertTrue(new_model is model) self.assertEqual(new_model.__dict__, model.__dict__)
def test_reference_size_inherited(self): for Ne in [1, 10, 100]: models = [ msprime.DiracCoalescent(psi=0.5, c=0), msprime.StandardCoalescent(), msprime.SmcApproxCoalescent(), msprime.SmcPrimeApproxCoalescent(), msprime.DiscreteTimeWrightFisher(), msprime.WrightFisherPedigree(), msprime.SweepGenicSelection( position=0.5, start_frequency=0.1, end_frequency=0.9, alpha=0.1, dt=0.01, ), msprime.BetaCoalescent(alpha=2), msprime.DiracCoalescent(psi=1, c=1), ] for model in models: new_model = msprime.model_factory(model, reference_size=Ne) self.assertEqual(new_model.reference_size, Ne)