Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added gradient spectrum calculations #170

Merged

Conversation

FrankZijlstra
Copy link
Collaborator

This PR adds the gradient spectrum calculation as available in MATLAB Pulseq: https://github.com/pulseq/pulseq/blob/master/matlab/demoUnsorted/gradSpectrum.m

The implementation provides a fair number of parameters to control how the calculation is actually done (as we're not entirely sure what is the best way to go about this). The defaults should be sufficient to avoid peaks in the spectrum at the acoustic resonances of your scanner (and avoid damaging the gradient coils).

Basic usage (will plot the spectrums and also return them):

seq.calculate_gradient_spectrum()

Or including plotting the acoustic resonance ranges from a Siemens .asc file:

from pypulseq.utils.siemens.readasc import readasc
from pypulseq.utils.siemens.asc_to_hw import asc_to_acoustic_resonances

asc = readasc(asc_name)
seq.calculate_gradient_spectrum(acoustic_resonances=asc_to_acoustic_resonances(asc[0]))

…ectrum`)

- Added `asc_to_acoustic_resonances` function to get acoustic resonance ranges from Siemens asc files
@FrankZijlstra FrankZijlstra added the enhancement New feature or request label Mar 1, 2024
Copy link
Collaborator

@schuenke schuenke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I am missing appropriate example data, I was not able to check if the code works as expected and especially if the results fit to the Matlab results. Would be nice if you could check this on your own and confirm that it's true.

Maybe you can even add a figure of the comparison to this PR.

Having (some) tests would ofc also be nice, but due to the fact that most of the code is currently untested, this is probably nothing I can ask for 😀

Still, I added some comments that might help to improve the code style and the compatibility with (future) type checkers.

pypulseq/Sequence/calc_grad_spectrum.py Show resolved Hide resolved
pypulseq/Sequence/calc_grad_spectrum.py Outdated Show resolved Hide resolved
pypulseq/Sequence/calc_grad_spectrum.py Outdated Show resolved Hide resolved
pypulseq/Sequence/calc_grad_spectrum.py Outdated Show resolved Hide resolved
pypulseq/Sequence/calc_grad_spectrum.py Outdated Show resolved Hide resolved
pypulseq/Sequence/calc_grad_spectrum.py Show resolved Hide resolved
pypulseq/Sequence/calc_grad_spectrum.py Outdated Show resolved Hide resolved
pypulseq/Sequence/calc_grad_spectrum.py Show resolved Hide resolved
pypulseq/Sequence/calc_grad_spectrum.py Outdated Show resolved Hide resolved
pypulseq/Sequence/calc_grad_spectrum.py Outdated Show resolved Hide resolved
- Minor beauty fixes and type hints
- Renamed 'sos' to 'rss'
- Renamed `spectrogram_sos` to `spectrogram_rss`
- Changed `time_range` type hint to `Union` to be compatible with older python versions
- Changed initialization of `spectrogram_rss` from `0` to `np.array(0)`
@FrankZijlstra
Copy link
Collaborator Author

FrankZijlstra commented Mar 15, 2024

Python output for seq.calculate_gradient_spectrum(max_frequency=10000, combine_mode='rss') (parameters to be compatible with MATLAB defaults):
image

MATLAB output:
image

The different Y-scale is due to scipy's spectrogram function using different units than the computations in the MATLAB script. For now the Y-scale is arbitrary anyway, until we can figure out reasonable limits for acoustic resonances.

@FrankZijlstra FrankZijlstra merged commit 38831e4 into imr-framework:dev Mar 27, 2024
4 checks passed
@FrankZijlstra FrankZijlstra deleted the calculate_gradient_spectrum branch May 31, 2024 13:05
@schuenke schuenke mentioned this pull request Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants