Skip to content

Commit

Permalink
Fix pre-commit configuration
Browse files Browse the repository at this point in the history
Relevant changes are only in .pre-commit-config.yaml

* run black on notebooks via pre-commit
* set proper line width (pyproject.toml is not used if we are running things from the repo root)
* compatible line width for black + isort
* fix json error in binder/overview.ipynb
* re-blacken everything
  • Loading branch information
dweindl committed Aug 30, 2023
1 parent 556fba4 commit f7cbca4
Show file tree
Hide file tree
Showing 80 changed files with 1,712 additions and 626 deletions.
8 changes: 3 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
hooks:
- id: isort
name: isort (python)
args: ["--profile", "black", "--filter-files"]
args: ["--profile", "black", "--filter-files", "--line-length", "79"]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
Expand All @@ -17,16 +17,14 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black
- id: black-jupyter
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.11
args: ["--line-length", "79"]
- id: black-jupyter
language_version: python3.11

exclude: '^(ThirdParty|models)/'
3 changes: 2 additions & 1 deletion binder/overview.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"\n",
"* [Interfacing JAX](../python/examples/example_jax/ExampleJax.ipynb)\n",
"\n",
" Provides guidance on how to combine AMICI with differential programming frameworks such as JAX.\n"
" Provides guidance on how to combine AMICI with differential programming frameworks such as JAX.\n",
"\n",
"* [Efficient spline interpolation](../python/examples/example_splines/ExampleSplines.ipynb)\n",
"\n",
" Shows how to add annotated spline formulas to existing SBML models in order to speed up AMICI's model import.\n",
Expand Down
16 changes: 12 additions & 4 deletions documentation/ExampleJax.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,9 @@
"\n",
"def amici_hcb_sllh(parameters: jnp.array):\n",
" sllh = amici_hcb_base(parameters)[\"sllh\"]\n",
" return jnp.asarray(tuple(sllh[par_id] for par_id in petab_problem.x_free_ids))"
" return jnp.asarray(\n",
" tuple(sllh[par_id] for par_id in petab_problem.x_free_ids)\n",
" )"
]
},
{
Expand Down Expand Up @@ -769,7 +771,9 @@
"metadata": {},
"outputs": [],
"source": [
"llh_jax, sllh_jax = jax_objective_with_parameter_transform(petab_problem.x_nominal_free)"
"llh_jax, sllh_jax = jax_objective_with_parameter_transform(\n",
" petab_problem.x_nominal_free\n",
")"
]
},
{
Expand All @@ -791,7 +795,9 @@
"# TODO remove me as soon as sllh in simulate_petab is fixed\n",
"sllh = {\n",
" name: value / (np.log(10) * par_value)\n",
" for (name, value), par_value in zip(r[\"sllh\"].items(), petab_problem.x_nominal_free)\n",
" for (name, value), par_value in zip(\n",
" r[\"sllh\"].items(), petab_problem.x_nominal_free\n",
" )\n",
"}"
]
},
Expand Down Expand Up @@ -942,7 +948,9 @@
"outputs": [],
"source": [
"jax.config.update(\"jax_enable_x64\", True)\n",
"llh_jax, sllh_jax = jax_objective_with_parameter_transform(petab_problem.x_nominal_free)"
"llh_jax, sllh_jax = jax_objective_with_parameter_transform(\n",
" petab_problem.x_nominal_free\n",
")"
]
},
{
Expand Down
16 changes: 12 additions & 4 deletions documentation/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def my_exhale_generate_doxygen(doxygen_input):
DomainDirectiveFactory as breathe_DomainDirectiveFactory,
)

old_breathe_DomainDirectiveFactory_create = breathe_DomainDirectiveFactory.create
old_breathe_DomainDirectiveFactory_create = (
breathe_DomainDirectiveFactory.create
)


def my_breathe_DomainDirectiveFactory_create(domain: str, args):
Expand All @@ -67,7 +69,9 @@ def my_breathe_DomainDirectiveFactory_create(domain: str, args):
return cls(domain + ":" + name, *args[1:])


breathe_DomainDirectiveFactory.create = my_breathe_DomainDirectiveFactory_create
breathe_DomainDirectiveFactory.create = (
my_breathe_DomainDirectiveFactory_create
)


