{ "cells": [ { "cell_type": "markdown", "id": "d008dddcf9999983", "metadata": {}, "source": [ "# PEtab 2.0 import\n", "\n", "**Note:** PEtab v2 support in AMICI is experimental and subject to change.\n", "\n", "This notebook demonstrates how to import a [PEtab v2 problem](https://petab.readthedocs.io/en/latest/v2/documentation_data_format.html) for computing the objective function and gradient, or for simulating individual conditions using AMICI. We assume that you are already familiar with the basics of AMICI and PEtab." ] }, { "cell_type": "code", "id": "initial_id", "metadata": { "collapsed": true }, "source": [ "import logging\n", "\n", "import amici\n", "from amici.petab.petab_importer import *\n", "from amici.petab.simulations import RDATAS\n", "from amici.plotting import *\n", "from petab.v2 import Problem" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "a7cc06d3ead53fc5", "metadata": {}, "source": [ "We load a PEtab problem from the [PEtab benchmark problem collection](https://github.com/Benchmarking-Initiative/Benchmark-Models-PEtab).\n", "Currently, these are only available in PEtab 1.0 format. However, when loading them with `petab.v2.Problem.from_yaml`, they will be automatically converted to the PEtab 2.0 format." ] }, { "cell_type": "code", "id": "bb11cbb310be1e2b", "metadata": {}, "source": [ "# Load a PEtab v2 problem\n", "problem_id = \"Boehm_JProteomeRes2014\"\n", "petab_yaml = f\"https://benchmarking-initiative.github.io/Benchmark-Models-PEtab/tree/Benchmark-Models/{problem_id}/{problem_id}.yaml\"\n", "problem = Problem.from_yaml(petab_yaml)\n", "\n", "print(problem)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "f3713e89cefc77f2", "metadata": {}, "source": "First, we create an AMICI model from the PEtab v2 problem via `PetabImporter`. This will account for the observation model encoded in the PEtab problem, as well as for the different experimental conditions. This is important to keep in mind when using the model anything else than the PEtab-encoded experiments, which should generally be avoided. For the actual simulations, the easiest way is to use a `PetabSimulator`.\n" }, { "cell_type": "code", "id": "1bc978a08382cd40", "metadata": {}, "source": [ "importer = PetabImporter(problem, verbose=logging.INFO)\n", "simulator = importer.create_simulator()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "e83a49495936c4eb", "metadata": {}, "source": "Now let's run the simulations:" }, { "cell_type": "code", "id": "cee93ebff9477aca", "metadata": {}, "source": [ "# simulate all conditions encoded in the PEtab problem for which there are measurements\n", "# using the nominal parameter values from the PEtab problem\n", "result = simulator.simulate(problem.get_x_nominal_dict())\n", "assert all(r.status == amici.AMICI_SUCCESS for r in result[RDATAS]), (\n", " \"Simulation failed.\"\n", ")\n", "result" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "3d73841ed61412ff", "metadata": {}, "source": [ "The returned dictionary contains the simulation results for all experimental conditions encoded in the PEtab problem in `result['rdatas']`.\n", "Those are the same objects as for any other AMICI simulation using `amici.run_simulation`.\n", "Additionally, the dictionary contains the `ExpData` instances used for the simulations in `result['edatas']`, which we will use below to visualize the PEtab-encoded measurements.\n", "`result['llh']` and `result['sllh']` contain the aggregated log-likelihood value and its gradient, respectively, over all experimental conditions.\n", "These can be used directly for parameter estimation. However, for parameter estimation, it is recommended to use the `pypesto` package that provides a full parameter estimation framework on top of AMICI and PEtab.\n", "\n", "Now, let's have a look at the results of the first experimental condition:" ] }, { "cell_type": "code", "id": "e532cbc3ebb361ef", "metadata": {}, "source": [ "rdata = result[\"rdatas\"][0]\n", "edata = result[\"edatas\"][0]\n", "plot_observable_trajectories(rdata, model=simulator.model, edata=edata)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "8e99536dd10b5116", "metadata": {}, "source": "Simulation settings can be adjusted via the `PetabSimulator.solver` and `PetabSimulator.model` attributes, which are instances of `amici.Solver` and `amici.Solver`, respectively." }, { "cell_type": "markdown", "id": "e27686ae7a27d369", "metadata": {}, "source": [ "## Simulating individual conditions\n", "\n", "It's also possible to simulate only specific experimental conditions encoded in the PEtab problem. The `ExperimentManager` takes care of creating `ExpData` the respective instances, and setting the condition-specific parameters, measurements, and initial states:" ] }, { "cell_type": "code", "id": "6b09db0d5fb98d5d", "metadata": {}, "source": [ "model = importer.create_model()\n", "# It's important to use the petab problem from the importer, as it was modified to encode the experimental conditions.\n", "em = ExperimentManager(model=model, petab_problem=importer.petab_problem)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "dcb79b9fca6d634d", "metadata": {}, "source": "These are the experiments encoded in the PEtab problem:" }, { "cell_type": "code", "id": "ee7582b9dc373519", "metadata": {}, "source": [ "importer.petab_problem.experiments" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "d240e8ecbe3d674e", "metadata": {}, "source": "We can now create an `ExpData` instance for any experiment, by passing the `petab.v2.Experiment` or its ID to `em.create_edata`, and simulate it as usual with AMICI:" }, { "cell_type": "code", "id": "fbd47a575ad57cd3", "metadata": {}, "source": [ "edata = em.create_edata(importer.petab_problem.experiments[-1])\n", "edata" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "9f24d8fdb8753fa1", "metadata": {}, "source": [ "rdata = model.simulate(edata=edata)\n", "assert rdata.status == amici.AMICI_SUCCESS, \"Simulation failed.\"\n", "rdata" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "53688eb0192a4793", "metadata": {}, "source": [ "plot_observable_trajectories(rdata, model=model, edata=edata)\n", "plot_state_trajectories(rdata, model=model)" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "id": "631939e37d5b0b71", "metadata": {}, "source": [ "rdata.xr.x.to_pandas()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "3ae8c49246a20092", "metadata": {}, "source": [ "The parameter values used for the simulation are the nominal values from the PEtab problem by default. You can, of course, also provide custom parameter values.\n", "The parameter values can be updated using `em.apply_parameters({'parameter1': 0.1, ... })`." ] }, { "cell_type": "markdown", "id": "41130d2623334d3f", "metadata": {}, "source": [ "That's it! You have successfully imported and simulated a PEtab v2 problem using AMICI.\n", "\n", "Changing simulation settings, e.g., tolerances, sensitivity methods, etc., works as usual with AMICI models. Check out the other AMICI notebooks for more information." ] } ], "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", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }