def simulate(self, model, time_max=100.): # simulate env = EnvironmentVarGuard() env.set( 'CONFIG__DOT__wc_lang__DOT__validation__DOT__validate_element_charge_balance', '0') with env: simulation = Simulation(model) _, results_dirname = simulation.run(time_max=time_max, ode_time_step=1., checkpoint_period=1., results_dir=self.tempdir) # get results results = RunResults(results_dirname) agg_states = results.get('aggregate_states') comp_mass = agg_states[(self.comp.id, 'mass')] comp_vol = results.get('functions')[self.volume.id] pops = results.get('populations') time = pops.index pop_constant = pops[self.spec_constant.id] pop_dynamic = pops[self.spec_dynamic.id] return (time, pop_constant, pop_dynamic, comp_mass, comp_vol)
def test_growth_transcription(self): # Construct model env = EnvironmentVarGuard() env.set('CONFIG__DOT__wc_kb__DOT__io__DOT__strict', '0') with env: self.kb = wc_kb.io.Reader().run( 'tests/fixtures/min_model_kb.xlsx', 'tests/fixtures/min_model_kb_seq.fna', )[wc_kb.KnowledgeBase][0] self.model = prokaryote.ProkaryoteModelGenerator( knowledge_base=self.kb, component_generators=[ prokaryote.InitalizeModel, prokaryote.TranscriptionSubmodelGenerator, prokaryote.RnaDegradationSubmodelGenerator, prokaryote.MetabolismSubmodelGenerator ], options={ 'component': { 'TranscriptionSubmodelGenerator': { 'beta': 1. }, 'RnaDegradationSubmodelGenerator': { 'beta': 1. } } }).run() # Simulate model checkpoint_period = 10 end_time = 100 simulation = Simulation(self.model) results = simulation.run(end_time, self.dir, checkpoint_period) self.assertIsInstance(results, tuple) # Check results num_events = results[0] run_results_dir = results[1] run_results = RunResults(run_results_dir) df = run_results.get('populations') cytosol = self.model.compartments.get_one(id='c') rna_ids = [] for rna in self.model.species_types.get( type=wc_ontology['WC:RNA']): # RNA rna_ids.append(rna.species.get_one(compartment=cytosol).id) avg_init_rna_cn = np.mean(df.loc[0.0, rna_ids].values) avg_final_rna_cn = np.mean(df.loc[100.0, rna_ids].values) print(simulation.provide_event_counts()) print('\n INIT AVG RNA COPY NUMBERS:', avg_init_rna_cn) print('\n FINAL AVG RNA COPY NUMBERS:', avg_final_rna_cn) self.assertGreater(avg_final_rna_cn, 2 * avg_init_rna_cn - avg_init_rna_cn * 0.15) self.assertLess(avg_final_rna_cn, 2 * avg_init_rna_cn + avg_init_rna_cn * 0.15)
def test_prepare(self): model = MakeModel.make_test_model( '2 species, 1 reaction, with rates given by reactant population', transform_prep_and_check=False) model.id = 'illegal id' simulation = Simulation(model) with self.assertRaises(MultialgorithmError): simulation._prepare()
def _default(self): args = self.app.pargs simulation = Simulation(args.model_file) simulation.run(time_max=args.time_max, results_dir=args.results_dir, checkpoint_period=args.checkpoint_period, dfba_time_step=args.dfba_time_step, profile=args.profile, verbose=args.verbose)
def run_model(model, results_dir, checkpoint_period=5, end_time=100): """ Simulates model """ if not os.path.exists(results_dir): os.makedirs(results_dir) simulation = Simulation(model) results = simulation.run(end_time, results_dir, checkpoint_period) return results
def test_flat_rates(self): model = self.model end_time = 400 checkpoint_period = 10 simulation = Simulation(model) results = simulation.run(end_time, self.tmp_dirname, checkpoint_period) self.assertIsInstance(results, tuple) self.assertTrue(len(results) == 2)
def simulate(self, end_time, checkpoint_period, peturbation=None): if perturbation: model = copy.deepcopy(self.MODEL) peturbation(model) else: model = self.MODEL sim = Simulation(self.MODEL) sim.run(time_max=end_time, results_dir=self._results_path, checkpoint_period=checkpoint_period) return RunResults(self._results_path)
def simulate(self, end_time, checkpoint_period=None, n_sims=1): results = [] simulation = Simulation(self.model) for i_sim in range(n_sims): temp_dir = tempfile.mkdtemp(dir=self.results_dir) results_dir = simulation.run( time_max=end_time, results_dir=temp_dir, checkpoint_period=checkpoint_period).results_dir run_results = RunResults(results_dir) results.append(run_results) return results
def test_reseed(self): # different seeds must make different results seeds = [17, 19] results = {} run_results = {} for seed in seeds: tmp_results_dir = tempfile.mkdtemp() with CaptureOutput(relay=False): num_events, results_dir = Simulation(TOY_MODEL_FILENAME).run( time_max=5, results_dir=tmp_results_dir, checkpoint_period=5, seed=seed) results[seed] = {} results[seed]['num_events'] = num_events run_results[seed] = RunResults(results_dir) shutil.rmtree(tmp_results_dir) self.assertNotEqual(results[seeds[0]]['num_events'], results[seeds[1]]['num_events']) self.assertFalse(run_results[seeds[0]].get('populations').equals( run_results[seeds[1]].get('populations'))) # a given seed must must always make the same result seed = 117 results = {} run_results = {} for rep in range(2): tmp_results_dir = tempfile.mkdtemp() with CaptureOutput(relay=False): num_events, results_dir = Simulation(TOY_MODEL_FILENAME).run( time_max=5, results_dir=tmp_results_dir, checkpoint_period=5, seed=seed) results[rep] = {} results[rep]['num_events'] = num_events run_results[rep] = RunResults(results_dir) shutil.rmtree(tmp_results_dir) self.assertEqual(results[0]['num_events'], results[1]['num_events']) self.assertTrue(run_results[0].get('populations').equals( run_results[1].get('populations'))) for component in RunResults.COMPONENTS: # metadata differs, because it includes timestamp if component != 'metadata': self.assertTrue(run_results[0].get(component).equals( run_results[1].get(component)))
def test_run(self): with CaptureOutput(relay=False): num_events, results_dir = Simulation(TOY_MODEL_FILENAME).run( time_max=2, results_dir=self.results_dir, checkpoint_period=1) # check time, and simulation config in checkpoints access_checkpoints = AccessCheckpoints(results_dir) for time in access_checkpoints.list_checkpoints(): ckpt = access_checkpoints.get_checkpoint(time=time) self.assertEqual(time, ckpt.time) self.assertTrue(ckpt.random_state != None) # test performance profiling results_dir = tempfile.mkdtemp(dir=self.test_dir) with CaptureOutput(relay=False) as capturer: stats, _ = Simulation(TOY_MODEL_FILENAME).run( time_max=20, results_dir=results_dir, checkpoint_period=1, profile=True, verbose=False) expected_profile_text = [ 'function calls', 'filename:lineno(function)' ] for text in expected_profile_text: self.assertIn(text, capturer.get_text()) self.assertTrue(isinstance(stats, pstats.Stats)) self.assertTrue(isinstance(RunResults(results_dir), RunResults)) with self.assertRaises(MultialgorithmError): Simulation(TOY_MODEL_FILENAME).run( time_max=2, results_dir=tempfile.mkdtemp(dir=self.test_dir), checkpoint_period=1, profile=True, verbose=True) with self.assertRaisesRegex( MultialgorithmError, 'cannot be simulated .* it contains no submodels'): Simulation(TOY_MODEL_FILENAME).run( time_max=5, results_dir=tempfile.mkdtemp(dir=self.test_dir), checkpoint_period=1, submodels_to_skip=['test_submodel'])
def test_get_simulation_engine(self): model = MakeModel.make_test_model( '2 species, 1 reaction, with rates given by reactant population') simulation = Simulation(model) self.assertEqual(simulation.get_simulation_engine(), None) with CaptureOutput(relay=False): simulation.run(time_max=5) self.assertTrue( isinstance(simulation.get_simulation_engine(), SimulationEngine))
def test_provide_event_counts(self): model = MakeModel.make_test_model( '2 species, 1 reaction, with rates given by reactant population') simulation = Simulation(model) self.assertTrue('execute run() to obtain event counts' in simulation.provide_event_counts()) with CaptureOutput(relay=False): simulation.run(time_max=100) self.assertTrue('Event type' in simulation.provide_event_counts())
def test_simulation_model_in_memory(self): model = MakeModel.make_test_model('2 species, 1 reaction', transform_prep_and_check=False) self.run_simulation(Simulation(model))
def verify_independently_solved_model(test_case, model_filename, results_dir): # read model while ignoring missing models, with std dev = 0 for integration_framework in ['deterministic_simulation_algorithm', 'ordinary_differential_equations']: # empty results_dir for file in os.listdir(results_dir): file_path = os.path.join(results_dir, file) if os.path.isfile(file_path): os.unlink(file_path) model = read_model_for_test(model_filename, integration_framework=f'WC:{integration_framework}') # simulate model time_max = model.parameters.get_one(id='time_max').value checkpoint_period = model.parameters.get_one(id='checkpoint_period').value args = dict(results_dir=results_dir, checkpoint_period=checkpoint_period, ode_time_step=1.) simulation = Simulation(model) start_time = time.perf_counter() _, results_dir = simulation.run(time_max=time_max, **args) elapsed_rt = time.perf_counter() - start_time print(f'ran {os.path.basename(model_filename)} with {integration_framework} in ' f'{elapsed_rt:.2e} (sec)') # test dynamics # read expected trajectories trajectories = define_trajectory_classes(model) SpeciesTrajectory = trajectories['SpeciesTrajectory'] AggregateTrajectory = trajectories['AggregateTrajectory'] trajectory_model_classes = list(trajectories.values()) expected_trajectories = \ obj_tables.io.Reader().run(model_filename, models=trajectory_model_classes, ignore_extra_models=True, ignore_attribute_order=True) # get expected species trajectories from model workbook expected_species_trajectories = {} species_ids = [species.id for species in model.get_species()] for species_id in species_ids: expected_species_trajectories[species_id] = [] for species_id in species_ids: for expected_species_trajectory in expected_trajectories[SpeciesTrajectory]: expected_pop = getattr(expected_species_trajectory, species_id_to_pop_attr(species_id)) expected_species_trajectories[species_id].append(expected_pop) expected_trajectory_times = [] for expected_species_trajectory in expected_trajectories[SpeciesTrajectory]: expected_trajectory_times.append(expected_species_trajectory.time) # plot trajectories plots_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'tests', 'results')) os.makedirs(plots_dir, exist_ok=True) plots = plot_expected_vs_simulated(simulation.dynamic_model, integration_framework, results_dir, trajectory_times=expected_trajectory_times, plots_dir=plots_dir, expected_species_trajectories=expected_species_trajectories) # get aggregate trajectories from model workbook expected_aggregate_trajectories = {} for dyn_compartment_id in simulation.dynamic_model.dynamic_compartments: expected_aggregate_trajectories[dyn_compartment_id] = defaultdict(list) expected_trajectory_times = [] for attr in AggregateTrajectory.Meta.local_attributes.values(): attr_name = attr.name if attr_name != 'time': if attr_name.startswith('compartment'): compartment_id = attr_name.split(' ')[1] for expected_aggregate_trajectory in expected_trajectories[AggregateTrajectory]: aggregate_prop = ' '.join(attr_name.split(' ')[2:]) expected_aggregate_trajectories[compartment_id][aggregate_prop].append( getattr(expected_aggregate_trajectory, attr.name)) else: for expected_aggregate_trajectory in expected_trajectories[AggregateTrajectory]: expected_trajectory_times.append(expected_aggregate_trajectory.time) # plot trajectories plots = plot_expected_vs_simulated(simulation.dynamic_model, integration_framework, results_dir, trajectory_times=expected_trajectory_times, plots_dir=plots_dir, expected_property_trajectories=expected_aggregate_trajectories) # compare expected & simulated trajectories check_simul_results(test_case, simulation.dynamic_model, results_dir, integration_framework, expected_times=expected_trajectory_times, expected_species_trajectories=expected_species_trajectories, expected_property_trajectories=expected_aggregate_trajectories)
import os import matplotlib.pyplot as plt import tempfile # import simulation and run results from wc_sim.simulation import Simulation from wc_sim.run_results import RunResults # setup inputs # use a toy model model_filename = os.path.join(os.path.dirname(__file__), '../tests/fixtures', '2_species_1_reaction.xlsx') results_dir = tempfile.mkdtemp() # create and run simulation simulation = Simulation(model_filename) num_events, results_dir = simulation.run(time_max=30, results_dir=results_dir, checkpoint_period=10) run_results = RunResults(results_dir) # view results # run_results contains 'populations', 'observables', 'functions', 'aggregate_states', 'random_states', and 'metadata' for component in RunResults.COMPONENTS: print('\ncomponent:', component) print(run_results.get(component)) # get the mass of compartment 'c' at time 10 aggregate_states_df = run_results.get('aggregate_states') print("\naggregate_states_df.loc[10, ('c', 'mass')] =", aggregate_states_df.loc[10, ('c', 'mass')])
def test(self): model_filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'MetabolismAndGeneExpression.xlsx') model = wc_lang.io.Reader().run(model_filename)[wc_lang.Model][0] simulation = Simulation(model) _, results_dirname = simulation.run(time_max=8 * 3600, ode_time_step=1., checkpoint_period=100., results_dir=self.tempdir) # get results results = RunResults(results_dirname) agg_states = results.get('aggregate_states') mass = agg_states[('c', 'mass')] pops = results.get('populations') time = pops.index funcs = results.get('functions') volume_c = funcs['volume_c'] volume_e = funcs['volume_e'] # assert self.assertGreater(mass.values[-1] / mass.values[0], 1.75) self.assertLess(mass.values[-1] / mass.values[0], 2.25) ala_c_fold_change = pops['ala[c]'].values[-1] / pops['ala[c]'].values[0] amp_c_fold_change = pops['amp[c]'].values[-1] / pops['amp[c]'].values[0] ala_e_fold_change = pops['ala[e]'].values[-1] / pops['ala[e]'].values[0] amp_e_fold_change = pops['amp[e]'].values[-1] / pops['amp[e]'].values[0] self.assertGreater(ala_c_fold_change, 1.75) self.assertGreater(amp_c_fold_change, 1.75) self.assertLess(ala_c_fold_change, 2.25) self.assertLess(amp_c_fold_change, 2.25) self.assertGreater(ala_e_fold_change, 0.95) self.assertGreater(amp_e_fold_change, 0.95) self.assertLess(ala_e_fold_change, 1.0) self.assertLess(amp_e_fold_change, 1.0) prot_fold_change = (pops['protein_rnapol[c]'].values[-1] + pops['protein_ribosome[c]'].values[-1] + pops['protein_rnase[c]'].values[-1] + pops['protein_protease[c]'].values[-1]) \ / (pops['protein_rnapol[c]'].values[0] + pops['protein_ribosome[c]'].values[0] + pops['protein_rnase[c]'].values[0] + pops['protein_protease[c]'].values[0]) self.assertGreater(prot_fold_change, 1.5) self.assertLess(prot_fold_change, 4.) # plot dirname = os.path.join(os.path.dirname(__file__), 'results') if not os.path.isdir(dirname): os.mkdir(dirname) fig, axes = pyplot.subplots(nrows=2, ncols=3) # mass axes[0][0].plot(time / 3600, 1e18 * mass) axes[0][0].set_ylabel('Mass (fg)') # volume axes[1][0].plot(time / 3600, 1e15 * volume_c) axes[1][0].set_xlabel('Time (h)') axes[1][0].set_ylabel('Volume (al)') # metabolites axes[0][1].plot( time / 3600, 1e3 * numpy.stack( ((pops['ala[e]'] / volume_e / NA).values, (pops['amp[e]'] / volume_e / NA).values)).T) axes[0][1].set_title('Extracellular metabolites') axes[0][1].set_ylabel('mM') axes[1][1].plot( time / 3600, 1e3 * numpy.stack( ((pops['ala[c]'] / volume_c / NA).values, (pops['amp[c]'] / volume_c / NA).values)).T) axes[1][1].set_title('Cytosol metabolites') axes[1][1].set_ylabel('mM') # RNA axes[0][2].plot( time / 3600, numpy.stack( (pops['rna_rnapol[c]'].values, pops['rna_ribosome[c]'].values, pops['rna_rnase[c]'].values, pops['rna_protease[c]'].values)).T) axes[0][2].set_title('RNA') axes[0][2].set_ylabel('Count (molecule)') # protein axes[1][2].plot( time / 3600, numpy.stack((pops['protein_rnapol[c]'].values, pops['protein_ribosome[c]'].values, pops['protein_rnase[c]'].values, pops['protein_protease[c]'].values)).T) axes[1][2].set_title('Protein') axes[1][2].set_ylabel('Count (molecule)') # save figure filename = os.path.join(dirname, 'MetabolismAndGeneExpression.pdf') fig.savefig(filename) pyplot.close(fig)
def test_simulate_deterministic_simulation_algorithm_submodel(self): model = MakeModel.make_test_model('1 species, 1 reaction') self.transform_model_for_dsa_simulation(model) simulation = Simulation(model) num_events, _ = simulation.run(time_max=100) self.assertGreater(num_events, 0)
def test_simulate_wo_output_files(self): with CaptureOutput(relay=False): num_events, results_dir = Simulation(TOY_MODEL_FILENAME).run( time_max=5) self.assertTrue(0 < num_events) self.assertEqual(results_dir, None)
def test_check_simul_results(self): init_volume = 1E-16 init_density = 1000 molecular_weight = 100. default_species_copy_number = 10_000 init_accounted_mass = molecular_weight * default_species_copy_number / Avogadro init_accounted_density = init_accounted_mass / init_volume expected_initial_values_compt_1 = dict( init_volume=init_volume, init_accounted_mass=init_accounted_mass, init_mass=init_volume * init_density, init_density=init_density, init_accounted_density=init_accounted_density, accounted_fraction=init_accounted_density / init_density) expected_initial_values = {'compt_1': expected_initial_values_compt_1} model = MakeModel.make_test_model( '1 species, 1 reaction', init_vols=[expected_initial_values_compt_1['init_volume']], init_vol_stds=[0], density=init_density, molecular_weight=molecular_weight, default_species_copy_number=default_species_copy_number, default_species_std=0, submodel_framework='WC:deterministic_simulation_algorithm') multialgorithm_simulation = MultialgorithmSimulation( model, self.wc_sim_config) _, dynamic_model = multialgorithm_simulation.build_simulation() check_simul_results(self, dynamic_model, None, expected_initial_values=expected_initial_values) # test dynamics simulation = Simulation(model) _, results_dir = simulation.run(time_max=2, **self.args) nan = float('NaN') check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 9999., 9998.]}) check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[nan, nan, nan]}) with self.assertRaises(AssertionError): check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 10000., 9998.]}) with self.assertRaises(AssertionError): check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 10000.]}) check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 9999., 9998.]}, rel_tol=1E-5) check_simul_results(self, dynamic_model, results_dir, expected_property_trajectories={ 'compt_1': { 'mass': [1.000e-13, 9.999e-14, 9.998e-14] } }) check_simul_results(self, dynamic_model, results_dir, expected_property_trajectories={ 'compt_1': { 'mass': [nan, nan, nan] } }) with self.assertRaises(AssertionError): check_simul_results(self, dynamic_model, results_dir, expected_property_trajectories={ 'compt_1': { 'mass': [1.000e-13, 1.000e-13, 9.999e-14] } }, rel_tol=0) plots_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', 'tests', 'results')) os.makedirs(plots_dir, exist_ok=True) plot_expected_vs_simulated(dynamic_model, 'ordinary_differential_equations', results_dir, trajectory_times=[0, 1, 2], plots_dir=plots_dir, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 10000., 9998.]}, expected_property_trajectories=\ {'compt_1': {'mass':[1.000e-13, 1.000e-13, 9.999e-14]}}) plot_expected_vs_simulated(dynamic_model, 'ordinary_differential_equations', results_dir, trajectory_times=[0, 1, 2], plots_dir=plots_dir, expected_property_trajectories=\ {'compt_1': {'mass':[1.000e-13, 1.000e-13, 9.999e-14]}}) plot_expected_vs_simulated(dynamic_model, 'ordinary_differential_equations', results_dir, trajectory_times=[0, 1, 2], plots_dir=plots_dir, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 10000., 9998.]}) plot_expected_vs_simulated(dynamic_model, 'ordinary_differential_equations', results_dir, trajectory_times=[0, 1, 2], plots_dir=plots_dir, expected_species_trajectories=\ {'spec_type_0[compt_1]':[nan, nan, nan]}, expected_property_trajectories=\ {'compt_1': {'mass':[nan, nan, nan]}})
def test_simulation_model_in_file(self): self.run_simulation(Simulation(TOY_MODEL_FILENAME), time_max=5)
from wc_sim.simulation import Simulation from wc_sim.run_results import RunResults import numpy import wc_lang import wc_lang.io model_filename = 'model.xlsx' results_parent_dirname = 'results' checkpoint_period = 100. time_max = 3600. * 8. # read model model = wc_lang.io.Reader().run(model_filename)[wc_lang.Model][0] # run simulation sim = Simulation(model) _, results_dirname = sim.run(time_max=time_max, results_dir=results_parent_dirname, checkpoint_period=checkpoint_period) results = RunResults(results_dirname) # plot results def plot(model, results, filename): # get expected results c = model.compartments.get_one(id='c') species_type = model.species_types.get_one(id='rna') species = species_type.species.get_one(compartment=c) exp_avg_rna = species.concentration.value
def test_simulation_errors(self): with self.assertRaisesRegex( MultialgorithmError, 'model must be a `wc_lang Model` or a pathname for a model'): Simulation(3)