# END Monkeypatch breathe
Expand Down Expand Up @@ -102,7 +106,9 @@ def install_doxygen():
subprocess.run(cmd, shell=True, check=True)
assert os.path.islink(os.path.join(some_dir_on_path, "doxygen"))
# verify it's available
res = subprocess.run(["doxygen", "--version"], check=False, capture_output=True)
res = subprocess.run(
["doxygen", "--version"], check=False, capture_output=True
)
print(res.stdout.decode(), res.stderr.decode())
assert version in res.stdout.decode()

Expand Down Expand Up @@ -294,7 +300,9 @@ def install_doxygen():
"verboseBuild": True,
}

mtocpp_filter = os.path.join(amici_dir, "matlab", "mtoc", "config", "mtocpp_filter.sh")
mtocpp_filter = os.path.join(
amici_dir, "matlab", "mtoc", "config", "mtocpp_filter.sh"
)
exhale_projects_args = {
"AMICI_CPP": {
"exhaleDoxygenStdin": "\n".join(
Expand Down
8 changes: 6 additions & 2 deletions documentation/recreate_reference_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def get_sub_bibliography(year, by_year, bibfile):

entries = ",".join(["@" + x for x in by_year[year]])
stdin_input = (
"---\n" f"bibliography: {bibfile}\n" f'nocite: "{entries}"\n...\n' f"# {year}"
"---\n"
f"bibliography: {bibfile}\n"
f'nocite: "{entries}"\n...\n'
f"# {year}"
)

out = subprocess.run(
Expand All @@ -67,7 +70,8 @@ def main():
with open(outfile, "w") as f:
f.write("# References\n\n")
f.write(
"List of publications using AMICI. " f"Total number is {num_total}.\n\n"
"List of publications using AMICI. "
f"Total number is {num_total}.\n\n"
)
f.write(
"If you applied AMICI in your work and your publication is "
Expand Down
14 changes: 9 additions & 5 deletions python/benchmark/benchmark_pysb.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@

with amici.add_path(os.path.dirname(pysb.examples.__file__)):
with amici.add_path(
os.path.join(os.path.dirname(__file__), "..", "tests", "pysb_test_models")
os.path.join(
os.path.dirname(__file__), "..", "tests", "pysb_test_models"
)
):
pysb.SelfExporter.cleanup() # reset pysb
pysb.SelfExporter.do_export = True
Expand All @@ -52,9 +54,9 @@
integrator_options={"rtol": rtol, "atol": atol},
)
time_pysb = (
timeit.Timer("pysb_simres = sim.run()", globals={"sim": sim}).timeit(
number=N_REPEATS
)
timeit.Timer(
"pysb_simres = sim.run()", globals={"sim": sim}
).timeit(number=N_REPEATS)
/ N_REPEATS
)

Expand All @@ -76,7 +78,9 @@
observables=list(pysb_model.observables.keys()),
)

amici_model_module = amici.import_model_module(pysb_model.name, outdir)
amici_model_module = amici.import_model_module(
pysb_model.name, outdir
)

model_pysb = amici_model_module.getModel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
"source": [
"from IPython.display import Image\n",
"\n",
"fig = Image(filename=(\"../../../documentation/gfx/steadystate_solver_workflow.png\"))\n",
"fig = Image(\n",
" filename=(\"../../../documentation/gfx/steadystate_solver_workflow.png\")\n",
")\n",
"fig"
]
},
Expand Down Expand Up @@ -252,7 +254,9 @@
")\n",
"model_reduced = model_reduced_module.getModel()\n",
"\n",
"model_module = amici.import_model_module(model_name, os.path.abspath(model_output_dir))\n",
"model_module = amici.import_model_module(\n",
" model_name, os.path.abspath(model_output_dir)\n",
")\n",
"model = model_module.getModel()\n",
"\n",
"\n",
Expand Down Expand Up @@ -431,7 +435,9 @@
"solver.setMaxSteps(100)\n",
"rdata = amici.runAmiciSimulation(model, solver)\n",
"print(\"Status of postequilibration:\", rdata[\"posteq_status\"])\n",
"print(\"Number of steps employed in postequilibration:\", rdata[\"posteq_numsteps\"])"
"print(\n",
" \"Number of steps employed in postequilibration:\", rdata[\"posteq_numsteps\"]\n",
")"
]
},
{
Expand Down Expand Up @@ -470,7 +476,8 @@
"\n",
"print(\"Status of postequilibration:\", rdata_reduced[\"posteq_status\"])\n",
"print(\n",
" \"Number of steps employed in postequilibration:\", rdata_reduced[\"posteq_numsteps\"]\n",
" \"Number of steps employed in postequilibration:\",\n",
" rdata_reduced[\"posteq_numsteps\"],\n",
")"
]
},
Expand Down Expand Up @@ -542,7 +549,9 @@
"rdata = amici.runAmiciSimulation(model, solver)\n",
"\n",
"print(\"Status of postequilibration:\", rdata[\"posteq_status\"])\n",
"print(\"Number of steps employed in postequilibration:\", rdata[\"posteq_numsteps\"])\n",
"print(\n",
" \"Number of steps employed in postequilibration:\", rdata[\"posteq_numsteps\"]\n",
")\n",
"print(\"Computed state sensitivities:\")\n",
"print(rdata[\"sx\"][0, :, :])"
]
Expand Down Expand Up @@ -585,15 +594,19 @@
"source": [
"# Call simulation with singular Jacobian and newtonOnly mode (will fail)\n",
"model.setTimepoints(np.full(1, np.inf))\n",
"model.setSteadyStateSensitivityMode(amici.SteadyStateSensitivityMode.newtonOnly)\n",
"model.setSteadyStateSensitivityMode(\n",
" amici.SteadyStateSensitivityMode.newtonOnly\n",
")\n",
"solver = model.getSolver()\n",
"solver.setSensitivityMethod(amici.SensitivityMethod.forward)\n",
"solver.setSensitivityOrder(amici.SensitivityOrder.first)\n",
"solver.setMaxSteps(10000)\n",
"rdata = amici.runAmiciSimulation(model, solver)\n",
"\n",
"print(\"Status of postequilibration:\", rdata[\"posteq_status\"])\n",
"print(\"Number of steps employed in postequilibration:\", rdata[\"posteq_numsteps\"])\n",
"print(\n",
" \"Number of steps employed in postequilibration:\", rdata[\"posteq_numsteps\"]\n",
")\n",
"print(\"Computed state sensitivities:\")\n",
"print(rdata[\"sx\"][0, :, :])"
]
Expand Down Expand Up @@ -621,7 +634,9 @@
"source": [
"# Call postequilibration by setting an infinity timepoint\n",
"model_reduced.setTimepoints(np.full(1, np.inf))\n",
"model.setSteadyStateSensitivityMode(amici.SteadyStateSensitivityMode.newtonOnly)\n",
"model.setSteadyStateSensitivityMode(\n",
" amici.SteadyStateSensitivityMode.newtonOnly\n",
")\n",
"solver_reduced = model_reduced.getSolver()\n",
"solver_reduced.setNewtonMaxSteps(10)\n",
"solver_reduced.setSensitivityMethod(amici.SensitivityMethod.forward)\n",
Expand All @@ -631,7 +646,8 @@
"\n",
"print(\"Status of postequilibration:\", rdata_reduced[\"posteq_status\"])\n",
"print(\n",
" \"Number of steps employed in postequilibration:\", rdata_reduced[\"posteq_numsteps\"]\n",
" \"Number of steps employed in postequilibration:\",\n",
" rdata_reduced[\"posteq_numsteps\"],\n",
")\n",
"print(\"Computed state sensitivities:\")\n",
"print(rdata_reduced[\"sx\"][0, :, :])"
Expand Down Expand Up @@ -687,7 +703,9 @@
"edata.setObservedData([1.8] * 2)\n",
"edata.fixedParameters = np.array([3.0, 5.0])\n",
"\n",
"model_reduced.setSteadyStateSensitivityMode(amici.SteadyStateSensitivityMode.newtonOnly)\n",
"model_reduced.setSteadyStateSensitivityMode(\n",
" amici.SteadyStateSensitivityMode.newtonOnly\n",
")\n",
"solver_reduced = model_reduced.getSolver()\n",
"solver_reduced.setNewtonMaxSteps(10)\n",
"solver_reduced.setSensitivityMethod(amici.SensitivityMethod.adjoint)\n",
Expand All @@ -697,7 +715,8 @@
"\n",
"print(\"Status of postequilibration:\", rdata_reduced[\"posteq_status\"])\n",
"print(\n",
" \"Number of steps employed in postequilibration:\", rdata_reduced[\"posteq_numsteps\"]\n",
" \"Number of steps employed in postequilibration:\",\n",
" rdata_reduced[\"posteq_numsteps\"],\n",
")\n",
"print(\n",
" \"Number of backward steps employed in postequilibration:\",\n",
Expand Down Expand Up @@ -733,17 +752,22 @@
],
"source": [
"# Call adjoint postequilibration with model with singular Jacobian\n",
"model.setSteadyStateSensitivityMode(amici.SteadyStateSensitivityMode.newtonOnly)\n",
"model.setSteadyStateSensitivityMode(\n",
" amici.SteadyStateSensitivityMode.newtonOnly\n",
")\n",
"solver = model.getSolver()\n",
"solver.setNewtonMaxSteps(10)\n",
"solver.setSensitivityMethod(amici.SensitivityMethod.adjoint)\n",
"solver.setSensitivityOrder(amici.SensitivityOrder.first)\n",
"rdata = amici.runAmiciSimulation(model, solver, edata)\n",
"\n",
"print(\"Status of postequilibration:\", rdata[\"posteq_status\"])\n",
"print(\"Number of steps employed in postequilibration:\", rdata[\"posteq_numsteps\"])\n",
"print(\n",
" \"Number of backward steps employed in postequilibration:\", rdata[\"posteq_numstepsB\"]\n",
" \"Number of steps employed in postequilibration:\", rdata[\"posteq_numsteps\"]\n",
")\n",
"print(\n",
" \"Number of backward steps employed in postequilibration:\",\n",
" rdata[\"posteq_numstepsB\"],\n",
")\n",
"print(\"Computed gradient:\", rdata[\"sllh\"])"
]
Expand Down Expand Up @@ -890,7 +914,9 @@
"edata.setTimepoints(np.array([0.0, 0.1, 1.0]))\n",
"\n",
"# create the solver object and run the simulation, singular Jacobian, enforce Newton solver for sensitivities\n",
"model.setSteadyStateSensitivityMode(amici.SteadyStateSensitivityMode.newtonOnly)\n",
"model.setSteadyStateSensitivityMode(\n",
" amici.SteadyStateSensitivityMode.newtonOnly\n",
")\n",
"solver = model.getSolver()\n",
"solver.setNewtonMaxSteps(10)\n",
"solver.setSensitivityMethod(amici.SensitivityMethod.forward)\n",
Expand Down Expand Up @@ -1086,7 +1112,9 @@
"solver_reduced = model_reduced.getSolver()\n",
"solver_reduced.setNewtonMaxSteps(10)\n",
"solver_reduced.setSensitivityMethod(amici.SensitivityMethod.adjoint)\n",
"solver_reduced.setSensitivityMethodPreequilibration(amici.SensitivityMethod.adjoint)\n",
"solver_reduced.setSensitivityMethodPreequilibration(\n",
" amici.SensitivityMethod.adjoint\n",
")\n",
"solver_reduced.setSensitivityOrder(amici.SensitivityOrder.first)\n",
"rdata_reduced = amici.runAmiciSimulation(model_reduced, solver_reduced, edata)\n",
"\n",
Expand Down Expand Up @@ -1193,14 +1221,18 @@
"solver_reduced.setAbsoluteToleranceSteadyState(1e-3)\n",
"solver_reduced.setRelativeToleranceSteadyStateSensi(1e-2)\n",
"solver_reduced.setAbsoluteToleranceSteadyStateSensi(1e-3)\n",
"rdata_reduced_lax = amici.runAmiciSimulation(model_reduced, solver_reduced, edata)\n",
"rdata_reduced_lax = amici.runAmiciSimulation(\n",
" model_reduced, solver_reduced, edata\n",
")\n",
"\n",
"# run with strict tolerances\n",
"solver_reduced.setRelativeToleranceSteadyState(1e-12)\n",
"solver_reduced.setAbsoluteToleranceSteadyState(1e-16)\n",
"solver_reduced.setRelativeToleranceSteadyStateSensi(1e-12)\n",
"solver_reduced.setAbsoluteToleranceSteadyStateSensi(1e-16)\n",
"rdata_reduced_strict = amici.runAmiciSimulation(model_reduced, solver_reduced, edata)\n",
"rdata_reduced_strict = amici.runAmiciSimulation(\n",
" model_reduced, solver_reduced, edata\n",
")\n",
"\n",
"# compare ODE outputs\n",
"print(\"\\nODE solver steps, which were necessary to reach steady state:\")\n",
Expand Down
Loading

0 comments on commit f7cbca4

Please sign in to comment.