-
Notifications
You must be signed in to change notification settings - Fork 33
Add support in JAX backend for PEtab v2 #3115
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
Changes from all commits
f867567
5d3e3f7
1a1a2f4
1742052
7342b51
2d5bbcb
7e5b901
2845a9f
7582858
ee769a3
7ddd9c4
f22cb29
cb75417
0c295e7
a0f7ee5
6da5af4
1661093
9dab28b
a654319
496ef67
aa26269
6044118
b1bf285
db0be00
89eda36
b7e3852
86444bb
c226014
c7e9981
da5809b
9c23d7a
319dd58
767c63b
fa8b606
3cb0c83
00b254e
eb84774
82cd004
ed6c476
cc8f021
dcc0234
34882b1
f3d0539
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,8 @@ | |
| "outputs": [], | ||
| "source": [ | ||
| "import petab.v1 as petab\n", | ||
| "from amici.importers.petab.v1 import import_petab_problem\n", | ||
| "from amici.importers.petab import *\n", | ||
| "from petab.v2 import Problem\n", | ||
| "\n", | ||
| "# Define the model name and YAML file location\n", | ||
| "model_name = \"Boehm_JProteomeRes2014\"\n", | ||
|
|
@@ -41,14 +42,20 @@ | |
| " f\"master/Benchmark-Models/{model_name}/{model_name}.yaml\"\n", | ||
| ")\n", | ||
| "\n", | ||
| "# Load the PEtab problem from the YAML file\n", | ||
| "petab_problem = petab.Problem.from_yaml(yaml_url)\n", | ||
| "# Load the PEtab problem from the YAML file as a PEtab v2 problem\n", | ||
| "# (the JAX backend only supports PEtab v2)\n", | ||
| "petab_problem = Problem.from_yaml(yaml_url)\n", | ||
| "\n", | ||
| "# Import the PEtab problem as a JAX-compatible AMICI problem\n", | ||
| "jax_problem = import_petab_problem(\n", | ||
| " petab_problem,\n", | ||
| " verbose=False, # no text output\n", | ||
| " jax=True, # return jax problem\n", | ||
| "pi = PetabImporter(\n", | ||
| " petab_problem=petab_problem,\n", | ||
| " module_name=model_name,\n", | ||
| " compile_=True,\n", | ||
| " jax=True,\n", | ||
| ")\n", | ||
| "\n", | ||
| "jax_problem = pi.create_simulator(\n", | ||
| " force_import=True,\n", | ||
| ")" | ||
| ] | ||
| }, | ||
|
|
@@ -75,6 +82,16 @@ | |
| "llh, results = run_simulations(jax_problem)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "6c5b2980-13f0-42e9-b13e-0fce05793910", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "results" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "415962751301c64a", | ||
|
|
@@ -90,11 +107,11 @@ | |
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Define the simulation condition\n", | ||
| "simulation_condition = (\"model1_data1\",)\n", | ||
| "# # Define the simulation condition\n", | ||
| "experiment_condition = \"_petab_experiment_condition___default__\"\n", | ||
| "\n", | ||
| "# Access the results for the specified condition\n", | ||
| "ic = results[\"simulation_conditions\"].index(simulation_condition)\n", | ||
| "# # Access the results for the specified condition\n", | ||
| "ic = results[\"dynamic_conditions\"].index(experiment_condition)\n", | ||
| "print(\"llh: \", results[\"llh\"][ic])\n", | ||
| "print(\"state variables: \", results[\"x\"][ic, :])" | ||
| ] | ||
|
|
@@ -146,8 +163,8 @@ | |
| "import matplotlib.pyplot as plt\n", | ||
| "import numpy as np\n", | ||
| "\n", | ||
| "# Define the simulation condition\n", | ||
| "simulation_condition = (\"model1_data1\",)\n", | ||
| "# Define the experiment condition\n", | ||
| "experiment_condition = \"_petab_experiment_condition___default__\"\n", | ||
| "\n", | ||
| "\n", | ||
| "def plot_simulation(results):\n", | ||
|
|
@@ -158,7 +175,7 @@ | |
| " results (dict): Simulation results from run_simulations.\n", | ||
| " \"\"\"\n", | ||
| " # Extract the simulation results for the specific condition\n", | ||
| " ic = results[\"simulation_conditions\"].index(simulation_condition)\n", | ||
| " ic = results[\"dynamic_conditions\"].index(experiment_condition)\n", | ||
| "\n", | ||
| " # Create a new figure for the state trajectories\n", | ||
| " plt.figure(figsize=(8, 6))\n", | ||
|
|
@@ -172,7 +189,7 @@ | |
| " # Add labels, legend, and grid\n", | ||
| " plt.xlabel(\"Time\")\n", | ||
| " plt.ylabel(\"State Values\")\n", | ||
| " plt.title(simulation_condition)\n", | ||
| " plt.title(experiment_condition)\n", | ||
| " plt.legend()\n", | ||
| " plt.grid(True)\n", | ||
| " plt.show()\n", | ||
|
|
@@ -187,18 +204,7 @@ | |
| "id": "4fa97c33719c2277", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "`run_simulations` enables users to specify the simulation conditions to be executed. For more complex models, this allows for restricting simulations to a subset of conditions. Since the Böhm model includes only a single condition, we demonstrate this functionality by simulating no condition at all." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "7950774a3e989042", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "llh, results = run_simulations(jax_problem, simulation_conditions=tuple())\n", | ||
| "results" | ||
| "`run_simulations` enables users to specify the simulation experiments to be executed. For more complex models, this allows for restricting simulations to a subset of experiments by passing a tuple of experiment ids under the keyword `simulation_experiments` to `run_simulations`." | ||
| ] | ||
| }, | ||
| { | ||
|
|
@@ -384,8 +390,8 @@ | |
| "from amici.jax import ReturnValue\n", | ||
| "\n", | ||
| "# Define the simulation condition\n", | ||
| "simulation_condition = (\"model1_data1\",)\n", | ||
| "ic = jax_problem.simulation_conditions.index(simulation_condition)\n", | ||
| "experiment_condition = \"_petab_experiment_condition___default__\"\n", | ||
| "ic = 0\n", | ||
| "\n", | ||
| "# Load condition-specific data\n", | ||
| "ts_dyn = jax_problem._ts_dyn[ic, :]\n", | ||
|
|
@@ -397,7 +403,7 @@ | |
| "nps = jax_problem._np_numeric[ic, :]\n", | ||
| "\n", | ||
| "# Load parameters for the specified condition\n", | ||
| "p = jax_problem.load_model_parameters(simulation_condition[0])\n", | ||
| "p = jax_problem.load_model_parameters(jax_problem._petab_problem.experiments[0], is_preeq=False)\n", | ||
| "\n", | ||
| "\n", | ||
| "# Define a function to compute the gradient with respect to dynamic timepoints\n", | ||
|
|
@@ -431,13 +437,17 @@ | |
| "cell_type": "markdown", | ||
| "id": "19ca88c8900584ce", | ||
| "metadata": {}, | ||
| "source": "## Model training" | ||
| "source": [ | ||
| "## Model training" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "7f99c046d7d4e225", | ||
| "metadata": {}, | ||
| "source": "This setup makes it pretty straightforward to train models using [equinox](https://docs.kidger.site/equinox/) and [optax](https://optax.readthedocs.io/en/latest/) frameworks. Below we provide barebones implementation that runs training for 5 steps using Adam." | ||
| "source": [ | ||
| "This setup makes it pretty straightforward to train models using [equinox](https://docs.kidger.site/equinox/) and [optax](https://optax.readthedocs.io/en/latest/) frameworks. Below we provide barebones implementation that runs training for 5 steps using Adam." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
|
|
@@ -569,16 +579,20 @@ | |
| "from amici.sim.sundials.petab.v1 import simulate_petab\n", | ||
| "\n", | ||
| "# Import the PEtab problem as a standard AMICI model\n", | ||
| "amici_model = import_petab_problem(\n", | ||
| " petab_problem,\n", | ||
| " verbose=False,\n", | ||
| " jax=False, # load the amici model this time\n", | ||
| "pi = PetabImporter(\n", | ||
| " petab_problem=petab_problem,\n", | ||
| " module_name=model_name,\n", | ||
| " compile_=True,\n", | ||
| " jax=False,\n", | ||
| ")\n", | ||
| "\n", | ||
| "amici_model = pi.create_simulator(\n", | ||
| " force_import=True,\n", | ||
| ")\n", | ||
| "\n", | ||
| "# Configure the solver with appropriate tolerances\n", | ||
| "solver = amici_model.create_solver()\n", | ||
| "solver.set_absolute_tolerance(1e-8)\n", | ||
| "solver.set_relative_tolerance(1e-16)\n", | ||
| "amici_model.solver.set_absolute_tolerance(1e-8)\n", | ||
| "amici_model.solver.set_relative_tolerance(1e-16)\n", | ||
| "\n", | ||
| "# Prepare the parameters for the simulation\n", | ||
| "problem_parameters = dict(\n", | ||
|
|
@@ -594,86 +608,65 @@ | |
| "outputs": [], | ||
| "source": [ | ||
| "# Profile simulation only\n", | ||
| "solver.set_sensitivity_order(SensitivityOrder.none)" | ||
| "amici_model.solver.set_sensitivity_order(SensitivityOrder.none)" | ||
| ] | ||
| }, | ||
| { | ||
| "metadata": {}, | ||
| "cell_type": "code", | ||
| "outputs": [], | ||
| "execution_count": null, | ||
| "id": "42cbc67bc09b67dc", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "%%timeit\n", | ||
| "simulate_petab(\n", | ||
| " petab_problem,\n", | ||
| " amici_model=amici_model,\n", | ||
| " solver=solver,\n", | ||
| " problem_parameters=problem_parameters,\n", | ||
| " scaled_parameters=True,\n", | ||
| " scaled_gradients=True,\n", | ||
| ")" | ||
| ], | ||
| "id": "42cbc67bc09b67dc" | ||
| "amici_model.simulate(petab_problem.get_x_nominal_dict())" | ||
| ] | ||
| }, | ||
| { | ||
| "metadata": {}, | ||
| "cell_type": "code", | ||
| "outputs": [], | ||
| "execution_count": null, | ||
| "id": "4f1c06c5893a9c07", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Profile gradient computation using forward sensitivity analysis\n", | ||
| "solver.set_sensitivity_order(SensitivityOrder.first)\n", | ||
| "solver.set_sensitivity_method(SensitivityMethod.forward)" | ||
| ], | ||
| "id": "4f1c06c5893a9c07" | ||
| "amici_model.solver.set_sensitivity_order(SensitivityOrder.first)\n", | ||
| "amici_model.solver.set_sensitivity_method(SensitivityMethod.forward)" | ||
| ] | ||
| }, | ||
| { | ||
| "metadata": {}, | ||
| "cell_type": "code", | ||
| "outputs": [], | ||
| "execution_count": null, | ||
| "id": "7367a19bcea98597", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "%%timeit\n", | ||
| "simulate_petab(\n", | ||
| " petab_problem,\n", | ||
| " amici_model=amici_model,\n", | ||
| " solver=solver,\n", | ||
| " problem_parameters=problem_parameters,\n", | ||
| " scaled_parameters=True,\n", | ||
| " scaled_gradients=True,\n", | ||
| ")" | ||
| ], | ||
| "id": "7367a19bcea98597" | ||
| "amici_model.simulate(petab_problem.get_x_nominal_dict())" | ||
| ] | ||
| }, | ||
| { | ||
| "metadata": {}, | ||
| "cell_type": "code", | ||
| "outputs": [], | ||
| "execution_count": null, | ||
| "id": "a31e8eda806c2d7", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Profile gradient computation using adjoint sensitivity analysis\n", | ||
| "solver.set_sensitivity_order(SensitivityOrder.first)\n", | ||
| "solver.set_sensitivity_method(SensitivityMethod.adjoint)" | ||
| ], | ||
| "id": "a31e8eda806c2d7" | ||
| "amici_model.solver.set_sensitivity_order(SensitivityOrder.first)\n", | ||
| "amici_model.solver.set_sensitivity_method(SensitivityMethod.adjoint)" | ||
| ] | ||
| }, | ||
| { | ||
| "metadata": {}, | ||
| "cell_type": "code", | ||
| "outputs": [], | ||
| "execution_count": null, | ||
| "id": "3f2ab1acb3ba818f", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "%%timeit\n", | ||
| "simulate_petab(\n", | ||
| " petab_problem,\n", | ||
| " amici_model=amici_model,\n", | ||
| " solver=solver,\n", | ||
| " problem_parameters=problem_parameters,\n", | ||
| " scaled_parameters=True,\n", | ||
| " scaled_gradients=True,\n", | ||
| ")" | ||
| ], | ||
| "id": "3f2ab1acb3ba818f" | ||
| "amici_model.simulate(petab_problem.get_x_nominal_dict())" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the notebook runs there is an error from this cell that the FIM was not computed: https://github.com/AMICI-dev/AMICI/actions/runs/21985309084/job/63517900881?pr=3115 @dweindl can you advise on that?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My fault. I hope #3125 fixes that. |
||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
|
|
@@ -691,7 +684,8 @@ | |
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3" | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.12.3" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.