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 7d161a6 commit 67afbd9
Show file tree
Hide file tree
Showing 82 changed files with 3,426 additions and 1,250 deletions.
8 changes: 5 additions & 3 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,12 +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"]

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
87 changes: 57 additions & 30 deletions documentation/ExampleJax.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
"output_type": "stream",
"text": [
"Cloning into 'tmp/benchmark-models'...\n",
"remote: Enumerating objects: 336, done.\u001B[K\n",
"remote: Counting objects: 100% (336/336), done.\u001B[K\n",
"remote: Compressing objects: 100% (285/285), done.\u001B[K\n",
"remote: Total 336 (delta 88), reused 216 (delta 39), pack-reused 0\u001B[K\n",
"remote: Enumerating objects: 336, done.\u001b[K\n",
"remote: Counting objects: 100% (336/336), done.\u001b[K\n",
"remote: Compressing objects: 100% (285/285), done.\u001b[K\n",
"remote: Total 336 (delta 88), reused 216 (delta 39), pack-reused 0\u001b[K\n",
"Receiving objects: 100% (336/336), 2.11 MiB | 7.48 MiB/s, done.\n",
"Resolving deltas: 100% (88/88), done.\n"
]
Expand All @@ -58,7 +58,8 @@
"source": [
"!git clone --depth 1 https://github.com/Benchmarking-Initiative/Benchmark-Models-PEtab.git tmp/benchmark-models || (cd tmp/benchmark-models && git pull)\n",
"from pathlib import Path\n",
"folder_base = Path('.') / \"tmp\" / \"benchmark-models\" / \"Benchmark-Models\""
"\n",
"folder_base = Path(\".\") / \"tmp\" / \"benchmark-models\" / \"Benchmark-Models\""
]
},
{
Expand All @@ -77,6 +78,7 @@
"outputs": [],
"source": [
"import petab\n",
"\n",
"model_name = \"Boehm_JProteomeRes2014\"\n",
"yaml_file = folder_base / model_name / (model_name + \".yaml\")\n",
"petab_problem = petab.Problem.from_yaml(yaml_file)"
Expand Down Expand Up @@ -570,6 +572,7 @@
],
"source": [
"from amici.petab_import import import_petab_problem\n",
"\n",
"amici_model = import_petab_problem(petab_problem, force_compile=True)"
]
},
Expand Down Expand Up @@ -606,14 +609,16 @@
"source": [
"from amici.petab_objective import simulate_petab\n",
"import amici\n",
"\n",
"amici_solver = amici_model.getSolver()\n",
"amici_solver.setSensitivityOrder(amici.SensitivityOrder.first)\n",
"\n",
"\n",
"def amici_hcb_base(parameters: jnp.array):\n",
" return simulate_petab(\n",
" petab_problem, \n",
" amici_model, \n",
" problem_parameters=dict(zip(petab_problem.x_free_ids, parameters)), \n",
" petab_problem,\n",
" amici_model,\n",
" problem_parameters=dict(zip(petab_problem.x_free_ids, parameters)),\n",
" scaled_parameters=True,\n",
" solver=amici_solver,\n",
" )"
Expand All @@ -635,13 +640,14 @@
"outputs": [],
"source": [
"def amici_hcb_llh(parameters: jnp.array):\n",
" return amici_hcb_base(parameters)['llh']\n",
" return amici_hcb_base(parameters)[\"llh\"]\n",
"\n",
"\n",
"def amici_hcb_sllh(parameters: jnp.array):\n",
" sllh = amici_hcb_base(parameters)['sllh']\n",
" return jnp.asarray(tuple(\n",
" sllh[par_id] for par_id in petab_problem.x_free_ids\n",
" ))"
" sllh = amici_hcb_base(parameters)[\"sllh\"]\n",
" return jnp.asarray(\n",
" tuple(sllh[par_id] for par_id in petab_problem.x_free_ids)\n",
" )"
]
},
{
Expand All @@ -663,6 +669,8 @@
"from jax import custom_jvp\n",
"\n",
"import numpy as np\n",
"\n",
"\n",
"@custom_jvp\n",
"def jax_objective(parameters: jnp.array):\n",
" return hcb.call(\n",
Expand Down Expand Up @@ -695,7 +703,9 @@
" sllh = hcb.call(\n",
" amici_hcb_sllh,\n",
" parameters,\n",
" result_shape=jax.ShapeDtypeStruct((petab_problem.parameter_df.estimate.sum(),), np.float64),\n",
" result_shape=jax.ShapeDtypeStruct(\n",
" (petab_problem.parameter_df.estimate.sum(),), np.float64\n",
" ),\n",
" )\n",
" return llh, sllh.dot(x_dot)"
]
Expand All @@ -717,19 +727,25 @@
"source": [
"from jax import value_and_grad\n",
"\n",
"parameter_scales = petab_problem.parameter_df.loc[petab_problem.x_free_ids, petab.PARAMETER_SCALE].values\n",
"parameter_scales = petab_problem.parameter_df.loc[\n",
" petab_problem.x_free_ids, petab.PARAMETER_SCALE\n",
"].values\n",
"\n",
"\n",
"@jax.jit\n",
"@value_and_grad\n",
"def jax_objective_with_parameter_transform(parameters: jnp.array):\n",
" par_scaled = jnp.asarray(tuple(\n",
" value if scale == petab.LIN\n",
" else jnp.log(value) if scale == petab.LOG\n",
" else jnp.log10(value)\n",
" for value, scale in zip(parameters, parameter_scales)\n",
" ))\n",
" return jax_objective(par_scaled)\n",
" "
" par_scaled = jnp.asarray(\n",
" tuple(\n",
" value\n",
" if scale == petab.LIN\n",
" else jnp.log(value)\n",
" if scale == petab.LOG\n",
" else jnp.log10(value)\n",
" for value, scale in zip(parameters, parameter_scales)\n",
" )\n",
" )\n",
" return jax_objective(par_scaled)"
]
},
{
Expand All @@ -755,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 @@ -777,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 All @@ -802,7 +822,8 @@
],
"source": [
"import pandas as pd\n",
"pd.Series(dict(amici=r['llh'], jax=float(llh_jax)))"
"\n",
"pd.Series(dict(amici=r[\"llh\"], jax=float(llh_jax)))"
]
},
{
Expand Down Expand Up @@ -905,7 +926,9 @@
}
],
"source": [
"pd.DataFrame(index=sllh.keys(), data=dict(amici=sllh.values(), jax=np.asarray(sllh_jax)))"
"pd.DataFrame(\n",
" index=sllh.keys(), data=dict(amici=sllh.values(), jax=np.asarray(sllh_jax))\n",
")"
]
},
{
Expand All @@ -925,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 Expand Up @@ -956,7 +981,7 @@
}
],
"source": [
"pd.Series(dict(amici=r['llh'], jax=float(llh_jax)))"
"pd.Series(dict(amici=r[\"llh\"], jax=float(llh_jax)))"
]
},
{
Expand Down Expand Up @@ -1059,7 +1084,9 @@
}
],
"source": [
"pd.DataFrame(index=sllh.keys(), data=dict(amici=sllh.values(), jax=np.asarray(sllh_jax)))"
"pd.DataFrame(\n",
" index=sllh.keys(), data=dict(amici=sllh.values(), jax=np.asarray(sllh_jax))\n",
")"
]
}
],
Expand Down
11 changes: 6 additions & 5 deletions documentation/GettingStarted.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"outputs": [],
"source": [
"import amici\n",
"sbml_importer = amici.SbmlImporter('model_steadystate_scaled.xml')"
"\n",
"sbml_importer = amici.SbmlImporter(\"model_steadystate_scaled.xml\")"
]
},
{
Expand All @@ -42,8 +43,8 @@
"metadata": {},
"outputs": [],
"source": [
"model_name = 'model_steadystate'\n",
"model_dir = 'model_dir'\n",
"model_name = \"model_steadystate\"\n",
"model_dir = \"model_dir\"\n",
"sbml_importer.sbml2amici(model_name, model_dir)"
]
},
Expand Down Expand Up @@ -82,7 +83,7 @@
"metadata": {},
"outputs": [],
"source": [
"model.setParameterByName('p1',1e-3)"
"model.setParameterByName(\"p1\", 1e-3)"
]
},
{
Expand Down Expand Up @@ -122,7 +123,7 @@
"outputs": [],
"source": [
"# set timepoints\n",
"model.setTimepoints([0,1])\n",
"model.setTimepoints([0, 1])\n",
"rdata = amici.runAmiciSimulation(model, solver)"
]
},
Expand Down
29 changes: 22 additions & 7 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 @@ -176,7 +182,10 @@ def install_doxygen():

