From e2fb5ac320be49ee6051d645dfca072dbe93e359 Mon Sep 17 00:00:00 2001 From: PaulJonasJost Date: Sun, 31 Mar 2024 20:11:59 -0600 Subject: [PATCH 1/4] Added a brief roadrunner example. to be extended. --- doc/example.rst | 1 + doc/example/roadrunner.ipynb | 326 +++++++++++++++++++++++ pypesto/objective/roadrunner/__init__.py | 2 +- test/run_notebook.sh | 1 + 4 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 doc/example/roadrunner.ipynb diff --git a/doc/example.rst b/doc/example.rst index a6f694110..4c7a263c8 100644 --- a/doc/example.rst +++ b/doc/example.rst @@ -54,6 +54,7 @@ Algorithms and features example/ordinal_data.ipynb example/censored_data.ipynb example/semiquantitative_data.ipynb + example/roadrunner.ipynb Application examples -------------------- diff --git a/doc/example/roadrunner.ipynb b/doc/example/roadrunner.ipynb new file mode 100644 index 000000000..df44964f9 --- /dev/null +++ b/doc/example/roadrunner.ipynb @@ -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]() 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]() 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](). 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 explaination 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 +} diff --git a/pypesto/objective/roadrunner/__init__.py b/pypesto/objective/roadrunner/__init__.py index c2b3325da..3b6e800e2 100644 --- a/pypesto/objective/roadrunner/__init__.py +++ b/pypesto/objective/roadrunner/__init__.py @@ -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 diff --git a/test/run_notebook.sh b/test/run_notebook.sh index c885350c5..cee778651 100755 --- a/test/run_notebook.sh +++ b/test/run_notebook.sh @@ -29,6 +29,7 @@ nbs_1=( 'censored_data.ipynb' 'semiquantitative_data.ipynb' 'getting_started.ipynb' + 'roadrunner.ipynb' ) # Sampling notebooks From b9d1b050e6876a631f8c48b4bffbbb64fa283033 Mon Sep 17 00:00:00 2001 From: PaulJonasJost Date: Tue, 2 Apr 2024 11:02:05 +0200 Subject: [PATCH 2/4] Added roadrunner into tox.ini for notebook 1 --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 2aa6d02e8..2c860f336 100644 --- a/tox.ini +++ b/tox.ini @@ -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 = From bf99fc8deeb1047873fb5c94284a0f449c46a5dc Mon Sep 17 00:00:00 2001 From: PaulJonasJost Date: Fri, 5 Apr 2024 11:19:10 +0200 Subject: [PATCH 3/4] Filled in hyperrefs --- doc/example/roadrunner.ipynb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/example/roadrunner.ipynb b/doc/example/roadrunner.ipynb index df44964f9..b80b2bb9e 100644 --- a/doc/example/roadrunner.ipynb +++ b/doc/example/roadrunner.ipynb @@ -65,7 +65,7 @@ "source": [ "## Creating pyPESTO problem from PEtab\n", "\n", - "The [PEtab file format]() 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." + "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 @@ -125,7 +125,7 @@ { "cell_type": "markdown", "source": [ - "For more details on interacting with the roadrunner instance, we refer to the [documentation of libroadrunner](). However, we point out that while theoretical possible, we **strongly advice against** changing the model in that manner." + "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 @@ -208,7 +208,7 @@ "source": [ "### Visualization Methods\n", "\n", - "In order to visualize the optimization, there are a few things possible. For a more extensive explaination we refer to the [\"getting started\" notebook]()." + "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 From 332514b97867efd3ad273016e5064a60e5f24871 Mon Sep 17 00:00:00 2001 From: PaulJonasJost Date: Fri, 5 Apr 2024 13:13:09 +0200 Subject: [PATCH 4/4] Added missing hyperref --- doc/example/roadrunner.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/example/roadrunner.ipynb b/doc/example/roadrunner.ipynb index b80b2bb9e..06b540937 100644 --- a/doc/example/roadrunner.ipynb +++ b/doc/example/roadrunner.ipynb @@ -7,7 +7,7 @@ "\n", "**After going through this notebook, you will be able to...**\n", "\n", - "* ... create a pyPESTO problem using [RoadRunner]() as a simulator directly from a PEtab problem.\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": {