|
11 | 11 | ] |
12 | 12 | }, |
13 | 13 | { |
14 | | - "metadata": {}, |
15 | 14 | "cell_type": "code", |
16 | | - "outputs": [], |
17 | 15 | "execution_count": null, |
| 16 | + "id": "b9e6cb8843da8bc3", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
18 | 19 | "source": [ |
19 | 20 | "%matplotlib inline\n", |
20 | 21 | "import os\n", |
21 | 22 | "from contextlib import suppress\n", |
22 | 23 | "from pathlib import Path\n", |
23 | 24 | "\n", |
24 | | - "import amici\n", |
25 | 25 | "import matplotlib.pyplot as plt\n", |
26 | 26 | "import numpy as np\n", |
27 | 27 | "from amici import (\n", |
28 | 28 | " SbmlImporter,\n", |
29 | | - " SensitivityMethod,\n", |
30 | | - " SensitivityOrder,\n", |
31 | | - " SteadyStateSensitivityMode,\n", |
32 | 29 | " import_model_module,\n", |
33 | | - " run_simulation,\n", |
34 | | - " simulation_status_to_str,\n", |
35 | 30 | ")\n", |
36 | 31 | "from amici.importers.petab.v1 import (\n", |
37 | 32 | " EDATAS,\n", |
38 | 33 | " RDATAS,\n", |
39 | 34 | " import_petab_problem,\n", |
40 | 35 | " simulate_petab,\n", |
41 | 36 | ")\n", |
42 | | - "from amici.plotting import plot_jacobian, plot_state_trajectories\n", |
| 37 | + "from amici.sim.sundials import (\n", |
| 38 | + " AMICI_SUCCESS,\n", |
| 39 | + " ExpData,\n", |
| 40 | + " SensitivityMethod,\n", |
| 41 | + " SensitivityOrder,\n", |
| 42 | + " SteadyStateSensitivityMode,\n", |
| 43 | + " run_simulation,\n", |
| 44 | + " simulation_status_to_str,\n", |
| 45 | + " unscale_parameter,\n", |
| 46 | + ")\n", |
| 47 | + "from amici.sim.sundials.plotting import plot_jacobian, plot_state_trajectories\n", |
43 | 48 | "from petab.v1.sbml import get_sbml_model\n", |
44 | 49 | "\n", |
45 | 50 | "try:\n", |
|
51 | 56 | " import benchmark_models_petab\n", |
52 | 57 | " except ModuleNotFoundError:\n", |
53 | 58 | " print(\"** Please restart the kernel. **\")" |
54 | | - ], |
55 | | - "id": "b9e6cb8843da8bc3" |
| 59 | + ] |
56 | 60 | }, |
57 | 61 | { |
58 | 62 | "cell_type": "markdown", |
|
131 | 135 | "\n", |
132 | 136 | "The number of steps the solver has to take is closely related to the chosen error tolerance. More accurate results, more steps. Therefore, this problem can be solved in two ways:\n", |
133 | 137 | "\n", |
134 | | - "1. Increasing the maximum number of steps via [amici.Solver.set_max_steps](https://amici.readthedocs.io/en/latest/generated/amici.amici.Solver.html#amici.amici.Solver.set_max_steps). Note that this will increase the time required for simulation, and that simulation may still fail eventually. Sometimes it may be preferable to not increase this limit but rather fail fast. Also note that increasing the number of allowed steps increase RAM requirements (even if fewer steps are actually taken), so don't set this to ridiculously large values in order to avoid this error.\n", |
| 138 | + "1. Increasing the maximum number of steps via [Solver.set_max_steps](https://amici.readthedocs.io/en/latest/generated/amici.sim.sundials.Solver.html#amici.sim.sundials.Solver.set_max_steps). Note that this will increase the time required for simulation, and that simulation may still fail eventually. Sometimes it may be preferable to not increase this limit but rather fail fast. Also note that increasing the number of allowed steps increase RAM requirements (even if fewer steps are actually taken), so don't set this to ridiculously large values in order to avoid this error.\n", |
135 | 139 | "\n", |
136 | | - "2. Reducing the number of steps CVODES has to take. This is determined by the required error tolerance. There are various solver error tolerances than can be adjusted. The most relevant ones are those controlled via [amici.Solver.set_relative_tolerance()](https://amici.readthedocs.io/en/latest/generated/amici.amici.Solver.html#amici.amici.Solver.set_relative_tolerance) and [amici.Solver.set_absolute_tolerance()](https://amici.readthedocs.io/en/latest/generated/amici.amici.Solver.html#amici.amici.Solver.set_absolute_tolerance).\n", |
| 140 | + "2. Reducing the number of steps CVODES has to take. This is determined by the required error tolerance. There are various solver error tolerances than can be adjusted. The most relevant ones are those controlled via [Solver.set_relative_tolerance()](https://amici.readthedocs.io/en/latest/generated/amici.sim.sundials.Solver.html#amici.sim.sundials.Solver.set_relative_tolerance) and [Solver.set_absolute_tolerance()](https://amici.readthedocs.io/en/latest/generated/amici.sim.sundials.Solver.html#amici.sim.sundials.Solver.set_absolute_tolerance).\n", |
137 | 141 | "\n", |
138 | 142 | "So, let's fix that:" |
139 | 143 | ] |
|
162 | 166 | " \"Status:\",\n", |
163 | 167 | " [simulation_status_to_str(rdata.status) for rdata in res[RDATAS]],\n", |
164 | 168 | ")\n", |
165 | | - "assert all(rdata.status == amici.AMICI_SUCCESS for rdata in res[RDATAS])\n", |
| 169 | + "assert all(rdata.status == AMICI_SUCCESS for rdata in res[RDATAS])\n", |
166 | 170 | "print(\"Simulations finished successfully.\")\n", |
167 | 171 | "print()\n", |
168 | 172 | "\n", |
|
183 | 187 | " \"Status:\",\n", |
184 | 188 | " [simulation_status_to_str(rdata.status) for rdata in res[RDATAS]],\n", |
185 | 189 | ")\n", |
186 | | - "assert all(rdata.status == amici.AMICI_SUCCESS for rdata in res[RDATAS])\n", |
| 190 | + "assert all(rdata.status == AMICI_SUCCESS for rdata in res[RDATAS])\n", |
187 | 191 | "print(\"Simulations finished successfully.\")" |
188 | 192 | ] |
189 | 193 | }, |
|
253 | 257 | "outputs": [], |
254 | 258 | "source": [ |
255 | 259 | "# Create a copy of this simulation condition\n", |
256 | | - "edata = amici.ExpData(res[EDATAS][0])\n", |
| 260 | + "edata = ExpData(res[EDATAS][0])\n", |
257 | 261 | "edata.set_timepoints(np.linspace(0, 0.33011, 5000))\n", |
258 | 262 | "amici_solver = amici_model.create_solver()\n", |
259 | 263 | "rdata = run_simulation(amici_model, amici_solver, edata)\n", |
|
403 | 407 | " \"Status:\",\n", |
404 | 408 | " [simulation_status_to_str(rdata.status) for rdata in res[RDATAS]],\n", |
405 | 409 | ")\n", |
406 | | - "assert all(rdata.status == amici.AMICI_SUCCESS for rdata in res[RDATAS])\n", |
| 410 | + "assert all(rdata.status == AMICI_SUCCESS for rdata in res[RDATAS])\n", |
407 | 411 | "print(\"Simulations finished successfully.\")" |
408 | 412 | ] |
409 | 413 | }, |
|
631 | 635 | "unscaled_parameter = dict(\n", |
632 | 636 | " zip(\n", |
633 | 637 | " amici_model.get_free_parameter_ids(),\n", |
634 | | - " starmap(\n", |
635 | | - " amici.unscale_parameter, zip(edata.free_parameters, edata.pscale)\n", |
636 | | - " ),\n", |
| 638 | + " starmap(unscale_parameter, zip(edata.free_parameters, edata.pscale)),\n", |
637 | 639 | " )\n", |
638 | 640 | ")\n", |
639 | 641 | "print(dict((p, unscaled_parameter[p]) for p in (\"Kd\", \"Kp\", \"n_par\")))" |
|
796 | 798 | " \"Status:\",\n", |
797 | 799 | " [simulation_status_to_str(rdata.status) for rdata in res[RDATAS]],\n", |
798 | 800 | ")\n", |
799 | | - "assert all(rdata.status == amici.AMICI_SUCCESS for rdata in res[RDATAS])" |
| 801 | + "assert all(rdata.status == AMICI_SUCCESS for rdata in res[RDATAS])" |
800 | 802 | ] |
801 | 803 | }, |
802 | 804 | { |
|
835 | 837 | " \"Status:\",\n", |
836 | 838 | " [simulation_status_to_str(rdata.status) for rdata in res[RDATAS]],\n", |
837 | 839 | ")\n", |
838 | | - "assert all(rdata.status == amici.AMICI_SUCCESS for rdata in res[RDATAS])" |
| 840 | + "assert all(rdata.status == AMICI_SUCCESS for rdata in res[RDATAS])" |
839 | 841 | ] |
840 | 842 | }, |
841 | 843 | { |
|
972 | 974 | "\n", |
973 | 975 | "# hard to reproduce on GHA\n", |
974 | 976 | "if os.getenv(\"GITHUB_ACTIONS\") is None:\n", |
975 | | - " assert all(rdata.status == amici.AMICI_SUCCESS for rdata in res[RDATAS])" |
| 977 | + " assert all(rdata.status == AMICI_SUCCESS for rdata in res[RDATAS])" |
976 | 978 | ] |
977 | 979 | }, |
978 | 980 | { |
|
998 | 1000 | " scaled_parameters=True,\n", |
999 | 1001 | " solver=amici_solver,\n", |
1000 | 1002 | " )\n", |
1001 | | - " if all(rdata.status == amici.AMICI_SUCCESS for rdata in res[RDATAS]):\n", |
| 1003 | + " if all(rdata.status == AMICI_SUCCESS for rdata in res[RDATAS]):\n", |
1002 | 1004 | " print(\n", |
1003 | 1005 | " f\"-> Succeeded with relative steady state tolerance {amici_solver.get_relative_tolerance_steady_state()}\\n\"\n", |
1004 | 1006 | " )\n", |
|
1014 | 1016 | "rdata = res[RDATAS][0]\n", |
1015 | 1017 | "print(f\"preeq_status={rdata.preeq_status}\")\n", |
1016 | 1018 | "print(f\"{rdata.preeq_numsteps=}\")\n", |
1017 | | - "assert all(rdata.status == amici.AMICI_SUCCESS for rdata in res[RDATAS])" |
| 1019 | + "assert all(rdata.status == AMICI_SUCCESS for rdata in res[RDATAS])" |
1018 | 1020 | ] |
1019 | 1021 | }, |
1020 | 1022 | { |
|
0 commit comments