intersphinx_mapping = {
"pysb": ("https://pysb.readthedocs.io/en/stable/", None),
"petab": ("https://petab.readthedocs.io/projects/libpetab-python/en/latest/", None),
"petab": (
"https://petab.readthedocs.io/projects/libpetab-python/en/latest/",
None,
),
"pandas": ("https://pandas.pydata.org/docs/", None),
"numpy": ("https://numpy.org/devdocs/", None),
"sympy": ("https://docs.sympy.org/latest/", None),
Expand Down Expand Up @@ -291,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 Expand Up @@ -504,10 +515,14 @@ def process_docstring(app, what, name, obj, options, lines):
for old, new in typemaps.items():
lines[i] = lines[i].replace(old, new)
lines[i] = re.sub(
r"amici::(Model|Solver|ExpData) ", r":class:`amici\.amici\.\1\`", lines[i]
r"amici::(Model|Solver|ExpData) ",
r":class:`amici\.amici\.\1\`",
lines[i],
)
lines[i] = re.sub(
r"amici::(runAmiciSimulation[s]?)", r":func:`amici\.amici\.\1`", lines[i]
r"amici::(runAmiciSimulation[s]?)",
r":func:`amici\.amici\.\1`",
lines[i],
)


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
Loading

0 comments on commit 67afbd9

Please sign in to comment.