Exemple #1
0
    def setUp(self):

        # make a Compartment & use it to make a DynamicCompartment
        comp_id = 'comp_id'
        self.mean_init_volume = 1E-17
        self.compartment = Compartment(id=comp_id,
                                       name='name',
                                       init_volume=InitVolume(
                                           mean=self.mean_init_volume,
                                           std=self.mean_init_volume / 40.))
        self.compartment.init_density = Parameter(
            id='density_{}'.format(comp_id),
            value=1100.,
            units=unit_registry.parse_units('g l^-1'))

        self.random_state = RandomStateManager.instance()
        self.dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                      self.compartment)

        self.abstract_compartment = Compartment(
            id=comp_id,
            name='name',
            init_volume=InitVolume(mean=self.mean_init_volume, std=0),
            physical_type=onto['WC:abstract_compartment'])
        self.abstract_dynamic_compartment = DynamicCompartment(
            None, self.random_state, self.abstract_compartment)
Exemple #2
0
    def test_str(self):
        dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                 self.compartment)
        self.assertIn("has not been initialized", str(dynamic_compartment))
        self.assertIn(self.dynamic_compartment.id, str(dynamic_compartment))

        dynamic_compartment.initialize_mass_and_density(self.local_species_pop)
        self.assertIn("has been initialized", str(dynamic_compartment))
        self.assertIn('Fold change total mass: 1.0', str(dynamic_compartment))

        # test abstract compartment
        self.abstract_dynamic_compartment.initialize_mass_and_density(
            self.local_species_pop)
        self.assertNotIn('Fraction of mass accounted for',
                         str(self.abstract_dynamic_compartment))
Exemple #3
0
 def create_dynamic_compartments(self):
     """ Create the :obj:`DynamicCompartment`\ s for this simulation
     """
     # create DynamicCompartments
     self.temp_dynamic_compartments = {}
     for compartment in self.model.get_compartments():
         self.temp_dynamic_compartments[
             compartment.id] = DynamicCompartment(None, self.random_state,
                                                  compartment)
Exemple #4
0
 def specify_init_accounted_fraction(self, desired_init_accounted_fraction):
     # make a DynamicCompartment with accounted_fraction ~= desired_init_accounted_fraction
     # without changing init_accounted_mass or init_volume
     init_density = self.dynamic_compartment.init_accounted_mass / \
         (desired_init_accounted_fraction * self.dynamic_compartment.init_volume)
     self.compartment.init_density = Parameter(
         id='density_x',
         value=init_density,
         units=unit_registry.parse_units('g l^-1'))
     return DynamicCompartment(None, self.random_state, self.compartment)
Exemple #5
0
    def setUp(self):
        comp_id = 'comp_id'

        # make a LocalSpeciesPopulation
        self.num_species = 100
        species_nums = list(range(0, self.num_species))
        self.species_ids = list(
            map(lambda x: "species_{}[{}]".format(x, comp_id), species_nums))
        self.all_pops = 1E6
        self.init_populations = dict(
            zip(self.species_ids, [self.all_pops] * len(species_nums)))
        self.all_m_weights = 50
        self.molecular_weights = dict(
            zip(self.species_ids, [self.all_m_weights] * len(species_nums)))
        self.local_species_pop = LocalSpeciesPopulation(
            'test',
            self.init_populations,
            self.molecular_weights,
            random_state=RandomStateManager.instance())

        # make Compartments & use them to make a DynamicCompartments
        self.mean_init_volume = 1E-17
        self.compartment = Compartment(id=comp_id,
                                       name='name',
                                       init_volume=InitVolume(
                                           mean=self.mean_init_volume,
                                           std=self.mean_init_volume / 40.))
        self.compartment.init_density = Parameter(
            id='density_{}'.format(comp_id),
            value=1100.,
            units=unit_registry.parse_units('g l^-1'))
        self.random_state = RandomStateManager.instance()
        self.dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                      self.compartment,
                                                      self.species_ids)

        self.abstract_compartment = Compartment(
            id=comp_id,
            name='name',
            init_volume=InitVolume(mean=self.mean_init_volume, std=0),
            physical_type=onto['WC:abstract_compartment'])
        self.abstract_dynamic_compartment = DynamicCompartment(
            None, self.random_state, self.abstract_compartment)
