"""The multiple-shooting transcription: #563's first consumer of ADR-0109's layer.
:class:`MultipleShootingProblem` implements the two abstract methods
:class:`~pybnf.transcription.augmented.TranscriptionProblem` declares, plus ``certify``,
and gets the augmented-Lagrangian outer loop, the penalty schedule, the homotopy, the
best-iterate certification, and the reporting for free. Everything specific to multiple
shooting is here:
* which data point belongs to which segment (:mod:`pybnf.shooting.grid`);
* how a segment is simulated from a state the transcription supplies
(:mod:`pybnf.shooting.backend`);
* how the fit's own objective is assembled over the *pieces*; and
* the continuity constraints ``c_j = Phi_j(z_j, theta) - z_{j+1}`` and their Jacobian.
The objective half needs no new residual math
----------------------------------------------
This is the structural finding the #563 prototype established, and it is what keeps this
module small. A segment-start state enters the data fit as an ``IC`` route with chain-rule
factor **1** -- ``sensitivity_ic`` is ``dy(t)/dy0``, and the transcription sets ``y0``
directly -- so an auxiliary variable is, to
:func:`~pybnf.gradient.assembly.assemble_gradient_and_fisher_hessian`, an ordinary free
parameter with an ordinary route. Each segment is presented to the assembly as its own
*experiment*: its own simulated ``Data``, its own slice of the observations, and its own
:class:`~pybnf.gradient.routing.ExperimentRouting`. The assembly already sums across
experiments, so the segmented data fit is the unsegmented one rearranged, and its gradient
and Fisher block come out over the augmented column list in one pass.
Two things that rearrangement has to get right, and both are structural rather than
cosmetic:
**Segment ``j > 0`` does not read the model's own initial conditions.** They were
overridden by ``z_j``. A reported free parameter that is a fitted initial condition
therefore has *no* effect on that segment, and its ``IC`` contribution is dropped from that
segment's routing. Keeping it would credit ``init_Z_state`` with a derivative it does not
have on ``m - 1`` of the ``m`` segments -- a wrong column the fit cannot detect from its own
objective. Segment 0 keeps it, which is exactly how a fitted initial condition reaches the
first continuity row.
**A quantity profiled or normalised across a whole series cannot be cut.** An analytic
per-series scale (ADR-0066) and a ``Data``-level normalization (ADR-0053/0102) are functions
of the series they are computed over, so splitting the series changes them; a
cumulative-to-incident transform (ADR-0051) is a difference between neighbouring rows, and
the row before a knot is in another piece. Those are refused by the ``ms`` fit type's gates
rather than silently rearranged. An analytically profiled **noise scale** (ADR-0108) is the
opposite case and needs no gate: it is profiled over the pooled residuals of every supplied
experiment, so cutting one series into ``m`` pieces pools exactly the same residuals and
gives exactly the same ``sigma_hat``.
The constraint terms never enter ``f``
---------------------------------------
:attr:`~pybnf.transcription.augmented.AugmentedModel.objective_value` is the fit's own
objective, scored by the fit's own objective function on the segmented trajectory. The
penalty lives strictly outside it (ADR-0109), which is what keeps an estimated noise scale
from absorbing continuity violation as measurement noise -- and 13 of the 23 slugs in the
motivating benchmark corpus estimate at least one.
"""
import numpy as np
from ..data import Data
from ..gradient import IC, NONE, PARAM, ExperimentRouting, ParamRoute
from ..gradient.assembly import assemble_gradient_and_fisher_hessian
from ..printing import PybnfError
from ..pset import FreeParameter
from ..transcription import (
AugmentedLayout,
StageSkipped,
BlockJacobian,
Certificate,
EqualityModel,
EqualitySystem,
JacobianBlock,
ObjectiveModel,
TranscriptionProblem,
VariableBlock,
)
from .backend import SegmentSimulationFailed
from .grid import EQUAL_TIME, SegmentGrid, max_segments
from .parallel import SERIAL, SegmentTask
#: Floor under a state magnitude, as a fraction of the experiment's largest, so a species that
#: is identically zero over the whole horizon still gets a strictly positive constraint scale
#: and a finite auxiliary box. The layer requires strictly positive scales precisely so this
#: decision is made once, here, rather than being discovered as a divide-by-zero inside the
#: penalty term. A fraction, not a number: an absolute 1e-12 was larger than every state of a
#: model in femtomolar units, which then neither scaled nor converged its segmented rungs.
STATE_FLOOR = 1e-12
#: What a knot coordinate is offset by, as a fraction of its state's scale: a knot ``u`` holds
#: the state ``10**u - KNOT_OFFSET * scale``. A plain log10 cannot hold an exact 0, and writing
#: one as a small positive floor changed the fit wherever the model amplifies it (an unseeded
#: autocatalyst regrows from it, a ratio of two floored species reads 0.5 where it is 0);
#: every test of whether a floor mattered was fooled in one direction or the other. With the
#: offset a 0 is exact at the seed (``10**log10(c) - c == 0.0``), any value above about 1e-16
#: of the offset keeps its relative precision, and the chain factor ``d state / d u =
#: ln(10) * 10**u`` is unchanged. Relative to the scale so the transcription is the same in
#: any units, and each rung's own, from the scale it is seeded at: a ladder whose first rung
#: was seeded where a state reached 8.5e21 and whose next was seeded where it is 1.6 decoded
#: that state to within 3e-5 (to 0 when the first scale was 4e30), so the rung was not feasible
#: at its own seed. A coordinate therefore means a different state on two rungs, and the
#: ladder carries a knot as the state it holds (:meth:`MultipleShootingProblem.carried_from`).
KNOT_OFFSET = 1e-12
#: Half-width, in decades, of an auxiliary state's box around its own magnitude. Wide
#: enough that the box is not a constraint on the search (the #563 prototype used a fixed
#: ``1e-6 .. 1e3`` window and never reported a knot pinned at a bound), finite because the
#: inner optimizers this layer feeds are bound-constrained and a segment-start
#: concentration that is allowed to go negative is not a state the simulator can restart
#: from.
AUX_DECADES = 6.0
[docs]
class ShootingExperiment:
"""One scored ``(model, condition)`` pair, at the fit level: everything about it that
does **not** depend on the segment count.
Built once per fit and shared by every rung of the ladder, which is what makes a stage
cheap to construct: a homotopy builds one :class:`SegmentedExperiment` per rung around
the same backend, observations, and routing.
:param key: ``(model name, suffix)`` -- the identity this experiment has in the
simulated/experimental data dictionaries the objective consumes.
:param backend: Its :class:`~pybnf.shooting.backend.SegmentBackend`.
:param exp_data: Its observations, as an ordinary PyBNF :class:`~pybnf.data.Data`.
:param routing: The experiment's prebuilt
:class:`~pybnf.gradient.routing.ExperimentRouting` over the **reported** free
parameters, exactly as the gradient optimizers build it once per fit.
:param times: Its measurement times. Defaults to ``exp_data``'s independent-variable
column.
:param label: The label its knots are named under. Defaults to the suffix.
:param start: Where the simulation starts. Defaults to the first measurement time; a
time course whose first measurement is after ``t = 0`` should pass ``0.0``.
:param placement: How this experiment's knots are placed -- one of
:data:`~pybnf.shooting.grid.PLACEMENTS`. Fixed for the whole fit, so every rung of
the ladder places its knots by the same rule and their fractions mean one thing.
:param knots: Explicit knot times for the finest rung, which force
``placement = 'explicit'``.
"""
def __init__(self, key, backend, exp_data, routing, times=None, label=None, start=None,
horizon=None, placement=EQUAL_TIME, knots=None):
self.key = (str(key[0]), str(key[1]))
self.backend = backend
self.exp_data = exp_data
self.routing = routing
if times is None:
times = np.asarray(exp_data.data, dtype=float)[:, exp_data.cols[exp_data.indvar]]
self.times = np.asarray(times, dtype=float).reshape(-1)
self.label = str(label if label is not None else self.key[1])
self.start = start
self.horizon = horizon
self.placement = str(placement)
self.knots = tuple(float(t) for t in (knots or ()))
[docs]
def grid(self, n_segments):
"""This experiment's knots at one segment count."""
return SegmentGrid(self.times, n_segments, label=self.label, start=self.start,
horizon=self.horizon, placement=self.placement,
knots=self.knots or None)
@property
def max_segments(self):
"""The finest rung this experiment's data supports under its own placement."""
return max_segments(self.times, self.placement, self.knots or None)
@property
def state_names(self):
return tuple(self.backend.state_names)
[docs]
class SegmentedExperiment:
"""One :class:`ShootingExperiment` cut at the knots of one stage."""
def __init__(self, spec, grid):
self.spec = spec
self.key = spec.key
self.backend = spec.backend
self.exp_data = spec.exp_data
self.routing = spec.routing
self.grid = grid
#: The ``m = 1`` grid of the same experiment -- what :meth:`certify` simulates.
#: Not a special case: the unsegmented problem is the one-segment transcription.
self.certification_grid = spec.grid(1)
self._subsets = {}
@property
def state_names(self):
return tuple(self.backend.state_names)
[docs]
def segment_key(self, segment):
"""The dictionary key one segment is scored under.
A synthetic suffix, distinct per segment, because the objective scores a
``{model: {suffix: Data}}`` mapping and the ``m`` pieces of one experiment must be
``m`` entries rather than one that overwrites the others.
"""
return '%s#seg%i' % (self.key[1], segment)
[docs]
def segment_exp_data(self, segment):
"""This experiment's observations restricted to one segment's rows.
Cached: the slice depends only on the grid, never on the fit point, so it is built
once per stage rather than once per evaluation.
"""
segment = int(segment)
if segment not in self._subsets:
self._subsets[segment] = _subset_rows(self.exp_data, self.grid.rows_in(segment))
return self._subsets[segment]
def has_data(self, segment):
return len(self.grid.rows_in(segment)) > 0
[docs]
class MultipleShootingProblem(TranscriptionProblem, EqualitySystem):
"""One rung of the segment ladder: the fit, transcribed at a fixed segment count.
:param experiments: The :class:`SegmentedExperiment`\\ s, all at the same segment
count.
:param objective: The fit's own objective function.
:param variables: The fit's reported free parameters, in ``Configuration.variables``
order -- the same order every existing PyBNF seam uses.
:param pset_from_u: ``(u_reported) -> PSet``: the algorithm's own sampling-space to
parameter-set bridge (:meth:`~pybnf.algorithms.base.Algorithm._pset_from_u`), so
this module never re-derives the ``theta <-> u`` transform.
:param blocks: The auxiliary :class:`~pybnf.transcription.layout.VariableBlock`\\ s,
one per knot of every experiment, already seeded (see :func:`seed_stage`).
:param scales: ``{experiment key: per-state constraint scale}`` -- the magnitude each
continuity defect is measured against.
:param name: The stage label for the trace (``'m=4'``).
:param pool: The :class:`~pybnf.shooting.parallel.SegmentPool` that runs this stage's
segment passes. Defaults to the serial one, so a caller that does not ask for lanes
gets exactly the behaviour that shipped with ADR-0110.
"""
def __init__(self, experiments, objective, variables, pset_from_u, blocks, scales,
name=None, pool=None):
self.experiments = list(experiments)
self._pool = pool or SERIAL
self.objective = objective
self.variables = list(variables)
self._pset_from_u = pset_from_u
self._scales = dict(scales)
#: Each experiment's per-state knot offsets: :data:`KNOT_OFFSET` of this stage's own
#: scales.
self._offsets = {key: KNOT_OFFSET * np.asarray(value, dtype=float)
for key, value in self._scales.items()}
self._name = name or ('m=%i' % (self.experiments[0].grid.n_segments
if self.experiments else 1))
lower = np.array([v.to_sampling_space(v.lower_bound) if v.bounded else -np.inf
for v in self.variables], dtype=float)
upper = np.array([v.to_sampling_space(v.upper_bound) if v.bounded else np.inf
for v in self.variables], dtype=float)
self._layout = AugmentedLayout([v.name for v in self.variables], lower, upper, blocks)
# Constraint identity, fixed for the stage: n_state rows per knot, in the layout's
# own block order, so a Jacobian block's row range is a plain slice.
names, rows_of = [], {}
for experiment in self.experiments:
for block_name in experiment.grid.block_names:
rows_of[block_name] = slice(len(names), len(names) + len(experiment.state_names))
names.extend('%s::%s' % (block_name, state)
for state in experiment.state_names)
self._constraint_names = tuple(names)
self._constraint_rows = rows_of
self._constraint_scales = np.array(
[self._scales[experiment.key][i]
for experiment in self.experiments
for _block in experiment.grid.block_names
for i in range(len(experiment.state_names))], dtype=float)
self._cache_key = None
self._cache = None
# The assembled objective at the last point asked for. Separate from the trace cache
# above because it must survive an interleaved `equality_at` at another point, and
# because it is what guarantees each simulated trajectory is scored once (#578; see
# :meth:`objective_at`).
self._objective_key = None
self._objective_model = None
self.n_certifications = 0
# -- identity ---------------------------------------------------------------
@property
def layout(self):
return self._layout
@property
def name(self):
return self._name
@property
def constraint_names(self):
return self._constraint_names
[docs]
def describe(self):
"""The stage in one line: what it added, and where."""
return '%s -- %s; %s' % (self._name, self._layout.describe(),
'; '.join(e.grid.describe() for e in self.experiments))
# -- the homotopy step ------------------------------------------------------
[docs]
def carried_from(self, previous, point):
"""Carry ``point``, where the rung before (``previous``) stopped, onto this rung, each
knot as the **state** it holds.
A knot coordinate ``log10(state + c)`` depends on the offset ``c`` its rung took from
its own scale (:data:`KNOT_OFFSET`), so the same coordinate is a different state on
two rungs: carried as a coordinate, a 0 held by a rung whose scale was below this one's
became -5e-23 here. Each carried knot is decoded with the rung before's offsets and
encoded with this rung's, so a 0 stays exactly 0 and every other value keeps its
precision. A state this rung's coordinate cannot hold (at or below ``-c``), or whose
coordinate is outside this rung's box, keeps this rung's own seed, as any out-of-box
carried value does
(:meth:`~pybnf.transcription.layout.AugmentedLayout.carry_over`).
"""
if not isinstance(previous, MultipleShootingProblem):
return super().carried_from(previous, point)
source = previous.layout
states = {}
for experiment in previous.experiments:
before = previous._offsets[experiment.key]
here = self._offsets[experiment.key]
for block_name in experiment.grid.block_names:
state = _knot_value(source.internal_of(point, block_name), before)
with np.errstate(invalid='ignore', divide='ignore'):
states[block_name] = np.log10(state + here)
translated = source.pack(source.reported_of(point), states)
return source.carry_over(translated, self._layout)
# -- the segment pass -------------------------------------------------------
def _knot_state(self, u, experiment, segment):
"""Segment ``segment``'s start state as ``{state name: value}``, or ``None`` for
segment 0 (which starts from the model's own initial conditions)."""
if segment == 0:
return None
block = experiment.grid.block_names[segment - 1]
values = _knot_value(self._layout.internal_of(u, block), self._offsets[experiment.key])
return dict(zip(experiment.state_names, values))
def _traces(self, u):
"""Every segment of every experiment, simulated once at ``u``.
Cached on the point, because the outer loop asks for the objective and the
constraints at the *same* point (``AugmentedSubproblem.at`` linearises both
together) and a segment simulation is the expensive thing here. ``None`` when any
segment failed to integrate: the caller turns that into a non-finite local model,
which is the signal the inner solver's trust region backs off on.
The pass itself belongs to :class:`~pybnf.shooting.parallel.SegmentPool` -- the
segments of one point are independent (each is one span from a state the
transcription already knows, with no data flowing between them), so whether they run
one at a time or several at once is a scheduling decision and not a property of the
transcription. This method's job is to say *which* spans, from *which* states.
"""
u = np.asarray(u, dtype=float).reshape(-1)
key = u.tobytes()
if self._cache_key == key:
return self._cache
pset = self._pset_from_u(self._layout.reported_of(u))
tasks, shape = [], []
for experiment in self.experiments:
shape.append(experiment.grid.n_segments)
for segment in range(experiment.grid.n_segments):
times, _rows = experiment.grid.sample_times(segment)
tasks.append(SegmentTask(experiment.backend, times,
self._knot_state(u, experiment, segment),
experiment.state_names))
flat, ok = self._pool.run(pset, tasks)
traces = _regroup(flat, shape) if ok else None
if traces is not None:
self._require_sensitivities(traces)
self._cache_key = key
self._cache = (pset, traces) if ok else None
return self._cache
def _require_sensitivities(self, traces):
"""Refuse a segment pass that came back carrying no derivatives at all.
A pass exists to build a local model, so a sensitivity-free segment is not a
property of the point the way a failed integration is -- it is a request that never
reached the simulator, and every point in the fit will come back the same way.
Saying so here, naming the segment seam, is the difference between a run that stops
with an actionable sentence and one that stops on the gradient assembly's own
message, which tells a ``job_type = ms`` user to enable a gradient path they did
enable: the sensitivity request is applied **per action** (#475/#482), so a segment
simulated under a suffix the request never reached carries a perfectly good
trajectory and nothing else (#584).
"""
for experiment, per_segment in zip(self.experiments, traces):
for segment, trace in enumerate(per_segment):
if trace.data.output_sensitivities is None:
raise PybnfError(
"Multiple shooting simulated segment %i of experiment '%s' and got "
"a trajectory carrying no forward sensitivities. A segment run has "
"to return both axes -- d(y)/d(theta) for the data rows and "
"d(end state)/d(start state) for the continuity block -- so there "
"is no local model to assemble from this one."
% (segment, experiment.key[1]),
hint='This is an internal wiring error rather than a property of '
'the fit: a sensitivity request is applied per action, so a '
'segment simulated under a suffix the request never reached '
'comes back bare. Please report it.')
def _free_params(self, u, pset):
"""The augmented free-parameter list, in layout order.
The reported block is read straight off the evaluated PSet -- so every
``d theta/d u`` factor is taken at the point actually simulated, exactly as
:meth:`~pybnf.algorithms.optimizers.gradient_base.GradientOptimizer.gradient_at`
does -- and each auxiliary coordinate is a ``loguniform_var``
:class:`~pybnf.pset.FreeParameter` carrying its own box. Presenting an auxiliary
state as a genuine free parameter is what lets the assembly treat it as one: it
supplies the ``ln(10) * 10**u`` chain factor from the same
:mod:`pybnf.priors.scale` code the reported parameters use, rather than from a
second implementation of log10. Its value is ``10**u``, the state plus its offset
(:data:`KNOT_OFFSET` of its scale): the offset does not change the chain factor, which
is all the assembly reads from it.
"""
params = [pset.get_param(v.name) for v in self.variables]
for block in self._layout.blocks:
values = self._layout.internal_of(u, block.name)
for name, lo, hi, value in zip(block.qualified_names, block.lower, block.upper,
values):
params.append(FreeParameter(name, 'loguniform_var', 10.0 ** lo, 10.0 ** hi,
value=10.0 ** value))
return params
# -- the objective ----------------------------------------------------------
[docs]
def objective_at(self, u):
"""The fit's objective and its derivatives at ``u``, memoised on the point.
The memo is not an optimisation first — it is what keeps this method **scoring each
simulated trajectory exactly once** (#578). Scoring goes through
``objective.evaluate_multiple``, which asks the measurement layer to materialise
every ``observable: <id>, formula: ...`` column *into the trajectory in place*
(ADR-0036), and that materialisation deliberately refuses a column that already
exists. Every ordinary fit satisfies it for free: the propose/score loop scores a
freshly simulated :class:`~pybnf.data.Data` each time. Multiple shooting does not,
because :meth:`_traces` caches the segment trajectories per point so that one
augmented-model evaluation costs one pass of segment simulations rather than two --
and the outer loop then re-evaluates at the point the inner solver finished at, which
is a cache hit on the very same objects.
Memoising the assembled model rather than copying the trajectories fixes that at the
cause: the second call returns the first call's answer instead of re-scoring
anything. It also removes a redundant gradient/Fisher assembly per outer iteration,
which is the larger of the two costs. Sound because this method does not depend on
the multipliers -- only
:class:`~pybnf.transcription.augmented.AugmentedModel` combines them -- so the
objective at a point is a property of the point alone.
"""
u = np.asarray(u, dtype=float).reshape(-1)
key = u.tobytes()
if self._objective_key == key:
return self._objective_model
model = self._build_objective(u)
self._objective_key, self._objective_model = key, model
return model
def _build_objective(self, u):
cached = self._traces(u)
if cached is None:
return self._unusable_objective()
pset, traces = cached
sim_dict, exp_dict, items = {}, {}, []
for experiment, per_segment in zip(self.experiments, traces):
model_name = experiment.key[0]
for segment, trace in enumerate(per_segment):
if not experiment.has_data(segment):
# A segment with no observations contributes no residual; its
# auxiliary state is determined by continuity alone, which is the
# normal case for an unobserved species and the corner the constraint
# scaling exists for.
continue
key = experiment.segment_key(segment)
sim_dict.setdefault(model_name, {})[key] = trace.data
exp_dict.setdefault(model_name, {})[key] = experiment.segment_exp_data(segment)
items.append((trace.data, experiment.segment_exp_data(segment),
self._segment_routing(experiment, segment), key))
# Score first, assemble second -- the same order the gradient optimizers use. The
# measurement layer materialises its expression columns during scoring, and the
# assembly reads those columns.
value = self.objective.evaluate_multiple(sim_dict, exp_dict, pset, show_warnings=False)
if value is None or not np.isfinite(value):
return self._unusable_objective()
grad = assemble_gradient_and_fisher_hessian(
self.objective, items, self._free_params(u, pset))
return ObjectiveModel.from_gradient_result(value, grad)
def _unusable_objective(self):
"""The local model at a point that did not simulate, or did not score.
Non-finite by construction, which is the signal
:meth:`~pybnf.transcription.augmented.AugmentedModel.is_finite` carries to the
inner solver: shrink the trust region and try a shorter step, rather than end the
run. A segment that fails is a property of the point.
"""
return ObjectiveModel(np.inf, np.full(self._layout.size, np.nan))
def _segment_routing(self, experiment, segment):
"""This segment's routing over the **augmented** free-parameter list.
Three populations, and the whole of the ``IC``-route finding is in the second:
* every reported free parameter keeps its route -- minus, for ``segment > 0``, any
``IC`` contribution to a state this segment overrode (see the module docstring);
* this segment's own auxiliary block routes to ``IC`` on its states with factor 1;
* every other auxiliary block gets an empty route, because this segment's
trajectory does not depend on another segment's start state. An empty route is a
structural zero column, not an omission: the assembly indexes routes by name into
the augmented column list, so a block that is absent from the routing would leave
an uninitialised column rather than a zero one.
"""
overridden = set(experiment.state_names) if segment > 0 else set()
routes = {}
for name, route in experiment.routing.routes.items():
kept = tuple(c for c in route.contributions
if not (c.target == IC and c.key in overridden))
routes[name] = ParamRoute(free_param=name, contributions=kept)
own = (experiment.grid.block_names[segment - 1] if segment > 0 else None)
for block in self._layout.blocks:
states = (experiment.state_names if block.name == own else ())
for qualified, state in zip(block.qualified_names, block.labels):
routes[qualified] = (
ParamRoute.single(qualified, IC, state, 1.0) if state in states
else ParamRoute(free_param=qualified, contributions=()))
return ExperimentRouting(routes=routes,
nominal_values=experiment.routing.nominal_values,
condition=experiment.routing.condition)
# -- the constraints --------------------------------------------------------
[docs]
def equality_at(self, u):
if not self._constraint_names:
return self.empty_model()
cached = self._traces(u)
if cached is None:
return self._unusable_equality()
_pset, traces = cached
u = np.asarray(u, dtype=float).reshape(-1) # this method indexes u by block slice
m = len(self._constraint_names)
residual = np.zeros(m)
blocks = []
reported = slice(0, self._layout.n_reported)
factors = self._reported_scale_factors(u)
for experiment, per_segment in zip(self.experiments, traces):
n_state = len(experiment.state_names)
for knot, block_name in enumerate(experiment.grid.block_names):
segment = knot # segment `knot` ends at knot `knot + 1`
trace = per_segment[segment]
rows = self._constraint_rows[block_name]
next_slice = self._layout.slice_of(block_name)
z_next = 10.0 ** u[next_slice] # d(state)/du / ln(10), not the state
residual[rows] = trace.end_state - _knot_value(u[next_slice],
self._offsets[experiment.key])
# d(end state)/d(reported parameters), folded from this segment's routing
# exactly as the objective's columns are, then taken into sampling space.
# The same routing the data rows of this segment were assembled through, so
# a fitted initial condition contributes here on segment 0 and nowhere else.
routing = self._segment_routing(experiment, segment)
d_reported = np.zeros((n_state, self._layout.n_reported))
for j, v in enumerate(self.variables):
d_reported[:, j] = _fold_route(routing.routes[v.name], trace)
blocks.append(JacobianBlock(rows, reported, d_reported * factors[np.newaxis, :]))
# d(end state)/d(this segment's own start state) -- the IC route, chain-ruled
# through the auxiliary variable's own log10 space.
if segment > 0:
own = experiment.grid.block_names[segment - 1]
own_slice = self._layout.slice_of(own)
z_own = 10.0 ** u[own_slice]
columns = _ic_columns(trace, experiment.state_names)
blocks.append(JacobianBlock(
rows, own_slice, columns * (np.log(10.0) * z_own)[np.newaxis, :]))
# d(-z_{j+1})/d(its own coordinates): the constant -I of the transcription,
# in the auxiliary variable's log10 space.
blocks.append(JacobianBlock(
rows, next_slice, -np.diag(np.log(10.0) * z_next)))
return EqualityModel(residual, BlockJacobian((m, self._layout.size), blocks),
scales=self._constraint_scales, names=self._constraint_names)
def _unusable_equality(self):
"""The constraint linearisation at a point that did not simulate: non-finite, so
the augmented model is, so the inner solver backs off."""
m = len(self._constraint_names)
return EqualityModel(np.full(m, np.inf),
BlockJacobian((m, self._layout.size), ()),
scales=self._constraint_scales, names=self._constraint_names)
def _reported_scale_factors(self, u):
"""``d theta/d u`` for the reported block at ``u``.
The constraint rows are in the model's own state units but their columns are in the
space the optimizer walks, so the reported half of every continuity block takes the
same chain factor the objective's Jacobian does.
"""
reported = self._layout.reported_of(u)
return np.array([v.d_from_sampling_space(reported[j]) if v.log_space else 1.0
for j, v in enumerate(self.variables)], dtype=float)
# -- certification ----------------------------------------------------------
[docs]
def certify(self, reported):
"""Reconstruct ``reported`` through the fit's ordinary unsegmented path.
Every auxiliary state is discarded and each experiment is simulated once over its
whole horizon from the model's own initial conditions -- which *is* the single-shoot
problem -- and scored by the fit's own objective. That score is the only one
comparable with an ordinary PyBNF fit's, and under ADR-0109 it is the only one this
run may report: the augmented objective at an infeasible point is computed on
trajectories that do not join up.
"""
self.n_certifications += 1
pset = self._pset_from_u(np.asarray(reported, dtype=float))
sim_dict, exp_dict = {}, {}
for experiment in self.experiments:
model_name, suffix = experiment.key
times, _rows = experiment.certification_grid.sample_times(0)
try:
data = experiment.backend.simulate(pset, times, None)
except SegmentSimulationFailed as exc:
experiment.backend.last_failure = str(exc)
return Certificate.reject('the reconstructed trajectory did not '
'simulate (%s)' % exc)
sim_dict.setdefault(model_name, {})[suffix] = data
exp_dict.setdefault(model_name, {})[suffix] = experiment.exp_data
value = self.objective.evaluate_multiple(sim_dict, exp_dict, pset, show_warnings=False)
if value is None or not np.isfinite(value):
return Certificate.reject('the reconstructed trajectory scored non-finite')
return Certificate.accept(value, detail='single-shoot reconstruction')
# ---------------------------------------------------------------------------
# Stage construction
# ---------------------------------------------------------------------------
[docs]
def seed_stage(specs, n_segments, objective, variables, pset_from_u, reported,
aux_decades=AUX_DECADES, name=None, pool=None):
"""Build one rung of the ladder, with its auxiliary states seeded from ``reported``.
Each experiment is simulated **once**, unsegmented, at the incoming parameters, and the
state is read off at each knot. Seeding this way makes the transcription feasible at
iteration zero: every continuity defect is exactly zero at the start point, so any
discontinuity the run subsequently holds is the optimizer's own choice rather than an
artifact of how the stage was built. It is also why a homotopy stage is a *callable*
in :func:`~pybnf.transcription.homotopy.run_homotopy` -- the seeds are not knowable
until the previous stage has finished moving ``theta``.
A start point that does not simulate is not a failure: the knots fall back to the
model's own declared state magnitudes, the first inner solve sees a large defect, and
the run proceeds (or stops with a stated reason) rather than dying at construction.
``aux_decades`` sets each auxiliary variable's box: ``+/- aux_decades`` around the
state's own magnitude, in log10. Wide enough not to constrain the search, finite
because the inner optimizers are bound-constrained and a segment-start concentration
that is allowed to go negative is not a state a simulator can restart from.
"""
reported = np.asarray(reported, dtype=float).reshape(-1)
pset = pset_from_u(reported)
name = name or ('m=%i' % n_segments)
staged, blocks, scales = [], [], {}
for spec in specs:
experiment = SegmentedExperiment(spec, spec.grid(n_segments))
seeds, magnitude = _seed_knots(experiment, pset)
scales[experiment.key] = magnitude
offset = KNOT_OFFSET * magnitude
centre = np.log10(magnitude)
lower, upper = centre - aux_decades, centre + aux_decades
for block_name, knot_time, seed in zip(experiment.grid.block_names,
experiment.grid.knot_times, seeds):
_require_positive_knot(experiment, knot_time, seed, magnitude, name)
# Every value from 0 up is stored exactly (KNOT_OFFSET): a floor written in place
# of a 0 changed the fit wherever the model amplified it, and one that did not know
# the model's units moved a decay that had reached 5e-13 up to 1e-12. A value just
# below 0, within the solver's noise (_require_positive_knot), is that noise: 0.
initial = _knot_coordinate(seed, offset)
# A knot's own value is always inside its box. A state that has fallen more than
# aux_decades below its peak by the knot used to be clipped up to the box's floor,
# so the rung started from a state the trajectory does not reach, and the scaled
# defect, measured against the peak, still read as continuous (a decay scored as
# ln(A) fitted a different series on its segmented rungs).
block_lower = np.minimum(lower, initial - 1.0)
block_upper = np.maximum(upper, initial + 1.0)
blocks.append(VariableBlock(block_name, experiment.state_names, block_lower,
block_upper, initial))
staged.append(experiment)
return MultipleShootingProblem(staged, objective, variables, pset_from_u, blocks, scales,
name=name, pool=pool)
def _seed_knots(experiment, pset):
"""``(per-knot seed states, per-state magnitude)`` from one unsegmented simulation.
The magnitude is the largest value the seeding trajectory reaches, which starts from the
state's own value at this point under this experiment's condition. Declared values
understate a species that starts empty and grows -- which on an oscillator is most of
them -- and a constraint scale that understates its state hands the penalty term a
condition number for free, which is the corner ADR-0109's scaling exists to close. They
also overstate one a condition lowers. A species declared at 0 used to take the median of
the other declarations (:attr:`~pybnf.shooting.backend.SegmentBackend.nominal_state`),
which made a product at 4e-3 beside a substrate at 1e6 read a hundredfold defect as
feasible. That stand-in is used only when the seeding run fails, and the knots then fall
back to it too. Every magnitude is floored at :data:`STATE_FLOOR` of the experiment's
largest.
"""
grid = experiment.grid
nominal = np.asarray(experiment.backend.nominal_state, dtype=float)
fallback = ([], _floored(nominal)) if grid.n_knots == 0 else (
[nominal.copy() for _ in range(grid.n_knots)], _floored(nominal))
if grid.n_knots == 0:
return fallback
try:
data = experiment.backend.simulate(pset, grid.seed_times(), None)
except SegmentSimulationFailed:
return fallback
columns = [data.cols[name] for name in experiment.state_names]
trajectory = np.abs(np.asarray(data.data, dtype=float)[:, columns])
if not np.all(np.isfinite(trajectory)):
return fallback
times = np.asarray(data.data, dtype=float)[:, data.cols[data.indvar]]
# The seeding run starts from each state's own value under this experiment's condition at
# this point, so its trajectory alone is the scale; a declared value is the model as
# written, and taking the larger of the two scaled a state a condition lowers by the wild
# type's value (a knot holding twice its state read as continuous).
magnitude = np.max(trajectory, axis=0)
seeds = []
for knot_time in grid.knot_times:
row = int(np.argmin(np.abs(times - knot_time)))
seeds.append(np.asarray(data.data, dtype=float)[row, columns])
return seeds, _floored(magnitude)
def _floored(magnitude):
"""``magnitude`` with every entry at least :data:`STATE_FLOOR` of the largest: strictly
positive, and in the model's own units. An experiment whose every state is 0 is given a
unit scale, as the backends' own stand-in for a zero declaration does."""
magnitude = np.abs(np.asarray(magnitude, dtype=float))
largest = float(np.max(magnitude)) if magnitude.size else 0.0
if not np.isfinite(largest) or largest <= 0.0:
largest = 1.0
return np.maximum(magnitude, STATE_FLOOR * largest)
#: How far below zero, in units of a state's numerical error, a knot value may sit and still be
#: the solver's noise around zero rather than a negative state.
NEGATIVE_KNOT_NOISE = 10.0
def _require_positive_knot(experiment, knot_time, seed, magnitude, stage):
"""Skip a rung whose seed puts a negative state at a knot: a knot is stored as the log10 of
each state, so it cannot hold one.
"Negative" means below the solver's own noise: a species that has decayed to 3e-8 and is
integrated at ``atol = 1e-4`` may come back as -4e-8, which is a zero to this test. The
noise is ``atol + rtol * |state|`` at the backend's tolerances
(:meth:`~pybnf.shooting.backend.SegmentBackend.integration_tolerances`), with room for the
gap between local and global error.
The rung is skipped, not the run ended: whether a state is negative at a knot depends on
the point the rung is seeded from (a start whose trajectory dips below zero, in a fit whose
answer is positive throughout), and the ladder's last rung has no knots. A species that
counts time up from -10 used to be floored and clipped to a positive value there, so the
segments after the knot integrated a different series.
"""
atol, rtol = experiment.backend.integration_tolerances()
negative = [(name, value) for name, value, scale
in zip(experiment.state_names, seed, magnitude)
if value < -NEGATIVE_KNOT_NOISE * (atol + rtol * scale)]
if not negative:
return
raise StageSkipped(stage, (
"a knot stores the log10 of each state, and in experiment '%s' at the knot at "
"t = %g %s at the point this rung is seeded from" % (
experiment.key[1], knot_time,
', '.join("'%s' is %.6g" % (name, value) for name, value in negative))))
# ---------------------------------------------------------------------------
# Small helpers
# ---------------------------------------------------------------------------
def _knot_coordinate(state, offset):
"""The knot coordinate that holds ``state`` (a value just below 0, the solver's noise
around it, is held as 0): ``log10(state + offset)``."""
return np.log10(np.maximum(np.asarray(state, dtype=float), 0.0) + offset)
def _knot_value(u, offset):
"""The state a knot coordinate holds: ``10**u - offset``, written as
``offset * expm1((u - log10(offset)) ln 10)`` so that the coordinate of a 0 decodes to
exactly 0 for any offset (``10**log10(1e-18) - 1e-18`` is 1.9e-34, from which an
autocatalyst regrows), and a value near 0 keeps its precision. Its derivative is
``ln(10) 10**u`` either way."""
offset = np.asarray(offset, dtype=float)
return offset * np.expm1((np.asarray(u, dtype=float) - np.log10(offset)) * np.log(10.0))
def _regroup(flat, shape):
"""A flat per-segment list back into one list per experiment."""
out, start = [], 0
for n in shape:
out.append(flat[start:start + n])
start += n
return out
def _fold_route(route, trace):
"""``d(end state)/d(one free parameter)``: the route's contributions, summed.
The same fold :func:`pybnf.gradient.assembly._raw_sensitivity_accessor` performs for a
scored column, applied to the end-knot row instead -- one place, so a condition's
chain-rule factor and a multi-column route reach the continuity block exactly as they
reach the data rows. A :data:`~pybnf.gradient.routing.NONE` term (a free noise scale)
and a pinned zero-factor term contribute nothing, by the same test the assembly uses.
"""
total = np.zeros(len(trace.end_state))
for contribution in route.contributions:
if contribution.target == NONE or contribution.factor == 0.0:
continue
if contribution.target == PARAM:
axis, tensor = trace.param_axis, trace.d_end_param
else:
axis, tensor = trace.ic_axis, trace.d_end_ic
if tensor is None or contribution.key not in axis:
raise PybnfError(
"Multiple shooting needs the %s sensitivity column '%s' at a segment's end "
"knot, and the segment simulation did not return it. The continuity "
"Jacobian is built from the same forward-sensitivity request the data rows "
"use, so a missing column is an internal wiring error."
% (contribution.target, contribution.key))
total = total + contribution.factor * tensor[:, axis.index(contribution.key)]
return total
def _ic_columns(trace, state_names):
"""``d(end state)/d(start state)`` as an ``(n_state, n_state)`` block.
The ``IC`` route with chain-rule factor 1, read off the tensor the segment already
returned: column ``s`` is ``d(end state)/d(z_s)``. Reordered to ``state_names`` so the
block's columns line up with the auxiliary block's own component order rather than with
whatever order the backend requested its sensitivity axis in.
"""
if trace.d_end_ic is None:
raise PybnfError(
'Multiple shooting needs initial-condition sensitivities at a segment end knot '
'(the continuity Jacobian is d(end state)/d(start state)), and the segment '
'simulation returned none.')
missing = [name for name in state_names if name not in trace.ic_axis]
if missing:
raise PybnfError(
'A multiple-shooting segment simulation returned no initial-condition '
'sensitivity axis for state(s) %s.' % ', '.join(sorted(missing)))
return trace.d_end_ic[:, [trace.ic_axis.index(name) for name in state_names]]
[docs]
class SegmentData(Data):
"""A segment's rows of an experiment's data, which still answers for the whole series
where scoring reads a whole-series property.
The one such property scoring reads from the data it is handed is a column's mean: the
divisor of ``ave_norm_sos`` and of a ``column_mean`` sigma. Computed over a segment's own
rows it gave each segment its own weighting -- the late segments of a decay, with small
means, far more than their share -- so the segmented rungs fitted a different objective
and steered the fit away from its optimum (#959). The mean depends only on the data, so
the whole series' value is exact here, and a segmented trajectory that joins up scores
exactly what the unsegmented fit scores.
"""
def __init__(self, arr, column_means):
super().__init__(arr=arr)
self._series_means = dict(column_means)
[docs]
def column_mean(self, col_header):
return self._series_means[col_header]
def _subset_rows(data, rows):
"""One experiment's :class:`~pybnf.data.Data` restricted to a row subset, as a
:class:`SegmentData`.
Everything scoring reads per row comes with the rows. Bootstrap weights are per point
(``sqrt(w)``-folded into both the residual and the Jacobian), so a segment must carry
its own rows' weights or a bootstrap refit would silently reweight the fit it is
resampling. The per-measurement binding table (``measurement_params``, ADR-0045) is
indexed by data row too, so each segment gets the tokens of the rows it keeps, in their
order; it used to be dropped, and a per-measurement model under ``job_type = ms`` stopped
asking for a table the experiment did declare (#1049). A column's mean is the whole
series' (see :class:`SegmentData`).
"""
rows = np.asarray(rows, dtype=int)
out = SegmentData(np.asarray(data.data, dtype=float)[rows, :].copy(),
{name: data.column_mean(name) for name in data.cols})
out.cols = dict(data.cols)
out.headers = dict(data.headers)
out.indvar = data.indvar
if data.weights is not None:
out.weights = np.asarray(data.weights, dtype=float)[rows, :].copy()
table = getattr(data, 'measurement_params', None)
if table is not None:
out.measurement_params = {
column: {placeholder: [tokens[row] for row in rows]
for placeholder, tokens in by_placeholder.items()}
for column, by_placeholder in table.items()}
return out