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

BF: time units #35

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
This document contains the nifti_mrs_tools release history in reverse chronological order.

1.3.3 (Friday 8th November 2024)
-----------------------------------
- Handle different time units in nifti header for dwelltime (with thanks to @Septem).
- Handle singleton coil dimensions with `mrs_tools vis` (with thanks to @Septem).

1.3.2 (Wednesday 23rd October 2024)
-----------------------------------
- Better visualisation using `mrs_tools vis` for data containing ISIS, metabolite cycling or editing.
Expand Down
9 changes: 8 additions & 1 deletion src/nifti_mrs/nifti_mrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,14 @@ def set_version_info(self, major, minor):
@property
def dwelltime(self):
'''Return dwelltime in seconds'''
return self.header['pixdim'][4]
t_unit = self.header.get_xyzt_units()[1]
dwelltime = self.header['pixdim'][4]
if t_unit == "msec":
return dwelltime * 1e-3
elif t_unit == "usec":
return dwelltime * 1e-6
else:
return dwelltime

@dwelltime.setter
def dwelltime(self, new_dt):
Expand Down
3 changes: 2 additions & 1 deletion src/nifti_mrs/vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def vis_nifti_mrs(data, display_dim=None, ppmlim=None, plot_avg=False, mask=None

if data.ndim > 4 \
and 'DIM_COIL' in data.dim_tags\
and display_dim != 'DIM_COIL':
and display_dim != 'DIM_COIL'\
and data.shape[data.dim_position('DIM_COIL')] > 1:
print('Performing coil combination')
data = nifti_mrs_proc.coilcombine(data)

Expand Down
19 changes: 19 additions & 0 deletions tests/test_nifti_mrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ def test_modification_mrs_meta():
assert obj.nifti_mrs_version == '1.3'


def test_time_units():
obj = NIFTI_MRS(data['unprocessed'])

obj.image.header.set_xyzt_units('mm', 'sec')
obj.dwelltime = 1 / 5000
assert obj.spectralwidth == 5000
assert obj.bandwidth == 5000

obj.image.header.set_xyzt_units('mm', 'msec')
obj.dwelltime = 1E3 / 5000
assert obj.spectralwidth == 5000
assert obj.bandwidth == 5000

obj.image.header.set_xyzt_units('mm', 'usec')
obj.dwelltime = 1E6 / 5000
assert obj.spectralwidth == 5000
assert obj.bandwidth == 5000


def test_hdr_ext():
obj = NIFTI_MRS(data['unprocessed'])

Expand Down
48 changes: 48 additions & 0 deletions tests/test_nifti_mrs_vis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from pathlib import Path

import pytest

testsPath = Path(__file__).parent
test_data_raw = testsPath / 'test_data' / 'metab_raw.nii.gz'
test_data_proc = testsPath / 'test_data' / 'metab.nii.gz'


def test_vis_error(tmp_path):
try:
import fsl_mrs # noqa: F401
except ImportError:
with pytest.raises(
ImportError,
match="NIfTI-MRS visualisation requires FSL-MRS tools to be installed. "
"See fsl-mrs.com for installation instructions."):
import nifti_mrs.vis # noqa: F401
else:
pytest.skip("fsl-mrs present, skipping test")


@pytest.mark.with_fsl_mrs
def test_vis_svs(tmp_path):
import nifti_mrs.vis as vis
from nifti_mrs.nifti_mrs import NIFTI_MRS
import matplotlib

nmrs = NIFTI_MRS(test_data_raw)
fig = vis.vis_nifti_mrs(nmrs)
assert isinstance(fig, matplotlib.figure.Figure)

nmrs = NIFTI_MRS(test_data_proc)
fig = vis.vis_nifti_mrs(nmrs)
assert isinstance(fig, matplotlib.figure.Figure)


@pytest.mark.with_fsl_mrs
def test_vis_svs_singleton_channels(tmp_path):
import nifti_mrs.vis as vis
from nifti_mrs.nifti_mrs import NIFTI_MRS
import matplotlib

nmrs = NIFTI_MRS(test_data_proc)
nmrs.set_dim_tag(4, 'DIM_COIL')
assert nmrs.shape[-1] == 1
fig = vis.vis_nifti_mrs(nmrs)
assert isinstance(fig, matplotlib.figure.Figure)
Loading