Exemple #6
0
 def test_dynamic_compartment(self):
     volumes = []
     # test mean initial volume
     for i_trial in range(10):
         dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                  self.compartment)
         volumes.append(dynamic_compartment.init_volume)
     self.assertLess(
         numpy.abs((numpy.mean(volumes) - self.mean_init_volume) /
                   self.mean_init_volume), 0.1)
Exemple #7
0
    def test_initialize_mass_and_density(self):
        self.dynamic_compartment.initialize_mass_and_density(
            self.local_species_pop)
        estimated_accounted_mass = self.num_species * self.all_pops * self.all_m_weights / Avogadro
        self.assertAlmostEqual(self.dynamic_compartment.init_accounted_mass,
                               estimated_accounted_mass,
                               places=IEEE_64_BIT_FLOATING_POINT_PLACES)
        self.assertTrue(0 < self.dynamic_compartment.init_mass)
        self.assertAlmostEqual(self.dynamic_compartment.accounted_fraction,
                               self.dynamic_compartment.init_accounted_mass / \
                                self.dynamic_compartment.init_mass,
                               places=IEEE_64_BIT_FLOATING_POINT_PLACES)

        # test abstract compartment
        self.abstract_dynamic_compartment.initialize_mass_and_density(
            self.local_species_pop)
        self.assertAlmostEqual(
            self.abstract_dynamic_compartment.init_accounted_mass,
            estimated_accounted_mass,
            places=IEEE_64_BIT_FLOATING_POINT_PLACES)
        self.assertEqual(self.abstract_dynamic_compartment.init_accounted_mass,
                         self.abstract_dynamic_compartment.init_mass)
        self.assertFalse(
            hasattr(self.abstract_dynamic_compartment,
                    'init_accounted_density'))

        empty_local_species_pop = LocalSpeciesPopulation(
            'test', {}, {}, random_state=RandomStateManager.instance())
        dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                 self.compartment)
        with self.assertRaisesRegex(MultialgorithmError,
                                    "initial accounted ratio is 0"):
            dynamic_compartment.initialize_mass_and_density(
                empty_local_species_pop)

        dynamic_compartment = self.specify_init_accounted_fraction(
            (DynamicCompartment.MAX_ALLOWED_INIT_ACCOUNTED_FRACTION + 1) / 2)
        with warnings.catch_warnings(record=True) as w:
            dynamic_compartment.initialize_mass_and_density(
                self.local_species_pop)
            self.assertIn("initial accounted ratio (", str(w[-1].message))
            self.assertIn(") greater than 1.0", str(w[-1].message))

        dynamic_compartment = self.specify_init_accounted_fraction(2)
        with self.assertRaises(MultialgorithmError):
            dynamic_compartment.initialize_mass_and_density(
                self.local_species_pop)
Exemple #8
0
    def test_dynamic_compartment_exceptions(self):
        compartment = Compartment(id='id',
                                  name='name',
                                  init_volume=InitVolume(mean=0))
        with self.assertRaises(MultialgorithmError):
            DynamicCompartment(None, self.random_state, compartment)

        self.compartment.init_density = Parameter(
            id='density_{}'.format(self.compartment.id),
            value=float('nan'),
            units=unit_registry.parse_units('g l^-1'))
        with self.assertRaises(MultialgorithmError):
            DynamicCompartment(None, self.random_state, self.compartment)

        self.compartment.init_density = Parameter(
            id='density_{}'.format(self.compartment.id),
            value=0.,
            units=unit_registry.parse_units('g l^-1'))
        with self.assertRaises(MultialgorithmError):
            DynamicCompartment(None, self.random_state, self.compartment)

        self.compartment.init_volume = None
        with self.assertRaises(MultialgorithmError):
            DynamicCompartment(None, self.random_state, self.compartment)
