Simulation¶
m.simulator(...) returns a simulator; scenarios say what to run. See
Simulation and exploration.
tenqsp.Scenario
dataclass
¶
What to run: params (values by name, as m.solve(params=)), regimen (a
Regimen; left out: the model's own doses; Regimen(): none),
times (output times), start (default 0) and initial (the starting state: a
PreRun, or values by state name; left out: the model's own
initial values), under a name.
replace(**changes)
¶
A copy with changes. params given here are merged into this scenario's (the
names given change, the others stay; for a per-category parameter, the labels given
change, the others stay); every other field is replaced.
initial_vector(cm)
¶
The starting state as one number per state of the compiled model, NaN where the
model's own initial value stays; None if no initial is given.
save(path)
¶
Write the scenario as JSON (readable, and diffable under version control).
tenqsp.Simulator
¶
A model ready to run many times: sim = m.simulator(outputs=["conc"]), then
sim(scenario) or sim(params=..., regimen=..., times=...). Solver settings
(backend, method, rtol, atol, max_step, ledger, max_steps) are
fixed here, so every run is comparable. Changing values never
rebuilds the model.
function(scenario=None, *, wrt, derivatives='auto', record_room=None, batch=None, check=True, backend='jax')
¶
The simulation as a JAX function of the numbers named in wrt, for estimation
and any JAX code: f = sim.function(scenario, wrt=["CL", "V"]); f.names,
f.x0 (their values in the scenario), f(x) -> {output: array}, with x a
1-D array in f.names order; jax.grad, jax.jit and jax.vmap work on
it. A failed run gives NaN (f.status(x) says why; f.steps(x): the most
solver steps between two doses or events). Names: a parameter ("CL", "k"
for every entry, "k[R]"), or a number of the scenario's regimen, from 1
in time order: "dose 1 amount", "dose 1 time", "infusion 1 rate" (or
amount, duration, start). Always uses the JAX solver; see
gradients. derivatives=: "auto" (the default: "forward" up to 40
names in wrt, "reverse" above), "forward" (sensitivities carried along
the run; cost grows with the number of names) or "reverse" (the run recorded,
then replayed backwards; a gradient costs the same whatever the number of names);
f.derivatives says which. derivatives=False: for simulating (a population with
jax.vmap, a sweep) without differentiating: no record of the runs, so much less
memory and 2-3x faster; differentiating it raises.
record_room= (reverse only): the record holds this many times the steps of the
run at f.x0 (default 2; f.capacity: its size in steps). A run at an x
needing more gives NaN, and f.status(x) names the room to ask for.
A list of scenarios (sim.function([sc1, sc2, ...], wrt=...)) gives one function
of a table X (one row per scenario; fs.x0): fs(X) ->
{output: (scenarios, times, ...)}, NaN past each scenario's own times
(fs.t, fs.mask, fs.n_times), fs.status(X) one status per scenario.
Each scenario keeps its regimen, times, start and starting state; all run in one
call of one build, padded exactly (slots at t = +inf). batch=: "vmap" (the
default: each scenario its own steps, in one jax.vmap call) or "stacked" (the
scenarios as one system, with one sequence of steps chosen for the least easy of
them: several times faster for a small model and similar scenarios, slower where the
scenarios differ much, as a dose ladder does: see gradients); fs.batch says
which. check=False: no run of the scenarios
when the list is built (by default one call checks that each runs at its values, and
that call's compile serves later calls of fs; a caller that compiles its own
function around fs would compile twice). Calls take rtol, atol and
max_steps for that call, without a new compile, and with_steps=True for the
steps of the same run. backend="native" (a list of scenarios, forward or no
derivatives, a model without events): the solve compiled by numba, the
same steps as the JAX solver up to round-off and several times faster on the CPU;
the first build of a model compiles it (cached on disk).
sensitivity(scenario=None, *, wrt, relative=False)
¶
d output / d each name in wrt at every output time, at the scenario's values:
s = sim.sensitivity(scenario, wrt=["CL", "V"]), then s["conc"]["CL"],
s.values["conc"], s.table(). relative=True: d log y / d log theta (NaN
where y is 0). A failing run raises SolverError. Names as function.
batch(scenarios, *, times=None, n_jobs=1)
¶
Many scenarios in one call: b = sim.batch(tq.grid(params={"CL": [3, 5, 8]},
regimens={...}), times=...) or sim.batch([sc1, sc2, ...]). Returns a
BatchResult: b["conc"] (scenario x time x ...),
b.table(), b.to_xarray(), b.failed. Every scenario needs the same number of
output times (times= here sets them for all). A scenario that fails gives NaN and
is listed in b.failed; the others still run. n_jobs: that many processes. On
backend="jax" the scenarios of one shape run as one jax.vmap call. See
explore.
pre_run(scenario=None, *, until='steady', t_max=1000000.0)
¶
Run ahead and keep the end state, to start other scenarios from it:
pre = sim.pre_run(sc, until="steady") (or until=336, a fixed time), then
sim(sc2.replace(initial=pre)). "steady": with a regimen that repeats at even
intervals, until two intervals in a row end in the same state (the end is just
before the next dose); otherwise until every rate of change is within the
tolerances, after the doses. Gives up at t_max with a warning (pre.reached
False). The end state is a fixed starting point: derivatives (sim.function) do
not flow back through it. See explore.
tenqsp.SimResult
¶
The outputs of one run: r["conc"] (shape (n_t, ...)), r.t, r.scenario
(what was run, with every layer of values merged), r.to_xarray(), and the full
Solution as r.solution (all states, ledger, messages).
tenqsp.grid(params=None, regimens=None, *, base=None)
¶
Every combination of the values given, as scenarios: tq.grid(params={"CL": [3, 5],
"V": [40, 60]}, regimens={"100 q12h": r1, "200 q24h": r2}) makes 2 x 2 x 2 = 8, the
last name varying fastest (regimens last). params: a list of values per name (a
per-category value is a dict, e.g. {"k": [{"S": 1}, {"S": 2}]}); regimens: a
dict of named regimens, or a list (named "regimen 1", ...). base: a scenario whose
other settings (times, start, other values) every scenario starts from.
tenqsp.Grid
¶
Bases: list
A list of scenarios made by grid, which also knows what was varied:
columns (the parameter names, then "regimen" if regimens were given) and, per
scenario, labels[i] (its value of each column).
tenqsp.BatchResult
dataclass
¶
The outputs of many scenarios: b["conc"] (scenario x time x ...), b.t,
b.scenarios, b.failed ((index, name, reason) of each scenario that failed;
its outputs are NaN), b.table() (pandas, long), b.to_xarray(). b[i] runs
scenario i again and returns its full SimResult (the same
numbers; a batch keeps only the outputs, to keep its memory small).
table()
¶
One row per scenario x time x output (x field cell): columns scenario, the
varied values (self.columns), time, the output's axes, output, value.
Needs pandas.
to_xarray()
¶
An xarray.Dataset: each output with dims (scenario, time, its axes); the
varied values as coordinates along scenario. Needs xarray.
tenqsp.PreRun
dataclass
¶
The end of a pre-run: state (by name, as sol gives them), y (flat),
t (how long it ran), reached (until="steady": whether it got there), and
cycles (dosing intervals run, for a steady state under a repeated regimen). Start a
scenario from it with sc.replace(initial=pre).