Skip to content

Commit

Permalink
Validate cupsoda init kwargs and integrator opts (#383)
Browse files Browse the repository at this point in the history
* Validate cupsoda init kwargs and integrator opts

Previously, unrecognised kwargs and integrator options were
silently ignored. Now a ValueError is raised if they are not
recognised.

* Fix cupsoda unit test
  • Loading branch information
alubbock committed Oct 18, 2018
1 parent a75dadd commit d8ad0ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
34 changes: 27 additions & 7 deletions pysb/simulator/cupsoda.py
Expand Up @@ -146,18 +146,38 @@ class CupSodaSimulator(Simulator):
'n_blocks': None, # number of GPU blocks
'memory_usage': 'sharedconstant'}} # see _memory_options dict

_integrator_options_allowed = {'max_steps', 'atol', 'rtol', 'n_blocks',
'memory_usage', 'vol'}

def __init__(self, model, tspan=None, initials=None, param_values=None,
verbose=False, **kwargs):
super(CupSodaSimulator, self).__init__(model, tspan=tspan,
initials=initials,
param_values=param_values,
verbose=verbose, **kwargs)
self.gpu = kwargs.get('gpu', 0)
self._obs_species_only = kwargs.get('obs_species_only', True)
self._cleanup = kwargs.get('cleanup', True)
self._prefix = kwargs.get('prefix', self._model.name.replace('.', '_'))
self._base_dir = kwargs.get('base_dir', None)
self.integrator = kwargs.get('integrator', 'cupsoda')
self.gpu = kwargs.pop('gpu', 0)
self._obs_species_only = kwargs.pop('obs_species_only', True)
self._cleanup = kwargs.pop('cleanup', True)
self._prefix = kwargs.pop('prefix', self._model.name.replace('.', '_'))
self._base_dir = kwargs.pop('base_dir', None)
self.integrator = kwargs.pop('integrator', 'cupsoda')
integrator_options = kwargs.pop('integrator_options', {})

if kwargs:
raise ValueError('Unknown keyword argument(s): {}'.format(
', '.join(kwargs.keys())
))

unknown_integrator_options = set(integrator_options.keys()).difference(
self._integrator_options_allowed
)
if unknown_integrator_options:
raise ValueError(
'Unknown integrator_options: {}. Allowed options: {}'.format(
', '.join(unknown_integrator_options),
', '.join(self._integrator_options_allowed)
)
)

# generate the equations for the model
pysb.bng.generate_equations(self._model, self._cleanup, self.verbose)
Expand All @@ -171,7 +191,7 @@ def __init__(self, model, tspan=None, initials=None, param_values=None,
else:
raise SimulatorException(
"Integrator type '" + self.integrator + "' not recognized.")
options.update(kwargs.get('integrator_options', {})) # overwrite
options.update(integrator_options) # overwrite

# defaults
self.opts = options
Expand Down
11 changes: 10 additions & 1 deletion pysb/tests/test_simulator_cupsoda.py
Expand Up @@ -80,11 +80,20 @@ def test_run_tyson(self):

def test_verbose(self):
solver = CupSodaSimulator(model, tspan=self.tspan, verbose=True,
vol=1e-5,
integrator_options={'atol': 1e-12,
'rtol': 1e-12,
'vol': 1e-5,
'max_steps': 20000})
solver.run()

def test_run_cupsoda_instance(self):
run_cupsoda(model, tspan=self.tspan)

@raises(ValueError)
def test_invalid_init_kwarg(self):
CupSodaSimulator(model, tspan=self.tspan, spam='eggs')

@raises(ValueError)
def test_invalid_integrator_option(self):
CupSodaSimulator(model, tspan=self.tspan,
integrator_options={'spam': 'eggs'})

0 comments on commit d8ad0ae

Please sign in to comment.