The NIfTI-MRS class
FSL-MRS inherits and extends the NIFTI_MRS from nifti-mrs-tools.
The main extension in FSL-MRS is a method to generate an MRS object directly from a NIfTI-MRS object (NIFTI_MRS.generate_mrs() or NIFTI_MRS.mrs(), see MRS and MRSI classes for more details).
Loading data
Valid NIfTI-MRS files can be loaded like this:
from fsl_mrs.core.nifti_mrs import NIFTI_MRS
nmrs = NIFTI_MRS('path/to/data.nii.gz')
The above command, takes a boolean argument validate_on_creation. If True (default), then the code will validate the header extension and SpectralWidth fields upon creation.
If the intent code format (condition 1) causes a failure during loading, then you can use spec2nii clean command to update the file:
spec2nii clean <file> [-f <output_file> -o <output_folder>]
If the header extension (condition 3) causes a failure, then you can read and write the file as follows - as long as the file has a valid JSON sidecar:
from fsl_mrs.utils.mrs_io import fsl_io
nmrs = fsl_io.readNIFTI('path/to/data.nii.gz')
nmrs.save('path/to/fixed_data.nii.gz')
Note
This method may also work for the other failure modes too.
Axes class
A NIfTI-MRS object by default creates an Axes object that can be accessed by NIFTI_MRS.axes. Alternatively, you may create an Axes object from a NIfTI-MRS object via the constructor Axes.from_nifti_mrs(nmrs)
Note
PPM axis and indices with no shift mean the values are zero-centred, whereas with shift mean the values are centred around the chemical shift reference value.
MRS and MRSI classes
An MRS object is the basic unit for fitting and encapsulates a single spectrum, the basis spectra, and water reference information required to carry out fitting. Similarly, an MRSI object encapsulates the same information but for multiple voxels.
A NIfTI-MRS object can be used to generate an MRS or MRSI object.
from fsl_mrs.core.nifti_mrs import NIFTI_MRS
nmrs = NIFTI_MRS('path/to/data.nii.gz')
mrs = nmrs.mrs()
The NIFTI_MRS.mrs() (and NIFTI_MRS.generate_mrs()) methods may also take dim, basis_file, basis, ref_data, and spatial_index arguments. See the code docstring for more details.
An MRS object can also be created from FID and Axes variables:
from fsl_mrs.core import MRS
mrs = MRS.from_axes(fid, axes)
Plotting data
A NIfTI-MRS object has a plotting method for MR spectra:
nmrs = NIFTI_MRS('path/to/data.nii.gz')
mrs = nmrs.plot()
This method may also take display_dim (Dim tag), ppmlim (ppm range), plot_avg (boolean), mask (filename), and legend (boolean) arguments.
Note
For MRSI data, this is equivalent to calling MRSI.plot() method.
Note
For SVS data, MRS.plot() is similar but not equivalent to NIFTI_MRS.plot(), as the latter plots multiple spectra on the same figure.
NIfTI-MRS data can also be viewed in FSLeyes with the MRS plugin