Exemple #9
0
class TestInitializedDynamicCompartment(unittest.TestCase):
    """ Test DynamicCompartments with species populations
    """
    def setUp(self):
        comp_id = 'comp_id'

        # make a LocalSpeciesPopulation
        self.num_species = 100
        species_nums = list(range(0, self.num_species))
        self.species_ids = list(
            map(lambda x: "species_{}[{}]".format(x, comp_id), species_nums))
        self.all_pops = 1E6
        self.init_populations = dict(
            zip(self.species_ids, [self.all_pops] * len(species_nums)))
        self.all_m_weights = 50
        self.molecular_weights = dict(
            zip(self.species_ids, [self.all_m_weights] * len(species_nums)))
        self.local_species_pop = LocalSpeciesPopulation(
            'test',
            self.init_populations,
            self.molecular_weights,
            random_state=RandomStateManager.instance())

        # make Compartments & use them to make a DynamicCompartments
        self.mean_init_volume = 1E-17
        self.compartment = Compartment(id=comp_id,
                                       name='name',
                                       init_volume=InitVolume(
                                           mean=self.mean_init_volume,
                                           std=self.mean_init_volume / 40.))
        self.compartment.init_density = Parameter(
            id='density_{}'.format(comp_id),
            value=1100.,
            units=unit_registry.parse_units('g l^-1'))
        self.random_state = RandomStateManager.instance()
        self.dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                      self.compartment,
                                                      self.species_ids)

        self.abstract_compartment = Compartment(
            id=comp_id,
            name='name',
            init_volume=InitVolume(mean=self.mean_init_volume, std=0),
            physical_type=onto['WC:abstract_compartment'])
        self.abstract_dynamic_compartment = DynamicCompartment(
            None, self.random_state, self.abstract_compartment)

    def specify_init_accounted_fraction(self, desired_init_accounted_fraction):
        # make a DynamicCompartment with accounted_fraction ~= desired_init_accounted_fraction
        # without changing init_accounted_mass or init_volume
        init_density = self.dynamic_compartment.init_accounted_mass / \
            (desired_init_accounted_fraction * self.dynamic_compartment.init_volume)
        self.compartment.init_density = Parameter(
            id='density_x',
            value=init_density,
            units=unit_registry.parse_units('g l^-1'))
        return DynamicCompartment(None, self.random_state, self.compartment)

    def test_initialize_mass_and_density(self):
        self.dynamic_compartment.initialize_mass_and_density(
            self.local_species_pop)
        estimated_accounted_mass = self.num_species * self.all_pops * self.all_m_weights / Avogadro
        self.assertAlmostEqual(self.dynamic_compartment.init_accounted_mass,
                               estimated_accounted_mass,
                               places=IEEE_64_BIT_FLOATING_POINT_PLACES)
        self.assertTrue(0 < self.dynamic_compartment.init_mass)
        self.assertAlmostEqual(self.dynamic_compartment.accounted_fraction,
                               self.dynamic_compartment.init_accounted_mass / \
                                self.dynamic_compartment.init_mass,
                               places=IEEE_64_BIT_FLOATING_POINT_PLACES)

        # test abstract compartment
        self.abstract_dynamic_compartment.initialize_mass_and_density(
            self.local_species_pop)
        self.assertAlmostEqual(
            self.abstract_dynamic_compartment.init_accounted_mass,
            estimated_accounted_mass,
            places=IEEE_64_BIT_FLOATING_POINT_PLACES)
        self.assertEqual(self.abstract_dynamic_compartment.init_accounted_mass,
                         self.abstract_dynamic_compartment.init_mass)
        self.assertFalse(
            hasattr(self.abstract_dynamic_compartment,
                    'init_accounted_density'))

        empty_local_species_pop = LocalSpeciesPopulation(
            'test', {}, {}, random_state=RandomStateManager.instance())
        dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                 self.compartment)
        with self.assertRaisesRegex(MultialgorithmError,
                                    "initial accounted ratio is 0"):
            dynamic_compartment.initialize_mass_and_density(
                empty_local_species_pop)

        dynamic_compartment = self.specify_init_accounted_fraction(
            (DynamicCompartment.MAX_ALLOWED_INIT_ACCOUNTED_FRACTION + 1) / 2)
        with warnings.catch_warnings(record=True) as w:
            dynamic_compartment.initialize_mass_and_density(
                self.local_species_pop)
            self.assertIn("initial accounted ratio (", str(w[-1].message))
            self.assertIn(") greater than 1.0", str(w[-1].message))

        dynamic_compartment = self.specify_init_accounted_fraction(2)
        with self.assertRaises(MultialgorithmError):
            dynamic_compartment.initialize_mass_and_density(
                self.local_species_pop)

    def test_fold_changes(self):
        # ensure that initial fold changes == 1
        self.dynamic_compartment.initialize_mass_and_density(
            self.local_species_pop)
        self.assertAlmostEqual(
            self.dynamic_compartment.fold_change_total_mass(0), 1.0)
        self.assertAlmostEqual(
            self.dynamic_compartment.fold_change_total_volume(0), 1.0)

    def test_init_volume_and_eval(self):
        self.dynamic_compartment.initialize_mass_and_density(
            self.local_species_pop)
        self.assertAlmostEqual(self.dynamic_compartment.volume(),
                               self.dynamic_compartment.init_volume,
                               places=IEEE_64_BIT_FLOATING_POINT_PLACES)
        self.assertAlmostEqual(self.dynamic_compartment.eval(),
                               self.dynamic_compartment.init_mass,
                               places=IEEE_64_BIT_FLOATING_POINT_PLACES)

        # test abstract compartment
        self.abstract_dynamic_compartment.initialize_mass_and_density(
            self.local_species_pop)
        self.assertEqual(
            self.abstract_dynamic_compartment.accounted_mass(time=0),
            self.abstract_dynamic_compartment.init_mass)
        self.assertEqual(
            self.abstract_dynamic_compartment.accounted_volume(time=0),
            self.abstract_dynamic_compartment.init_volume)
        self.assertEqual(self.abstract_dynamic_compartment.mass(time=0),
                         self.abstract_dynamic_compartment.init_mass)
        self.assertEqual(self.abstract_dynamic_compartment.volume(time=0),
                         self.abstract_dynamic_compartment.init_volume)

    def test_str(self):
        dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                 self.compartment)
        self.assertIn("has not been initialized", str(dynamic_compartment))
        self.assertIn(self.dynamic_compartment.id, str(dynamic_compartment))

        dynamic_compartment.initialize_mass_and_density(self.local_species_pop)
        self.assertIn("has been initialized", str(dynamic_compartment))
        self.assertIn('Fold change total mass: 1.0', str(dynamic_compartment))

        # test abstract compartment
        self.abstract_dynamic_compartment.initialize_mass_and_density(
            self.local_species_pop)
        self.assertNotIn('Fraction of mass accounted for',
                         str(self.abstract_dynamic_compartment))
