def test_get_variables_function(self): def check(variables): self.assertIsInstance(variables[0], PopulationSizeVariable) self.assertIsInstance(variables[1], PopulationSizeVariable) self.assertIsInstance(variables[2], PopulationSizeVariable) self.assertIsInstance(variables[3], MigrationVariable) self.assertEqual(variables[3].units, "genetic") self.assertIsInstance(variables[4], TimeVariable) self.assertEqual(variables[4].units, "physical") self.assertIsInstance(variables[5], FractionVariable) self.assertIsInstance(variables[6], GrowthRateVariable) self.assertEqual(variables[6].units, "universal") # TODO it is not correct behavior self.assertIsInstance(variables[7], SelectionVariable) p_ids = ['nu1', 'nu2', 'n', 'm_gen', 't_phys', 'p', 'g_1', 'gamma'] lower_bound = [1e-2, 1e-2, 1e-5, 0, 0, 0, -1e-3, 0] upper_bound = [10, 1, 3, 4, 10000, 1, 1e-3, 5] variables = get_variables(p_ids, lower_bound, upper_bound) check(variables) for var, lb, ub in zip(variables, lower_bound, upper_bound): self.assertEqual(var.domain[0], lb) self.assertEqual(var.domain[1], ub) variables = get_variables(p_ids, None, None) check(variables) for var in variables: if var.units != "physical": self.assertEqual(list(var.domain), list(var.__class__.default_domain)) else: self.assertEqual(var.name, "t_phys") self.assertNotEqual(list(var.domain), list(var.__class__.default_domain)) variables = get_variables(None, lower_bound, upper_bound) for var, lb, ub in zip(variables, lower_bound, upper_bound): self.assertIsInstance(var, ContinuousVariable) self.assertEqual(var.domain[0], lb) self.assertEqual(var.domain[1], ub) self.assertRaises(AssertionError, get_variables, None, None, None) self.assertRaises(AssertionError, get_variables, p_ids, None, upper_bound) self.assertRaises(AssertionError, get_variables, p_ids, lower_bound, None)
def test_get_variables_function(self): def check(variables): self.assertIsInstance(variables[0], PopulationSizeVariable) self.assertIsInstance(variables[1], PopulationSizeVariable) self.assertIsInstance(variables[2], PopulationSizeVariable) self.assertIsInstance(variables[3], MigrationVariable) self.assertIsInstance(variables[4], TimeVariable) self.assertIsInstance(variables[5], FractionVariable) p_ids = ['nu1', 'nu2', 'n', 'm', 't', 'p'] lower_bound = [1e-2, 1e-2, 1e-5, 0, 0, 0] upper_bound = [10, 1, 3, 4, 6, 1] variables = get_variables(p_ids, lower_bound, upper_bound) check(variables) for var, lb, ub in zip(variables, lower_bound, upper_bound): self.assertEqual(var.domain[0], lb) self.assertEqual(var.domain[1], ub) variables = get_variables(p_ids, None, None) check(variables) for var in variables: self.assertEqual(list(var.domain), list(var.__class__.default_domain)) variables = get_variables(None, lower_bound, upper_bound) for var, lb, ub in zip(variables, lower_bound, upper_bound): self.assertIsInstance(var, ContinuousVariable) self.assertEqual(var.domain[0], lb) self.assertEqual(var.domain[1], ub) self.assertRaises(AssertionError, get_variables, None, None, None) self.assertRaises(AssertionError, get_variables, p_ids, None, upper_bound) self.assertRaises(AssertionError, get_variables, p_ids, lower_bound, None)
def test_code_generation(self): # old format warnings.filterwarnings(action='ignore', category=UserWarning, module='.*\.dadi_moments_common', lineno=350) # missed lines in vcf warnings.filterwarnings(action='ignore', category=UserWarning, module='.*\.dadi_moments_common', lineno=693) warnings.filterwarnings(action='ignore', category=UserWarning, module='.*\.dadi_moments_common', lineno=703) # repeats in vcf file warnings.filterwarnings(action='ignore', category=UserWarning, module='.*\.dadi_moments_common', lineno=710) # Theta0 is not set and translation of Nanc variable with theta0=1 could be wrong warnings.filterwarnings(action='ignore', category=UserWarning, module='.*\.dadi_moments_common', lineno=237) warnings.filterwarnings(action='ignore', category=UserWarning, module='.*\.momi_engine', lineno=89) nu1 = PopulationSizeVariable('nu1') nu2 = PopulationSizeVariable('nu2') nu3 = PopulationSizeVariable('nu3') t = TimeVariable('t1') t2 = TimeVariable('t2') m = MigrationVariable('m') s = SelectionVariable('s') d1 = DynamicVariable('d1') d2 = DynamicVariable('d2') f = FractionVariable('f') h = FractionVariable('h') f1 = FractionVariable('f1') f2 = FractionVariable('f2') fxnu1 = Multiplication(f, nu1) tf = Multiplication(f, t) t_copy = copy.deepcopy(t) model1 = EpochDemographicModel() model1.add_epoch(t, [nu1]) model1.add_split(0, [nu1, nu2]) model1.add_epoch(t, [nu2, fxnu1], [[0, m], [0, 0]], [d1, d2]) model1.add_split(1, [nu2, nu1]) model1.add_epoch(t, [nu1, nu2, nu1], None, None) model2 = EpochDemographicModel() model2.add_epoch(t, [nu1]) model2.add_split(0, [nu1, nu2]) model2.add_epoch(0.5, [nu2, fxnu1], [[0, m], [0, 0]], [d1, d2], [0, s]) model2.add_split(1, [nu2, nu1]) model2.add_epoch(t, [nu1, nu2, nu1], None, None, [s, 0, s]) model2.get_involved_for_split_time_vars(1) model3 = EpochDemographicModel() model3.add_epoch(t, [nu1]) model3.add_split(0, [nu1, nu2]) model3.add_epoch(tf, [nu2, fxnu1], [[0, m], [0, 0]], [d1, d2], [0, s], [0.1, 0.8]) model3.add_split(1, [nu2, nu1]) model3.add_epoch(t, [nu1, nu2, nu1], None, [d1, 'Sud', 'Sud'], [s, 0, s], [h, 0.5, 0]) # create models with ancestral size Nanc = PopulationSizeVariable("Nanc", units="physical") Nu2 = PopulationSizeVariable("N2", units="physical") T3 = TimeVariable("T3", units="physical") model4 = EpochDemographicModel(Nanc_size=Nanc) model4.add_epoch(t, [nu1]) model4.add_split(0, [nu1, nu2]) model4.add_epoch(t, [nu2, fxnu1], [[0, m], [0, 0]], [d1, d2]) model5 = EpochDemographicModel(Nanc_size=Nanc) model5.add_epoch(t, [nu1]) model5.add_split(0, [nu1, nu2]) model5.add_epoch(T3, [0.5, fxnu1], [[0, m], [0, 0]], ["Sud", d2]) model5.add_split(1, [nu2, nu1]) model5.add_epoch(t, [nu1, nu2, nu1], None, None) model6 = EpochDemographicModel(Nanc_size=Nanc) model6.add_epoch(t, [nu1]) model6.add_split(0, [nu1, nu2]) model6.add_epoch(T3, [Nu2, fxnu1], [[0, m], [0, 0]], [d1, d2]) model6.add_split(1, [nu2, nu1]) model6.add_epoch(t, [nu1, nu2, nu1], None, None) model6.add_inbreeding([f1, f2, 0.5]) model_struct_1 = StructureDemographicModel( initial_structure=[2, 1, 1], final_structure=[2, 1, 1], has_migs=True, has_sels=False, has_dyns=True, sym_migs=True, frac_split=True, ) model_struct_2 = StructureDemographicModel( initial_structure=[2, 1, 1], final_structure=[2, 1, 1], has_anc_size=True, has_migs=True, has_sels=False, has_dyns=True, sym_migs=True, frac_split=True, ) values = { nu1: 2, nu2: 0.5, nu3: 0.5, t_copy: 0.3, t2: 0.5, m: 0.1, s: 0.1, d1: 'Exp', d2: 'Lin', f: 0.5, h: 0.3, f1: 0.1, f2: 0.3, Nanc: 10000, Nu2: 5000, T3: 4000, 'nu1F': 1.0, 'nu2B': 0.7, 'nu2F': 1.0, 'T': 0.5, 'Tp': 0.3, 'N_1F': 20000, 'r_2': -1e-5, 'N_2F': 5000, 'T_1': 500, 'T_2': 100 } for engine in all_available_engines(): #print("!!!", engine) models = [model1, model2, model3, model5, model6] if engine.id == "momentsLD": # momentsLD has problems with model 5 and 6 because of physical values models = models[:3] if engine.can_evaluate: customfile = os.path.join( EXAMPLE_FOLDER, "MODELS", f"demographic_model_{engine.id}_3pops.py") spec = importlib.util.spec_from_file_location( f"module_{engine.id}", customfile) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) sys.modules[f'module_{engine.id}'] = module func = module.model_func par_labels = get_par_labels_from_file(customfile) variables = get_variables(par_labels, None, None, engine=engine.id) model7 = CustomDemographicModel(func, variables) if engine.id == "momentsLD": model8 = copy.deepcopy(model7) model7.fixed_anc_size = 10000 model8.has_anc_size = True model8.add_variable( PopulationSizeVariable("Nanc", units="physical")) model8.fix_variable(model8.variables[-1], 10000) model8.unfix_variable(model8._variables[-1]) models.append(model8) models.append(model7) models.append(model_struct_1) models.append(model_struct_2) for ind, model in enumerate(models): if engine.id == "momentsLD": dataset = self._vcf_datasets_ld_precomputed() else: dataset = self._sfs_datasets() for description, data in dataset: msg = f"for model {ind + 1} and {description} data and " \ f"{engine.id} engine" #print(msg) if engine.id == "momi" and isinstance( model, StructureDemographicModel): if not model.has_anc_size: continue if engine.id == 'dadi': options = {'pts': [4, 6, 8]} args = (options['pts'], ) else: options = {} args = () if isinstance(model, StructureDemographicModel): input_values = { var.name: 0.5 + np.random.uniform(-0.2, 0.2) if not isinstance(var, DiscreteVariable) else "Sud" for var in model.variables } if model.has_anc_size: input_values[model.Nanc_size.name] = 10000 else: input_values = copy.copy(values) if engine.id in ["momi", "momentsLD"]: # there is an error in momi for that case # data for moments was simulated with msprime, which can't work with Lin DynVar if description == "fs without pop labels": continue for key in input_values: if input_values[key] == "Lin": input_values[key] = "Exp" data.sequence_length = 50818468 # we read data but save only updated data_holder engine.data = data engine.inner_data = None engine.model = model Nanc = None if engine.id not in ["dadi", "moments"]: Nanc = 10000 new_model = copy.deepcopy(model) if new_model.has_anc_size and isinstance( new_model, StructureDemographicModel) and isinstance( new_model.Nanc_size, Variable): input_values[new_model.Nanc_size.name] = Nanc else: new_model.Nanc_size = Nanc new_model.mutation_rate = 1.25e-8 engine.model = new_model moments.LD.Inference._varcov_inv_cache = {} cmd = engine.generate_code(input_values, None, *args, nanc=Nanc) #print(cmd) if engine.can_evaluate: # read data engine.data = data true_ll = engine.evaluate(input_values, **options) d = {} exec(cmd, d) msg += f": {true_ll} != {d['ll_model']}" self.assertTrue(np.allclose(true_ll, d['ll_model']), msg=msg) if (description == "dadi snp file" and engine.id in ["dadi", "moments"]): engine.data_holder.population_labels = None self.assertRaises(ValueError, engine.generate_code, input_values, None, *args, nanc=Nanc) if description == "fs without pop labels": engine.data_holder.population_labels = None engine.generate_code(input_values, None, *args, nanc=Nanc)