Skip to content

Commit

Permalink
pre-commit autoupdate
Browse files Browse the repository at this point in the history
`pre-commit autoupdate` and fix/ignore pyupgrade issues.
  • Loading branch information
dweindl committed Jan 6, 2025
1 parent b99a3f4 commit 1860e71
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@
"for reaction in sbml_model.getListOfReactions():\n",
" reactants = \" + \".join(\n",
" [\n",
" \"%s %s\"\n",
" % (\n",
" \"{} {}\".format(\n",
" int(r.getStoichiometry()) if r.getStoichiometry() > 1 else \"\",\n",
" r.getSpecies(),\n",
" )\n",
Expand All @@ -134,8 +133,7 @@
" )\n",
" products = \" + \".join(\n",
" [\n",
" \"%s %s\"\n",
" % (\n",
" \"{} {}\".format(\n",
" int(r.getStoichiometry()) if r.getStoichiometry() > 1 else \"\",\n",
" r.getSpecies(),\n",
" )\n",
Expand All @@ -144,7 +142,7 @@
" )\n",
" reversible = \"<\" if reaction.getReversible() else \"\"\n",
" print(\n",
" \"%3s: %10s %1s->%10s\\t\\t[%s]\"\n",
" \"%3s: %10s %1s->%10s\\t\\t[%s]\" # noqa: UP031\n",
" % (\n",
" reaction.getId(),\n",
" reactants,\n",
Expand Down Expand Up @@ -370,7 +368,7 @@
"\n",
"# np.set_printoptions(threshold=8, edgeitems=2)\n",
"for key, value in rdata.items():\n",
" print(\"%12s: \" % key, value)"
" print(f\"{key:12s}: \", value)"
]
},
{
Expand Down Expand Up @@ -664,15 +662,15 @@
"Since the Eigenvalues of the Jacobian are negative and since the Jacobian at steady state is a fixed matrix, this system has a simple algebraic solution:\n",
"\n",
" $$p(t) = e^{t J(x^*, \\theta)^T} p_{\\text{end}}.$$\n",
" \n",
"\n",
"As a consequence, the quadratures in adjoint computation also reduce to a matrix-vector product:\n",
"\n",
" $$Q(x, \\theta) = Q(x^*, \\theta) = p_{\\text{integral}} * \\frac{\\partial f}{\\partial \\theta}$$\n",
"\n",
"with\n",
"\n",
" $$p_{\\text{integral}} = \\int_0^\\infty p(s) ds = (J(x^*, \\theta)^T)^{-1} p_{\\text{end}}.$$ \n",
" \n",
" $$p_{\\text{integral}} = \\int_0^\\infty p(s) ds = (J(x^*, \\theta)^T)^{-1} p_{\\text{end}}.$$\n",
"\n",
"However, this solution is given in terms of a linear system of equations defined by the transposed Jacobian of the right hand side. Hence, if the (transposed) Jacobian is singular, it is not applicable.\n",
"In this case, standard integration must be carried out."
]
Expand Down Expand Up @@ -922,7 +920,7 @@
"\n",
"for key, value in rdata.items():\n",
" if key[0:6] == \"preeq_\":\n",
" print(\"%20s: \" % key, value)"
" print(f\"{key:20s}:\", value)"
]
},
{
Expand Down Expand Up @@ -960,7 +958,7 @@
"\n",
"for key, value in rdata.items():\n",
" if key[0:6] == \"preeq_\":\n",
" print(\"%20s: \" % key, value)"
" print(f\"{key:20s}:\", value)"
]
},
{
Expand Down Expand Up @@ -993,7 +991,7 @@
"\n",
"for key, value in rdata_reduced.items():\n",
" if key[0:6] == \"preeq_\":\n",
" print(\"%20s: \" % key, value)"
" print(f\"{key:20s}:\", value)"
]
},
{
Expand Down Expand Up @@ -1044,7 +1042,7 @@
"\n",
"for key, value in rdata_reduced.items():\n",
" if key[0:6] == \"preeq_\":\n",
" print(\"%20s: \" % key, value)\n",
" print(f\"{key:20s}:\", value)\n",
"print(\"Gradient:\", rdata_reduced[\"sllh\"])"
]
},
Expand Down Expand Up @@ -1079,7 +1077,7 @@
"\n",
"for key, value in rdata_reduced.items():\n",
" if key[0:6] == \"preeq_\":\n",
" print(\"%20s: \" % key, value)\n",
" print(f\"{key:20s}:\", value)\n",
"print(\"Gradient:\", rdata_reduced[\"sllh\"])"
]
},
Expand Down Expand Up @@ -1117,7 +1115,7 @@
"\n",
"for key, value in rdata_reduced.items():\n",
" if key[0:6] == \"preeq_\":\n",
" print(\"%20s: \" % key, value)\n",
" print(f\"{key:20s}:\", value)\n",
"print(\"Gradient:\", rdata_reduced[\"sllh\"])"
]
},
Expand Down Expand Up @@ -1160,7 +1158,7 @@
"\n",
"for key, value in rdata.items():\n",
" if key[0:6] == \"preeq_\":\n",
" print(\"%20s: \" % key, value)\n",
" print(f\"{key:20s}:\", value)\n",
"print(\"Gradient:\", rdata[\"sllh\"])"
]
},
Expand Down
8 changes: 2 additions & 6 deletions python/examples/example_errors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@
"outputs": [],
"source": [
"petab_problem = benchmark_models_petab.get_problem(\"Fujita_SciSignal2010\")\n",
"amici_model = import_petab_problem(\n",
" petab_problem, verbose=False, compile_=None\n",
")\n",
"amici_model = import_petab_problem(petab_problem, verbose=False, compile_=None)\n",
"\n",
"np.random.seed(2991)\n",
"problem_parameters = dict(\n",
Expand Down Expand Up @@ -422,9 +420,7 @@
"outputs": [],
"source": [
"petab_problem = benchmark_models_petab.get_problem(\"Weber_BMC2015\")\n",
"amici_model = import_petab_problem(\n",
" petab_problem, verbose=False, compile_=None\n",
")\n",
"amici_model = import_petab_problem(petab_problem, verbose=False, compile_=None)\n",
"\n",
"np.random.seed(4)\n",
"problem_parameters = dict(\n",
Expand Down
50 changes: 26 additions & 24 deletions python/examples/example_petab/petab.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
},
{
"cell_type": "markdown",
"source": [
"## Importing a PEtab problem"
],
"metadata": {
"collapsed": false
}
},
"source": [
"## Importing a PEtab problem"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -134,6 +134,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"app = PetabProblem(petab_problem)\n",
Expand All @@ -143,43 +147,41 @@
"\n",
"# ExpData for a single condition:\n",
"edata = app.get_edata(\"model1_data1\")"
],
"metadata": {
"collapsed": false
},
"execution_count": null
]
},
{
"cell_type": "code",
"outputs": [],
"source": [
"rdata = runAmiciSimulation(amici_model, solver=amici_model.getSolver(), edata=edata)\n",
"rdata"
],
"execution_count": null,
"metadata": {
"collapsed": false
},
"execution_count": null
"outputs": [],
"source": [
"rdata = runAmiciSimulation(\n",
" amici_model, solver=amici_model.getSolver(), edata=edata\n",
")\n",
"rdata"
]
},
{
"cell_type": "code",
"outputs": [],
"source": [
"plot_state_trajectories(rdata)"
],
"execution_count": null,
"metadata": {
"collapsed": false
},
"execution_count": null
"outputs": [],
"source": [
"plot_state_trajectories(rdata)"
]
},
{
"cell_type": "markdown",
"source": [
"For further information, check out the [AMICI documentation](https://amici.readthedocs.io/en/latest/)."
],
"metadata": {
"collapsed": false
}
},
"source": [
"For further information, check out the [AMICI documentation](https://amici.readthedocs.io/en/latest/)."
]
}
],
"metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@
"for reaction in sbml_model.getListOfReactions():\n",
" reactants = \" + \".join(\n",
" [\n",
" \"%s %s\"\n",
" % (\n",
" \"{} {}\".format(\n",
" int(r.getStoichiometry()) if r.getStoichiometry() > 1 else \"\",\n",
" r.getSpecies(),\n",
" )\n",
Expand All @@ -99,8 +98,7 @@
" )\n",
" products = \" + \".join(\n",
" [\n",
" \"%s %s\"\n",
" % (\n",
" \"{} {}\".format(\n",
" int(r.getStoichiometry()) if r.getStoichiometry() > 1 else \"\",\n",
" r.getSpecies(),\n",
" )\n",
Expand All @@ -109,7 +107,7 @@
" )\n",
" reversible = \"<\" if reaction.getReversible() else \"\"\n",
" print(\n",
" \"%3s: %10s %1s->%10s\\t\\t[%s]\"\n",
" \"%3s: %10s %1s->%10s\\t\\t[%s]\" # noqa: UP031\n",
" % (\n",
" reaction.getName(),\n",
" reactants,\n",
Expand Down Expand Up @@ -334,7 +332,7 @@
"metadata": {},
"source": [
"For many biological systems, it is reasonable to assume that they start in a\n",
" steady state. In this example we want to specify an experiment where a pretreatment with a drug is performed _before_ the kinase is added. We assume that the pretreatment is sufficiently long such that the system reaches steadystate before the kinase is added. To implement this in amici, we can specify `fixedParametersPreequilibration` in the `ExpData` object. This automatically adds a preequilibration phase where the model is run to steadystate, before regular simulation starts. Here we set `DRUG_0=3` and `KIN_0=0` for the preequilibration. This means that there is no kinase available in the preequilibration phase. "
" steady state. In this example we want to specify an experiment where a pretreatment with a drug is performed _before_ the kinase is added. We assume that the pretreatment is sufficiently long such that the system reaches steadystate before the kinase is added. To implement this in amici, we can specify `fixedParametersPreequilibration` in the `ExpData` object. This automatically adds a preequilibration phase where the model is run to steadystate, before regular simulation starts. Here we set `DRUG_0=3` and `KIN_0=0` for the preequilibration. This means that there is no kinase available in the preequilibration phase."
]
},
{
Expand Down
119 changes: 59 additions & 60 deletions python/examples/example_steadystate/ExampleSteadystate.ipynb

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions python/sdist/amici/_codegen/model_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ def get_model_override_implementation(
eval_signature=remove_argument_types(func_info.arguments(ode)),
)
)
return "{return_type} f{fun}({signature}) override {{{body}}}\n".format(
return_type=func_info.return_type,
fun=fun,
signature=func_info.arguments(ode),
body=body,
)
return f"{func_info.return_type} f{fun}({func_info.arguments(ode)}) override {{{body}}}\n"


