Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOTFIX: Test all solvers #794

Merged
merged 2 commits into from
May 16, 2022
Merged
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
197 changes: 105 additions & 92 deletions test/test_all_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,93 +32,92 @@

class TestAllSolvers(unittest.TestCase):

solvers = [
SSACSolver,
ODESolver,
NumPySSASolver,
TauLeapingSolver,
TauHybridSolver,
ODECSolver,
TauLeapingCSolver,
TauHybridCSolver,
]

sbml_features = {
"AssignmentRule": lambda model, variable:
model.add_assignment_rule(gillespy2.AssignmentRule(variable=variable, formula="1/(t+1)")),
"RateRule": lambda model, variable:
model.add_rate_rule(gillespy2.RateRule(variable=variable, formula="2*t")),
"Event": lambda model, variable:
model.add_event(gillespy2.Event(
trigger=gillespy2.EventTrigger(expression="t>1"),
assignments=[gillespy2.EventAssignment(variable=variable, expression="100")]
)),
"FunctionDefinition": lambda model, variable:
model.add_function_definition(
gillespy2.FunctionDefinition(name="fn", function="variable", args=["variable"])),
}

# List of supported SBML features for each solver.
# When a feature is implemented for a particular solver, add the feature to its list.
solver_supported_sbml_features = {
NumPySSASolver: [],
TauLeapingSolver: [],
ODESolver: [],
TauHybridSolver: [
"AssignmentRule",
"RateRule",
"Event",
"FunctionDefinition",
],

SSACSolver: [],
ODECSolver: [],
TauLeapingCSolver: [],
TauHybridCSolver: [
"RateRule",
"Event",
],
}

model = Example()
for sp in model.listOfSpecies.values():
sp.mode = 'discrete'
results = {}
labeled_results = {}
labeled_results_more_trajectories = {}

for solver in solvers:
solver = solver(model=model)
labeled_results[solver.name] = model.run(solver=solver, number_of_trajectories=1, seed=1)
labeled_results_more_trajectories[solver.name] = model.run(solver=solver, number_of_trajectories=2)

def test_instantiated(self):
@classmethod
def setUpClass(cls):
cls.solvers = [
SSACSolver,
ODESolver,
NumPySSASolver,
TauLeapingSolver,
TauHybridSolver,
ODECSolver,
TauLeapingCSolver,
TauHybridCSolver,
]

cls.sbml_features = {
"AssignmentRule": lambda model, variable:
model.add_assignment_rule(gillespy2.AssignmentRule(variable=variable, formula="1/(t+1)")),
"RateRule": lambda model, variable:
model.add_rate_rule(gillespy2.RateRule(variable=variable, formula="2*t")),
"Event": lambda model, variable:
model.add_event(gillespy2.Event(
trigger=gillespy2.EventTrigger(expression="t>1"),
assignments=[gillespy2.EventAssignment(variable=variable, expression="100")]
)),
"FunctionDefinition": lambda model, variable:
model.add_function_definition(
gillespy2.FunctionDefinition(name="fn", function="variable", args=["variable"])),
}

# List of supported SBML features for each solver.
# When a feature is implemented for a particular solver, add the feature to its list.
cls.solver_supported_sbml_features = {
NumPySSASolver: [],
TauLeapingSolver: [],
ODESolver: [],
TauHybridSolver: [
"AssignmentRule",
"RateRule",
"Event",
"FunctionDefinition",
],

SSACSolver: [],
ODECSolver: [],
TauLeapingCSolver: [],
TauHybridCSolver: [
"RateRule",
"Event",
],
}

cls.model = Example()

cls.results = {}
cls.labeled_results = {}
cls.labeled_results_more_trajectories = {}

def test_extraneous_args(self):
for solver in self.solvers:
self.model.run(solver=solver(model=self.model))
with self.subTest(solver=solver.name), self.assertLogs('GillesPy2', level='WARN'):
model = Example()
solver = solver(model=model)
model.run(solver=solver, nonsense='ABC')

def test_to_array(self):
for solver in self.solvers:
self.assertTrue(isinstance(self.labeled_results[solver.name].to_array()[0], np.ndarray))
def test_instantiated(self):
for sp in self.model.listOfSpecies.values():
sp.mode = 'discrete'

