def test_operation_eq(self): a = PopulationSizeVariable("a") b = PopulationSizeVariable("b") comb1 = operation_creation(Addition, a, b) comb2 = operation_creation(Addition, b, a) self.assertEqual(comb1, comb1) self.assertEqual(comb2, comb2) self.assertEqual(comb1, comb2) comb2 = operation_creation(Addition, a, b) self.assertEqual(comb1, comb2) c = TimeVariable("c") comb1 = Addition(c, Multiplication(a, b)) comb2 = Addition(Multiplication(b, a), c) self.assertEqual(comb1, comb2) self.assertNotEqual(Division(a, b), Division(b, a)) self.assertNotEqual(Subtraction(a, b), Subtraction(b, a)) self.assertEqual(Addition(a, b), Addition(b, a)) self.assertEqual(Multiplication(a, b), Multiplication(b, a)) self.assertEqual(Exp(a), Exp(a)) self.assertNotEqual(Exp(b), Exp(a)) self.assertNotEqual(Log(b), Exp(b)) self.assertNotEqual(Log(b), Multiplication(a, b)) self.assertEqual(Exp(Multiplication(a, b)), Exp(Multiplication(b, a)))
def test_translation_from_epoch_to_tree3(self): N_a, nu1B, nu1, nu1F, nu2B, nu2F, t1, t2, t3 = self.get_genetic_variables_model3( ) var2values = { 'N_a': 1e5, 'nu1B': 0.4, 'nu1': 0.8, 'nu1F': 0.1, 'nu2B': 0.2, 'nu2F': 0.5, 't1': 2, 't2': 4, 't3': 5 } cm = TreeDemographicModel(gen_time=29, mutation_rate=1.25e-8) cm.add_leaf(0, size_pop=nu1F) cm.add_leaf(1, size_pop=nu2F) cm.change_pop_size(0, t=t1, size_pop=nu1) cm.change_pop_size(0, t=t2, size_pop=nu1B) cm.change_pop_size(1, t=t1, size_pop=nu2B) cm.move_lineages(1, 0, t=t3, size_pop=N_a) em = EpochDemographicModel(gen_time=29, Nanc_size=N_a, mutation_rate=1.25e-8) em.add_split(0, [nu1B, nu2B]) em.add_epoch(operation_creation(Subtraction, t3, t2), [nu1B, nu2B]) em.add_epoch(operation_creation(Subtraction, t2, t1), [nu1, nu2B]) em.add_epoch(operation_creation(Subtraction, t1, 0), [nu1F, nu2F]) translated_model, _ = em.translate_to(TreeDemographicModel, var2values) self.assertTrue(translated_model.equals(cm, var2values))
def test_creation_log(self): a = PopulationSizeVariable("a") b = PopulationSizeVariable("b") l1 = operation_creation(Log, a) self.assertIsInstance(l1, Log) exp_b = Exp(b) self.assertEqual(operation_creation(Log, exp_b), b) self.assertEqual(operation_creation(Log, 1), 0) self.assertRaises(ValueError, operation_creation, Log, 0, 0)
def test_creation_exp(self): a = PopulationSizeVariable("a") b = PopulationSizeVariable("b") e1 = operation_creation(Exp, a) self.assertIsInstance(e1, Exp) log_b = Log(b) self.assertEqual(operation_creation(Exp, log_b), b) self.assertEqual(operation_creation(Exp, 0), 1) self.assertRaises(ValueError, operation_creation, Exp, 0, 0)
def test_creation_division(self): a = PopulationSizeVariable("a") b = PopulationSizeVariable("b") self.assertEqual(operation_creation(Division, 8, 2), 4) self.assertEqual(operation_creation(Division, a, 1), a) self.assertRaises(ValueError, operation_creation, Division, a, 0) self.assertEqual(operation_creation(Division, b, b), 1) self.assertRaises(ValueError, operation_creation, Division, a)
def test_translation_from_epoch_to_tree1(self): from .test_tree_dem_model import TestTreeDemModel N_a, nu1, nu2, nu2F, t1, t2 = TestTreeDemModel.get_genetic_variables_model1( ) var2values = { 'nu1': 0.4, 'nu2F': 0.7, 'nu2': 0.5, 'N_a': 1e5, 't1': 1, 't2': 5 } em = EpochDemographicModel(gen_time=29, Nanc_size=N_a, mutation_rate=1.25e-8) em.add_split(0, [nu1, nu2]) em.add_epoch(operation_creation(Subtraction, t2, t1), [nu1, nu2]) em.add_epoch(operation_creation(Subtraction, t1, 0), [nu1, nu2F]) cm = TreeDemographicModel(gen_time=29, mutation_rate=1.25e-8) cm.add_leaf(0, size_pop=nu1) cm.add_leaf(1, size_pop=nu2F) cm.change_pop_size(1, t=t1, size_pop=nu2) cm.move_lineages(1, 0, t=t2, size_pop=N_a) translated_model, _ = em.translate_to(TreeDemographicModel, var2values) self.assertTrue(translated_model.equals(cm, var2values)) # fails self.assertRaises(ValueError, EpochDemographicModel.create_from, cm) self.assertRaises(ValueError, TreeDemographicModel.create_from, cm.events[0], None) tm = TreeDemographicModel() self.assertRaises(ValueError, tm.translate_to, EpochDemographicModel, []) self.assertRaises(ValueError, tm.translate_to, Epoch, [])
def test_creation_add(self): a = PopulationSizeVariable("a") b = PopulationSizeVariable("b") self.assertEqual(operation_creation(Addition, 0, 1), 1) self.assertEqual(operation_creation(Addition, a, 0), a) self.assertEqual(operation_creation(Addition, 0, b), b) self.assertIsInstance(operation_creation(Addition, a, 1), Addition) self.assertIsInstance(operation_creation(Addition, 1, b), Addition) self.assertIsInstance(operation_creation(Addition, a, b), Addition) self.assertRaises(ValueError, operation_creation, Addition, 0)
def test_creation_subtract(self): a = PopulationSizeVariable("a") b = PopulationSizeVariable("b") self.assertEqual(operation_creation(Subtraction, 1, 0), 1) self.assertEqual(operation_creation(Subtraction, a, 0), a) # (1 + a * b) - ((b * a) + 1) = 0 self.assertEqual( operation_creation( Subtraction, operation_creation(Addition, 1, operation_creation(Multiplication, a, b)), operation_creation(Addition, operation_creation(Multiplication, b, a), 1)), 0) self.assertRaises(ValueError, operation_creation, Subtraction, 0)
def test_creation_multiplication(self): a = PopulationSizeVariable("a") b = PopulationSizeVariable("b") self.assertEqual(operation_creation(Multiplication, 2, 4), 8) self.assertEqual(operation_creation(Multiplication, 1, a), a) self.assertEqual(operation_creation(Multiplication, a, 1), a) self.assertEqual(operation_creation(Multiplication, 0, b), 0) self.assertEqual(operation_creation(Multiplication, b, 0), 0) self.assertIsInstance(operation_creation(Multiplication, a, b), Multiplication) self.assertRaises(ValueError, operation_creation, Multiplication, a)