diff --git a/spatialpy/core/model.py b/spatialpy/core/model.py index 68b74b03..9de2a5ca 100644 --- a/spatialpy/core/model.py +++ b/spatialpy/core/model.py @@ -318,6 +318,15 @@ def compile_prep(self): self.__update_diffusion_restrictions() self.__apply_initial_conditions() self.__resolve_parameters() + + sanitized_params = self.sanitized_parameter_names() + for species in self.listOfSpecies.values(): + diff_coeff = species.diffusion_coefficient + if isinstance(diff_coeff, str): + if diff_coeff not in sanitized_params: + raise ModelError(f"Parameterm {diff_coeff} doesn't exist.") + species.diffusion_coefficient = sanitized_params[diff_coeff] + self.__get_expression_utility() stoich_matrix = self.__create_stoichiometric_matrix() dep_graph = self.__create_dependency_graph() diff --git a/spatialpy/core/species.py b/spatialpy/core/species.py index c1f03142..88824583 100644 --- a/spatialpy/core/species.py +++ b/spatialpy/core/species.py @@ -40,9 +40,9 @@ def __init__(self, name=None, diffusion_coefficient=None, restrict_to=None): if diffusion_coefficient is None: raise SpeciesError("Species must have a diffusion_coefficient.") - if not (isinstance(diffusion_coefficient, (Parameter, float, int)) or \ + if not (isinstance(diffusion_coefficient, (Parameter, str, float, int)) or \ type(diffusion_coefficient).__name__ == 'Parameter'): - raise SpeciesError("Diffusion coefficient must be a spatialpy.Parameter, float, or int.") + raise SpeciesError("Diffusion coefficient must be a spatialpy.Parameter, str, float, or int.") if isinstance(diffusion_coefficient, (float, int)) and diffusion_coefficient < 0: raise SpeciesError("Diffusion coefficient must be non-negative.") @@ -73,9 +73,9 @@ def set_diffusion_coefficient(self, diffusion_coefficient): :raises SpeciesError: If diffusion_coefficient is negative or not a valid type. """ - if not (isinstance(diffusion_coefficient, (Parameter, float, int)) or \ + if not (isinstance(diffusion_coefficient, (Parameter, str, float, int)) or \ type(diffusion_coefficient).__name__ == 'Parameter'): - raise SpeciesError("Diffusion coefficient must be a spatialpy.Parameter, float, or int.") + raise SpeciesError("Diffusion coefficient must be a spatialpy.Parameter, str, float, or int.") if diffusion_coefficient < 0: raise SpeciesError("Diffusion coefficient must be non-negative.") diff --git a/test/test_species.py b/test/test_species.py index 3ef5cd62..77f5b543 100644 --- a/test/test_species.py +++ b/test/test_species.py @@ -65,10 +65,10 @@ def test_constructor__parameter_diffusion_coefficient(self): self.assertIsInstance(species.diffusion_coefficient, Parameter) - def test_constructor__diffusion_coefficient_not_int_or_float(self): - """ Test the Species constructor with non-int or non-float diffusion_coefficient. """ + def test_constructor__diffusion_coefficient_not_str_int_or_float(self): + """ Test the Species constructor with non-str, non-int, or non-float diffusion_coefficient. """ with self.assertRaises(SpeciesError): - species = Species(name="test_species", diffusion_coefficient="0") + species = Species(name="test_species", diffusion_coefficient=[0]) def test_constructor__restrict_to_not_accepted_type(self): @@ -111,8 +111,8 @@ def test_set_diffusion_coefficient__negative_diffusion_coefficient(self): species.set_diffusion_coefficient(diffusion_coefficient=-1) - def test_set_diffusion_coefficient__diffusion_coefficient_not_int_or_float(self): - """ Test Species.set_diffusion_coefficient method with non-int or non-float diffusion_coefficient. """ + def test_set_diffusion_coefficient__diffusion_coefficient_not_str_int_or_float(self): + """ Test Species.set_diffusion_coefficient method with non-str, non-int, or non-float diffusion_coefficient. """ species = Species(name="test_species", diffusion_coefficient=0) with self.assertRaises(SpeciesError): - species.set_diffusion_coefficient(diffusion_coefficient="1") + species.set_diffusion_coefficient(diffusion_coefficient=[0])