From 9f3074cb9a169b63636a80a1765859d6c63a69b4 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Wed, 17 Jan 2018 17:41:49 +0000 Subject: [PATCH] New MGHImage class, for loading .mgz/mgh files. --- fsl/data/mghimage.py | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 fsl/data/mghimage.py diff --git a/fsl/data/mghimage.py b/fsl/data/mghimage.py new file mode 100644 index 000000000..c65411dd5 --- /dev/null +++ b/fsl/data/mghimage.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# +# mghimage.py - The MGHImage class +# +# Author: Paul McCarthy <pauldmccarthy@gmail.com> +# +"""This module provides the :class:`MGHImage` class, which can be used to load +Freesurfer ``mgh``/``mgz`` image files. + +.. autosummary:: + :nosignatures: + + looksLikeMGHImage +""" + + +import six + +import nibabel as nib + +import fsl.data.image as fslimage + + +class MGHImage(fslimage.Image): + """The ``MGHImage`` class is a NIFTI :class:`Image` which has been converted + from a Freesurfer ``.mgh`` file. + + .. see:: https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat + + .. see:: http://nipy.org/nibabel/reference/nibabel.freesurfer.html + """ + + def __init__(self, image, *args, **kwargs): + """Create a ``MGHImage``. + + :arg image: Name of MGH file, or a + ``nibabel.freesurfer.mghformat.MGHImage`` instance. + + All other arguments are passed through to :meth:`Image.__init__` + """ + + if isinstance(image, six.string_types): + image = nib.load(image) + + data = image.get_data() + affine = image.affine + + fslimage.Image.__init__(self, data, xform=affine) + + +ALLOWED_EXTENSIONS = ['.mgz', '.mgh'] +"""List of file extensions interpreted as MGH image files. +""" + + +EXTENSION_DESCRIPTIONS = ['Compressed MGH image', 'MGH image'] +"""A description for each of the :attr:`ALLOWED_EXTENSIONS`.""" + + +def looksLikeMGHImage(filename): + """Returns ``True`` if the given file looks like a MGH image, ``False`` + otherwise. + """ + return fslimage.looksLikeImage(filename, ALLOWED_EXTENSIONS) -- GitLab