Skip to content

Commit

Permalink
Unit tests (closes #28) and standardize cupsoda run signature (fixes
Browse files Browse the repository at this point in the history
…#26)

Note that cupsoda unit tests are not run on travis due to lack of GPU/CUDA
infrastructure. Developers should run the tests locally before committing
to github.
  • Loading branch information
alubbock committed Sep 23, 2016
1 parent 0a63b35 commit 68f4782
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ before_install:
install:
python setup.py build --build-lib=build/lib
script:
nosetests build/lib/pysb --with-coverage --cover-inclusive --cover-package=build/lib/pysb
nosetests build/lib/pysb --with-coverage --cover-inclusive
--cover-package=build/lib/pysb -a '!gpu'
after_success:
coveralls
22 changes: 20 additions & 2 deletions pysb/bng.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ def read_netfile(self):

def read_simulation_results(self):
"""
Reads the results of a BNG simulation and parses them into a numpy
array
Reads the results of a network-based BNG simulation and parses them
into a numpy array
"""
# Read concentrations data
cdat_arr = numpy.loadtxt(self.base_filename + '.cdat', skiprows=1)
Expand All @@ -257,6 +257,24 @@ def read_simulation_results(self):

return yfull

def read_nfsim_results(self):
"""
Reads the results of a network-free BNG simulation and parses them
into a numpy array
"""
# Read groups data
if not len(self.model.observables):
raise BngInterfaceError('Network-free simulation requires at '
'least one observable to be specified')

gdat_arr = numpy.loadtxt(self.base_filename + '.gdat', skiprows=1,
dtype={'names': ['time'] +
self.model.observables.keys(),
'formats': [float] * (
len(self.model.observables) + 1)})

return gdat_arr


class BngConsole(BngBaseInterface):
""" Interact with BioNetGen through BNG Console """
Expand Down
25 changes: 24 additions & 1 deletion pysb/cupsoda.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def __init__(self, model, tspan=None, cleanup=True, verbose=False,
# overwrite default integrator options
self.options.update(integrator_options)

def run(self, param_values, y0, tspan=None, outdir=None,
def run(self, tspan=None, param_values=None, y0=None, outdir=None,
prefix=None, **integrator_options):
"""Perform a set of integrations.
Expand Down Expand Up @@ -275,6 +275,29 @@ def run(self, param_values, y0, tspan=None, outdir=None,
"""

start_time = time.time()

if y0 is None and param_values is None:
warnings.warn("Neither 'y0' nor 'param_values' were supplied. "
"Running a single simulation with model defaults.")

if y0 is None:
# Run simulation using same parameters, varying initial conditions
species_names = [str(s) for s in self.model.species]
y0 = np.zeros(len(species_names))
for ic in self.model.initial_conditions:
y0[species_names.index(str(ic[0]))] = ic[1].value
y0 = np.repeat([y0],
1 if param_values is None else
param_values.shape[0],
axis=0)

if param_values is None:
# Run simulation using same initial conditions, varying parameters
param_values = np.repeat(np.array([[p.value for p in
self.model.parameters]]),
y0.shape[0],
axis=0)

# make sure tspan is defined somewhere
if tspan is not None:
self.tspan = tspan
Expand Down
39 changes: 39 additions & 0 deletions pysb/tests/test_cupsoda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pysb.examples.tyson_oscillator import model
from pysb.cupsoda import CupsodaSolver
import numpy as np
import warnings
from nose.plugins.attrib import attr


@attr('gpu')
def test_cupsoda_tyson():
tspan = np.linspace(0, 500, 501)

solver = CupsodaSolver(model, tspan=tspan, atol=1e-12, rtol=1e-6,
max_steps=20000, verbose=False)

n_sims = 3

# Rate constants
param_values = np.ones((n_sims, len(model.parameters)))
for i in range(len(param_values)):
for j in range(len(param_values[i])):
param_values[i][j] *= model.parameters[j].value

# Initial concentrations
y0 = np.zeros((n_sims, len(model.species)))
for i in range(len(y0)):
for ic in model.initial_conditions:
for j in range(len(y0[i])):
if str(ic[0]) == str(model.species[j]):
y0[i][j] = ic[1].value
break

with warnings.catch_warnings():
warnings.filterwarnings('ignore', "Neither 'y0' nor 'param_values' "
"were supplied.")
solver.run(param_values=None, y0=None)

solver.run(param_values=param_values, y0=None)
solver.run(param_values=None, y0=y0)
solver.run(param_values=param_values, y0=y0)

0 comments on commit 68f4782

Please sign in to comment.