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

MincHeader.get_xyzt_units() #1098

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 26 additions & 1 deletion nibabel/minc1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from numbers import Integral

import numpy as np
from typing import Tuple

from .externals.netcdf import netcdf_file

Expand Down Expand Up @@ -115,6 +116,9 @@ def get_affine(self):
aff[:nspatial, nspatial] = origin
return aff

def get_units(self) -> Tuple[str, str, str]:
return tuple(d.units.decode('utf-8') for d in self._dims)

def _get_valid_range(self):
""" Return valid range for image data

Expand Down Expand Up @@ -289,6 +293,18 @@ class MincHeader(SpatialHeader):
# We don't use the data layout - this just in case we do later
data_layout = 'C'

def __init__(self,
data_dtype=np.float32,
shape=(0,),
zooms=None,
units='unknown'):
super().__init__(data_dtype, shape, zooms)
self._units = units

def copy(self):
return self.__class__(self.get_data_dtype(), self.get_data_shape(), self.get_zooms(),
self._units)

def data_to_fileobj(self, data, fileobj, rescale=True):
""" See Header class for an implementation we can't use """
raise NotImplementedError
Expand All @@ -297,6 +313,9 @@ def data_from_fileobj(self, fileobj):
""" See Header class for an implementation we can't use """
raise NotImplementedError

def get_xyzt_units(self) -> Tuple[str, str]:
return self._units, 'unknown'


class Minc1Header(MincHeader):

Expand Down Expand Up @@ -334,7 +353,13 @@ def from_file_map(klass, file_map, *, mmap=True, keep_file_open=None):
data_dtype = minc_file.get_data_dtype()
shape = minc_file.get_data_shape()
zooms = minc_file.get_zooms()
header = klass.header_class(data_dtype, shape, zooms)
unit = 'unknown'
units = minc_file.get_units()
if units:
if any(u != units[0] for u in units[1:]):
raise ValueError(f'Image has different units for different dimensions: {units}')
unit = units[0]
header = klass.header_class(data_dtype, shape, zooms, unit)
data = klass.ImageArrayProxy(minc_file)
return klass(data, affine, header, extra=None, file_map=file_map)

Expand Down
8 changes: 7 additions & 1 deletion nibabel/minc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ def from_file_map(klass, file_map, *, mmap=True, keep_file_open=None):
data_dtype = minc_file.get_data_dtype()
shape = minc_file.get_data_shape()
zooms = minc_file.get_zooms()
header = klass.header_class(data_dtype, shape, zooms)
unit = 'unknown'
units = minc_file.get_units()
if units:
if any(u != units[0] for u in units[1:]):
raise ValueError(f'Image has different units for different dimensions: {units}')
unit = units[0]
header = klass.header_class(data_dtype, shape, zooms, unit)
data = klass.ImageArrayProxy(minc_file)
return klass(data, affine, header, extra=None, file_map=file_map)

Expand Down
4 changes: 4 additions & 0 deletions nibabel/tests/test_minc1.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,7 @@ def test_data_to_from_fileobj(self):
img.header.data_to_fileobj(arr, bio)
with pytest.raises(NotImplementedError):
img.header.data_from_fileobj(bio)

def test_xyzt_units(self):
img = self.module.load(self.eg_images[0])
assert img.header.get_xyzt_units() == ('mm', 'unknown')
4 changes: 4 additions & 0 deletions nibabel/tests/test_minc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ class TestMinc2Image(tm2.TestMinc1Image):
image_class = Minc2Image
eg_images = (pjoin(data_path, 'small.mnc'),)
module = minc2

def test_xyzt_units(self):
img = self.module.load(self.eg_images[0])
assert img.header.get_xyzt_units() == ('mm', 'unknown')