From 9fd9e62d640855db4856a8bfd4785b1fa8cd8037 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Mon, 10 Apr 2017 13:45:02 +0100 Subject: [PATCH] Fixes for nifti units, and to image initialisation --- fsl/data/constants.py | 13 +++++++++++++ fsl/data/image.py | 27 +++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/fsl/data/constants.py b/fsl/data/constants.py index d257945ec..5a870ccf0 100644 --- a/fsl/data/constants.py +++ b/fsl/data/constants.py @@ -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 diff --git a/fsl/data/image.py b/fsl/data/image.py index 4efea8e02..0952f5855 100644 --- a/fsl/data/image.py +++ b/fsl/data/image.py @@ -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, -- GitLab