Fix:missing-t_ktraj-return-from-calculate_kspace - #282
Conversation
|
As far as I can see, the Matlab example sequences usually use the There is also (an old?) So in my opinion we should keep some consistency and either leave the Python function as it is or adjust it to match the Matlab |
|
I ran into this when I reimplemented One non-breaking option is to add a |
|
I think your suggestions makes sense. |
|
|
|
OK, then we can not mirror Pulseq calculateKspacePP. I think Franks suggestion sounds like a good solution. I think everyone that wants to match k-space data from a Skope fieldcam to the nominal trajectory would highly appreciate this change. I do not know how to alter this pull request accordingly. Frank can you help me? |
|
If we are going to introduce a breaking change with an option to roll back via a |
|
I don't think it makes sense to have the rollback option not set as a default, because then it would still break old code unless the new parameter is set. With it set to default we can give a deprecation warning. If we break things, I agree that returning a To be clear, I'm not opposed to a breaking change, because even when we deprecate the current result values, at some point the breaking change needs to happen anyway. But this is something that would need to be documented. And a user should not get a "too many values to unpack" error without any indication why. |
…t without breaking backwards compatibility (see pulseq#282).
|
I also found I needed this parameter and so rolled up a quick solution in line with what Bilal suggested here which seems to pass the standard tests. Is it worth me making a new pull request or merging it with this one somehow? |
|
I will have a look at this again. Probably tuesday next week. |
|
we see some other k-space inconsistencies, but it would be better if this PR is solved in 1.4 and 1.5 so we can rule some things out. so thumbs up for a solution to this, we are also happy to help here. |
|
okay, I agree it makes sense to keep the default behaviour as it is so that we dont break existing code. IMO, there are two options now:
What's your preference @FrankZijlstra @mzaiss @lukeje @btasdelen @Nikbert Ofc the variable names are up for discussion as well |
I would prefer this option (as implemented in my branch here: https://github.com/lukeje/pypulseq/tree/calculate_kspace_dict but with |
|
2 |
|
Okay, I implemented option 2 in c8cb776 As always when touching some files, I also did some additional refactoring. The most important things:
It should be easy to track the changes if you go through them step by step. It would be great if @Nikbert and maybe one more maintainer could review the changes. I prefer to avoid approving my own implementations / changes. |
|
This updated branch works for me. Though I noticed that |
|
Thanks for your feedback. I will have a look at the existing function calls and might update them accordingly. |
|
Looks good to me! Thanks for following up on this issue. |
|
Since its been a while and I wasn't sure myself if everything was ported correctly, I let Claude do another review that might help whoever wants to review this PR: Here you go: PR #282 Review: calculate_kspace refactoring +
|
| File | Change |
|---|---|
| Sequence/calculate_kspace.py | New file — extracted code with helper functions, renamed variables, added output_as_dict |
| Sequence/sequence.py | Thin wrapper delegating to ext_calculate_kspace |
| Sequence/ext_test_report.py | Updated to use output_as_dict=True |
| tests/test_calculate_kspace.py | New file — 3 tests for legacy, dict, and deprecated path |
| tests/test_sequence.py | 3 call sites updated to use output_as_dict=True |
Line-by-line comparison: nothing lost
All logic was preserved with only variable renames for clarity:
| Old (master) | New (PR) |
|---|---|
gw_pp |
grad_waveforms_pp |
gm_pp |
grad_moments_pp |
ng |
n_grad_channels |
tc |
time_candidates |
ii |
ramp_idx / idx_in_support |
dk |
k_offset |
ii_next_excitation |
next_excitation_idx |
ii_next_refocusing |
next_refocusing_idx |
self.* |
seq.* (standalone function) |
The three helper functions (_build_time_axis, _sample_gradient_moments, _apply_excitation_and_refocusing_resets) are clean extractions of the original inline code.
Improvements found during transfer
-
Bugfix in warning message — The old code had
error={t_ktraj(i_period) - t_excitation(ii_next_excitation)}using()(function call syntax) instead of[](indexing). This would have crashed with a TypeError if the warning was ever triggered. The new code correctly uses[]. -
Removed dead commented-out code —
# dk = -k_traj[:, i_period]was cleaned up. -
Robustified empty-list handling — Old
if tc != []:replaced withif len(time_candidates) > 0:plus explicitelse: time_candidates = np.zeros(0). Functionally equivalent but cleaner.
Things worth noting (not bugs, just observations)
-
slice_posis computed but never returned — This exists in both old and new code. The MATLABcalculateKspacePPdoes returnslicepos. Sinceoutput_as_dictwas introduced to make the return value extensible, it would be natural to addslice_posto the dict in a follow-up. Not a regression — this was pre-existing. -
calculate_kspacePP correctly forwards
output_as_dict— The deprecated wrapper properly passes through the new parameter. ✓ -
ext_test_report.py migration is correct — Old unpacking k_traj_adc, _, t_excitation, _, t_adc = self.calculate_kspace() replaced with dict access. ✓
-
test_sequence.py comparisons with
Approx— Switched from tuple to dict comparison. Works becausepytest.approxsupports dicts natively. ✓
Verdict
Safe to merge. The refactoring faithfully preserves all original logic, the new output_as_dict feature is backwards-compatible (defaults to False), and the transfer actually fixed a latent bug in the warning message. The only optional follow-up would be including slice_pos in the dict output to eventually match MATLAB's calculateKspacePP.
schuenke
left a comment
There was a problem hiding this comment.
Since I did a lot of refactoring myself, I would suggest that somebody else reviews this PR as well.
This adds t_ktraj to the return values of calculate_kspace, to make it consistent with the Matlab Pulseq Version.
t_ktraj is very handy when comparing measured and nominal trajectories.
All the Best