A protected frame is on screen, so it anchors the walk

`domain/select`: the choosing half of a selection, pure and handed a plain
vector by each site. `pose/held-frame` was already the shared reading half;
nothing chose automatically at all.

The greedy walk rather than the DP, deliberately — it is what the prototype
shipped, and having two implementations is how the DP gets tested. Several tests
pin its exact output for that comparison and will need rewriting when it lands.

Protection is settled before the walk and is not unioned onto its result,
because a protected frame anchors the walk like any other kept frame. The anchor
is what is on screen, so measuring the next frame's drift from a frame that is
no longer displayed holds a one-frame closure across two frames and shows it
twice as long as it was measured. The test that says so is about the picture
rather than the algorithm: the frames the mouth was measured shut on and the
frames the selection shows it shut on are the same list.

A strict local extremum compares against the nearest DIFFERING samples, not the
immediate neighbours, and a run of equal samples is one extremum at the frame it
begins. Immediate neighbours find nothing at all on a closure lasting more than
one frame, which is most of them.

One width for the whole signal, so `distance` is never comparing the prefix two
samples happen to share; a non-finite tolerance falls back to nought, since NaN
compares false against everything and would quietly collapse a performance to a
single pose.

THE EXTREMA HALF HAS NO CALLER under the plan as it now stands, and this is the
commit to revert if it stays that way: `segments`, `turns`, the `:extrema`
branch of `propose` and the multi-component refusal that only guards it, plus
the three tests over them. Plate selections take no extrema by design, and the
performance half gets its closures from the `[:vis]` cut `flow/freeze` already
stores, so nothing asks this code for anything. It is correct and tested and
speculative; docs/frame-selection.md says so beside the build order.

Also corrects `ring/subsample-slots`, which claimed every even budget lands on
the cardinal positions. The corners do; the lip centres survive only multiples
of 4, so the aperture pair cannot be read off a subsampled ring at verts 6, 10,
14 or 18. That is why the performance signal reads a stored cut instead.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-10-01 10:13:00 -04:00
parent 14673385d4
commit 02069e88f0
3 changed files with 413 additions and 3 deletions

View file

@ -13,9 +13,15 @@
back to positions with indexOf would silently pick the wrong slot if a table
ever repeated an id.
For even n this naturally lands on the cardinal positions (corners and lip
centres) of a 20-point ring. Fixed indices, never adaptive decimation: the
vertex at slot k means the same thing on every frame of the shot."
Fixed indices, never adaptive decimation: the vertex at slot k means the same
thing on every frame of the shot.
WHICH CARDINAL POSITIONS SURVIVE DEPENDS ON n, and not merely on n being even.
On a 20-point ring the corners at 0 and 10 are kept by every even n, but the lip
centres at 5 and 15 are kept only when n is a multiple of 4: n = 6, 10, 14 and
18 all drop them. So nothing may assume a named slot is still in the output —
the aperture pair least of all. A signal that needs those two landmarks reads
them from a measurement, not from a subsampled ring."
[len n]
(mapv (fn [k] (mod (js/Math.round (/ (* k len) n)) len)) (range n)))

View file