def get_sunindex_override_implementation(
Expand Down
7 changes: 1 addition & 6 deletions python/sdist/amici/cxxcodeprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ def _print_min_max(self, expr, cpp_fun: str, sympy_fun):
)
if len(expr.args) == 1:
return self._print(arg0)
return "{}{}({}, {})".format(
self._ns,
cpp_fun,
self._print(arg0),
self._print(sympy_fun(*expr.args[1:])),
)
return f"{self._ns}{cpp_fun}({self._print(arg0)}, {self._print(sympy_fun(*expr.args[1:]))})"

def _print_Min(self, expr):
from sympy.functions.elementary.miscellaneous import Min
Expand Down
7 changes: 3 additions & 4 deletions python/sdist/amici/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import (
Any,
SupportsFloat,
Union,
)
from collections.abc import Callable
from collections.abc import Iterable, Sequence
Expand All @@ -30,7 +29,7 @@ class SBMLException(Exception):
pass


SymbolDef = dict[sp.Symbol, Union[dict[str, sp.Expr], sp.Expr]]
SymbolDef = dict[sp.Symbol, dict[str, sp.Expr] | sp.Expr]


# Monkey-patch toposort CircularDependencyError to handle non-sortable objects,
Expand Down Expand Up @@ -407,7 +406,7 @@ def _parse_special_functions(sym: sp.Expr, toplevel: bool = True) -> sp.Expr:
if sym.__class__.__name__ == "plus" and not sym.args:
return sp.Float(0.0)

