Skip to content
Snippets Groups Projects
Commit 9fd9e62d authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Fixes for nifti units, and to image initialisation

parent 654999ba
No related branches found
No related tags found
No related merge requests found
......@@ -85,6 +85,19 @@ NIFTI_XFORM_ANALYZE = 5
"""Code which indicates that this is an ANALYZE image, not a NIFTI image. """
# NIFTI unit codes
NIFTI_UNITS_UNKNOWN = 0
NIFTI_UNITS_METER = 1
NIFTI_UNITS_MM = 2
NIFTI_UNITS_MICRON = 3
NIFTI_UNITS_SEC = 8
NIFTI_UNITS_MSEC = 16
NIFTI_UNITS_USEC = 24
NIFTI_UNITS_HZ = 32
NIFTI_UNITS_PPM = 40
NIFTI_UNITS_RADS = 48
# NIFTI file intent codes
NIFTI_INTENT_NONE = 0
NIFTI_INTENT_CORREL = 2
......
......@@ -332,6 +332,10 @@ class Nifti(notifier.Notifier):
def xyzUnits(self):
"""Returns the NIFTI XYZ dimension unit code. """
# analyze images have no unit field
if self.niftiVersion == 0:
return constants.NIFTI_UNITS_MM
# The nibabel get_xyzt_units returns labels,
# but we want the NIFTI codes. So we use
# the (undocumented) nifti1.unit_codes field
......@@ -345,6 +349,10 @@ class Nifti(notifier.Notifier):
def timeUnits(self):
"""Returns the NIFTI time dimension unit code. """
# analyze images have no unit field
if self.niftiVersion == 0:
return constants.NIFTI_UNITS_SEC
# See xyzUnits
units = self.header.get_xyzt_units()[1]
units = nib.nifti1.unit_codes[units]
......@@ -742,10 +750,20 @@ class Image(Nifti):
# We default to NIFTI1 and not
# NIFTI2, because the rest of
# FSL is not yet NIFTI2 compatible.
nibImage = nib.nifti1.Nifti1Image(image,
xform,
header=header)
# FSL is not yet NIFTI2 compatible.
if header is None:
ctr = nib.nifti1.Nifti1Image
# But if a nibabel header has been provided,
# we use the corresponding image type
if isinstance(header, nib.nifti2.Nifti2Header):
ctr = nib.nifti2.Nifti2Image
elif isinstance(header, nib.nifti1.Nifti1Header):
ctr = nib.nifti1.Nifti1Image
elif isinstance(header, nib.analyze.AnalyzeHeader):
ctr = nib.analyze.AnalyzeImage
nibImage = ctr(image, xform, header=header)
# otherwise, we assume that it is a nibabel image
else:
......@@ -1026,6 +1044,7 @@ class Image(Nifti):
.. note:: Modifying image data will force the entire image to be
loaded into memory if it has not already been loaded.
"""
values = np.array(values)
log.debug('{}: __setitem__ [{} = {}]'.format(self.name,
sliceobj,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment