Time Series Sensitivities

Note

For these demonstrations we use a toy sbml model prepackaged with roadrunner

Using time series sensitivities

In this example we show you how to use roadrunners time series sensitivities feature. Roadrunner will integrate the model and compute sensitivities at each time step.

Start integration with sensitivities at 0, end at 10 and collect 11 data points The return type is a 4-Tuple, a time vector and a 3D sensitivity matrix with dimensions time x parameters x variables - that is, an array of matrices corresponding to the time points in the time vector with parameters down the rows and model variables (species) accross the columns. The third and forth elements are row and column names respectively.

 1import roadrunner
 2from roadrunner import RoadRunner
 3from roadrunner.testing.TestModelFactory import TestModelFactory, getAvailableTestModels
 4from os.path import join, dirname
 5
 6# note: use getAvailableTestModels() to see options for test models.
 7sbml = TestModelFactory("OpenLinearFlux").str() # get the test model's sbml string
 8
 9# print(sbml) # uncomment if you want to see the sbml
10
11# Create a RoadRunner instance
12model = RoadRunner(sbml)
13sensSolver = model.getSensitivitySolver()
14
15time, sens, rownames, colnames = model.timeSeriesSensitivities(0, 10, 11)
16
17print("time: ", time)
18print("rownames: ", rownames)
19print("colnames: ", colnames)
20print(sens)
21
22fname = join(dirname(__file__), "OpenLinearFluxSensitivities.png")
23roadrunner.plotTimeSeriesSens(time, sens, rownames, colnames, ncol=2, fname=fname)
24print(f"Plot image saved to \"{fname}\"")
25

Selecting parameters

By default, all model parameters are automatically selected. You can be more selective about which parameters to compute sensitivities for. This will have a performance advantage, because sensitivities are only computed for the parameters specified here.

 1from roadrunner import RoadRunner
 2from roadrunner.testing.TestModelFactory import TestModelFactory, getAvailableTestModels
 3
 4
 5# note: use getAvailableTestModels() to see options for test models.
 6sbml = TestModelFactory("OpenLinearFlux").str() # get the test model's sbml string
 7
 8# print(sbml) # uncomment if you want to see the sbml
 9
10# Create a RoadRunner instance
11model = RoadRunner(sbml)
12sensSolver = model.getSensitivitySolver()
13
14time, sens, rownames, colnames = model.timeSeriesSensitivities(0, 10, 11, params=["kin"])
15
16print("time: ", time)
17print("rownames: ", rownames)
18print("colnames: ", colnames)
19print(sens)
20

Selecting Species

By default, all model species are returned in the results matrix. We can be more selective by using the species argument. This does not have any additional performance advantage and in fact cost additional resources because the full results matrix is sliced to select the selected columns.

 1from roadrunner import RoadRunner
 2from roadrunner.testing.TestModelFactory import TestModelFactory, getAvailableTestModels
 3
 4
 5# note: use getAvailableTestModels() to see options for test models.
 6sbml = TestModelFactory("OpenLinearFlux").str() # get the test model's sbml string
 7
 8# print(sbml) # uncomment if you want to see the sbml
 9
10# Create a RoadRunner instance
11model = RoadRunner(sbml)
12sensSolver = model.getSensitivitySolver()
13
14time, sens, rownames, colnames = model.timeSeriesSensitivities(0, 10, 11, species=["S1"])
15
16print("time: ", time)
17print("rownames: ", rownames)
18print("colnames: ", colnames)
19print(sens)

Sensitivity Method

RoadRunner time series sensitivities uses cvodes under the hood. By default, we use the “simultaneous” method, whereby sensitivity equations are solved at the same time as the variables in the ODE’s. This can be changed so that the variables are solved first followed by the sensitivity equations; aka in a staggered approach.

 1from roadrunner import RoadRunner
 2from roadrunner.testing.TestModelFactory import TestModelFactory, getAvailableTestModels
 3
 4
 5# note: use getAvailableTestModels() to see options for test models.
 6sbml = TestModelFactory("OpenLinearFlux").str() # get the test model's sbml string
 7
 8# print(sbml) # uncomment if you want to see the sbml
 9
10# Create a RoadRunner instance
11model = RoadRunner(sbml)
12sensSolver = model.getSensitivitySolver()
13
14sensSolver.setValue("sensitivity_method", "staggered")
15time, sens, rownames, colnames = model.timeSeriesSensitivities(0, 10, 11, k=0)
16
17print("using a staggered approach")
18print("time: ", time)
19print("rownames: ", rownames)
20print("colnames: ", colnames)
21print(sens)
22sensSolver.setValue("sensitivity_method", "simultaneous") # return to simultaneous mode

Sensitivity Difference Quotient Method

RoadRunner via cvodes uses finite differences for approximating gradients. By default we use the “centered” method, while the alternative is the “forward” method Both methods are implemented in cvodes and automatically figure out the best value of delta (perturbation amount) to use. Moreover, there is an adaptive switching mechanism in use that makes it unlikely that users should need to modify this setting.

 1from roadrunner import RoadRunner
 2from roadrunner.testing.TestModelFactory import TestModelFactory, getAvailableTestModels
 3
 4
 5# note: use getAvailableTestModels() to see options for test models.
 6sbml = TestModelFactory("OpenLinearFlux").str() # get the test model's sbml string
 7
 8# print(sbml) # uncomment if you want to see the sbml
 9
10# Create a RoadRunner instance
11model = RoadRunner(sbml)
12sensSolver = model.getSensitivitySolver()
13
14
15
16sensSolver.setValue("DQ_method", "forward")
17time, sens, rownames, colnames = model.timeSeriesSensitivities(0, 10, 11, k=0)
18
19
20print("using forward method of finite different approximation")
21print("time: ", time)
22print("rownames: ", rownames)
23print("colnames: ", colnames)
24print(sens)
25sensSolver.setValue("DQ_method", "centered") # put back the default value

Sensitivity Solver Type

By default, RoadRunner uses the sundials “newton” nonlinear solver. This can be changed to the “fixed_point” nonlinear solver

 1from roadrunner import RoadRunner
 2from roadrunner.testing.TestModelFactory import TestModelFactory, getAvailableTestModels
 3
 4
 5# note: use getAvailableTestModels() to see options for test models.
 6sbml = TestModelFactory("OpenLinearFlux").str() # get the test model's sbml string
 7
 8# print(sbml) # uncomment if you want to see the sbml
 9
10# Create a RoadRunner instance
11model = RoadRunner(sbml)
12sensSolver = model.getSensitivitySolver()
13
14
15
16# By default, RoadRunner uses the sundials "newton" nonlinear solver.
17# This can be changed to the fixed_point nonlinear solver
18sensSolver.setValue("nonlinear_solver", "fixed_point")
19time, sens, rownames, colnames = model.timeSeriesSensitivities(0, 10, 11, k=0)
20
21print("using the fixed point nonlinear solver")
22print("time: ", time)
23print("rownames: ", rownames)
24print("colnames: ", colnames)
25print(sens)