Repository navigation
Add sc.sequence_kspace, the exact trajectory of a compiled or read-back sequence - #62
Merged
Merged
Conversation
…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>
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.
Summary
A reconstruction has the written
.seqfile rather than the tree, so it needs the nominal trajectory of apypulseq.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 byD/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-backSequence. It returns the same named fields assc.kspace:k_adc,t_adc,k,t_k,t_excitation,t_refocusing.sc.kspace(tree, opts)is nowsc.sequence_kspace(sc.compile(tree, opts)). Its behaviour and dense-trajectory contract are unchanged.calculate_kspacePP(). A move to official PyPulseq would switch it to officialcalculate_kspace(), which is already exact there, without changing what it returns.What changes
src/seqcraft/analysis.pyandsrc/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:calculate_kspace()is still biased, and that a −D/2trajectory_delaydoes not repair it. It fails when the pin moves to a package whosecalculate_kspace()is exact, which is the signal to update the doc.examples/bssfp_2d/01_build.ipynbreads its trajectory withsc.sequence_kspace(seq).|kx| at echo max 0.897 1/m = 0.224 of one k-space step, explained as half a dwell of sampling.|kx| at echo max 8.1e-11 1/m = 2.0e-11 of one k-space step, with an assertion.symmetry_residual_s, a TE-versus-TR/2 timing quantity, not a k-space residual.examples/bssfp_3d/01_build.ipynbremoves the same claim from a comment. Its printed results are unchanged.docs/pypulseq_dependency.md, new. It covers:Opts.max_b1,rf_samples_limit,make_slr_pulse, themake_trapezoid(area, duration)shape, infinite construction limits, the trajectory API, the file version);It is linked from
README.md,docs/testing.mdanddocs/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 staleout.kspace()), andCHANGELOG.md.Checks run
These ran locally in
seqcraft-devon the unchanged pin:pulseq_systemsisn't installed.ruff check .and the strict mypy set all pass.sequence_kspaceswapped for one built on the pinnedcalculate_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_kspacein01,02and two tests, and drop its paragraph aboutcalculate_kspace().Recommendation: merge. It is additive (one public function), leaves
sc.kspaceunchanged, and changes no dependency.🤖 Generated with Claude Code