def test_change_migration_rates_simple_two_deme_migration_bad_matrix(self): """ For a 2-deme model, the mig matrix is [0, 1 1, 0] so that all offspring have both parents from the other deme, which gives us an easy check on how many migration events will be recorded by the test simulation. After 3 generations, we reset the migration rates to be [[1,0], [1,0]], which leads to there being no parents for deme 1, raising a MigrationError exception. """ mm = np.array([0, 1, 1, 0]).reshape(2, 2) mmigs = [fwdpy11.move_individuals(0, 0, 1, 0.5)] smr = [ fwdpy11.SetMigrationRates(3, np.array([1, 0, 1, 0]).reshape(2, 2)) ] d = fwdpy11.DiscreteDemography(mass_migrations=mmigs, migmatrix=mm, set_migration_rates=smr) with self.assertRaises(fwdpy11.MigrationError): ddr.DiscreteDemography_roundtrip(self.rng, self.pop, d, 5)
def test_change_migration_rates_simple_two_deme_migration(self): """ For a 2-deme model, the mig matrix is [[0, 1] [1, 0]] so that all offspring have both parents from the other deme, which gives us an easy check on how many migration events will be recorded by the test simulation. After 3 generations, we reset the migration rates to be [[0.5, 0.5], [[0, 0]] so that all parents are from deme zero. """ mm = np.array([0, 1, 1, 0]).reshape(2, 2) mmigs = [fwdpy11.move_individuals(0, 0, 1, 0.5)] smr = [ fwdpy11.SetMigrationRates(3, np.array([0.5, 0.5, 0, 0]).reshape(2, 2)) ] d = fwdpy11.DiscreteDemography(mass_migrations=mmigs, migmatrix=mm, set_migration_rates=smr) N = self.pop.N migevents = ddr.DiscreteDemography_roundtrip(self.rng, self.pop, d, 5) self.assertEqual(N, self.pop.N) self.assertEqual(len(migevents), 2 * N * 3 + 2 * N) md = np.array(self.pop.diploid_metadata, copy=False) deme_sizes = np.unique(md['deme'], return_counts=True) for i in deme_sizes[1]: self.assertEqual(i, self.pop.N // 2)
def test_pickle(self): m = fwdpy11.SetMigrationRates(0, 0, np.array([0, 1, 2])) p = pickle.dumps(m, -1) up = pickle.loads(p) self.assertEqual(m.when, up.when) self.assertEqual(m.deme, up.deme) self.assertEqual(m.migrates.tolist(), up.migrates.tolist())
def test_migrration_rates_larger_than_one(self): """ Same as a previous tests, but rates are "weights" rather than "probabilities" """ mm = np.array([0, 2, 2, 0]).reshape(2, 2) mmigs = [fwdpy11.move_individuals(0, 0, 1, 0.5)] smr = [ fwdpy11.SetMigrationRates(3, np.array([1.5, 1.5, 0, 0]).reshape(2, 2)) ] d = fwdpy11.DiscreteDemography(mass_migrations=mmigs, migmatrix=mm, set_migration_rates=smr) N = self.pop.N migevents = ddr.DiscreteDemography_roundtrip(self.rng, self.pop, d, 5) self.assertEqual(N, self.pop.N) self.assertEqual(len(migevents), 2 * N * 3 + 2 * N) md = np.array(self.pop.diploid_metadata, copy=False) deme_sizes = np.unique(md['deme'], return_counts=True) for i in deme_sizes[1]: self.assertEqual(i, self.pop.N // 2)
def test_reset_entire_matrix(self): m = fwdpy11.SetMigrationRates(3, np.identity(3)) self.assertTrue(np.array_equal(m.migrates, np.identity(3)))
def test_init_from_numpy(self): m = fwdpy11.SetMigrationRates(0, 0, np.array([0, 1, 2])) self.assertEqual(m.when, 0) self.assertEqual(m.deme, 0) self.assertTrue(np.array_equal(m.migrates, np.array([0, 1, 2])))