def setUp (self): sbml = SBML () sbml.load_file ('input/simple_enzymatic.xml') self.__model = sbml_to_odes (sbml) self.__experiments = ExperimentSet ('input/simple_enzymatic.data') self.__theta_priors = define_sbml_params_priors (sbml, 'input/simple_enzymatic.priors')
def run_task (model_file, priors_file, experiment_file, \ iterations_phase1, sigma_update_n, iterations_phase2, \ iterations_phase3, nof_process, signetms_path, seed=42): # pylint: disable=reimported import time start_time = time.time() # importing local modules... sys.path.insert(0, signetms_path) from model.SBML import SBML from experiment.ExperimentSet import ExperimentSet from model.SBMLtoODES import sbml_to_odes from marginal_likelihood.MarginalLikelihood \ import MarginalLikelihood from model.PriorsReader import define_sbml_params_priors import seed_manager # Now the actual code... seed_manager.set_seed(seed) sbml = SBML() sbml.load_file(model_file) odes = sbml_to_odes(sbml) experiments = ExperimentSet(experiment_file) theta_priors = define_sbml_params_priors(sbml, priors_file) ml = MarginalLikelihood (iterations_phase1, sigma_update_n, iterations_phase2, iterations_phase3, 20, 2, \ verbose=False, n_process=nof_process) log_l = ml.estimate_marginal_likelihood(experiments, odes, theta_priors) elapsed_time = time.time() - start_time return log_l, elapsed_time
def test_save_to_file(self): """ Tests if one can save a set of experiments to a file. """ times = [1, 2, 3, 4, 5] measure1 = 'ax + b' measure2 = 'e = mc ** 2' values1 = [123, 44, 3.2, 4, 30] values2 = [321, 44, 2.3, 4, 3] experiment1 = Experiment(times, values1, measure1) experiment2 = Experiment(times, values2, measure2) experiment_set = ExperimentSet() experiment_set.add(experiment1) experiment_set.add(experiment2) out_file = 'tmp_exp_set.xml' experiment_set.save_to_file(out_file) f = open(out_file, 'r') data = f.read() f.close() os.remove(out_file) measures = [measure1, measure2] values = values1 + values2 assert all([str(t) in data for t in times]) assert all([m in data for m in measures]) assert all([str(v) in data for v in values])
def test_write_then_read(self): """ Tests if one can save experiments to a file and then read. """ exp1 = Experiment([1, 2, 3, 4], [.1, .2, .3, .4], 'x1') exp2 = Experiment([1, 3, 9, 27], [.1, .2, .3, .4], 'x2') exp_set = ExperimentSet() exp_set.add(exp1) exp_set.add(exp2) out_file = 'tmp_exp_set_file.xml' exp_set.save_to_file(out_file) read_exp_set = ExperimentSet(out_file) self.assertEqual(read_exp_set.get_size(), 2) exp0 = read_exp_set[0] exp1 = read_exp_set[1] self.assertListEqual(list(exp0.times), [1, 2, 3, 4]) self.assertListEqual(list(exp0.values), [.1, .2, .3, .4]) self.assertEqual(exp0.measure_expression, 'x1') self.assertListEqual(list(exp1.times), [1, 3, 9, 27]) self.assertListEqual(list(exp1.values), [.1, .2, .3, .4]) self.assertEqual(exp1.measure_expression, 'x2') os.remove(out_file)
def perform_marginal_likelihood (sbml_file, priors_file, \ experiment_file, burnin1_iterations, sigma_update_n, \ burnin2_iterations, sampling_iterations, verbose=False, \ n_process=0, sample_output_file=None, seed=0): print ("Performing marginal likelihood calculations of model: " + \ sbml_file) sbml = SBML() sbml.load_file(sbml_file) odes = sbml_to_odes(sbml) experiments = ExperimentSet(experiment_file) theta_priors = define_sbml_params_priors(sbml, priors_file) seed_manager.set_seed(seed) ml = MarginalLikelihood (burnin1_iterations, sigma_update_n, burnin2_iterations, sampling_iterations, 20, 2, \ verbose=verbose, n_process=n_process) log_l = ml.estimate_marginal_likelihood(experiments, odes, theta_priors) ml.print_sample(output_file=sample_output_file) print("log_l = " + str(log_l)) return log_l
def test_read_multiple_measurements(self): """ Tests if the module can read a data experiment with multiple measurements. """ exp_set = ExperimentSet("input/goodwin3.data") self.assertEqual(exp_set[0].measure_expression, "x1") self.assertEqual(exp_set[1].measure_expression, "x2")
def add_noise (values): for i in range (len (values)): eps = np.random.normal (0, 3) if values[i] + eps > 0: values[i] += eps sbml = SBML () sbml.load_file ('final_model.sbml') odes = sbml_to_odes (sbml) time = [30, 60, 180, 300, 900, 1800] # Simple experiment: run final_model simulations adding a Gaussian noise values = odes.evaluate_exp_on ('MAPK_PP + MAPK_P', time) experiment_set = ExperimentSet () for i in range (3): noised_values = list (values) add_noise (noised_values) experiment = Experiment (time, noised_values, 'MAPK_PP + MAPK_P') experiment_set.add (experiment) experiment_set.save_to_file ('gauss_noise.data') with open ('gaus_noise_abcsysbio.data', 'w') as f: f.write (experiment_set.get_as_abcsysbio_syntax ()) f.close () # More complex experiment: run final_model with perturbations on # catalytic constants perturbation_exp = ExperimentSet ()
model_file = sys.argv[2] experiment_file = sys.argv[3] # define model sbml = SBML () sbml.load_file (model_file) ode = sbml_to_odes (sbml) ode.print_equations () # get param names param_names = [] for param in sbml.get_all_param (): param_names.append (param) # experiment info exp_set = ExperimentSet (experiment_file) experiment_times = exp_set[-1].times experiment_measure = exp_set[-1].measure_expression experiment_observations = [] for exp in exp_set: obs = exp.values experiment_observations.append (obs) parameter_regex = re.compile (r"Current theta: \[(.*)\]") f = open (trace_file) theta = None step = 0 for line in f: m = parameter_regex.match(line)
from marginal_likelihood.SBMLtoODES import sbml_to_odes from experiment.ExperimentSet import ExperimentSet from experiment.Experiment import Experiment import numpy as np def add_noise(values): for i in range(len(values)): eps = np.random.normal(0, .01) if values[i] + eps > 0: values[i] += eps sbml = SBML() sbml.load_file('model1.xml') odes = sbml_to_odes(sbml) time = [0, 2, 5, 10, 20, 40, 60, 100] values = odes.evaluate_on(time) experiment_set = ExperimentSet() for i in range(3): noised_values = {} for x in values: noised_values[x] = list(values[x]) add_noise(noised_values["Rpp"]) experiment = Experiment(time[1:], noised_values["Rpp"][1:], "Rpp") experiment_set.add(experiment) experiment_set.save_to_file('experiment.data')
import numpy as np def add_noise (values): for i in range (len (values)): eps = np.random.normal (0, .01) if values[i] + eps > 0: values[i] += eps sbml = SBML () sbml.load_file ('model1.xml') odes = sbml_to_odes (sbml) time = np.linspace (0, 1000, 8) values = odes.evaluate_on (time) experiment_set = ExperimentSet () for i in range (3): noised_values = {} for x in values: noised_values[x] = list (values[x]) add_noise (noised_values["B"]) experiment = Experiment (time, noised_values["B"], "B") experiment_set.add (experiment) experiment_set.save_to_file ('experiment.data') with open ('abcsysbiio-experiment.data', 'w') as f: f.write (experiment_set.get_as_abcsysbio_syntax ()) f.close ()