Skip to content

Bound legalization's raster resample and report it in moments - #61

Merged
yeewantung merged 2 commits into
mainfrom
fix/bounded-raster-resample
Nov 5, 2026
Merged

yeewantung merged 2 commits into
mainfrom
fix/bounded-raster-resample

Conversation

@yeewantung

Copy link
Copy Markdown
Collaborator

Bounds legalization's raster resample, refuses a resample beyond the bound, and reports each one in k, m0 and m1. ADR-008 amends the resampled-block clause of ADR-007. ADR-007's per-block, two-stage comparison is unchanged, and no whole-sequence m1 check comes back.

Why

Legalization resamples a block-axis onto the gradient raster when no pulseq gradient can hold its knots. The usual case is a trapezoid summed with a gradient sampled at raster centres.

ADR-007 compares the emitted block with the tree resampled the same way. That checks the emission, but not the approximation. The approximation's only contract was a warning giving the largest amplitude change as a fraction of max_grad. Measured on main:

  • The amplitude figure does not measure moments. The same 1.87 % went with no m0 change for a raster-rate zigzag, and with 31.7 1/m (7 Δk) for a legal half-raster input (oversampling=True) summed with a trapezoid.
  • Errors accumulate. Fifty resampled blocks added up to 1.75 Δk.
  • Some resamples were avoidable. A raster-centre gradient that starts inside its block and is then cut is the same event with a delay, but it was resampled.

The contract (ADR-008)

m0 and k. Keeping only raster-edge values replaces each interval's centre-sample tent by its chord. Over a stretch of samples the change telescopes to the two ends:

Δm0 = (r/8)·(D_end − D_start)
Δm1 = (r/8)·(t_b·D_end − t_a·D_start) − (r²/8)·(g_b − g_a)     about the block's start

D is the sample step across each end. Within the slew limit, a stretch moves the running k by at most r²·max_slew/4 at every instant, whatever its duration. That is 0.160 1/m at 10 µs and 150 T/m/s.

m1. By parts, Δm1 = L·Δk(L) − ∫Δk dt, so |Δm1| ≤ 2·L·sup|Δk|. The guard is therefore on the running k at every instant, not on block m0. A half-raster input whose edges rise and then fall keeps block m0 within the bound (0.16 1/m) while k climbs 15.9 1/m and m1 moves by 0.016 s/m. An m0-only guard would accept it.

Both closed forms and the identity were checked against measurement: equal to every printed digit for every raster-centre case, and the identity holds in every case, half-raster included.

Changes

  • _resampled measures the resample's running k exactly, on the union of knots and at zero crossings. It raises CompileError above r²·max_slew/4 per non-edge piece in the block. The only allowance is a relative 1e-9 for float rounding.
  • The resample warning keeps its amplitude figure and adds how far k moved against the bound, and the block's m0 and m1.
  • A new resample_total warning, for an axis resampled in more than one block, gives the net m0 and the summed magnitudes of the k and m1 changes. There is no signed m1 total: block m1 values share no origin.
  • _as_arbitrary holds a raster-centre run padded by zeros inside its block exactly, as an arbitrary gradient with a delay. This applies only to runs of five samples or more: PyPulseq stores four or fewer 5 µs short, so those stay a bounded resample. That includes the case that prompted the review.
  • Docs: ADR-008, docs/compiler.md, docs/api_reference.md, the CHANGELOG, and an "Amended by" line on ADR-007.

Validation

  • New tests: 9, in tests/compiler/test_resample_bound.py.
    • Three characterise the derived contract and pass on main too: the closed forms on a resampled sine stretch, a max-slew triangle reaching the bound, and a smooth half-raster sine compiling.
    • Six protect the change and fail on main: the warning's moments, the net and summed totals, the half-raster refusal, the m0-cancelling refusal, the exact delayed split, and four samples staying a bounded resample.
  • Gate: pytest 2042 passed, 21 skipped (the optional pulseq_systems import); slow tier 1; doctests 94; ruff, mypy, and the API-reference and prose checks.
  • Head-to-head against main: the 25 build notebooks (111 compiles) and 13 long trains (radial, spiral, rosette, EPI2D, ContinuousEPI2D) give the same verdicts and byte-identical emitted sequences.

The review and the m1 derivation are in the workspace: docs/plans/module-mining/compiler_findings/resample_bound/.

🤖 Generated with Claude Code

yeewantung and others added 2 commits November 5, 2026 11:16
A raster resample changed the waveform under a warning that stated only its
largest amplitude change. Over a stretch of raster-centre samples its area
changes telescope to the two ends, so within the slew limit it moves the
running k by at most raster^2 * max_slew / 4 at every instant, and m1 by at
most twice the block's length times that.

A resample measured beyond that bound, as a half-raster gradient summed with a
trapezoid can be, is now refused. Each resample's warning states how far it
moved k, m0 and m1, and an axis resampled in several blocks gets the net m0
and the summed magnitudes. A raster-centre run padded by zeros inside its block
is emitted exactly with a delay when PyPulseq can store it. ADR-008 amends
ADR-007's resampled-block clause.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yeewantung
yeewantung merged commit 8f06826 into main Nov 5, 2026
7 checks passed
@yeewantung
yeewantung deleted the fix/bounded-raster-resample branch November 5, 2026 18:34
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