예제 #1
0
 def make_dynamic_model(self, model_filename):
     # read and initialize a model
     self.model = TestDynamicModel.models[model_filename]
     de_simulation_config = SimulationConfig(time_max=10)
     wc_sim_config = WCSimulationConfig(de_simulation_config)
     multialgorithm_simulation = MultialgorithmSimulation(
         self.model, wc_sim_config)
     multialgorithm_simulation.initialize_components()
     self.dynamic_model = DynamicModel(
         self.model, multialgorithm_simulation.local_species_population,
         multialgorithm_simulation.temp_dynamic_compartments)
예제 #2
0
    def test_initialize_infrastructure(self):
        self.multialgorithm_simulation.initialize_components()
        self.multialgorithm_simulation.initialize_infrastructure()
        self.assertTrue(isinstance(self.multialgorithm_simulation.dynamic_model, DynamicModel))

        de_simulation_config = SimulationConfig(time_max=10, output_dir=self.results_dir)
        wc_sim_config = WCSimulationConfig(de_simulation_config, dfba_time_step=1, checkpoint_period=10)
        multialg_sim = MultialgorithmSimulation(self.model, wc_sim_config)
        multialg_sim.initialize_components()
        multialg_sim.initialize_infrastructure()
        self.assertEqual(multialg_sim.checkpointing_sim_obj.checkpoint_dir, self.results_dir)
        self.assertTrue(multialg_sim.checkpointing_sim_obj.access_state_object is not None)
        self.assertTrue(isinstance(multialg_sim.checkpointing_sim_obj, MultialgorithmicCheckpointingSimObj))
        self.assertTrue(isinstance(multialg_sim.dynamic_model, DynamicModel))
예제 #3
0
def make_dynamic_submodel_params(model, lang_submodel):

    de_simulation_config = SimulationConfig(time_max=10)
    wc_sim_config = WCSimulationConfig(de_simulation_config)
    multialgorithm_simulation = MultialgorithmSimulation(model, wc_sim_config)
    multialgorithm_simulation.initialize_components()
    multialgorithm_simulation.dynamic_model = \
        DynamicModel(multialgorithm_simulation.model,
                     multialgorithm_simulation.local_species_population,
                     multialgorithm_simulation.temp_dynamic_compartments)

    return (lang_submodel.id,
            multialgorithm_simulation.dynamic_model,
            lang_submodel.reactions,
            lang_submodel.get_children(kind='submodel', __type=Species),
            multialgorithm_simulation.get_dynamic_compartments(lang_submodel),
            multialgorithm_simulation.local_species_population)
예제 #4
0
    def test_dynamic_model(self):
        self.make_dynamic_model(self.MODEL_FILENAME)
        self.assertEqual(len(self.dynamic_model.cellular_dyn_compartments), 1)
        self.assertEqual(self.dynamic_model.cellular_dyn_compartments[0].id,
                         'c')
        self.assertEqual(self.dynamic_model.get_num_submodels(), 2)

        model = TestDynamicModel.models[self.MODEL_FILENAME]
        for compartment in self.model.get_compartments():
            compartment.biological_type = onto['WC:extracellular_compartment']
        de_simulation_config = SimulationConfig(time_max=10)
        wc_sim_config = WCSimulationConfig(de_simulation_config)
        multialgorithm_simulation = MultialgorithmSimulation(
            model, wc_sim_config)
        multialgorithm_simulation.initialize_components()
        with self.assertRaisesRegex(
                MultialgorithmError,
                'must have at least 1 cellular compartment'):
            DynamicModel(model,
                         multialgorithm_simulation.local_species_population,
                         multialgorithm_simulation.temp_dynamic_compartments)
