Newer
Older
#!/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.
"""

Paul McCarthy
committed
import fsl.utils.path as fslpath
import fsl.utils.transform as transform
import fsl.data.image as fslimage
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`."""
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
- 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):
filename = op.abspath(image)
name = op.basename(filename)

Paul McCarthy
committed
name = 'MGH image'

Paul McCarthy
committed
data = image.get_data()
affine = image.affine
vox2surf = image.header.get_vox2ras_tkr()
fslimage.Image.__init__(self,
data,
xform=affine,

Paul McCarthy
committed
name=name,
dataSource=filename)
if filename is not None:
self.setMeta('mghImageFile', filename)

Paul McCarthy
committed
self.__voxToSurfMat = vox2surf
self.__surfToVoxMat = transform.invert(vox2surf)
self.__surfToWorldMat = transform.concat(affine, self.__surfToVoxMat)
self.__worldToSurfMat = transform.invert(self.__surfToWorldMat)

Paul McCarthy
committed
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
def mghImageFile(self):
"""If this ``MGHImage`` was loaded from a file, returns the file
name. Otherwise returns ``None``.
"""
return self.getMeta('mghImageFile', None)

Paul McCarthy
committed
@property
def voxToSurfMat(self):
"""Returns an affine which can be used to transform voxel
coordinates into the surface coordinate system for this image.
See: http://www.grahamwideman.com/gw/brain/fs/coords/fscoords.htm

Paul McCarthy
committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
See: https://surfer.nmr.mgh.harvard.edu/fswiki/CoordinateSystems
"""
return self.__voxToSurfMat
@property
def surfToVoxMat(self):
"""Returns an affine which can be used to transform surface
coordinates into the voxel coordinate system for this image.
"""
return self.__surfToVoxMat
@property
def surfToWorldMat(self):
"""Returns an affine which can be used to transform surface
coordinates into the world coordinate system for this image.
"""
return self.__surfToWorldMat
@property
def worldToSurfMat(self):
"""Returns an affine which can be used to transform world
coordinates into the surface coordinate system for this image.
"""
return self.__worldToSurfMat