@ -0,0 +1,191 @@
(ns arthur.domain.select
"Which frames out of a dense measurement are worth keeping.
A SELECTION IS A SET OF FRAMES CHOSEN OUT OF A DENSE MEASUREMENT, AND READ BY
HOLDING THE LATEST ONE AT OR BEFORE NOW. The holding half is `pose/held-frame`
and is already shared by tracing and by pose tracks; this is the choosing half,
which nothing did automatically before. One component, two sites: the plate
frames an artist has to draw a head on, and the frames a performance changes a
shape on.
`domain/cadence` answers the same question with no opinion about content, and
cannot know that the one frame where a mouth is fully shut is worth more than
its neighbours. That is the gap `:protect` closes.
THREE LAYERS, AND THE MIDDLE ONE IS DERIVED. A selection is a `:policy` (what
the proposer was asked for), a `:keep` and a `:drop` (what the hand insists on
and refuses). Only the hand's two sets are the document: the proposal is
recomputed whenever the policy or the signal changes, which is the whole reason
the hand's decisions are stored as their own sets rather than as the resulting
frame list. Re-suggesting at a new tolerance must never cost somebody their
pinned blink.
Pure. No store access and no clip access — each site reads its own dense data
and hands over a plain vector. Proposing is a command and not a subscription:
this allocates, which is fine for a button press over a few hundred frames and
would not be fine on the per-frame path.")
(defn- sample
"One sample as a flat vector of numbers, or nil where there is no measurement.
A plain number is a one-component sample, and nested points are flattened:
four transformed corners and the eight numbers in them are the same signal, so
neither site has to flatten on the way in. Nil is an ABSENT measurement — a
frame where the face was not found — which says nothing about the signal and is
not the same fact as a frame being skipped."
[s]
(cond
(nil? s) nil
(number? s) [s]
:else (not-empty (vec (flatten s)))))
(defn- distance
"How far apart two samples are: the largest absolute difference over their
components. A tolerance therefore means \"this far\" in the units the site
handed over, whether it handed over an aperture or a quad of corners, and
nobody has to say which.
Both samples are the same width, which `propose` has already insisted on rather
than comparing whatever prefix the two happen to share."
[a b]
(reduce (fn [m i] (max m (js/Math.abs (- (nth a i) (nth b i)))))
0 (range (count a))))
(defn- walk
"The greedy walk: keep frame 0 as the anchor, keep every frame in `forced`, and
keep every other frame that has moved further than `tolerance` from the anchor.
Each kept frame becomes the anchor.
A FORCED FRAME BECOMES THE ANCHOR LIKE ANY OTHER, which is why protection is
settled before the walk rather than unioned into its result. The anchor is what
is on screen; once a protected frame is kept the viewer is looking at it, so
measuring drift from a frame that is no longer displayed is simply wrong. Doing
it the other way holds a protected one-frame closure across two frames and
shows it twice as long as it was measured, which is the perceptual error the
protection was added to prevent.
Order-dependent and slightly suboptimal, and deliberately the first
implementation anyway — it is what the prototype shipped, and the dynamic
program that replaces it gets tested by being compared against it. Keeping the
anchor honest here leaves greed as the only difference between the two.
An absent sample is not a change: it cannot move the anchor and it is not worth
a frame. A measurement arriving where the anchor has none is, since a signal
coming back is a frame the picture has to show something on. A forced frame
with no measurement is still kept — it was asked for — and leaves the anchor
where it was, there being nothing there to measure from."
[n sig tolerance forced]
(loop [f 1 anchor (nth sig 0) kept (transient [0])]
(if (>= f n)
(persistent! kept)
(let [s (nth sig f)]
(if (or (contains? forced f)
(and s (or (nil? anchor) (> (distance anchor s) tolerance))))
(recur (inc f) (or s anchor) (conj! kept f))
(recur (inc f) anchor kept))))))
(defn- segments
"Maximal runs of consecutive measured frames. A gap is not something to compare
across: the samples either side of an absent measurement are not neighbours."
[n sig]
(->> (range n)
(partition-by #(some? (nth sig %)))
(remove #(nil? (nth sig (first %))))))
(defn- turns
"Frames in one measured segment that are a strict local extremum by more than
`tolerance` — a full mouth closure, a full blink. These are exactly the frames
a zero-order hold is most wrong about, and exactly what a cadence drops.
A run of equal samples is ONE extremum, reported at the frame it begins: the
hold reads from there, so naming any later frame of the run would show the
closure late. The ends of a segment are not extrema; they have only one side."
[sig tolerance frames]
(let [runs (vec (partition-by #(nth sig %) frames))
at #(first (nth sig %))]
(for [i (range 1 (dec (count runs)))
:let [f (first (nth runs i))
v (at f)
p (at (first (nth runs (dec i))))
q (at (first (nth runs (inc i))))]
:when (and (or (and (< v p) (< v q)) (and (> v p) (> v q)))
(> (min (js/Math.abs (- v p)) (js/Math.abs (- v q)))
tolerance))]
f)))
(defn propose
"Frames worth keeping out of `n`, given `signal`.
`signal` is a vector of n samples, each a number or a seq of numbers in a fixed
order. FRAME REMOVAL, NOT KEY EXTRACTION: every frame is a candidate and the
question is which can be dropped, which is why this walks and holds rather than
looking for peaks.
`:tolerance` is how far the signal may move before a frame has to be kept, in
whatever units the signal is in. `:protect` is the ONE input for frames that
have to be kept whatever the walk thought, and it carries two kinds of thing at
once so that it never has to become two parameters:
:extrema keep the signal's own strict local extrema as well
[12 30] keep these frames, whatever the walk thought
#{:extrema 12} both
The frames are how one selection constrains another — every kept plate frame is
a protected frame of the performance selection, so the drawing and the
performance can never cut against each other on neighbouring frames — and it is
the same mechanism that keeps a blink, which is why there is only one input.
`:extrema` is refused on a multi-component signal. The magnitude of a vector of
landmarks has local maxima that mean nothing; an aperture's closure is a real
extremum and a head has no such thing as an extreme position worth protecting.
Protection is settled BEFORE the walk, not unioned into its result, because a
protected frame anchors the walk like any other kept frame — see `walk`. The
result is therefore not `(walk) ∪ (protected)`: protected frames change what is
kept after them, which is the whole reason they are not applied afterwards.
Always contains frame 0 when there is a frame at all. The result is an ascending
vector of distinct frames, which is the shape both sites already store."
[n signal {:keys [tolerance protect]}]
(if-not (pos? n)
[]
(let [;; A slider that has been cleared reads as NaN, and every comparison
;; against NaN is false, which would propose frame 0 alone and quietly
;; collapse a whole performance to one pose. Nought keeps every frame
;; that changes, which is merely the cadence back again: wrong in a way
;; somebody can see and undo.
tol (if (js/Number.isFinite tolerance) tolerance 0)
sig (mapv #(sample (nth signal % nil)) (range n))
;; One width for the whole signal, so `distance` is never comparing the
;; prefix two samples happen to share. A reader that drops a component
;; on one frame is a bug with a loud version and a silent version, and
;; the silent version is a signal that is quietly the wrong shape.
seen (into #{} (map count) (remove nil? sig))
width (first seen)
p (cond (nil? protect) #{}
(keyword? protect) #{protect}
:else (set protect))
pins (into #{} (filter #(and (integer? %) (<= 0 %) (< % n))) p)]
(when (< 1 (count seen))
(throw (ex-info "signal samples are not all the same width"
{:widths (vec (sort seen))})))
(when (and (contains? p :extrema) width (< 1 width))
(throw (ex-info ":protect :extrema needs a single-component signal"
{:components width})))
(walk n sig tol (cond-> pins
(contains? p :extrema)
(into (mapcat #(turns sig tol %) (segments n sig))))))))
(defn effective
"`proposed` with the hand's decisions applied. Always contains 0: something has
to be on screen at the start.
Manual precedence is absolute — a drop beats a proposal, always — and it is the
proposal it beats, not the start of the take. Hand decisions apply whether or
not the proposer is switched on, which is why they are not a mode: turning
smart picking off must not throw away the frames somebody pinned."
[proposed keep drop]
(-> (reduce disj (into (set proposed) keep) drop)
(conj 0)
sort
vec))