예제 #5
0
class TestMultialgorithmSimulationStatically(unittest.TestCase):

    MODEL_FILENAME = os.path.join(os.path.dirname(__file__), 'fixtures', 'test_model.xlsx')

    def setUp(self):
        # read and initialize a model
        self.model = Reader().run(self.MODEL_FILENAME, ignore_extra_models=True)[Model][0]
        for conc in self.model.distribution_init_concentrations:
            conc.std = 0.
        PrepForWcSimTransform().run(self.model)
        de_simulation_config = SimulationConfig(time_max=10)
        self.wc_sim_config = WCSimulationConfig(de_simulation_config, dfba_time_step=1)
        self.multialgorithm_simulation = MultialgorithmSimulation(self.model, self.wc_sim_config)
        self.test_dir = tempfile.mkdtemp()
        self.results_dir = tempfile.mkdtemp(dir=self.test_dir)

    def tearDown(self):
        shutil.rmtree(self.test_dir)

    def test_init(self):
        self.model.submodels = []
        with self.assertRaises(MultialgorithmError):
            MultialgorithmSimulation(self.model, self.wc_sim_config)

    def test_prepare_skipped_submodels(self):
        multialgorithm_simulation = MultialgorithmSimulation(self.model, self.wc_sim_config)
        self.assertEqual(multialgorithm_simulation.skipped_submodels(), set())
        submodels_to_skip = ['submodel_1']
        self.wc_sim_config.submodels_to_skip = submodels_to_skip
        multialgorithm_simulation = MultialgorithmSimulation(self.model, self.wc_sim_config)
        self.assertEqual(multialgorithm_simulation.skipped_submodels(), set(submodels_to_skip))

        submodels_to_skip = ['no_such_submodel']
        self.wc_sim_config.submodels_to_skip = submodels_to_skip
        with self.assertRaisesRegex(MultialgorithmError,
                                    "'submodels_to_skip' contains submodels that aren't in the model:"):
            MultialgorithmSimulation(self.model, self.wc_sim_config)

    def test_molecular_weights_for_species(self):
        multi_alg_sim = self.multialgorithm_simulation
        expected = {
            'species_6[c]': float('nan'),
            'H2O[c]': 18.0152
        }
        actual = multi_alg_sim.molecular_weights_for_species(set(expected.keys()))
        self.assertEqual(actual['H2O[c]'], expected['H2O[c]'])
        self.assertTrue(np.isnan(actual['species_6[c]']))

        # add a species_type without a structure
        species_type_wo_structure = self.model.species_types.create(
            id='st_wo_structure',
            name='st_wo_structure')
        cellular_compartment = self.model.compartments.get(**{'id': 'c'})[0]
        species_wo_structure = self.model.species.create(
            species_type=species_type_wo_structure,
            compartment=cellular_compartment)
        species_wo_structure.id = species_wo_structure.gen_id()

        actual = multi_alg_sim.molecular_weights_for_species([species_wo_structure.id])
        self.assertTrue(np.isnan(actual[species_wo_structure.id]))

        # test obtain weights for all species
        actual = multi_alg_sim.molecular_weights_for_species()
        self.assertEqual(actual['H2O[c]'], expected['H2O[c]'])
        self.assertTrue(np.isnan(actual['species_6[c]']))
        self.assertEqual(len(actual), len(self.model.get_species()))

    def test_create_dynamic_compartments(self):
        self.multialgorithm_simulation.create_dynamic_compartments()
        self.assertEqual(set(['c', 'e']), set(self.multialgorithm_simulation.temp_dynamic_compartments))
        for id, dynamic_compartment in self.multialgorithm_simulation.temp_dynamic_compartments.items():
            self.assertEqual(id, dynamic_compartment.id)
            self.assertTrue(0 < dynamic_compartment.init_density)

    def test_prepare_dynamic_compartments(self):
        self.multialgorithm_simulation.create_dynamic_compartments()
        self.multialgorithm_simulation.init_species_pop_from_distribution()
        self.multialgorithm_simulation.local_species_population = \
            self.multialgorithm_simulation.make_local_species_population(retain_history=False)
        self.multialgorithm_simulation.prepare_dynamic_compartments()
        for dynamic_compartment in self.multialgorithm_simulation.temp_dynamic_compartments.values():
            self.assertTrue(dynamic_compartment._initialized())
            self.assertTrue(0 < dynamic_compartment.accounted_mass())
            self.assertTrue(0 < dynamic_compartment.mass())

    def test_init_species_pop_from_distribution(self):
        self.multialgorithm_simulation.create_dynamic_compartments()
        self.multialgorithm_simulation.init_species_pop_from_distribution()
        species_wo_init_conc = ['species_1[c]', 'species_3[c]']
        for species_id in species_wo_init_conc:
            self.assertEqual(self.multialgorithm_simulation.init_populations[species_id], 0)
        for concentration in self.model.get_distribution_init_concentrations():
            self.assertTrue(0 <= self.multialgorithm_simulation.init_populations[concentration.species.id])

        # todo: statistically evaluate sampled population
        # ensure that over multiple runs of init_species_pop_from_distribution():
        # mean(species population) ~= mean(volume) * mean(concentration)

    def test_make_local_species_population(self):
        self.multialgorithm_simulation.create_dynamic_compartments()
        self.multialgorithm_simulation.init_species_pop_from_distribution()
        local_species_population = self.multialgorithm_simulation.make_local_species_population()
        self.assertEqual(local_species_population._molecular_weights,
            self.multialgorithm_simulation.molecular_weights_for_species())

        # test the initial population slopes
        # continuous adjustments are only allowed on species used by continuous submodels
        used_by_continuous_submodels = \
            ['species_1[e]', 'species_2[e]', 'species_1[c]', 'species_2[c]', 'species_3[c]']
        adjustments = {species_id: 0. for species_id in used_by_continuous_submodels}
        self.assertEqual(local_species_population.adjust_continuously(1, adjustments), None)
        not_in_a_reaction = ['H2O[e]', 'H2O[c]']
        used_by_discrete_submodels = ['species_4[c]', 'species_5[c]', 'species_6[c]']
        adjustments = {species_id: 0. for species_id in used_by_discrete_submodels + not_in_a_reaction}
        with self.assertRaises(DynamicSpeciesPopulationError):
            local_species_population.adjust_continuously(2, adjustments)

    def test_set_simultaneous_execution_priorities(self):
        expected_order_of_sim_obj_classes = [SsaSubmodel,
                                             NrmSubmodel,
                                             DsaSubmodel,
                                             DfbaSubmodel,
                                             OdeSubmodel,
                                             MultialgorithmicCheckpointingSimObj]
        self.multialgorithm_simulation.set_simultaneous_execution_priorities()
        # ensure that expected_order_of_sim_obj_classes are arranged in decreasing priority
        for i in range(len(expected_order_of_sim_obj_classes) - 1):
            simulation_object_class = expected_order_of_sim_obj_classes[i]
            next_simulation_object_class = expected_order_of_sim_obj_classes[i+1]
            self.assertLess(simulation_object_class.metadata.class_priority,
                            next_simulation_object_class.metadata.class_priority)

    def test_initialize_components(self):
        self.multialgorithm_simulation.initialize_components()
        self.assertTrue(isinstance(self.multialgorithm_simulation.local_species_population,
                        LocalSpeciesPopulation))
        for dynamic_compartment in self.multialgorithm_simulation.temp_dynamic_compartments.values():
            self.assertTrue(isinstance(dynamic_compartment.species_population, LocalSpeciesPopulation))

    def test_initialize_infrastructure(self):
        self.multialgorithm_simulation.initialize_components()
        self.multialgorithm_simulation.initialize_infrastructure()
        self.assertTrue(isinstance(self.multialgorithm_simulation.dynamic_model, DynamicModel))

        de_simulation_config = SimulationConfig(time_max=10, output_dir=self.results_dir)
        wc_sim_config = WCSimulationConfig(de_simulation_config, dfba_time_step=1, checkpoint_period=10)
        multialg_sim = MultialgorithmSimulation(self.model, wc_sim_config)
        multialg_sim.initialize_components()
        multialg_sim.initialize_infrastructure()
        self.assertEqual(multialg_sim.checkpointing_sim_obj.checkpoint_dir, self.results_dir)
        self.assertTrue(multialg_sim.checkpointing_sim_obj.access_state_object is not None)
        self.assertTrue(isinstance(multialg_sim.checkpointing_sim_obj, MultialgorithmicCheckpointingSimObj))
        self.assertTrue(isinstance(multialg_sim.dynamic_model, DynamicModel))

    def test_build_simulation(self):
        de_simulation_config = SimulationConfig(time_max=10, output_dir=self.results_dir)
        wc_sim_config = WCSimulationConfig(de_simulation_config, dfba_time_step=1, checkpoint_period=10)
        multialgorithm_simulation = MultialgorithmSimulation(self.model, wc_sim_config)
        simulation_engine, _ = multialgorithm_simulation.build_simulation()
        # 3 objects: 2 submodels, and the checkpointing obj:
        expected_sim_objs = set(['CHECKPOINTING_SIM_OBJ', 'submodel_1', 'submodel_2'])
        self.assertEqual(expected_sim_objs, set(list(simulation_engine.simulation_objects)))
        self.assertEqual(type(multialgorithm_simulation.checkpointing_sim_obj),
                         MultialgorithmicCheckpointingSimObj)
        self.assertEqual(multialgorithm_simulation.dynamic_model.get_num_submodels(), 2)

        # check that submodels receive options
        dfba_options = dict(dfba='fast but inaccurate')
        ssa_options = dict(ssa='accurate but slow')
        options = {'DfbaSubmodel': dict(options=dfba_options),
                   'SsaSubmodel': dict(options=ssa_options)
                  }
        multialgorithm_simulation = MultialgorithmSimulation(self.model, wc_sim_config, options)
        multialgorithm_simulation.build_simulation()
        dfba_submodel = multialgorithm_simulation.dynamic_model.dynamic_submodels['submodel_1']
        ssa_submodel = multialgorithm_simulation.dynamic_model.dynamic_submodels['submodel_2']
        self.assertEqual(dfba_submodel.options, dfba_options)
        self.assertEqual(ssa_submodel.options, ssa_options)

        # test skipped submodel
        submodels_to_skip = ['submodel_2']
        self.wc_sim_config.submodels_to_skip = submodels_to_skip
        ma_sim = MultialgorithmSimulation(self.model, self.wc_sim_config)
        _, dynamic_model = ma_sim.build_simulation()
        expected_dynamic_submodels = set([sm.id for sm in self.model.get_submodels()]) - ma_sim.skipped_submodels()
        self.assertEqual(expected_dynamic_submodels, set(dynamic_model.dynamic_submodels))

        submodel_1 = self.model.submodels.get(id='submodel_1')[0]
        # WC:modeling_framework is not an instance of a modeling framework
        submodel_1.framework = onto['WC:modeling_framework']
        ma_sim = MultialgorithmSimulation(self.model, self.wc_sim_config)
        with self.assertRaisesRegex(MultialgorithmError, 'Unsupported lang_submodel framework'):
            ma_sim.build_simulation()

    def test_get_dynamic_compartments(self):
        expected_compartments = dict(
            submodel_1=['c', 'e'],
            submodel_2=['c']
        )
        self.multialgorithm_simulation.build_simulation()
        for submodel_id in ['submodel_1', 'submodel_2']:
            submodel = self.model.submodels.get_one(id=submodel_id)
            submodel_dynamic_compartments = self.multialgorithm_simulation.get_dynamic_compartments(submodel)
            self.assertEqual(set(submodel_dynamic_compartments.keys()), set(expected_compartments[submodel_id]))

    def test_str(self):
        self.multialgorithm_simulation.create_dynamic_compartments()
        self.multialgorithm_simulation.init_species_pop_from_distribution()
        self.multialgorithm_simulation.local_species_population = \
            self.multialgorithm_simulation.make_local_species_population(retain_history=False)
        self.assertIn('species_1[e]', str(self.multialgorithm_simulation))
        self.assertIn('model:', str(self.multialgorithm_simulation))
예제 #6
0
    def test_dynamic_components(self):
        # test agregate properties like mass and volume against independent calculations of their values
        # calculations made in the model's spreadsheet

        # read model while ignoring missing models
        model = read_model_for_test(self.MODEL_FILENAME)
        # create dynamic model
        de_simulation_config = SimulationConfig(time_max=10)
        wc_sim_config = WCSimulationConfig(de_simulation_config)
        multialgorithm_simulation = MultialgorithmSimulation(
            model, wc_sim_config)
        multialgorithm_simulation.initialize_components()
        dynamic_model = DynamicModel(
            model, multialgorithm_simulation.local_species_population,
            multialgorithm_simulation.temp_dynamic_compartments)

        # a Model to store expected initial values
        class ExpectedInitialValue(obj_tables.Model):
            component = obj_tables.StringAttribute()
            attribute = obj_tables.StringAttribute()
            expected_initial_value = obj_tables.FloatAttribute()
            comment = obj_tables.StringAttribute()

            class Meta(obj_tables.Model.Meta):
                attribute_order = ('component', 'attribute',
                                   'expected_initial_value', 'comment')

        # get calculations of expected initial values from the workbook
        expected_initial_values = \
            obj_tables.io.Reader().run(self.MODEL_FILENAME, models=[ExpectedInitialValue],
                                       ignore_extra_models=True)[ExpectedInitialValue]
        for cellular_compartment in dynamic_model.cellular_dyn_compartments:
            compartment = dynamic_model.dynamic_compartments[
                cellular_compartment.id]
            actual_values = {
                'mass': compartment.mass(),
                'volume': compartment.volume(),
                'accounted mass': compartment.accounted_mass(),
                'accounted volume': compartment.accounted_volume()
            }
            for expected_initial_value in expected_initial_values:
                if expected_initial_value.component == cellular_compartment.id:
                    expected_value = expected_initial_value.expected_initial_value
                    actual_value = actual_values[
                        expected_initial_value.attribute]
                    numpy.testing.assert_approx_equal(actual_value,
                                                      expected_value)

        # cell mass, cell volume, etc.
        actual_values = {
            'cell mass': dynamic_model.cell_mass(),
            'cell volume': dynamic_model.cell_volume(),
            'cell accounted mass': dynamic_model.cell_accounted_mass(),
            'cell accounted volume': dynamic_model.cell_accounted_volume()
        }
        for expected_initial_value in expected_initial_values:
            if expected_initial_value.component == 'whole_cell':
                expected_value = expected_initial_value.expected_initial_value
                actual_value = actual_values[
                    f"cell {expected_initial_value.attribute}"]
                numpy.testing.assert_approx_equal(actual_value, expected_value)

        # test dynamic_model.get_aggregate_state()
        aggregate_state = dynamic_model.get_aggregate_state()
        for eiv_record in expected_initial_values:
            expected_value = eiv_record.expected_initial_value
            if eiv_record.component == 'whole_cell':
                actual_value = aggregate_state[f"cell {eiv_record.attribute}"]
                numpy.testing.assert_approx_equal(actual_value, expected_value)
            else:
                actual_value = aggregate_state['compartments'][
                    eiv_record.component][eiv_record.attribute]
                numpy.testing.assert_approx_equal(actual_value, expected_value)