Exemple #10
0
class TestUninitializedDynamicCompartment(unittest.TestCase):
    """ Test DynamicCompartments that do not have a species population
    """
    def setUp(self):

        # make a Compartment & use it to make a DynamicCompartment
        comp_id = 'comp_id'
        self.mean_init_volume = 1E-17
        self.compartment = Compartment(id=comp_id,
                                       name='name',
                                       init_volume=InitVolume(
                                           mean=self.mean_init_volume,
                                           std=self.mean_init_volume / 40.))
        self.compartment.init_density = Parameter(
            id='density_{}'.format(comp_id),
            value=1100.,
            units=unit_registry.parse_units('g l^-1'))

        self.random_state = RandomStateManager.instance()
        self.dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                      self.compartment)

        self.abstract_compartment = Compartment(
            id=comp_id,
            name='name',
            init_volume=InitVolume(mean=self.mean_init_volume, std=0),
            physical_type=onto['WC:abstract_compartment'])
        self.abstract_dynamic_compartment = DynamicCompartment(
            None, self.random_state, self.abstract_compartment)

    def test_dynamic_compartment(self):
        volumes = []
        # test mean initial volume
        for i_trial in range(10):
            dynamic_compartment = DynamicCompartment(None, self.random_state,
                                                     self.compartment)
            volumes.append(dynamic_compartment.init_volume)
        self.assertLess(
            numpy.abs((numpy.mean(volumes) - self.mean_init_volume) /
                      self.mean_init_volume), 0.1)

    def test_abstract_dynamic_compartment(self):
        self.assertTrue(self.abstract_dynamic_compartment._is_abstract())
        self.assertFalse(
            hasattr(self.abstract_dynamic_compartment, 'init_density'))

    def test_dynamic_compartment_exceptions(self):
        compartment = Compartment(id='id',
                                  name='name',
                                  init_volume=InitVolume(mean=0))
        with self.assertRaises(MultialgorithmError):
            DynamicCompartment(None, self.random_state, compartment)

        self.compartment.init_density = Parameter(
            id='density_{}'.format(self.compartment.id),
            value=float('nan'),
            units=unit_registry.parse_units('g l^-1'))
        with self.assertRaises(MultialgorithmError):
            DynamicCompartment(None, self.random_state, self.compartment)

        self.compartment.init_density = Parameter(
            id='density_{}'.format(self.compartment.id),
            value=0.,
            units=unit_registry.parse_units('g l^-1'))
        with self.assertRaises(MultialgorithmError):
            DynamicCompartment(None, self.random_state, self.compartment)

        self.compartment.init_volume = None
        with self.assertRaises(MultialgorithmError):
            DynamicCompartment(None, self.random_state, self.compartment)