Skip to content

Add sc.sequence_kspace, the exact trajectory of a compiled or read-back sequence - #62

Merged
yeewantung merged 1 commit into
mainfrom
fix/exact-sequence-kspace
Nov 8, 2026
Merged

yeewantung merged 1 commit into
mainfrom
fix/exact-sequence-kspace

Conversation

@yeewantung

Copy link
Copy Markdown
Collaborator

Summary

A reconstruction has the written .seq file rather than the tree, so it needs the nominal trajectory of a pypulseq.Sequence. The method within reach, seq.calculate_kspace(), is biased in the pinned PyPulseq (m-a-x-i-m-z/pypulseq-matlab-like@22ef2db). It samples gradients at raster edges and sums rectangles, so k comes out off by D/2·G(t), plus an interpolation term and a constant from any gradient through the RF centre. That is up to 4 1/m on the axis of a 5 mm slice, and 0.9 1/m on a Wave readout.

This PR adds one public function and changes no dependency:

  • sc.sequence_kspace(seq) takes a compiled or read-back Sequence. It returns the same named fields as sc.kspace: k_adc, t_adc, k, t_k, t_excitation, t_refocusing.
  • sc.kspace(tree, opts) is now sc.sequence_kspace(sc.compile(tree, opts)). Its behaviour and dense-trajectory contract are unchanged.
  • The backend is the pinned package's exact calculate_kspacePP(). A move to official PyPulseq would switch it to official calculate_kspace(), which is already exact there, without changing what it returns.
  • The pin is unchanged. No PyPulseq fork is introduced, and only one PyPulseq is installed.

What changes

  • src/seqcraft/analysis.py and src/seqcraft/__init__.py: the function, and the export.

  • tests/analysis/test_sequence_kspace.py, new. It checks the function against an independent NumPy integration of the block events:

    • Gradient shapes: a ramp, a trapezoid after a slice-selective excitation (a gradient through the RF centre), arbitrary gradients, Wave readouts with 5 and 6 cycles, and a spin echo.
    • Samplings: each shape is sampled on raster edges, half a raster off them, and at an incommensurate dwell (6.5 µs).
    • In memory and read back, for every case.
    • The dense trajectory at its own times, and the RF-centre fields.
    • A SeqCraft tree and its written file report one trajectory, with the echo sample at kx = 0 and no restart constant on the slice axis.
    • One test asserts that the pinned calculate_kspace() is still biased, and that a −D/2 trajectory_delay does not repair it. It fails when the pin moves to a package whose calculate_kspace() is exact, which is the signal to update the doc.
  • examples/bssfp_2d/01_build.ipynb reads its trajectory with sc.sequence_kspace(seq).

    • Before: |kx| at echo max 0.897 1/m = 0.224 of one k-space step, explained as half a dwell of sampling.
    • After: |kx| at echo max 8.1e-11 1/m = 2.0e-11 of one k-space step, with an assertion.
    • The half dwell it blamed is symmetry_residual_s, a TE-versus-TR/2 timing quantity, not a k-space residual.
  • examples/bssfp_3d/01_build.ipynb removes the same claim from a comment. Its printed results are unchanged.

  • docs/pypulseq_dependency.md, new. It covers:

    • the pin and why it is not official PyPulseq (Opts.max_b1, rf_samples_limit, make_slr_pulse, the make_trapezoid(area, duration) shape, infinite construction limits, the trajectory API, the file version);
    • the one-nominal-trajectory rule;
    • the defect and the supported routes;
    • the future backend switch;
    • the 10-item upgrade checklist.

    It is linked from README.md, docs/testing.md and docs/api_reference.md §7.7.

  • docs/api_reference.md §4.3, §4.6 and §9, docs/architecture.md, docs/writing_a_module.md (including a stale out.kspace()), and CHANGELOG.md.

Checks run

These ran locally in seqcraft-dev on the unchanged pin:

  • pytest: 2086 passed, 21 skipped. The skips are because the phantom data isn't in a fresh worktree and pulseq_systems isn't installed.
  • Static and documentation checks: 94 doctests, the API reference ("every example runs, and the index matches the package"), the prose attribution, ruff check . and the strict mypy set all pass.
  • Notebook smoke tier: all 25 notebooks execute.
  • Negative control: with sequence_kspace swapped for one built on the pinned calculate_kspace(), all 36 ADC-sample cases and both tree-and-file cases fail.

After merge

PR #60 will rebase onto this. It will then read the Wave files through sc.sequence_kspace in 01, 02 and two tests, and drop its paragraph about calculate_kspace().

Recommendation: merge. It is additive (one public function), leaves sc.kspace unchanged, and changes no dependency.

🤖 Generated with Claude Code

…ck sequence

A reconstruction has the written .seq file rather than the tree, so it needs the nominal
trajectory of a pypulseq.Sequence.  The method within reach, seq.calculate_kspace(), is biased in
the pinned PyPulseq: it samples gradients at raster edges and sums rectangles.

sc.sequence_kspace(seq) returns the named fields sc.kspace already returns, through the pinned
package's exact calculate_kspacePP(); sc.kspace(tree, opts) now delegates to it, with unchanged
behaviour.  The dependency pin is unchanged.

Tests check it against an independent integration of the block events (ramps, trapezoids after a
slice-selective excitation, arbitrary gradients, odd- and even-cycle Wave readouts, a spin echo;
samples on raster edges, half a raster off and at an incommensurate dwell; in memory and read
back), and that a tree and its written file report one trajectory.  One test pins that the
pinned calculate_kspace() is still biased, so a change of pin is noticed.

examples/bssfp_2d reads its trajectory through sc.sequence_kspace and no longer explains a
0.224-step kx at the echo as half a dwell of sampling: kx there is 8.1e-11 1/m.  bssfp_3d drops
the same claim from a comment.  docs/pypulseq_dependency.md records the pin, what official
PyPulseq does not yet reproduce, the defect, and the upgrade checklist.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yeewantung
yeewantung merged commit b6e06cd into main Nov 8, 2026
7 checks passed
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