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

Added a brief roadrunner example. #1348

Merged
merged 6 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Algorithms and features
example/ordinal_data.ipynb
example/censored_data.ipynb
example/semiquantitative_data.ipynb
example/roadrunner.ipynb

Application examples
--------------------
Expand Down
326 changes: 326 additions & 0 deletions doc/example/roadrunner.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,326 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
" # RoadRunner in pyPESTO\n",
"\n",
"**After going through this notebook, you will be able to...**\n",
"\n",
"* ... create a pyPESTO problem using [RoadRunner](https://www.libroadrunner.org) as a simulator directly from a PEtab problem.\n",
"* ... perform a parameter estimation using pyPESTO with RoadRunner as a simulator, setting advanced simulator features."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# install pyPESTO with roadrunner support\n",
"# %pip install pypesto[roadrunner,petab] --quiet"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# import\n",
"import random\n",
"import matplotlib as mpl\n",
"import petab\n",
"import pypesto.objective\n",
"import pypesto.optimize as optimize\n",
"import pypesto.objective.roadrunner as pypesto_rr\n",
"import pypesto.sample as sample\n",
"import pypesto.visualize as visualize\n",
"import pypesto.profile as profile\n",
"from IPython.display import Markdown, display\n",
"from pprint import pprint\n",
"\n",
"mpl.rcParams[\"figure.dpi\"] = 100\n",
"mpl.rcParams[\"font.size\"] = 18\n",
"\n",
"random.seed(1912)\n",
"\n",
"\n",
"# name of the model that will also be the name of the python module\n",
"model_name = \"boehm_JProteomeRes2014\"\n",
"\n",
"# output directory\n",
"model_output_dir = \"tmp/\" + model_name"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Creating pyPESTO problem from PEtab\n",
"\n",
"The [PEtab file format](https://petab.readthedocs.io/en/latest/documentation_data_format.html) stores all the necessary information to define a parameter estimation problem. This includes the model, the experimental data, the parameters to estimate, and the experimental conditions. Using the `pypesto_rr.PetabImporterRR` class, we can create a pyPESTO problem directly from a PEtab problem."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"petab_yaml = f\"./{model_name}/{model_name}.yaml\"\n",
"\n",
"petab_problem = petab.Problem.from_yaml(petab_yaml)\n",
"importer = pypesto_rr.PetabImporterRR(petab_problem)\n",
"problem = importer.create_problem()"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"We now have a pyPESTO problem that we can use to perform parameter estimation. We can get some information on the RoadRunnerObjective and access the RoadRunner model."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"pprint(problem.objective.get_config())"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# direct simulation of the model using roadrunner\n",
"sim_res = problem.objective.roadrunner_instance.simulate(\n",
" times=[0, 2.5, 5, 10, 20, 50]\n",
")\n",
"pprint(sim_res)\n",
"problem.objective.roadrunner_instance.plot();"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"For more details on interacting with the roadrunner instance, we refer to the [documentation of libroadrunner](https://libroadrunner.readthedocs.io/en/latest/). However, we point out that while theoretical possible, we **strongly advice against** changing the model in that manner."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"ret = problem.objective(\n",
" petab_problem.get_x_nominal(fixed=False,scaled=True),\n",
" mode=\"mode_fun\",\n",
" return_dict=True,\n",
")\n",
"pprint(ret)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Optimization\n",
"\n",
"To optimize a problem using a RoadRunner objective, we can set additional solver options for the ODE solver."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"optimizer = optimize.ScipyOptimizer()\n",
"\n",
"solver_options = pypesto_rr.SolverOptions(\n",
" relative_tolerance = 1e-6,\n",
" absolute_tolerance = 1e-12,\n",
" maximum_num_steps = 10000\n",
")\n",
"engine = pypesto.engine.SingleCoreEngine()\n",
"problem.objective.solver_options = solver_options"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"result = optimize.minimize(\n",
" problem=problem,\n",
" optimizer=optimizer,\n",
" n_starts=5, # usually a value >= 100 should be used\n",
" engine=engine\n",
")\n",
"display(Markdown(result.summary()))"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Disclaimer: Currently there are two main things not yet fully supported with roadrunner objectives. One is parallelization of the optimization using MultiProcessEngine. The other is explicit gradients of the objective function. While the former will be added in a near future version, we will show a workaround for the latter, until it is implemented."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"### Visualization Methods\n",
"\n",
"In order to visualize the optimization, there are a few things possible. For a more extensive explanation we refer to the \"getting started\" notebook."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"visualize.waterfall(result);"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"visualize.parameters(result);"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"visualize.parameters_correlation_matrix(result);"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"### Sensitivities via finite differences\n",
"\n",
"Some solvers need a way to calculate the sensitivities, which currently RoadRunner Objectives do not suport. For this scenario, we can use the FiniteDifferences objective in pypesto, which wraps a given objective into one, that computes sensitivities via finite differences."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# no support for sensitivities\n",
"try:\n",
" ret = problem.objective(\n",
" petab_problem.get_x_nominal(fixed=False,scaled=True),\n",
" mode=\"mode_fun\",\n",
" return_dict=True,\n",
" sensi_orders=(1,),\n",
" )\n",
" pprint(ret)\n",
"except ValueError as e:\n",
" pprint(e)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"objective_fd = pypesto.objective.FD(problem.objective)\n",
"# support through finite differences\n",
"try:\n",
" ret = objective_fd(\n",
" petab_problem.get_x_nominal(fixed=False,scaled=True),\n",
" mode=\"mode_fun\",\n",
" return_dict=True,\n",
" sensi_orders=(1,),\n",
" )\n",
" pprint(ret)\n",
"except ValueError as e:\n",
" pprint(e)"
],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
2 changes: 1 addition & 1 deletion pypesto/objective/roadrunner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from .petab_importer_roadrunner import PetabImporterRR
from .road_runner import RoadRunnerObjective
from .roadrunner_calculator import RoadRunnerCalculator
from .utils import simulation_to_measurement_df
from .utils import ExpData, SolverOptions, simulation_to_measurement_df
1 change: 1 addition & 0 deletions test/run_notebook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ nbs_1=(
'censored_data.ipynb'
'semiquantitative_data.ipynb'
'getting_started.ipynb'
'roadrunner.ipynb'
)

# Sampling notebooks
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ description =

[testenv:notebooks1]
allowlist_externals = bash
extras = example,amici,petab,pyswarm,pymc3,cma,nlopt,fides
extras = example,amici,petab,pyswarm,pymc3,cma,nlopt,fides,roadrunner
commands =
bash test/run_notebook.sh 1
description =
Expand Down
Loading