Fitting spectra
I have a series loaded and want peak amplitudes out of it — one averaged spectrum, or a map across an MRSI grid.
MNUtils does not implement AMARES. xmris does (on top
of pyAMARES), and a series hands its FIDs straight to it: the series knows its own
reference_frequency and carrier_ppm, so all you supply is the prior knowledge — which peaks
to look for. Every fit returns the xr.Dataset that xmris produces, unchanged.
| Method | Fits |
|---|---|
MRSSeries.fit_average_fid() | the averaged FID of a single-voxel series |
MRSISeries.fit_single_voxel() | one voxel of an MRSI grid |
MRSISeries.fit_all_voxels() | every voxel above an SNR threshold |
from mnutils.GESeries import MRSISeries, MRSSeries
from mnutils.testing import build_fake_exam
DATA_FOLDER = build_fake_exam("brain_mrs_mrsi_exam") / "data"1. Say which peaks to look for¶
A peak is a name plus a starting guess; chem_shift is in absolute ppm. The same mapping is used
for every fit below.
prior_knowledge = {
"PCr": {"amplitude": 1, "chem_shift": 0.0, "linewidth": 20},
"gATP": {"amplitude": 1, "chem_shift": -2.5, "linewidth": 30},
}2. Fit one averaged spectrum¶
Amplitudes are only comparable between series if they are scaled the same way, so pass a shared
normalisation_factor; without one the series scales by its own maximum and warns.
mrs = MRSSeries(DATA_FOLDER, 7)
fit = mrs.fit_average_fid(prior_knowledge, normalisation_factor=1.0)
fit[["amplitude", "chem_shift", "linewidth", "fit_status"]]fit_status is 0 for a fitted spectrum, 1 for no signal and 2 for a failed fit, so a failure
is never mistaken for a zero amplitude.
3. Fit a whole MRSI grid¶
fit_all_voxels fits only voxels whose SNR clears snr_threshold; the rest stay NaN on the full
(i, j, k) grid. testing_mode=True limits it to the first 100 such voxels, which is plenty for
trying a prior-knowledge file out.
mrsi = MRSISeries(DATA_FOLDER, 8)
grid_fit = mrsi.fit_all_voxels(prior_knowledge, testing_mode=True)
grid_fit["amplitude"].sel(metabolite="PCr").dims/home/runner/work/mnutils/mnutils/.venv/lib/python3.12/site-packages/pyAMARES/kernel/fid.py:357: RuntimeWarning: invalid value encountered in scalar divide
return signal_p / noise_p
('i', 'j', 'k')The result is also summarised as plain arrays, ready for visualize_fitted_metabolite_map:
list(mrsi.fitted_metabolite_maps), list(mrsi.goodness_of_fit_maps)(['PCr', 'gATP'], ['residual_rms', 'fit_status'])