From 902c10a3695077e133a5621c9e258b1778310110 Mon Sep 17 00:00:00 2001 From: Bryan Rumsey Date: Thu, 10 Feb 2022 13:48:36 -0500 Subject: [PATCH] Fixed incorrect exception raising. --- gillespy2/core/model.py | 48 +++++++------------ gillespy2/solvers/numpy/CLE_solver.py | 4 +- gillespy2/solvers/numpy/ode_solver.py | 4 +- gillespy2/solvers/numpy/ssa_solver.py | 4 +- gillespy2/solvers/numpy/tau_hybrid_solver.py | 4 +- gillespy2/solvers/numpy/tau_leaping_solver.py | 4 +- 6 files changed, 32 insertions(+), 36 deletions(-) diff --git a/gillespy2/core/model.py b/gillespy2/core/model.py index 0c881d77e..40055de4d 100644 --- a/gillespy2/core/model.py +++ b/gillespy2/core/model.py @@ -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)) @@ -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: @@ -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 @@ -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 == '': @@ -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: @@ -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.') @@ -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( @@ -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: diff --git a/gillespy2/solvers/numpy/CLE_solver.py b/gillespy2/solvers/numpy/CLE_solver.py index 511cfbbf5..7e25e53e6 100644 --- a/gillespy2/solvers/numpy/CLE_solver.py +++ b/gillespy2/solvers/numpy/CLE_solver.py @@ -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) diff --git a/gillespy2/solvers/numpy/ode_solver.py b/gillespy2/solvers/numpy/ode_solver.py index 957346903..8b8419dda 100644 --- a/gillespy2/solvers/numpy/ode_solver.py +++ b/gillespy2/solvers/numpy/ode_solver.py @@ -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) diff --git a/gillespy2/solvers/numpy/ssa_solver.py b/gillespy2/solvers/numpy/ssa_solver.py index 46c1984cf..61fd5f6e9 100644 --- a/gillespy2/solvers/numpy/ssa_solver.py +++ b/gillespy2/solvers/numpy/ssa_solver.py @@ -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) diff --git a/gillespy2/solvers/numpy/tau_hybrid_solver.py b/gillespy2/solvers/numpy/tau_hybrid_solver.py index 921e9becc..13d0599aa 100644 --- a/gillespy2/solvers/numpy/tau_hybrid_solver.py +++ b/gillespy2/solvers/numpy/tau_hybrid_solver.py @@ -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) diff --git a/gillespy2/solvers/numpy/tau_leaping_solver.py b/gillespy2/solvers/numpy/tau_leaping_solver.py index a54766a47..cbc0d8c7b 100644 --- a/gillespy2/solvers/numpy/tau_leaping_solver.py +++ b/gillespy2/solvers/numpy/tau_leaping_solver.py @@ -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)