def test_return_type_show_labels(self):
for solver in self.solvers:
self.assertTrue(isinstance(self.labeled_results[solver.name], Results))
self.assertTrue(isinstance(self.labeled_results[solver.name]['Sp'], np.ndarray))
self.assertTrue(isinstance(self.labeled_results[solver.name]['Sp'][0], np.float))

self.assertTrue(isinstance(self.labeled_results[solver.name][0], Trajectory))

self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver.name], Results))
self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver.name][0], Trajectory))
self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver.name][0]['Sp'], np.ndarray))
self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver.name][0]['Sp'][0], np.float))
with self.subTest(solver=solver.name):
solver = solver(model=self.model)
if "ODE" in solver.name:
self.labeled_results[solver.name] = self.model.run(solver=solver, number_of_trajectories=1)
else:
self.labeled_results[solver.name] = self.model.run(solver=solver, number_of_trajectories=1, seed=1)
self.labeled_results_more_trajectories[solver.name] = self.model.run(solver=solver, number_of_trajectories=2)

def test_random_seed(self):
for solver in self.solvers:
with self.subTest(solver=solver.name):
solver = solver(model=self.model)
same_results = self.model.run(solver=solver, seed=1)
compare_results = self.model.run(solver=solver,seed=1)
if "ODE" in solver.name:
same_results = self.model.run(solver=solver)
compare_results = self.model.run(solver=solver)
else:
same_results = self.model.run(solver=solver, seed=1)
compare_results = self.model.run(solver=solver,seed=1)
self.assertTrue(np.array_equal(same_results.to_array(), compare_results.to_array()))
if solver.name in ["ODESolver", "ODECSolver"]: continue
diff_results = self.model.run(solver=solver, seed=2)
Expand All @@ -133,27 +132,29 @@ def test_random_seed_unnamed_reactions(self):
for solver in self.solvers:
with self.subTest(solver=solver.name):
solver = solver(model=self.model)
same_results = self.model.run(solver=solver, seed=1)
compare_results = self.model.run(solver=solver,seed=1)
if "ODE" in solver.name:
same_results = self.model.run(solver=solver)
compare_results = self.model.run(solver=solver)
else:
same_results = self.model.run(solver=solver, seed=1)
compare_results = self.model.run(solver=solver,seed=1)
self.assertTrue(np.array_equal(same_results.to_array(), compare_results.to_array()))
if solver.name in ["ODESolver", "ODECSolver"]: continue
diff_results = self.model.run(solver=solver, seed=2)
self.assertFalse(np.array_equal(diff_results.to_array(), same_results.to_array()))

def test_extraneous_args(self):
def test_return_type_show_labels(self):
for solver in self.solvers:
with self.subTest(solver=solver.name), self.assertLogs('GillesPy2', level='WARN'):
model = Example()
solver = solver(model=model)
model.run(solver=solver, nonsense='ABC')
self.assertTrue(isinstance(self.labeled_results[solver.name], Results))
self.assertTrue(isinstance(self.labeled_results[solver.name]['Sp'], np.ndarray))
self.assertTrue(isinstance(self.labeled_results[solver.name]['Sp'][0], np.float))

def test_timeout(self):
for solver in self.solvers:
with self.subTest(solver=solver.name), self.assertLogs('GillesPy2', level='WARN'):
model = Oregonator()
model.timespan(np.linspace(0, 1000000, 1001))
solver = solver(model=model)
model.run(solver=solver, timeout=0.1)
self.assertTrue(isinstance(self.labeled_results[solver.name][0], Trajectory))

self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver.name], Results))
self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver.name][0], Trajectory))
self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver.name][0]['Sp'], np.ndarray))
self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver.name][0]['Sp'][0], np.float))

def test_sbml_feature_validation(self):
class TestModel(gillespy2.Model):
Expand Down Expand Up @@ -181,6 +182,18 @@ def __init__(self):
add_sbml_feature(model, "S")
solver.validate_sbml_features(model=model)

def test_timeout(self):
for solver in self.solvers:
with self.subTest(solver=solver.name), self.assertLogs('GillesPy2', level='WARN'):
model = Oregonator()
model.timespan(np.linspace(0, 1000000, 1001))
solver = solver(model=model)
model.run(solver=solver, timeout=0.1)

def test_to_array(self):
for solver in self.solvers:
self.assertTrue(isinstance(self.labeled_results[solver.name].to_array()[0], np.ndarray))


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