Skip to content

Compare m0 and m1 per compiled block, in two stages - #57

Merged
yeewantung merged 11 commits into
mainfrom
feature/local-moment-verification
Nov 5, 2026
Merged

yeewantung merged 11 commits into
mainfrom
feature/local-moment-verification

Conversation

@yeewantung

@yeewantung yeewantung commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Review and decision, 2026-11-05: merge, ADR-007 accepted

Brought onto main (374b240, with #56, #58 and #59) by a merge, cff4fe7, so there was no
rebase or force-push. The only conflict was CHANGELOG.md, and every section is kept in full. Then
7accd60 adds a short-piece control and marks ADR-007 accepted, and 1ae6bcd states exactly what comparing whole-sequence moments would add.

The semantics, question by question.

  • What the global check established. Compiler fidelity: the waveform PyPulseq holds has, per
    axis, the tree's m0 and m1. m0 sees area lost or added; m1 sees area moved in time. It is not a
    physical design claim (k at the echo, flow compensation); modules and PhysicalDesignScope own
    those on the design tree.
  • Why absolute m1 is length-dependent. A per-event storage error δm0 at time t adds
    t·δm0 to m1 about t = 0. So the verdict depended on where a legal waveform sat and on rounding.
    On main, a golden-angle RadialReadout train compiles at 100, 400, 3000 and 6000 spokes and is
    refused at 1000. A tolerance that absorbs this hides a 4.5 1/m blip played one raster late.
  • Which checks belong where.
    • Events: limits, rasters, ADC rules and PyPulseq's timing check. Unchanged.
    • Blocks: m0 and m1 about the block start, in two stages. These are where the compiler acts:
      placement, splitting, superposition, resampling and registration.
    • Global, and exact: the registered block grid must equal the handed one in ticks, along with
      duration and label addresses. With the grid exact, M0 = Σ m0_k and
      M1 = Σ (m1_k + a_k·m0_k) follow from the block moments, so comparing whole-sequence moments
      adds nothing a block-wise check misses.
  • Moment moved across a block boundary. It is now refused; the whole-sequence m0 could not see
    it (test_area_moved_across_a_block_boundary_is_refused).
  • PhysicalDesignScope. A scope designs moments on the tree, about its own origin, over a
    window of milliseconds. Per-block fidelity bounds the compiled error over that window with a lever
    arm at most the window's length; the global check gave the scope nothing more. The three
    flow-compensation notebooks (gre_2d/03, gre_spiral_2d/03, megre_2d/03) emit byte-identical
    sequences on main and here.
  • Malformed short centre-sampled pieces. PyPulseq stores a centre-sampled gradient of four
    samples or fewer without its last half raster, losing 0.025 1/m on the control. It is refused at
    registration, naming that stage, at 0 s and 1 s; five samples are stored whole. The new test,
    test_a_short_centre_sampled_gradient_stored_short_is_refused_at_registration, fails in all six
    cases with the registration comparison switched off. main refuses the same cases, but through
    the whole-sequence comparison.
  • Long trains from shipped modules, main against this branch:
train main here emitted sequence
radial golden angle, 100 / 400 / 3000 / 6000 spokes compiles compiles identical
radial golden angle, 1000 spokes refused (m1) compiles —
spiral, 8 interleaves × 50 / 400 compiles compiles identical
rosette, 16 shots × 16 / 400 compiles compiles identical
EPI2D 64 × 64, 10 / 150 volumes compiles (0.59 / 15.0 s) compiles (0.40 / 11.3 s) identical
ContinuousEPI2D, 10 / 150 windows compiles (0.15 / 2.24 s) compiles (0.04 / 0.58 s) identical
  • Every build notebook (the 25 on the smoke list, 111 compiles):
    • the same verdict on both sides, including the two deliberate refusals;
    • total compile time 48.6 s on main, 26.6 s here;
    • 108 of 109 compiled sequences identical, digest for digest. The one that differs is
      gre_epi_2d/01's blip placed with no barrier, where the compiler merges the blip into the
      lobes. 128 of 258 gradient events move by at most 7.4e-8 Hz/m, 5.8e-14 of the peak — the
      superpose-in-ticks change, as the CHANGELOG states. The written .seq files are identical.

Decisions.

  1. Adopt the per-block, two-stage check, and retire the whole-sequence m0 and m1 refusals.
  2. Model resampled blocks.
  3. Keep superpose in ticks.
  4. Keep the endpoint snap, modelled.
  5. Keep the per-piece time allowance.
  6. Tolerate six-digit deduplication at registration. Refining it with PyPulseq's ids can follow.
  7. ADR-007 is Accepted with the merge.

The prototype branch was deleted earlier, with its commit archived.

Still not claimed: waveform fidelity beyond two moments per block; registration changes within
about 1e-5 of a block's |g| integral; real playback.

Draft for review. Not for merge until the decisions below are taken.

Summary

The against-tree check compared each axis's m0 and m1 over the whole sequence, with m1 referenced to t = 0. Registration moves a stored shape's area by δm0, so the same legal waveform placed at t moved the absolute m1 by t·δm0.

  • A compressible shape that compiled at 0 s was refused at 1, 10 and 100 s, by a message that blamed "a gradient plays at the wrong time" and printed two equal-looking numbers.
  • No tolerance alone fixed that. One wide enough at 10 s (3e-4 s/m) would hide a 4.5 1/m blip played one raster late (4.5e-5 s/m).

This PR compares m0 and m1 per compiled block and axis, each about the block's own start, in two stages, and retires the whole-sequence m0 and m1 refusals. Duration and label addresses are checked as before. It is based on main at 9c03cf3. ADR-007 records the decision as Proposed.

What changes in verification semantics

before now
unit of comparison each axis, whole sequence each compiled block and axis
m1 reference t = 0 the block's own start; every lever arm is a tick difference
what is compared the tree with what PyPulseq stored, under one tolerance compile: the tree, restricted exactly to each block, with the ready blocks emission hands PyPulseq, measured before registration. registration: those ready blocks with what PyPulseq stored
whole-sequence m0 and m1 refused beyond their tolerances not checked: M0 = Σ m0_k and M1 = Σ (m1_k + a_k·m0_k), so they add nothing a block-wise check misses
resampled blocks compared with the tree as given legalization reports each (block, axis) it resampled, as data; the compile stage compares it with the tree resampled onto the raster from the block's start
a refusal says "compiled m1 … differs from the tree sum … a gradient plays at the wrong time" the stage, axis, moment, PyPulseq block, start time, both values, the difference and the tolerance

Verdicts that change on the controls:

  • Accepted now: compressible shapes at 1, 10 and 100 s; the dense low-duty shape (its strict xfail is removed); two trapezoids at 100000.0 and 100000.4 Hz/m that share PyPulseq's six-digit key; golden-angle radial and resampled spiral trains that main refuses by m1 (see Scaling); a summed end that cancels to 1 Hz/m, which main refused as a compiler bug.
  • Refused now, missed before: area moved across a block boundary, which leaves the whole m0 unchanged.
  • Still refused: lost area, and a piece one raster late, at every placement from 0 to 100 s.

tests/compiler/recheck.py captures the inputs sc.compile hands verify_against_tree, and every tampered recheck reruns that function on them. There is no old-global fallback.

Error budget per stage

u = 2⁻⁵³. Per block and axis: L is the block length, N a side's segment count, T0 its |g| integral. m1 carries the corresponding lever arms.

stage term bound (m0) domain cannot detect
both float summation, each side (N + 10)·u·T0 any N, any order —
compile superposing k pieces (k + 8)·u·E·L, E the summed largest end of the segments a cut value is interpolated from superpose in ticks from the block start a compile change below it
compile the raster resample, in float seconds from the block start 4·u·L·S·L, S the summed peak slope resampled blocks no control needs it (see untested)
compile endpoint snap ` v ·h/2at an edge where the tree's valuevis under1e-6·max_grad, h` the reach to the next knot
compile time representation one tick, more past ~1100 s, times variation and end values only pieces whose start sits within 4·u·t of half a tick, or blocks past ~1100 s a real displacement of that size on those pieces
registration compression, shapes only (1.5e-7 + (n + 10)·u)·P·S0, S0 the span any duty; includes the first-sample edge case a stored change below 1.5 quanta of the peak
registration six-digit key 1e-5·(1 + 1e-5)·(T0 + C·S0), C the summed shape steps of at least a tenth of the peak time parameters and shape lengths below 1e6 gradient rasters a stored change within PyPulseq's six-digit precision
  • Compile error and storage error stay apart. The key allowance is in the registration stage alone, and stored against tree is bounded by the two tolerances together.
  • Compression edge case. A first sample exactly on a half quantum makes rounding half away from zero carry one more quantum into every later sample. The bound is therefore 1.5 quanta, not 0.5, and a test needs more than 0.5 quanta plus the pointwise key allowance.

Production-path tests

Every negative control reruns the check sc.compile ran, on its captured inputs, with only the expected side tampered (or, for registration, PyPulseq's stored library entry).

file tests (cases) what they protect
tests/compiler/test_local_moments.py (new) 20 (47) correct translation: compressible zero-net and non-zero-net shapes compile at 0, 1, 10 and 100 s (test_a_compressible_shape_compiles_wherever_it_sits); one shape compares identically at 0 and 100 s; block moments sum to the whole-sequence ones. Lost area: a 0.05 1/m sliver at 0–100 s. A mistimed piece of non-zero area: a 4.5 1/m blip one raster late at 0–100 s, in a block held open so m0 cannot see it. Boundary crossing: 0.05 1/m moved across a block edge, whole m0 unchanged (test_area_moved_across_a_block_boundary_is_refused). Long blocks: a 97 ms shape and a blip in one block. Same-axis superposition: two overlapping pieces, one late; a blip summed onto a shape. Resampled blocks: test_a_resampled_stretch_with_sloped_ends_compiles, a trapezoid late inside a resampled block, and test_a_resample_on_one_axis_leaves_the_others_checked_block_by_block. Tolerance terms: ramps cut far from their knots, starts near half a tick, summed ends that cancel under the snap, shared six-digit keys (trapezoids and shapes), the compression edge case. The refusal text; no new warning
tests/compiler/test_invariants.py 11 (11) each invariant fires: m1 sees a 10 µs shift m0 cannot; m0 sees a lost lobe; test_registration_catches_a_waveform_pypulseq_stores_wrong (stored amplitude changed by 1e-3); labels; duration
tests/compiler/test_m0_tolerance.py 8 (16) compressible zero-net shapes compile; the stored area equals the shape's own rounding; four packagings, one verdict; lost area and a late lobe at 0, 1 and 10 s; test_a_dense_low_duty_shape_with_systematic_rounding_compiles (the strict xfail is gone)
tests/compiler/test_split_arbitrary_gradient.py (replaces test_m1_floor.py) 3 (20) ten split sign-changing gradients compile; the split residual is exactly the endpoint snap's −v·r/2; one raster late is refused
tests/compiler/test_legalization.py 2 (5) test_superposition_is_the_same_wherever_the_block_sits at 1, 10, 100 and 1000 s

tests/compiler/recheck.py is the shared capture helper. tests/compiler goes from 167 passed and 1 xfailed on main to 225 passed.

Each check switched off. A harness edits one copy of the source per row, by an exact replacement, and runs tests/compiler against it. It never touches the checkout.

switched off what that means compiler tests that fail
none the branch as committed 0 of 225: none
compile m0 off the compile stage never refuses m0 13 of 225: test_a_lost_sliver_is_refused_wherever_it_sits, test_a_resample_on_one_axis_leaves_the_others_checked_block_by_block, test_area_actually_lost_on_a_sign_changing_axis_is_still_refused, test_area_moved_across_a_block_boundary_is_refused, test_lost_area_is_refused_wherever_the_axis_sits, test_m0_still_catches_a_lost_lobe
compile m1 off the compile stage never refuses m1 17 of 225: test_a_blip_one_raster_late_is_refused_wherever_it_sits, test_a_lobe_one_raster_late_is_refused_wherever_the_axis_sits, test_a_long_block_compiles_and_still_sees_a_late_blip, test_a_refusal_names_the_stage_block_time_difference_and_tolerance, test_a_resample_on_one_axis_leaves_the_others_checked_block_by_block, test_a_split_gradient_played_one_raster_late_is_refused, test_m1_catches_a_gradient_that_plays_at_the_wrong_time, test_m1_still_refuses_a_piece_played_one_raster_late, test_superposed_pieces_compile_and_a_late_one_is_refused
registration off the registration stage never refuses 1 of 225: test_registration_catches_a_waveform_pypulseq_stores_wrong
m1 about the sequence start every side takes m1 about t = 0 again, as the whole-sequence check did 11 of 225: test_a_legal_sign_changing_gradient_compiles, test_a_ramp_cut_far_from_its_knots_compiles, test_a_shape_compares_the_same_wherever_it_sits, test_a_split_gradient_played_one_raster_late_is_refused, test_a_start_near_half_a_tick_off_compiles, test_block_moments_sum_to_the_whole_sequence_moments
resample model off a resampled block is compared with the tree as given 8 of 225: test_a_resample_on_one_axis_leaves_the_others_checked_block_by_block, test_a_resampled_stretch_with_sloped_ends_compiles, test_a_start_near_half_a_tick_off_compiles, test_a_trapezoid_late_inside_a_resampled_block_is_refused
resample interpolation term off the raster resample's float, in seconds from the block's start, is not allowed for 0 of 225: none
segment ends replaced by block peaks superposition float scaled by the peak inside the block, not by the ends of the segments a cut value is interpolated from 3 of 225: test_a_ramp_cut_far_from_its_knots_compiles
time representation term off a knot a tick off is not allowed for 3 of 225: test_a_start_near_half_a_tick_off_compiles
compression edge case off half a quantum per stored sample, as if no first sample sat on a half quantum 1 of 225: test_the_compression_edge_case_is_inside_the_registration_tolerance
key allowance off PyPulseq's six-digit deduplication is not allowed for 3 of 225: test_shapes_sharing_a_six_digit_key_are_within_the_registration_tolerance, test_six_digit_deduplication_is_within_the_registration_tolerance
coarse key term off a coarse shape difference that a shared key carries into later samples is not allowed for 1 of 225: test_shapes_sharing_a_six_digit_key_are_within_the_registration_tolerance
superpose at absolute seconds the compiler change of the second commit undone; the check's tolerance as committed 7 of 225: test_a_ramp_cut_far_from_its_knots_compiles, test_a_resampled_stretch_with_sloped_ends_compiles, test_a_trapezoid_late_inside_a_resampled_block_is_refused, test_superposition_is_the_same_wherever_the_block_sits
snap model off the endpoint snap is not modelled in the compile tolerance 12 of 225: test_a_legal_sign_changing_gradient_compiles, test_a_split_gradient_played_one_raster_late_is_refused, test_a_summed_end_that_cancels_to_nearly_zero_compiles
endpoint snap removed the compiler no longer zeroes a superposed edge value below 1e-6 of max_grad 10 of 225: test_a_summed_end_that_cancels_to_nearly_zero_compiles, test_the_split_residual_is_the_endpoint_snap

Thirteen of the fourteen switches fail at least one test. The raster-resample interpolation term fails none (and 40 random oscillating resampled blocks pass without it); it is kept as a stated bound.

Further evidence, all on 07f7326 and reproduced by scripts in the maintainers' planning records:

  • Long placements. Four controls at 600, 2000 and 4000 s all compile; all are refused with every gradient one raster late.
  • Starts near half a tick. 67 legal trees with a start up to 0.4999 tick off its tick, at 1 to 4000 s, all compile and are all refused one raster late; with the time allowance off, 10 of them are refused.
  • Random legal trees. For 60 s: 32 202 random trees (trapezoids, arbitrary gradients and extended trapezoids, random barriers, placements to 700 s) compiled, none refused by the check; 2923 were refused as input and counted apart.

Scaling

Measured with a script kept in the maintainers' planning records, against main (9c03cf3) and 07f7326, on one machine (Apple M3 Pro), one run after the other. Each tree is built once and compiled three times; the medians are shown. The check's time is measured by wrapping the stage functions sc.compile calls: on main, verify_against_tree, which integrates the whole sequence itself; on the branch, handed_gradients before emission plus verify_against_tree. Not a benchmark.

case blocks base compile (s) base check (s) base share branch compile (s) branch check (s) branch share compile ratio
GRE 128 x 1 slices 767 0.243 0.130 53 % 0.126 0.011 9 % 0.52
GRE 128 x 2 slices 1535 0.487 0.260 53 % 0.247 0.021 8 % 0.51
GRE 128 x 4 slices 3071 0.970 0.516 53 % 0.518 0.042 8 % 0.53
GRE 128 x 8 slices 6143 1.965 1.029 52 % 1.028 0.083 8 % 0.52
radial, 1000 spokes 2999 0.448 (refused) 0.337 75 % 0.142 0.027 19 % 0.32
radial, 3000 spokes 8999 1.343 (refused) 1.001 75 % 0.415 0.080 19 % 0.31
radial, 9000 spokes 26999 4.069 (refused) 3.011 74 % 1.284 0.242 19 % 0.32
spiral train, 100 TRs (resampled) 299 0.335 (refused) 0.085 25 % 0.348 0.056 16 % 1.04
spiral train, 400 TRs (resampled) 1199 1.326 (refused) 0.339 26 % 1.214 0.232 19 % 0.92
  • Linear in blocks on both sides: GRE from 767 to 6143 blocks, radial from 2999 to 26 999.
  • The realistic GRE (128 × 8, the slow tier's budget recipe): the check falls from 1.03 s, 52 % of the compile, to 0.083 s, 8 %. The whole compile falls from 1.97 s to 1.03 s.
  • main refuses all five other recipes. Golden-angle radial at every size, and the resampled spiral train at both, are refused by its whole-sequence m1 ("a gradient plays at the wrong time"), at differences of 0.01 to 0.3 s/m: the translated registration error this PR removes. The timing there is still complete, since the old check measures everything before it raises.
  • On the branch the check costs 8 to 19 % of the compile; at 27 000 radial blocks, 0.24 s of 1.28 s.

Known limitations

  • Not waveform fidelity. Two linear functionals per block cannot see a change that keeps every block's area and centroid (a distortion orthogonal to 1 and t − a, a zero-net piece moved inside its block, two equal pieces swapped), nor anything below the tolerance. RF/ADC timing, limits and continuity are checked elsewhere; the written file is not read back. Stated in verify_against_tree's docstring and docs/compiler.md.
  • Low-duty gap: closed. The compression bound is per stored sample over the span, at any duty; the strict xfail is removed.
  • Deduplication: tolerated, not corrected. An event sharing PyPulseq's six-digit key still plays the earlier event's values (100000.4 → 100000.0 Hz/m; a 3e-7 tail dropped). Both are tests.
  • Registration resolution. The key allowance applies to every block, since the check cannot tell which events share a library entry without PyPulseq's ids. A stored change within about 1e-5 of a block's |g| integral (plus 1e-5 of its coarse shape steps over its span) is invisible to registration: 0.22 1/m for the 27 ms compressible test shape, whose registration actually moves it by 2.9e-5. The compile stage is unaffected.
  • Compression edge case: the bound. 1.5 quanta; ordinary shapes see at most 0.5.
  • Resample ends: modelled. Sloped, non-zero ends tested at 0 and 10 s. A reported resample is accepted as the raster interpolant of the tree.
  • The endpoint snap stays (see the commits). Edge values below 1e-6·max_grad (~1.7 Hz/m at 40 mT/m) are still zeroed, and modelled.
  • Domain. Time parameters and shape lengths below 1e6 gradient rasters (10 s at 10 µs). Placements tested to 4000 s. PyPulseq refuses a 150 s block (pre-existing).
  • Diagnostics. The m0/m1 refusal text is new; it is internal error text, not API.

What remains untested

  • The raster-resample interpolation term: no control needs it (40 random oscillating resampled blocks pass without it). Kept as a stated bound, since the superposition term does not bound it in general.
  • Placements past 4000 s; shapes of 1e6 samples or more; time parameters of 10 s or more.
  • A PyPulseq registration fault other than a changed library amplitude.
  • Locally: the slow, simulation, cross-validation, hardware and lab-notebook tiers.
  • Memory of the ready-side summary (six float64 per block and axis, 144 bytes per block): derived, not measured.
  • Real playback, and the written .seq file.

The commits

The compiler change is in a commit of its own, together with the tolerance term it makes unnecessary. CI ran the full matrix on each of d953fda, 63e8afc, 132122b, c72a56c, 122e790, 3c1d45b and 07f7326 separately.

commit change justified by
d953fda the two-stage per-block check; resampled blocks reported as data and modelled; tests on the production path; docs the translated registration error above
63e8afc superpose interpolates in ticks from the block's start; the compile tolerance loses its absolute-time term a value at a cut carried an error that grew with placement: a block's area moved by up to 1.5e-10 1/m at 100 s and 1.2e-9 at 1000 s, against 1.1e-14 at 0 s. Now bit-identical at any placement
132122b narrowed the endpoint snap to float residue reverted by 3c1d45b
c72a56c the one-tick time allowance applies only to pieces that can land a tick off allowed everywhere it blunted the tolerance on oscillating shapes (up to 7.7e-5 1/m per block); without it, 10 of 67 legal near-half-tick trees were refused
82ede87 ADR-007, Proposed (accepted in 7accd60) compiler_maintenance.md asks for one when diagnostics may change
122e790 controls for the two terms no test yet needed the harness
3c1d45b keeps the endpoint snap at 1e-6·max_grad and models it again PyPulseq's unterminated-end check (decision 4)
07f7326 names the time term in the API reference; two phrasing fixes documentation

The frozen structural baseline is unchanged throughout.

Checks

Locally, with the pinned PyPulseq fork m-a-x-i-m-z/pypulseq-matlab-like at 22ef2db1f71ff38c8ce355c61913cfd8fceaac3b (archive sha256=22ffab17…828d), Python 3.12.13, NumPy 2.4.6:

  • ruff check . clean at each commit; strict mypy on the configured nine modules, no issues.
  • At 07f7326: pytest tests/compiler 225 passed; the fast tier (-m "not slow and not bloch and not crossval and not hardware") 1862 passed, 21 skipped (pulseq_systems not installed), 0 xfailed; tools/check_api_reference.py passes; tools/check_prose_attribution.py reports no blocking issue.
  • GitHub CI ran all seven jobs on d953fda, 63e8afc, 132122b, c72a56c, 122e790, 3c1d45b and 07f7326, each separately: every job passed every time.
  • Not run: the slow, simulation, cross-validation, hardware and lab-notebook tiers.

Decisions for review

# decision options recommendation
1 the per-block, two-stage check; whole-sequence m0/m1 refusals retired adopt; keep the old check adopt
2 resampled blocks model from the tree (here); merge a stretch's seams (the earlier design's recommendation) model: merging is wrong for sloped ends and coarsens the check
3 63e8afc keep; drop and restore the absolute-time term keep
4 the endpoint snap keep at 1e-6·max_grad, modelled (head); narrow to float residue (132122b, reverted); narrow only where a piece continues across the edge (untried) keep, modelled. This reverses the earlier design's recommendation, on the evidence below
5 c72a56c keep; one tick for every piece keep
6 six-digit deduplication tolerate in registration (here); refuse; apply the allowance only where an earlier, different event registered the entry (needs PyPulseq's ids) tolerate; the refinement can follow
7 ADR-007 Proposed; Accepted with the merge Accepted with the merge

Why the snap stays. PyPulseq's timing check refuses a gradient end above 1e-9 Hz/m that the next block does not continue. The snap keeps a summed end that cancels to nearly zero from being refused. Two pieces ending at +2e4 and −(2e4 − r) Hz/m with nothing after them:

r main 9c03cf3 narrowed (132122b) this PR
0.001 Hz/m compiles, end zeroed refused by PyPulseq compiles, end zeroed
1 Hz/m refused by the whole-sequence m0, as a compiler bug refused by PyPulseq compiles, end zeroed
2 Hz/m refused by PyPulseq refused by PyPulseq refused by PyPulseq

Recommendation

Recommend merging once decisions 1–7 are taken, as recommended above. No blocking issue is known: 13 of the 14 checks and terms switched off fail a test, and the fourteenth is a stated bound no case needs; CI is green on every commit; on the realistic GRE the check costs 8 % of the compile instead of 52 %. This PR is a draft and has not been merged; merging is the maintainer's call. The superseded trial branch proto/m1-local-reference is untouched.

🤖 Generated with Claude Code

yeewantung and others added 11 commits October 26, 2026 06:18
The against-tree check compared each axis's m0 and m1 over the whole
sequence, with m1 about t = 0. Registration moves a stored shape's area
by dm0, so the same legal waveform placed at t moved the absolute m1 by
t * dm0: a compressible shape that compiled at 0 s was refused at 1 s
and later, and no tolerance alone separated that from a blip played one
raster late far into a sequence.

m0 and m1 are now compared per compiled block and axis, each about the
block's own start, in two stages.

- Compile: the tree's placed events, restricted exactly to each block,
  against the ready blocks emission hands PyPulseq, measured before
  registration. The tolerance is SeqCraft's own arithmetic: float,
  superpose's interpolation at absolute times, the endpoint snap
  modelled from the tree, and a knot landing a tick off.
- Registration: those ready blocks against what PyPulseq stored. The
  tolerance is its storage precision: compression rounding of 1.5
  quanta of the peak, which is the first-sample edge case, and the
  six-digit key it deduplicates events by, which never enters the
  compile stage.

The whole-sequence m0 and m1 refusals are retired. They are sums of
the block moments and add nothing a block-wise check misses.

Legalization reports each resampled block and axis as data, and the
compile stage compares it with the tree resampled onto the raster the
same way. A resample on one axis coarsens nothing on another, and a
resampled stretch with sloped ends is compared exactly.

Tampered rechecks run the check sc.compile ran, on its captured inputs.
The low-duty shape's strict xfail is gone: the per-sample storage bound
has no duty condition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
superpose evaluated each piece at the union of knots in absolute float
seconds. A value interpolated at a cut, or at another piece's knot,
then carried an error that grew with the block's position in the
sequence: on two controls a block's area moved by up to 1.5e-10 1/m at
100 s and 1.2e-9 1/m at 1000 s, against 1.1e-14 at 0 s, and the
compile stage needed a tolerance term that grew with placement to
absorb it.

It now interpolates in ticks from the block's start, where every
difference is one of exact integers. The same block superposes to the
same knots, bit for bit, at any placement, and the compile tolerance
keeps only a block-length term for the raster resample, which still
interpolates in float seconds from the block's start. The frozen
structural baseline is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
axis_gradient set a superposed block's first or last value to zero
whenever it was below 1e-6 of max_grad. That threshold is about a
million times the float residue it exists to remove, so it also zeroed
genuine seam values, up to 1.36 Hz/m at 32 mT/m. The triangle it removed
was the whole of the split residue the whole-sequence m1 once needed a
floor for, and the per-block check has had to model it from the tree.

superpose now returns an end value as exactly zero only within the float
bound of the segments it is interpolated from: (k + 8) roundings of
their larger ends for k pieces, the same bound the compile tolerance
uses for any superposed value. Pieces cancelling at a seam still leave
an exact zero there; a genuine seam value is kept, so the emitted
waveform changes at those seams and nowhere else. The compile stage no
longer models a snap, and verify_against_tree loses its snap argument.
The frozen structural baseline is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The compile tolerance allowed every piece a tick of displacement, at
any placement, because superposing turns each knot's absolute float
time into ticks itself. That allowance scales with a piece's variation,
so on oscillating shapes it dominated the tolerance and blunted it: on
the sign-changing test gradients it allowed 9.3e-7 to 7.7e-5 1/m per
block, where the old endpoint snap moved them by 2.5e-7 to 4.0e-6.

A knot can land a tick from where the tree side puts it only if its
piece's start lies within a few roundings of u * t of half a tick from
its own tick -- the raster check accepts anything closer than half --
or, for every piece in a block, once u * t approaches a tick, past about
1100 s. The allowance now applies to those pieces alone and is zero for
the rest. Without it, legal trees with starts 0.4999 tick off at 100 s,
0.49 at 300 s and 0.3 at 4000 s were refused; those are now tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
compiler_maintenance.md asks for an ADR when a change may move
diagnostics or waveforms, and this one moves both: refusals are worded
per block and stage, and genuine seam values below 1e-6 of max_grad are
no longer zeroed. ADR-007 records the two-stage comparison, why the
whole-sequence refusals add nothing, why resampled blocks are modelled
rather than merged, and the two compiler commits that go with it. It is
marked Proposed until the change is reviewed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Switching terms of the per-block check off one at a time showed two that
no test depended on. Each now has a control that fails without it.

- The compile tolerance scales superposition float by the ends of the
  segments a cut value is interpolated from. Near the foot of a long
  ramp down, that end can be tens of milliseconds and a million Hz/m
  away. Scaled by the block's own peak, the tolerance refused 13 of 60
  random legal ramps cut into short blocks; three are now tests.
- The registration tolerance allows for a coarse shape difference that
  a shared six-digit key carries into every later sample. Two
  compressible shapes that print alike, one with a 3e-7 tail the other
  lacks, are stored as one: 7e-3 1/m of area moves, which the pointwise
  and compression allowances alone would refuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This reverts the compiler change of 132122b, which zeroed a superposed
edge value only where float could not tell it from zero.

PyPulseq's timing check refuses a gradient end above 1e-9 Hz/m that the
next block does not continue. The snap below
1e-6 of max_grad is what keeps a summed end that cancels to nearly zero
-- through a user's own arithmetic as much as SeqCraft's -- from being
refused. With the snap narrowed, two pieces ending at +2e4 and
-(2e4 - 0.001) Hz/m with nothing after them were refused by PyPulseq.
With it kept, they compile. On main the same tree with a 1 Hz/m residual
was refused by the whole-sequence m0 as a compiler bug; per block, with
the snap modelled, it compiles. Both are now tests.

The compile tolerance models the snap again from the tree's value at
each block edge, as it did after 63e8afc, and its margin now also allows
for a knot a tick off on an unsure piece. Seam values below the
threshold are zeroed as on main, so this PR changes no emitted waveform
beyond the float roundoff of 63e8afc. ADR-007 records the narrowing as
tried and reverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The API reference's summary of the compile stage left out the tick a
knot's time can land off. compiler.md's domain note read "gradient time
pieces", and handed_gradients claimed about 170 bytes per block where it
keeps six float64 per block and axis, 144 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in RosetteReadout (#56), ContinuousEPI2D (#59) and the EPI2D
divisor refusal (#58). The only conflict was CHANGELOG.md, where every
section is kept in full with this branch's entry first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…R-007

PyPulseq stores a centre-sampled gradient of four samples or fewer with an
explicit time shape and decodes it as ending on its last sample, so the
half raster out to `last` is lost: 0.025 1/m on the control. The
registration stage refuses it, wherever it sits, and names that stage;
five samples are stored whole. With the registration comparison switched
off, all six cases of the new test fail, so it guards that stage.

ADR-007 is accepted with this merge. Its consequences now say what stays
global and exact (the block grid in ticks, duration, label addresses), and
that a storage fault is refused at the stage where it happens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The accepted ADR said nothing is lost by not comparing them. Small errors
within each block's tolerance can still add up, so state the actual
argument: with the block grid exact, a whole-sequence comparison only
re-tests the block moments, within either the sum of their tolerances or
a narrower one that brings back the translated term.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yeewantung
yeewantung marked this pull request as ready for review November 5, 2026 10:26
@yeewantung
yeewantung merged commit 7f24c83 into main Nov 5, 2026
7 checks passed
@yeewantung
yeewantung deleted the feature/local-moment-verification branch November 5, 2026 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant