Repository navigation
Compare m0 and m1 per compiled block, in two stages - #57
Merged
Merged
Conversation
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>
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 norebase or force-push. The only conflict was
CHANGELOG.md, and every section is kept in full. Then7accd60adds a short-piece control and marks ADR-007 accepted, and1ae6bcdstates exactly what comparing whole-sequence moments would add.The semantics, question by question.
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
PhysicalDesignScopeownthose on the design tree.
δm0at timetaddst·δm0to m1 aboutt = 0. So the verdict depended on where a legal waveform sat and on rounding.On
main, a golden-angleRadialReadouttrain compiles at 100, 400, 3000 and 6000 spokes and isrefused at 1000. A tolerance that absorbs this hides a 4.5 1/m blip played one raster late.
placement, splitting, superposition, resampling and registration.
duration and label addresses. With the grid exact,
M0 = Σ m0_kandM1 = Σ (m1_k + a_k·m0_k)follow from the block moments, so comparing whole-sequence momentsadds nothing a block-wise check misses.
it (
test_area_moved_across_a_block_boundary_is_refused).PhysicalDesignScope. A scope designs moments on the tree, about its own origin, over awindow 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-identicalsequences on
mainand here.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 sixcases with the registration comparison switched off.
mainrefuses the same cases, but throughthe whole-sequence comparison.
mainagainst this branch:mainEPI2D64 × 64, 10 / 150 volumesContinuousEPI2D, 10 / 150 windowsmain, 26.6 s here;gre_epi_2d/01's blip placed with no barrier, where the compiler merges the blip into thelobes. 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.seqfiles are identical.Decisions.
superposein ticks.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-5of 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 attmoved the absolute m1 byt·δm0.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
mainat9c03cf3. ADR-007 records the decision as Proposed.What changes in verification semantics
t = 0M0 = Σ m0_kandM1 = Σ (m1_k + a_k·m0_k), so they add nothing a block-wise check misses(block, axis)it resampled, as data; the compile stage compares it with the tree resampled onto the raster from the block's startVerdicts that change on the controls:
xfailis 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 thatmainrefuses by m1 (see Scaling); a summed end that cancels to 1 Hz/m, whichmainrefused as a compiler bug.tests/compiler/recheck.pycaptures the inputssc.compilehandsverify_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:Lis the block length,Na side's segment count,T0its|g|integral. m1 carries the corresponding lever arms.(N + 10)·u·T0N, any orderkpieces(k + 8)·u·E·L,Ethe summed largest end of the segments a cut value is interpolated fromsuperposein ticks from the block start4·u·L·S·L,Sthe summed peak slopeat an edge where the tree's valuevis under1e-6·max_grad,h` the reach to the next knot4·u·tof half a tick, or blocks past ~1100 s(1.5e-7 + (n + 10)·u)·P·S0,S0the span1e-5·(1 + 1e-5)·(T0 + C·S0),Cthe summed shape steps of at least a tenth of the peak1e6gradient rastersProduction-path tests
Every negative control reruns the check
sc.compileran, on its captured inputs, with only the expected side tampered (or, for registration, PyPulseq's stored library entry).tests/compiler/test_local_moments.py(new)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, andtest_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 warningtests/compiler/test_invariants.pytest_registration_catches_a_waveform_pypulseq_stores_wrong(stored amplitude changed by 1e-3); labels; durationtests/compiler/test_m0_tolerance.pytest_a_dense_low_duty_shape_with_systematic_rounding_compiles(the strictxfailis gone)tests/compiler/test_split_arbitrary_gradient.py(replacestest_m1_floor.py)−v·r/2; one raster late is refusedtests/compiler/test_legalization.pytest_superposition_is_the_same_wherever_the_block_sitsat 1, 10, 100 and 1000 stests/compiler/recheck.pyis the shared capture helper.tests/compilergoes from 167 passed and 1 xfailed onmainto 225 passed.Each check switched off. A harness edits one copy of the source per row, by an exact replacement, and runs
tests/compileragainst it. It never touches the checkout.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_lobetest_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_refusedtest_registration_catches_a_waveform_pypulseq_stores_wrongtest_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_momentstest_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_refusedtest_a_ramp_cut_far_from_its_knots_compilestest_a_start_near_half_a_tick_off_compilestest_the_compression_edge_case_is_inside_the_registration_tolerancetest_shapes_sharing_a_six_digit_key_are_within_the_registration_tolerance,test_six_digit_deduplication_is_within_the_registration_tolerancetest_shapes_sharing_a_six_digit_key_are_within_the_registration_tolerancetest_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_sitstest_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_compilestest_a_summed_end_that_cancels_to_nearly_zero_compiles,test_the_split_residual_is_the_endpoint_snapThirteen 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
07f7326and reproduced by scripts in the maintainers' planning records:Scaling
Measured with a script kept in the maintainers' planning records, against
main(9c03cf3) and07f7326, 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 functionssc.compilecalls: onmain,verify_against_tree, which integrates the whole sequence itself; on the branch,handed_gradientsbefore emission plusverify_against_tree. Not a benchmark.mainrefuses 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.Known limitations
1andt − 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 inverify_against_tree's docstring anddocs/compiler.md.xfailis removed.1e-5of a block's|g|integral (plus1e-5of 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.1e-6·max_grad(~1.7 Hz/m at 40 mT/m) are still zeroed, and modelled.1e6gradient rasters (10 s at 10 µs). Placements tested to 4000 s. PyPulseq refuses a 150 s block (pre-existing).What remains untested
1e6samples or more; time parameters of 10 s or more..seqfile.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,3c1d45band07f7326separately.d953fda63e8afcsuperposeinterpolates in ticks from the block's start; the compile tolerance loses its absolute-time term132122b3c1d45bc72a56c82ede877accd60)compiler_maintenance.mdasks for one when diagnostics may change122e7903c1d45b1e-6·max_gradand models it again07f7326The frozen structural baseline is unchanged throughout.
Checks
Locally, with the pinned PyPulseq fork
m-a-x-i-m-z/pypulseq-matlab-likeat22ef2db1f71ff38c8ce355c61913cfd8fceaac3b(archivesha256=22ffab17…828d), Python 3.12.13, NumPy 2.4.6:ruff check .clean at each commit; strictmypyon the configured nine modules, no issues.07f7326:pytest tests/compiler225 passed; the fast tier (-m "not slow and not bloch and not crossval and not hardware") 1862 passed, 21 skipped (pulseq_systemsnot installed), 0 xfailed;tools/check_api_reference.pypasses;tools/check_prose_attribution.pyreports no blocking issue.d953fda,63e8afc,132122b,c72a56c,122e790,3c1d45band07f7326, each separately: every job passed every time.Decisions for review
63e8afc1e-6·max_grad, modelled (head); narrow to float residue (132122b, reverted); narrow only where a piece continues across the edge (untried)c72a56cWhy 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
+2e4and−(2e4 − r)Hz/m with nothing after them:rmain9c03cf3132122b)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-referenceis untouched.🤖 Generated with Claude Code