Skip to content

Commit 7410889

Browse files
committed
notebook
1 parent 7275c45 commit 7410889

File tree

5 files changed

+309
-299
lines changed

5 files changed

+309
-299
lines changed

doc/examples/example_petab/petab.ipynb

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
},
1414
{
1515
"cell_type": "code",
16-
"execution_count": null,
1716
"metadata": {},
18-
"outputs": [],
1917
"source": [
2018
"import petab\n",
2119
"from amici import run_simulation\n",
2220
"from amici.petab.petab_import import import_petab_problem\n",
2321
"from amici.petab.petab_problem import PetabProblem\n",
2422
"from amici.petab.simulations import simulate_petab\n",
2523
"from amici.plotting import plot_state_trajectories"
26-
]
24+
],
25+
"outputs": [],
26+
"execution_count": null
2727
},
2828
{
2929
"cell_type": "markdown",
@@ -43,16 +43,16 @@
4343
},
4444
{
4545
"cell_type": "code",
46-
"execution_count": null,
4746
"metadata": {},
48-
"outputs": [],
4947
"source": [
5048
"model_name = \"Boehm_JProteomeRes2014\"\n",
5149
"# local path or URL to the yaml file for the PEtab problem\n",
5250
"petab_yaml = f\"https://benchmarking-initiative.github.io/Benchmark-Models-PEtab/tree/Benchmark-Models/{model_name}/{model_name}.yaml\"\n",
5351
"# load the problem using the PEtab library\n",
5452
"petab_problem = petab.Problem.from_yaml(petab_yaml)"
55-
]
53+
],
54+
"outputs": [],
55+
"execution_count": null
5656
},
5757
{
5858
"cell_type": "markdown",
@@ -64,12 +64,10 @@
6464
},
6565
{
6666
"cell_type": "code",
67-
"execution_count": null,
6867
"metadata": {},
68+
"source": "amici_model = import_petab_problem(petab_problem, verbose=False)",
6969
"outputs": [],
70-
"source": [
71-
"amici_model = import_petab_problem(petab_problem, verbose=False, compile_=True)"
72-
]
70+
"execution_count": null
7371
},
7472
{
7573
"cell_type": "markdown",
@@ -86,12 +84,12 @@
8684
},
8785
{
8886
"cell_type": "code",
89-
"execution_count": null,
9087
"metadata": {},
91-
"outputs": [],
9288
"source": [
9389
"simulate_petab(petab_problem, amici_model)"
94-
]
90+
],
91+
"outputs": [],
92+
"execution_count": null
9593
},
9694
{
9795
"cell_type": "markdown",
@@ -102,9 +100,7 @@
102100
},
103101
{
104102
"cell_type": "code",
105-
"execution_count": null,
106103
"metadata": {},
107-
"outputs": [],
108104
"source": [
109105
"parameters = {\n",
110106
" x_id: x_val\n",
@@ -119,7 +115,9 @@
119115
" problem_parameters=parameters,\n",
120116
" scaled_parameters=True,\n",
121117
")"
122-
]
118+
],
119+
"outputs": [],
120+
"execution_count": null
123121
},
124122
{
125123
"cell_type": "markdown",
@@ -133,9 +131,7 @@
133131
},
134132
{
135133
"cell_type": "code",
136-
"execution_count": null,
137134
"metadata": {},
138-
"outputs": [],
139135
"source": [
140136
"app = PetabProblem(petab_problem, amici_model)\n",
141137
"\n",
@@ -144,30 +140,32 @@
144140
"\n",
145141
"# ExpData for a single condition:\n",
146142
"edata = app.get_edata(\"model1_data1\")"
147-
]
143+
],
144+
"outputs": [],
145+
"execution_count": null
148146
},
149147
{
150148
"cell_type": "code",
151-
"execution_count": null,
152149
"metadata": {},
153-
"outputs": [],
154150
"source": [
155151
"rdata = run_simulation(\n",
156152
" amici_model, solver=amici_model.create_solver(), edata=edata\n",
157153
")\n",
158154
"rdata"
159-
]
155+
],
156+
"outputs": [],
157+
"execution_count": null
160158
},
161159
{
162160
"cell_type": "code",
163-
"execution_count": null,
164161
"metadata": {
165162
"collapsed": false
166163
},
167-
"outputs": [],
168164
"source": [
169165
"plot_state_trajectories(rdata)"
170-
]
166+
],
167+
"outputs": [],
168+
"execution_count": null
171169
},
172170
{
173171
"cell_type": "markdown",
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "d008dddcf9999983",
6+
"metadata": {},
7+
"source": [
8+
"# PEtab 2.0 import\n",
9+
"\n",
10+
"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."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"id": "initial_id",
16+
"metadata": {
17+
"collapsed": true
18+
},
19+
"source": [
20+
"import logging\n",
21+
"\n",
22+
"import amici\n",
23+
"from amici.petab.petab_importer import *\n",
24+
"from amici.petab.simulations import RDATAS\n",
25+
"from amici.plotting import *\n",
26+
"from petab.v2 import Problem"
27+
],
28+
"outputs": [],
29+
"execution_count": null
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"id": "a7cc06d3ead53fc5",
34+
"metadata": {},
35+
"source": [
36+
"We load a PEtab problem from the [PEtab benchmark problem collection](https://github.com/Benchmarking-Initiative/Benchmark-Models-PEtab).\n",
37+
"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."
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"id": "bb11cbb310be1e2b",
43+
"metadata": {},
44+
"source": [
45+
"# Load a PEtab v2 problem\n",
46+
"problem_id = \"Boehm_JProteomeRes2014\"\n",
47+
"petab_yaml = f\"https://benchmarking-initiative.github.io/Benchmark-Models-PEtab/tree/Benchmark-Models/{problem_id}/{problem_id}.yaml\"\n",
48+
"problem = Problem.from_yaml(petab_yaml)\n",
49+
"\n",
50+
"print(problem)"
51+
],
52+
"outputs": [],
53+
"execution_count": null
54+
},
55+
{
56+
"cell_type": "markdown",
57+
"id": "f3713e89cefc77f2",
58+
"metadata": {},
59+
"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"
60+
},
61+
{
62+
"cell_type": "code",
63+
"id": "1bc978a08382cd40",
64+
"metadata": {},
65+
"source": [
66+
"importer = PetabImporter(problem, verbose=logging.INFO)\n",
67+
"simulator = importer.create_simulator()"
68+
],
69+
"outputs": [],
70+
"execution_count": null
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"id": "e83a49495936c4eb",
75+
"metadata": {},
76+
"source": "Now let's run the simulations:"
77+
},
78+
{
79+
"cell_type": "code",
80+
"id": "cee93ebff9477aca",
81+
"metadata": {},
82+
"source": [
83+
"# simulate all conditions encoded in the PEtab problem for which there are measurements\n",
84+
"# using the nominal parameter values from the PEtab problem\n",
85+
"result = simulator.simulate(problem.get_x_nominal_dict())\n",
86+
"assert all(r.status == amici.AMICI_SUCCESS for r in result[RDATAS]), (\n",
87+
" \"Simulation failed.\"\n",
88+
")\n",
89+
"result"
90+
],
91+
"outputs": [],
92+
"execution_count": null
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"id": "3d73841ed61412ff",
97+
"metadata": {},
98+
"source": [
99+
"The returned dictionary contains the simulation results for all experimental conditions encoded in the PEtab problem in `result['rdatas']`.\n",
100+
"Those are the same objects as for any other AMICI simulation using `amici.run_simulation`.\n",
101+
"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",
102+
"`result['llh']` and `result['sllh']` contain the aggregated log-likelihood value and its gradient, respectively, over all experimental conditions.\n",
103+
"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",
104+
"\n",
105+
"Now, let's have a look at the results of the first experimental condition:"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"id": "e532cbc3ebb361ef",
111+
"metadata": {},
112+
"source": [
113+
"rdata = result[\"rdatas\"][0]\n",
114+
"edata = result[\"edatas\"][0]\n",
115+
"plot_observable_trajectories(rdata, model=simulator.model, edata=edata)"
116+
],
117+
"outputs": [],
118+
"execution_count": null
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"id": "8e99536dd10b5116",
123+
"metadata": {},
124+
"source": "Simulation settings can be adjusted via the `PetabSimulator.solver` and `PetabSimulator.model` attributes, which are instances of `amici.Solver` and `amici.Solver`, respectively."
125+
},
126+
{
127+
"cell_type": "markdown",
128+
"id": "e27686ae7a27d369",
129+
"metadata": {},
130+
"source": [
131+
"## Simulating individual conditions\n",
132+
"\n",
133+
"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:"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"id": "6b09db0d5fb98d5d",
139+
"metadata": {},
140+
"source": [
141+
"model = importer.create_model()\n",
142+
"# It's important to use the petab problem from the importer, as it was modified to encode the experimental conditions.\n",
143+
"em = ExperimentManager(model=model, petab_problem=importer.petab_problem)"
144+
],
145+
"outputs": [],
146+
"execution_count": null
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"id": "dcb79b9fca6d634d",
151+
"metadata": {},
152+
"source": "These are the experiments encoded in the PEtab problem:"
153+
},
154+
{
155+
"cell_type": "code",
156+
"id": "ee7582b9dc373519",
157+
"metadata": {},
158+
"source": [
159+
"importer.petab_problem.experiments"
160+
],
161+
"outputs": [],
162+
"execution_count": null
163+
},
164+
{
165+
"cell_type": "markdown",
166+
"id": "d240e8ecbe3d674e",
167+
"metadata": {},
168+
"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:"
169+
},
170+
{
171+
"cell_type": "code",
172+
"id": "fbd47a575ad57cd3",
173+
"metadata": {},
174+
"source": [
175+
"edata = em.create_edata(importer.petab_problem.experiments[-1])\n",
176+
"edata"
177+
],
178+
"outputs": [],
179+
"execution_count": null
180+
},
181+
{
182+
"cell_type": "code",
183+
"id": "9f24d8fdb8753fa1",
184+
"metadata": {},
185+
"source": [
186+
"rdata = model.simulate(edata=edata)\n",
187+
"assert rdata.status == amici.AMICI_SUCCESS, \"Simulation failed.\"\n",
188+
"rdata"
189+
],
190+
"outputs": [],
191+
"execution_count": null
192+
},
193+
{
194+
"cell_type": "code",
195+
"id": "53688eb0192a4793",
196+
"metadata": {},
197+
"source": [
198+
"plot_observable_trajectories(rdata, model=model, edata=edata)\n",
199+
"plot_state_trajectories(rdata, model=model)"
200+
],
201+
"outputs": [],
202+
"execution_count": null
203+
},
204+
{
205+
"cell_type": "code",
206+
"id": "631939e37d5b0b71",
207+
"metadata": {},
208+
"source": [
209+
"rdata.xr.x.to_pandas()"
210+
],
211+
"outputs": [],
212+
"execution_count": null
213+
},
214+
{
215+
"cell_type": "markdown",
216+
"id": "3ae8c49246a20092",
217+
"metadata": {},
218+
"source": [
219+
"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",
220+
"The parameter values can be updated using `em.apply_parameters({'parameter1': 0.1, ... })`."
221+
]
222+
},
223+
{
224+
"cell_type": "markdown",
225+
"id": "41130d2623334d3f",
226+
"metadata": {},
227+
"source": [
228+
"That's it! You have successfully imported and simulated a PEtab v2 problem using AMICI.\n",
229+
"\n",
230+
"Changing simulation settings, e.g., tolerances, sensitivity methods, etc., works as usual with AMICI models. Check out the other AMICI notebooks for more information."
231+
]
232+
}
233+
],
234+
"metadata": {
235+
"kernelspec": {
236+
"display_name": "Python 3",
237+
"language": "python",
238+
"name": "python3"
239+
},
240+
"language_info": {
241+
"codemirror_mode": {
242+
"name": "ipython",
243+
"version": 2
244+
},
245+
"file_extension": ".py",
246+
"mimetype": "text/x-python",
247+
"name": "python",
248+
"nbconvert_exporter": "python",
249+
"version": "2.7.6"
250+
}
251+
},
252+
"nbformat": 4,
253+
"nbformat_minor": 5
254+
}

0 commit comments

Comments
 (0)