def test_single_model_list(self): model, events = ancestry._parse_model_arg([None]) assert model == msprime.StandardCoalescent() assert events == [] # Tuples are also accepted as input. model, events = ancestry._parse_model_arg((None, )) assert model == msprime.StandardCoalescent() assert events == [] model, events = ancestry._parse_model_arg(["hudson"]) assert model == msprime.StandardCoalescent() assert events == [] model, events = ancestry._parse_model_arg( [msprime.StandardCoalescent()]) assert model == msprime.StandardCoalescent() assert events == [] model, events = ancestry._parse_model_arg(["dtwf"]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [] model, events = ancestry._parse_model_arg( [msprime.DiscreteTimeWrightFisher()]) assert model == msprime.DiscreteTimeWrightFisher() assert events == []
def test_wf_hudson_back_and_forth(self): Ne = 100 t1 = 100 t2 = 200 ts = msprime.simulate( sample_size=10, model=msprime.DiscreteTimeWrightFisher(Ne), recombination_rate=0.1, demographic_events=[ msprime.SimulationModelChange(t1, msprime.StandardCoalescent(Ne)), msprime.SimulationModelChange( t2, msprime.DiscreteTimeWrightFisher(Ne)), ], random_seed=2, ) tree = ts.first() self.assertEqual(tree.num_roots, 1) times = ts.tables.nodes.time dtwf_times = times[np.logical_and(times > 0, times < t1, times > t2)] self.assertGreater(dtwf_times.shape[0], 0) self.assertTrue(np.all(dtwf_times == np.floor(dtwf_times))) coalescent_times = times[np.logical_and(times > t1, times < t2)] self.assertGreater(coalescent_times.shape[0], 0) self.assertTrue(np.all(coalescent_times != np.floor(coalescent_times)))
def test_one_event(self): expected_event = msprime.AncestryModelChange( time=1.33, model=msprime.StandardCoalescent()) model, events = ancestry._parse_model_arg(["dtwf", (1.33, "hudson")]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [expected_event] model, events = ancestry._parse_model_arg(["dtwf", (1.33, None)]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [expected_event] model, events = ancestry._parse_model_arg( ["dtwf", (1.33, msprime.StandardCoalescent())]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [expected_event] model, events = ancestry._parse_model_arg(["dtwf", expected_event]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [expected_event] # We should take a copy of the event. assert events[0] is not expected_event model, events = ancestry._parse_model_arg(["dtwf", (None, None)]) assert model == msprime.DiscreteTimeWrightFisher() assert events == [ msprime.AncestryModelChange(time=None, model=msprime.StandardCoalescent()) ]
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_one_event(self): expected_event = msprime.SimulationModelChange( time=1.33, model=msprime.StandardCoalescent()) model, events = msprime.parse_model_arg(["dtwf", (1.33, "hudson")]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [expected_event]) model, events = msprime.parse_model_arg(["dtwf", (1.33, None)]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [expected_event]) model, events = msprime.parse_model_arg( ["dtwf", (1.33, msprime.StandardCoalescent())]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [expected_event]) model, events = msprime.parse_model_arg(["dtwf", expected_event]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [expected_event]) # We should take a copy of the event. self.assertIsNot(events[0], expected_event) model, events = msprime.parse_model_arg(["dtwf", (None, None)]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual( events, [ msprime.SimulationModelChange( time=None, model=msprime.StandardCoalescent()) ], )
def test_single_model_list(self): model, events = msprime.parse_model_arg([None]) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) # Tuples are also accepted as input. model, events = msprime.parse_model_arg((None, )) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg(["hudson"]) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg([msprime.StandardCoalescent()]) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg(["dtwf"]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, []) model, events = msprime.parse_model_arg( [msprime.DiscreteTimeWrightFisher()]) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [])
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_single_model(self): models = ancestry._parse_model_arg("hudson") assert models == [msprime.StandardCoalescent()] models = ancestry._parse_model_arg(msprime.StandardCoalescent()) assert models == [msprime.StandardCoalescent()] models = ancestry._parse_model_arg("dtwf") assert models == [msprime.DiscreteTimeWrightFisher()] models = ancestry._parse_model_arg(msprime.DiscreteTimeWrightFisher()) assert models == [msprime.DiscreteTimeWrightFisher()]
def test_one_change(self): models = ancestry._parse_model_arg( [msprime.DiscreteTimeWrightFisher(duration=1.33), "hudson"]) assert models == [ msprime.DiscreteTimeWrightFisher(duration=1.33), msprime.StandardCoalescent(), ] models = ancestry._parse_model_arg( [msprime.DiscreteTimeWrightFisher(duration=1.33), None]) assert models == [ msprime.DiscreteTimeWrightFisher(duration=1.33), msprime.StandardCoalescent(), ]
def test_wf_hudson_different_specifications(self): Ne = 100 t = 100 ts1 = msprime.simulate( sample_size=10, model=msprime.DiscreteTimeWrightFisher(Ne), recombination_rate=0.1, demographic_events=[ msprime.SimulationModelChange(t, msprime.StandardCoalescent(Ne))], random_seed=2) ts2 = msprime.simulate( sample_size=10, recombination_rate=0.1, Ne=Ne, model="dtwf", demographic_events=[msprime.SimulationModelChange(t, "hudson")], random_seed=2) ts3 = msprime.simulate( sample_size=10, recombination_rate=0.1, Ne=Ne, model="dtwf", demographic_events=[msprime.SimulationModelChange(t)], random_seed=2) t1 = ts1.dump_tables() t2 = ts2.dump_tables() t3 = ts3.dump_tables() t1.provenances.clear() t2.provenances.clear() t3.provenances.clear() self.assertEqual(t1, t2) self.assertEqual(t1, t3)
def test_ped_wf_recombination(self): inds = np.array([1, 2, 3, 4, 5, 6]) parent_indices = np.array([4, 5, 4, 5, 4, 5, 4, 5, -1, -1, -1, -1]).reshape(-1, 2) times = np.array([0, 0, 0, 0, 1, 1]) is_sample = np.array([1, 1, 1, 1, 0, 0]) t = max(times) model = msprime.WrightFisherPedigree() ped = msprime.Pedigree(inds, parent_indices, times, is_sample, sex=None, ploidy=2) ts = msprime.simulate( sample_size=4, pedigree=ped, recombination_rate=0.1, demographic_events=[ msprime.SimulationModelChange( 1, msprime.DiscreteTimeWrightFisher(2)) ], model=model, ) tree = ts.first() self.assertEqual(tree.num_roots, 1) all_times = ts.tables.nodes.time ped_times = all_times[np.logical_and(all_times > 0, all_times <= t)] self.assertGreater(ped_times.shape[0], 0) self.assertTrue(np.all(ped_times == np.floor(ped_times))) wf_times = all_times[all_times > t] self.assertGreater(wf_times.shape[0], 0)
def test_wf_hudson_different_specifications(self): Ne = 100 t = 100 ts1 = msprime.sim_ancestry( samples=5, population_size=Ne, model=[msprime.DiscreteTimeWrightFisher(duration=t), "hudson"], recombination_rate=0.1, sequence_length=1, discrete_genome=False, random_seed=2, ) ts2 = msprime.simulate( sample_size=10, recombination_rate=0.1, Ne=Ne, model="dtwf", demographic_events=[msprime.SimulationModelChange(t, "hudson")], random_seed=2, ) ts3 = msprime.simulate( sample_size=10, recombination_rate=0.1, Ne=Ne, model="dtwf", demographic_events=[msprime.SimulationModelChange(t)], random_seed=2, ) # Not worth trying to puzzle out the slight differences in tables # between the old and new form. The edges are the same, good enough. assert ts1.tables.edges == ts2.tables.edges assert ts2.equals(ts3, ignore_provenance=True)
def test_pedigree_unsupported_events(self): inds = np.array([1, 2, 3, 4, 5, 6]) parent_indices = np.array([4, 5, 4, 5, 4, 5, 4, 5, -1, -1, -1, -1]).reshape(-1, 2) times = np.array([0, 0, 0, 0, 1, 1]) is_sample = np.array([1, 1, 1, 1, 0, 0]) t = max(times) ped = pedigrees.Pedigree(inds, parent_indices, times, is_sample, sex=None, ploidy=2) bad_model_change = msprime.SimulationModelChange( 0.5, msprime.DiscreteTimeWrightFisher()) with pytest.raises(RuntimeError, match="not support interruption"): msprime.simulate( 4, pedigree=ped, demographic_events=[bad_model_change], model="wf_ped", ) bad_demographic_event = msprime.PopulationParametersChange( t, initial_size=2) with pytest.raises(_msprime.LibraryError): msprime.simulate( 4, pedigree=ped, demographic_events=[bad_demographic_event], model="wf_ped", )
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_dtwf(self): for model in [msprime.DiscreteTimeWrightFisher(10)]: self.assertRaises(_msprime.LibraryError, msprime.simulate, 10, model=model, record_full_arg=True)
def test_pedigree_unsupported_events(self): inds = np.array([1, 2, 3, 4, 5, 6]) parent_indices = np.array([4, 5, 4, 5, 4, 5, 4, 5, -1, -1, -1, -1]).reshape(-1, 2) times = np.array([0, 0, 0, 0, 1, 1]) is_sample = np.array([1, 1, 1, 1, 0, 0]) t = max(times) ped = msprime.Pedigree(inds, parent_indices, times, is_sample, sex=None, ploidy=2) bad_model_change = msprime.SimulationModelChange( 0.5, msprime.DiscreteTimeWrightFisher()) self.assertRaises( NotImplementedError, msprime.simulate, 4, pedigree=ped, demographic_events=[bad_model_change], model="wf_ped", ) bad_demographic_event = msprime.PopulationParametersChange( t, initial_size=2) self.assertRaises( NotImplementedError, msprime.simulate, 4, pedigree=ped, demographic_events=[bad_demographic_event], model="wf_ped", )
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_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_single_model(self): model, events = msprime.parse_model_arg("hudson") self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg(msprime.StandardCoalescent()) self.assertEqual(model, msprime.StandardCoalescent()) self.assertEqual(events, []) model, events = msprime.parse_model_arg("dtwf") self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, []) model, events = msprime.parse_model_arg( msprime.DiscreteTimeWrightFisher()) self.assertEqual(model, msprime.DiscreteTimeWrightFisher()) self.assertEqual(events, [])
def test_dtwf(self): for model in [msprime.DiscreteTimeWrightFisher()]: with pytest.raises(_msprime.LibraryError): msprime.simulate( 10, model=model, record_full_arg=True, )
def sim_msprime_hybrid(sample_size, chrom_length_mb): return sim_msprime( sample_size, chrom_length_mb, model=[ msprime.DiscreteTimeWrightFisher(duration=100), msprime.StandardCoalescent(), ], )
def test_simulation_model_change(self): examples = [ msprime.SimulationModelChange(), msprime.SimulationModelChange(model="hudson"), msprime.SimulationModelChange( model=msprime.DiscreteTimeWrightFisher()), msprime.SimulationModelChange( model=msprime.BetaCoalescent(alpha=1, truncation_point=2)), ] self.assert_repr_round_trip(examples)
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_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_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_wf_hudson_single_locus(self): t = 10 ts = msprime.sim_ancestry( 10, population_size=100, model=[msprime.DiscreteTimeWrightFisher(duration=t), "hudson"], random_seed=3, ) tree = ts.first() assert tree.num_roots == 1 times = ts.tables.nodes.time dtwf_times = times[np.logical_and(times > 0, times < t)] assert dtwf_times.shape[0] > 0 assert np.all(dtwf_times == np.floor(dtwf_times)) coalescent_times = times[times > t] assert coalescent_times.shape[0] > 0
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_change_no_model_inherits_Ne(self): sim = msprime.simulator_factory( sample_size=2, Ne=1500, demographic_events=[ msprime.SimulationModelChange( 1, msprime.DiscreteTimeWrightFisher(500)), msprime.SimulationModelChange(2, None) ]) self.assertEqual(sim.model.reference_size, 1500) 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) self.assertEqual(sim.model_change_events[1].time, 2) self.assertEqual(sim.model_change_events[1].model.reference_size, 1500) self.assertEqual(sim.model_change_events[1].model.name, "hudson")