Skip to content

Commit

Permalink
Merge pull request #956 from benblamey/cpp_variables_issue
Browse files Browse the repository at this point in the history
added warning to c++ solvers when variables are specified for non-var…
  • Loading branch information
briandrawert authored Aug 1, 2024
2 parents aac2b4c + 2320590 commit b6335a4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
3 changes: 3 additions & 0 deletions gillespy2/solvers/cpp/ode_c_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ def run(self=None, model: Model = None, t: int = None, number_of_trajectories: i
"init_pop": populations,
"parameters": parameter_values
})
elif variables:
log.warning("'variables' argument ignored, because solver has variable=False.")

if integrator_options is not None:
integrator_options = ODECSolver.validate_integrator_options(integrator_options)
args.update(integrator_options)
Expand Down
2 changes: 2 additions & 0 deletions gillespy2/solvers/cpp/ssa_c_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def run(self=None, model: Model = None, t: int = None, number_of_trajectories: i
"init_pop": populations,
"parameters": parameter_values
})
elif variables:
log.warning("'variables' argument ignored, because solver has variable=False.")

seed = self._validate_seed(seed)
if seed is not None:
Expand Down
3 changes: 3 additions & 0 deletions gillespy2/solvers/cpp/tau_hybrid_c_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ def run(self=None, model: Model = None, t: int = None, number_of_trajectories: i
"init_pop": populations,
"parameters": parameter_values
})
elif variables:
log.warning("'variables' argument ignored, because solver has variable=False.")

if integrator_options is not None:
integrator_options = TauHybridCSolver.validate_integrator_options(integrator_options)
args.update(integrator_options)
Expand Down
5 changes: 4 additions & 1 deletion gillespy2/solvers/cpp/tau_leaping_c_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from gillespy2.solvers.utilities import solverutils as cutils
from gillespy2.core import GillesPySolver, Model
from gillespy2.core.gillespyError import *
from gillespy2.core import Results
from gillespy2.core import Results, log

from .c_solver import CSolver, SimulationReturnCode

Expand Down Expand Up @@ -68,6 +68,7 @@ def run(self=None, model: Model = None, t: int = None, number_of_trajectories: i
increment: int = None, seed: int = None, debug: bool = False, profile: bool = False, variables={},
resume=None, live_output: str = None, live_output_options: dict = {}, tau_tol=0.03, constant_tau_stepsize=None, **kwargs):


"""
:param model: The model on which the solver will operate. (Deprecated)
:type model: gillespy2.Model
Expand Down Expand Up @@ -172,6 +173,8 @@ def run(self=None, model: Model = None, t: int = None, number_of_trajectories: i
"init_pop": populations,
"parameters": parameter_values
})
elif variables:
log.warning("'variables' argument ignored, because solver has variable=False.")

seed = self._validate_seed(seed)
if seed is not None:
Expand Down
44 changes: 44 additions & 0 deletions test/test_variable_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import logging
import unittest
import tempfile
from gillespy2.core.gillespyError import DirectoryError, SimulationError
Expand Down Expand Up @@ -73,6 +74,49 @@ def test_invalid_variable(self):
for solver in self.solverlist:
results = self.model.run(solver=solver, variables={'foobar':0})

def test_non_variable_solver_ignored_variables_warnings(self):
"""
Ensure that non-variable solvers emit warnings for ignored variables.
"""
nonVarSolverList = [SSACSolver(self.model, variable=False),
ODECSolver(self.model, variable=False),
TauLeapingCSolver(self.model, variable=False),
TauHybridCSolver(self.model, variable=False)]
for nonVarSolver in nonVarSolverList:
with self.subTest(msg=f"for non-variable {nonVarSolver.__class__.__name__}"):
with self.subTest(msg="when variables is specified"):
self.model.run(solver=nonVarSolver, variables={'k1':1})
self.assertLogs(level=logging.WARN)
with self.subTest(msg="when variables is not specified"):
self.model.run(solver=nonVarSolver)
if hasattr(self, 'assertNoLogs'): # Python >= 3.10 only
self.assertNoLogs(level=logging.WARN)
with self.subTest(msg="when variables is {}"):
self.model.run(solver=nonVarSolver, variables={})
if hasattr(self, 'assertNoLogs'): # Python >= 3.10 only
self.assertNoLogs(level=logging.WARN)

def test_variable_solver_ignored_variables_warnings(self):
"""
Ensure that variable solvers do not emit warnings related to non-variable
solver variable ignoring.
"""
for varSolver in self.solverlist:
with self.subTest(msg=f"for variable {varSolver}"):
with self.subTest(msg="when variables is specified"):
self.model.run(solver=varSolver, variables={'k1':1})
if hasattr(self, 'assertNoLogs'): # Python >= 3.10 only
self.assertNoLogs(level=logging.WARN)
with self.subTest(msg="when variables is not specified"):
self.model.run(solver=varSolver)
if hasattr(self, 'assertNoLogs'): # Python >= 3.10 only
self.assertNoLogs(level=logging.WARN)
with self.subTest(msg="when variables is {}"):
self.model.run(solver=varSolver, variables={})
if hasattr(self, 'assertNoLogs'): # Python >= 3.10 only
self.assertNoLogs(level=logging.WARN)



if __name__ == '__main__':
unittest.main()

0 comments on commit b6335a4

Please sign in to comment.