Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions spatialpy/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions spatialpy/core/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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.")

Expand Down
12 changes: 6 additions & 6 deletions test/test_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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])