Skip to content

Commit

Permalink
Merge pull request #713 from StochSS/raise-err-objs
Browse files Browse the repository at this point in the history
Fix for error handling
  • Loading branch information
seanebum authored Feb 10, 2022
2 parents a944121 + 902c10a commit b98997d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
48 changes: 17 additions & 31 deletions gillespy2/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,28 @@ def sanitized_species_names(self):

def problem_with_name(self, name):
if name in Model.reserved_names:
return ModelError(
raise ModelError(
'Name "{}" is unavailable. It is reserved for internal GillesPy use. Reserved Names: ({}).'.format(name,
Model.reserved_names))
if name in self.listOfSpecies:
return ModelError('Name "{}" is unavailable. A species with that name exists.'.format(name))
raise ModelError('Name "{}" is unavailable. A species with that name exists.'.format(name))
if name in self.listOfParameters:
return ModelError('Name "{}" is unavailable. A parameter with that name exists.'.format(name))
raise ModelError('Name "{}" is unavailable. A parameter with that name exists.'.format(name))
if name in self.listOfReactions:
return ModelError('Name "{}" is unavailable. A reaction with that name exists.'.format(name))
raise ModelError('Name "{}" is unavailable. A reaction with that name exists.'.format(name))
if name in self.listOfEvents:
return ModelError('Name "{}" is unavailable. An event with that name exists.'.format(name))
raise ModelError('Name "{}" is unavailable. An event with that name exists.'.format(name))
if name in self.listOfRateRules:
return ModelError('Name "{}" is unavailable. A rate rule with that name exists.'.format(name))
raise ModelError('Name "{}" is unavailable. A rate rule with that name exists.'.format(name))
if name in self.listOfAssignmentRules:
return ModelError('Name "{}" is unavailable. An assignment rule with that name exists.'.format(name))
raise ModelError('Name "{}" is unavailable. An assignment rule with that name exists.'.format(name))
if name in self.listOfFunctionDefinitions:
return ModelError('Name "{}" is unavailable. A function definition with that name exists.'.format(name))
raise ModelError('Name "{}" is unavailable. A function definition with that name exists.'.format(name))
if name.isdigit():
return ModelError('Name "{}" is unavailable. Names must not be numeric strings.'.format(name))
raise ModelError('Name "{}" is unavailable. Names must not be numeric strings.'.format(name))
for special_character in Model.special_characters:
if special_character in name:
return ModelError(
raise ModelError(
'Name "{}" is unavailable. Names must not contain special characters: {}.'.format(name,
Model.special_characters))

Expand Down Expand Up @@ -335,9 +335,7 @@ def add_species(self, obj):
self.add_species(S)
else:
try:
problem = self.problem_with_name(obj.name)
if problem is not None:
raise problem
self.problem_with_name(obj.name)
self.listOfSpecies[obj.name] = obj
self._listOfSpecies[obj.name] = 'S{}'.format(len(self._listOfSpecies))
except Exception as e:
Expand Down Expand Up @@ -418,9 +416,7 @@ def add_parameter(self, params):
self.add_parameter(p)
else:
if isinstance(params, Parameter) or type(params).__name__ == 'Parameter':
problem = self.problem_with_name(params.name)
if problem is not None:
raise problem
self.problem_with_name(params.name)
self.update_namespace()
params._evaluate(self.namespace)
self.listOfParameters[params.name] = params
Expand Down Expand Up @@ -501,9 +497,7 @@ def add_reaction(self, reactions):
self.add_reaction(r)
else:
try:
problem = self.problem_with_name(reactions.name)
if problem is not None:
raise problem
self.problem_with_name(reactions.name)
reactions.verify()
self.validate_reactants_and_products(reactions)
if reactions.name is None or reactions.name == '':
Expand Down Expand Up @@ -543,9 +537,7 @@ def add_rate_rule(self, rate_rules):
self.add_rate_rule(rr)
else:
try:
problem = self.problem_with_name(rate_rules.name)
if problem is not None:
raise problem
self.problem_with_name(rate_rules.name)
if len(self.listOfAssignmentRules) != 0:
for i in self.listOfAssignmentRules.values():
if rate_rules.variable == i.variable:
Expand Down Expand Up @@ -588,9 +580,7 @@ def add_event(self, event):
self.add_event(e)
else:
try:
problem = self.problem_with_name(event.name)
if problem is not None:
raise problem
self.problem_with_name(event.name)
if event.trigger is None or not hasattr(event.trigger, 'expression'):
raise ModelError(
'An Event must contain a valid trigger.')
Expand All @@ -615,9 +605,7 @@ def add_function_definition(self, function_definitions):
self.add_function_definition(fd)
else:
try:
problem = self.problem_with_name(function_definitions.name)
if problem is not None:
raise problem
self.problem_with_name(function_definitions.name)
self.listOfFunctionDefinitions[function_definitions.name] = function_definitions
except Exception as e:
raise ParameterError(
Expand All @@ -635,9 +623,7 @@ def add_assignment_rule(self, assignment_rules):
self.add_assignment_rule(ar)
else:
try:
problem = self.problem_with_name(assignment_rules.name)
if problem is not None:
raise problem
self.problem_with_name(assignment_rules.name)
if len(self.listOfRateRules) != 0:
for i in self.listOfRateRules.values():
if assignment_rules.variable == i.variable:
Expand Down
4 changes: 3 additions & 1 deletion gillespy2/solvers/numpy/CLE_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ def run(self, model=None, t=20, number_of_trajectories=1, increment=None, seed=N
while self.result is None:
pass
if hasattr(self, 'has_raised_exception'):
raise self.has_raised_exception
raise SimulationError(
f"Error encountered while running simulation:\nReturn code: {int(self.rc)}.\n"
) from self.has_raised_exception

return Results.build_from_solver_results(self, live_output_options)

Expand Down
4 changes: 3 additions & 1 deletion gillespy2/solvers/numpy/ode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ def run(self, model=None, t=20, number_of_trajectories=1, increment=None, show_l
while self.result is None:
pass
if hasattr(self, 'has_raised_exception'):
raise self.has_raised_exception
raise SimulationError(
f"Error encountered while running simulation:\nReturn code: {int(self.rc)}.\n"
) from self.has_raised_exception

return Results.build_from_solver_results(self, live_output_options)

Expand Down
4 changes: 3 additions & 1 deletion gillespy2/solvers/numpy/ssa_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def run(self, model=None, t=20, number_of_trajectories=1, increment=None, seed=N
while self.result is None:
pass
if hasattr(self, 'has_raised_exception'):
raise self.has_raised_exception
raise SimulationError(
f"Error encountered while running simulation:\nReturn code: {int(self.rc)}.\n"
) from self.has_raised_exception

return Results.build_from_solver_results(self, live_output_options)

Expand Down
4 changes: 3 additions & 1 deletion gillespy2/solvers/numpy/tau_hybrid_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,9 @@ def run(self, model=None, t=20, number_of_trajectories=1, increment=None, seed=N
except:
pass
if hasattr(self, 'has_raised_exception'):
raise self.has_raised_exception
raise SimulationError(
f"Error encountered while running simulation:\nReturn code: {int(self.rc)}.\n"
) from self.has_raised_exception

return Results.build_from_solver_results(self, live_output_options)

Expand Down
4 changes: 3 additions & 1 deletion gillespy2/solvers/numpy/tau_leaping_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ def run(self, model=None, t=20, number_of_trajectories=1, increment=None, seed=N
while self.result is None:
pass
if hasattr(self, 'has_raised_exception'):
raise self.has_raised_exception
raise SimulationError(
f"Error encountered while running simulation:\nReturn code: {int(self.rc)}.\n"
) from self.has_raised_exception

return Results.build_from_solver_results(self, live_output_options)

Expand Down

0 comments on commit b98997d

Please sign in to comment.