Skip to content
Permalink
Browse files

Updating sensitivity analysis class to be general for any model param…

…eters (#426)

* Updating Sensitivity analysis class to be used with any parameters, not just initial conditions. Also added ability to pass a list of parameter names rather than requiring all from the model to be used.

* Adding check to if passed in list of parameters is actually in the model.

* Adding examples in documentation for sens_type='params' and with a list
 of parameters. Adding tests for parameter list.

* Adding test for using deprecated class name (InitialsSensitivity). This
will maintain compatibility.

* Fixing doc tests. Printing differences between python versions gave
some errors. They seem to be fixed.

* Adding last test case where sens_type=None and sample_list=None

* Removed a lot of the initializations of the solver. Not needed.
Added test to catch a non pysb simulator.

* Fixing logic in checking if sens_type sample_list is None and sens_type
is not in approved list.

* Addressing changes for pull request.

* Changing doctest to verify size of array, rather than printing.
Windows represents np.array.shape as type L, where on linux it
uses it uses int. This is due to differences in C types. Pointless fix
in the long run, but now it should pass doctests on python2 for windows.
It is discussed here.
numpy/numpy#5809

* Adding attribute and parameter descriptions. I made some attributes
private that were supposed to be for internal calculations.

* Documentation fixes for sensitivity analysis class
  • Loading branch information...
JamesPino authored and alubbock committed Apr 17, 2019
1 parent b6ba27d commit 9d2d3bfc5d406a323b333be7b3fbf3363420911a
@@ -60,7 +60,7 @@
'dateutil.parser', 'networkx', 'networkx.algorithms',
'networkx.algorithms.isomorphism',
'networkx.algorithms.isomorphism.vf2userfunc',
'nose', 'nose.tools']
'nose', 'nose.tools', 'matplotlib', 'matplotlib.pyplot']
for mod_name in MOCK_MODULES:
sys.modules[mod_name] = mock.MagicMock()
sys.modules['sympy'].Symbol = type('Symbol', (object,), {})
@@ -13,5 +13,6 @@ PySB Modules Reference
macros.rst
pattern.rst
tools/render.rst
tools/sensitivity_analysis.rst
importers/index.rst
export/index.rst
@@ -0,0 +1,5 @@
Sensitivity anaylsis (:py:mod:`pysb.tools.sensitivity_analysis`)
================================================================

.. automodule:: pysb.tools.sensitivity_analysis
:members:
@@ -1,12 +1,13 @@
from pysb.tools.sensitivity_analysis import \
InitialsSensitivity
PairwiseSensitivity, InitialsSensitivity
from pysb.examples.tyson_oscillator import model
import numpy as np
import numpy.testing as npt
import os
from pysb.simulator.scipyode import ScipyOdeSimulator
import tempfile
import shutil
from nose.tools import raises


class TestSensitivityAnalysis(object):
@@ -24,7 +25,7 @@ def setUp(self):
integrator_options={'rtol': 1e-8,
'atol': 1e-8,
'mxstep': 20000})
self.sens = InitialsSensitivity(
self.sens = PairwiseSensitivity(
solver=self.solver,
values_to_sample=self.vals,
objective_function=self.obj_func_cell_cycle,
@@ -65,15 +66,21 @@ def obj_func_cell_cycle(self, out):
local_freq = np.average(local_times) / len(local_times) * 2
return local_freq

def test_vode_run(self):
solver_vode = ScipyOdeSimulator(self.model,
tspan=self.tspan,
integrator='vode',
integrator_options={'rtol': 1e-8,
'atol': 1e-8,
'nsteps': 20000})
def test_run(self):

sens_vode = PairwiseSensitivity(
solver=self.solver,
values_to_sample=self.vals,
objective_function=self.obj_func_cell_cycle,
observable=self.observable
)
sens_vode.run()
npt.assert_almost_equal(self.sens.p_matrix, self.p_simulated,
decimal=3)

def test_old_class_naming(self):
sens_vode = InitialsSensitivity(
solver=solver_vode,
solver=self.solver,
values_to_sample=self.vals,
objective_function=self.obj_func_cell_cycle,
observable=self.observable
@@ -89,9 +96,6 @@ def test_p_matrix(self):
npt.assert_almost_equal(self.sens.p_matrix, self.p_simulated,
decimal=3)

def test_num_simulations(self):
assert len(self.sens.simulation_initials) == 25

def test_pmatrix_outfile_exists(self):
self.sens.run(save_name=self.savename,
out_dir=self.output_dir)
@@ -121,21 +125,81 @@ def test_create_png(self):
'test3_P_H_P_prime.png'))

def test_unique_simulations_only(self):
vals = [.8, .9, 1.1, 1.2, 1.3]
sens = PairwiseSensitivity(
values_to_sample=vals,
objective_function=self.obj_func_cell_cycle,
observable=self.observable,
solver=self.solver
)
sens.run()
self.sens.create_plot_p_h_pprime(save_name='test4',
out_dir=self.output_dir)
assert os.path.exists(os.path.join(self.output_dir,
'test4_P_H_P_prime.png'))

def test_param_pair(self):
vals = [.9, 1.0, 1.1]
sens = PairwiseSensitivity(
values_to_sample=vals,
objective_function=self.obj_func_cell_cycle,
observable=self.observable,
solver=self.solver,
sample_list=['k1', 'cdc0']
)
sens.run()
self.sens.create_plot_p_h_pprime(save_name='test4',
out_dir=self.output_dir)
assert os.path.exists(os.path.join(self.output_dir,
'test4_P_H_P_prime.png'))

def test_all_params(self):
vals = [.9, 1.1]

sens = PairwiseSensitivity(
values_to_sample=vals,
objective_function=self.obj_func_cell_cycle,
observable=self.observable,
solver=self.solver,
sens_type='all'
)
sens.run()
self.sens.create_plot_p_h_pprime(save_name='test4',
out_dir=None)
assert os.path.exists('test4_P_H_P_prime.png')

@raises(ValueError)
def test_param_not_in_model(self):
vals = [.8, .9, 1.1, 1.2, 1.3]
solver = ScipyOdeSimulator(self.model,
tspan=self.tspan,
integrator='lsoda',
integrator_options={'rtol': 1e-8,
'atol': 1e-8,
'mxstep': 20000})
sens = InitialsSensitivity(
sens = PairwiseSensitivity(
values_to_sample=vals,
objective_function=self.obj_func_cell_cycle,
observable=self.observable,
solver=solver
solver=solver, sample_list=['a0']
)

@raises(ValueError)
def test_sens_type_and_list_none(self):
vals = [.8, .9, 1.1, 1.2, 1.3]
sens = PairwiseSensitivity(
values_to_sample=vals,
objective_function=self.obj_func_cell_cycle,
observable=self.observable,
solver=self.solver, sample_list=None, sens_type=None
)

@raises(TypeError)
def test_bad_solver(self):
vals = [.8, .9, 1.1, 1.2, 1.3]
sens = PairwiseSensitivity(
values_to_sample=vals,
objective_function=self.obj_func_cell_cycle,
observable=self.observable,
solver=None, sample_list=None, sens_type=None
)
sens.run()
self.sens.create_plot_p_h_pprime(save_name='test4',
out_dir=self.output_dir)
assert os.path.exists(os.path.join(self.output_dir,
'test4_P_H_P_prime.png'))

0 comments on commit 9d2d3bf

Please sign in to comment.
You can’t perform that action at this time.