if isinstance(sym, (sp.Function, sp.Mul, sp.Add, sp.Pow)):
if isinstance(sym, (sp.Function | sp.Mul | sp.Add | sp.Pow)):
sym._args = args

elif toplevel and isinstance(sym, BooleanAtom):
Expand Down Expand Up @@ -637,7 +636,7 @@ def cast_to_sym(
:return:
typecast value
"""
if isinstance(value, (sp.RealNumber, numbers.Number)):
if isinstance(value, (sp.RealNumber | numbers.Number)):
value = sp.Float(float(value))
elif isinstance(value, BooleanAtom):
value = sp.Float(float(bool(value)))
Expand Down
1 change: 1 addition & 0 deletions python/sdist/amici/jax/petab.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""PEtab wrappers for JAX models.""" ""

import shutil
from numbers import Number
from collections.abc import Iterable
Expand Down
4 changes: 2 additions & 2 deletions python/sdist/amici/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def set_log_level(logger: logging.Logger, log_level: int | bool) -> None:

if logger.getEffectiveLevel() != log_level:
logger.debug(
"Changing log_level from %d to %d"
% (logger.getEffectiveLevel(), log_level)
f"Changing log_level from {logger.getEffectiveLevel()} "
f"to {log_level}"
)
logger.setLevel(log_level)

Expand Down
6 changes: 3 additions & 3 deletions python/sdist/amici/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from sympy.abc import _clash
from . import ExpData, ExpDataPtr, Model, ReturnData, ReturnDataPtr

StrOrExpr = Union[str, sp.Expr]
StrOrExpr = str | sp.Expr


class SwigPtrView(collections.abc.Mapping):
Expand Down Expand Up @@ -251,7 +251,7 @@ def __init__(self, rdata: ReturnDataPtr | ReturnData):
:param rdata: pointer to the ``ReturnData`` instance
"""
if not isinstance(rdata, (ReturnDataPtr, ReturnData)):
if not isinstance(rdata, (ReturnDataPtr | ReturnData)):
raise TypeError(
f"Unsupported pointer {type(rdata)}, must be"
f"amici.ReturnDataPtr or amici.ReturnData!"
Expand Down Expand Up @@ -397,7 +397,7 @@ def __init__(self, edata: ExpDataPtr | ExpData):
:param edata: pointer to the ExpData instance
"""
if not isinstance(edata, (ExpDataPtr, ExpData)):
if not isinstance(edata, (ExpDataPtr | ExpData)):
raise TypeError(
f"Unsupported pointer {type(edata)}, must be"
f"amici.ExpDataPtr!"
Expand Down
Loading

0 comments on commit 1860e71

Please sign in to comment.