Skip to content
Snippets Groups Projects
Commit fb4b597a authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Image.__init__ allows a data source to be specified - affects the initial save

state. Image.save ensures that file name has an extension. MGHImage uses both
of these changes so that its initial save state is accurate, and when it
is saved, it is saved as a nifti
parent dca6233b
No related branches found
No related tags found
No related merge requests found
...@@ -771,60 +771,64 @@ class Image(Nifti): ...@@ -771,60 +771,64 @@ class Image(Nifti):
calcRange=True, calcRange=True,
indexed=False, indexed=False,
threaded=False, threaded=False,
dataSource=None,
**kwargs): **kwargs):
"""Create an ``Image`` object with the given image data or file name. """Create an ``Image`` object with the given image data or file name.
:arg image: A string containing the name of an image file to load, :arg image: A string containing the name of an image file to load,
or a :mod:`numpy` array, or a :mod:`nibabel` image or a :mod:`numpy` array, or a :mod:`nibabel` image
object. object.
:arg name: A name for the image. :arg name: A name for the image.
:arg header: If not ``None``, assumed to be a :arg header: If not ``None``, assumed to be a
:class:`nibabel.nifti1.Nifti1Header` or :class:`nibabel.nifti1.Nifti1Header` or
:class:`nibabel.nifti2.Nifti2Header` to be used as the :class:`nibabel.nifti2.Nifti2Header` to be used as the
image header. Not applied to images loaded from file, image header. Not applied to images loaded from file,
or existing :mod:`nibabel` images. or existing :mod:`nibabel` images.
:arg xform: A :math:`4\\times 4` affine transformation matrix :arg xform: A :math:`4\\times 4` affine transformation matrix
which transforms voxel coordinates into real world which transforms voxel coordinates into real world
coordinates. If not provided, and a ``header`` is coordinates. If not provided, and a ``header`` is
provided, the transformation in the header is used. provided, the transformation in the header is used.
If neither a ``xform`` nor a ``header`` are provided, If neither a ``xform`` nor a ``header`` are provided,
an identity matrix is used. If both a ``xform`` and a an identity matrix is used. If both a ``xform`` and a
``header`` are provided, the ``xform`` is used in ``header`` are provided, the ``xform`` is used in
preference to the header transformation. preference to the header transformation.
:arg loadData: If ``True`` (the default) the image data is loaded :arg loadData: If ``True`` (the default) the image data is loaded
in to memory. Otherwise, only the image header in to memory. Otherwise, only the image header
information is read, and the image data is kept information is read, and the image data is kept
from disk. In either case, the image data is from disk. In either case, the image data is
accessed through an :class:`.ImageWrapper` instance. accessed through an :class:`.ImageWrapper` instance.
The data may be loaded into memory later on via the The data may be loaded into memory later on via the
:meth:`loadData` method. :meth:`loadData` method.
:arg calcRange: If ``True`` (the default), the image range is :arg calcRange: If ``True`` (the default), the image range is
calculated immediately (vi a call to calculated immediately (vi a call to
:meth:`calcRange`). Otherwise, the image range is :meth:`calcRange`). Otherwise, the image range is
incrementally updated as more data is read from memory incrementally updated as more data is read from memory
or disk. or disk.
:arg indexed: If ``True``, and the file is gzipped, it is opened :arg indexed: If ``True``, and the file is gzipped, it is opened
using the :mod:`indexed_gzip` package. Otherwise the using the :mod:`indexed_gzip` package. Otherwise the
file is opened by ``nibabel``. Ignored if ``loadData`` file is opened by ``nibabel``. Ignored if ``loadData``
is ``True``. is ``True``.
:arg threaded: If ``True``, the :class:`.ImageWrapper` will use a :arg threaded: If ``True``, the :class:`.ImageWrapper` will use a
separate thread for data range calculation. Defaults separate thread for data range calculation. Defaults
to ``False``. Ignored if ``loadData`` is ``True``. to ``False``. Ignored if ``loadData`` is ``True``.
:arg dataSource: If ``image`` is not a file name, this argument may be
used to specify the file from which the image was
loaded.
All other arguments are passed through to the ``nibabel.load`` function All other arguments are passed through to the ``nibabel.load`` function
(if it is called). (if it is called).
""" """
nibImage = None nibImage = None
dataSource = None fileobj = None
fileobj = None
if loadData: if loadData:
indexed = False indexed = False
...@@ -1132,6 +1136,10 @@ class Image(Nifti): ...@@ -1132,6 +1136,10 @@ class Image(Nifti):
filename = op.abspath(filename) filename = op.abspath(filename)
# make sure the extension is specified
if not looksLikeImage(filename):
filename = addExt(filename, mustExist=False)
log.debug('Saving {} to {}'.format(self.name, filename)) log.debug('Saving {} to {}'.format(self.name, filename))
# If this Image is not managing its # If this Image is not managing its
......
...@@ -19,6 +19,7 @@ import os.path as op ...@@ -19,6 +19,7 @@ import os.path as op
import six import six
import nibabel as nib import nibabel as nib
import fsl.utils.path as fslpath
import fsl.data.image as fslimage import fsl.data.image as fslimage
...@@ -54,7 +55,7 @@ class MGHImage(fslimage.Image): ...@@ -54,7 +55,7 @@ class MGHImage(fslimage.Image):
name = op.basename(filename) name = op.basename(filename)
image = nib.load(image) image = nib.load(image)
else: else:
name = None name = 'MGH image'
filename = None filename = None
data = image.get_data() data = image.get_data()
...@@ -63,12 +64,26 @@ class MGHImage(fslimage.Image): ...@@ -63,12 +64,26 @@ class MGHImage(fslimage.Image):
fslimage.Image.__init__(self, fslimage.Image.__init__(self,
data, data,
xform=affine, xform=affine,
name=name) name=name,
dataSource=filename)
if filename is not None: if filename is not None:
self.setMeta('mghImageFile', filename) self.setMeta('mghImageFile', filename)
def save(self, filename=None):
"""Overrides :meth:`.Image.save`. If a ``filename`` is not provided,
converts the original (MGH) file name into a NIFTI filename, before
passing it to the :meth:`.Image.save` method.
"""
if filename is None:
filename = self.dataSource
filename = fslpath.removeExt(filename, ALLOWED_EXTENSIONS)
return fslimage.Image.save(self, filename)
@property @property
def mghImageFile(self): def mghImageFile(self):
"""If this ``MGHImage`` was loaded from a file, returns the file """If this ``MGHImage`` was loaded from a file, returns the file
......
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