Skip to content

Commit

Permalink
Drop gifti support
Browse files Browse the repository at this point in the history
  • Loading branch information
emdupre committed Aug 20, 2018
1 parent 544ec79 commit 9d39a27
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 122 deletions.
3 changes: 0 additions & 3 deletions tedana/model/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ def fitmodels_direct(catd, mmix, mask, t2s, t2sG, tes, combmode, ref_img,
out[:, 2] = np.squeeze(utils.unmask(F_S0_maps[:, i], t2s != 0))
out[:, 3] = np.squeeze(utils.unmask(Z_maps[:, i], mask))

if utils.get_dtype(ref_img) == 'GIFTI':
continue # TODO: pass through GIFTI file data as below

ccimg = utils.new_nii_like(ref_img, out)

# Do simple clustering on F
Expand Down
12 changes: 1 addition & 11 deletions tedana/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
def test_get_dtype():
# various combinations of input types
good_inputs = [
(['echo1.func.gii', 'echo2.func.gii', 'echo3.func.gii'], 'GIFTI'),
('echo1.func.gii', 'GIFTI'),
(['echo1.nii.gz', 'echo2.nii.gz', 'echo3.nii.gz'], 'NIFTI'),
('echo1.nii.gz', 'NIFTI'),
(['echo1.unknown', 'echo2.unknown', 'echo3.unknown'], 'OTHER'),
Expand All @@ -31,7 +29,7 @@ def test_get_dtype():
assert utils.get_dtype(input) == expected

with pytest.raises(ValueError): # mixed arrays don't work
utils.get_dtype(['echo1.func.gii', 'echo1.nii.gz'])
utils.get_dtype(['echo1.unknown', 'echo1.nii.gz'])

with pytest.raises(TypeError): # non-img_like inputs don't work
utils.get_dtype(rs.rand(100, 100))
Expand Down Expand Up @@ -184,11 +182,3 @@ def test_new_nii_like():

def test_filewrite():
pass


def test_new_gii_like():
pass


def test_new_gii_darray():
pass
115 changes: 7 additions & 108 deletions tedana/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
Utilities for tedana package
"""
import os.path as op

import nibabel as nib
from nibabel.filename_parser import splitext_addext
from nilearn.image import new_img_like
Expand All @@ -13,8 +11,7 @@

from ..due import due, BibTeX

FORMATS = {'.nii': 'NIFTI',
'.gii': 'GIFTI'}
FORMATS = {'.nii': 'NIFTI'}


def get_dtype(data):
Expand All @@ -28,7 +25,7 @@ def get_dtype(data):
Returns
-------
dtype : {'NIFTI', 'GIFTI', 'OTHER'} str
dtype : {'NIFTI', 'OTHER'} str
Format of input data
"""

Expand Down Expand Up @@ -93,11 +90,7 @@ def load_image(data):
"""

if isinstance(data, str):
if get_dtype(data) == 'GIFTI':
fdata = np.column_stack([f.data for f in nib.load(data).darrays])
return fdata
elif get_dtype(data) == 'NIFTI':
data = check_niimg(data).get_data()
data = check_niimg(data).get_data()
elif isinstance(data, nib.spatialimages.SpatialImage):
data = check_niimg(data).get_data()

Expand All @@ -115,7 +108,7 @@ def load_data(data, n_echos=None):
data : (X x Y x M x T) array_like or :obj:`list` of img_like
Input multi-echo data array, where `X` and `Y` are spatial dimensions,
`M` is the Z-spatial dimensions with all the input echos concatenated,
and `T` is time. A list of image-like objects (e.g., .nii or .gii) are
and `T` is time. A list of image-like objects (e.g., .nii) are
accepted, as well
n_echos : :obj:`int`, optional
Number of echos in provided data array. Only necessary if `data` is
Expand All @@ -139,13 +132,8 @@ def load_data(data, n_echos=None):
raise ValueError('Cannot run `tedana` with only two echos: '
'{}'.format(data))
else: # individual echo files were provided (surface or volumetric)
if get_dtype(data) == 'GIFTI': # NOTE: only handles .[L/R].func.gii files
echos = np.array_split(data, n_echos)
fdata = np.stack([np.vstack([load_image(f) for f in e]) for e in echos], axis=1)
return np.atleast_3d(fdata), data[0]
else:
fdata = np.stack([load_image(f) for f in data], axis=1)
return np.atleast_3d(fdata), data[0]
fdata = np.stack([load_image(f) for f in data], axis=1)
return np.atleast_3d(fdata), data[0]

img = check_niimg(data)
(nx, ny), nz = img.shape[:2], img.shape[2] // n_echos
Expand Down Expand Up @@ -240,14 +228,10 @@ def make_min_mask(data):
return data.prod(axis=-1).prod(axis=-1).astype(bool)


def filewrite(data, filename, ref_img, gzip=False, copy_header=True,
copy_meta=False):
def filewrite(data, filename, ref_img, gzip=False, copy_header=True):
"""
Writes `data` to `filename` in format of `ref_img`
If `ref_img` dtype is GIFTI, then `data` is assumed to be stacked L/R
hemispheric and will be split and saved as two files
Parameters
----------
data : (S [x T]) array_like
Expand All @@ -261,9 +245,6 @@ def filewrite(data, filename, ref_img, gzip=False, copy_header=True,
if output dtype is NIFTI. Default: False
copy_header : :obj:`bool`, optional
Whether to copy header from `ref_img` to new image. Default: True
copy_meta : :obj:`bool`, optional
Whether to copy meta from `ref_img` to new image. Only applies if
output dtype is GIFTI. Default: False
Returns
-------
Expand All @@ -286,17 +267,6 @@ def filewrite(data, filename, ref_img, gzip=False, copy_header=True,
out = new_nii_like(ref_img, data, copy_header=copy_header)
name = '{}.{}'.format(root, 'nii.gz' if gzip else 'nii')
out.to_filename(name)
elif dtype == 'GIFTI':
# remove possible hemispheric denotations from root
root = op.join(op.dirname(root), op.basename(root).split('.')[0])
# save hemispheres separately
for n, (hdata, hemi) in enumerate(zip(np.split(data, 2, axis=0),
['L', 'R'])):
out = new_gii_like(ref_img, hdata,
copy_header=copy_header,
copy_meta=copy_meta)
name = '{}.{}.func.gii'.format(root, hemi)
out.to_filename(name)

return name

Expand Down Expand Up @@ -332,77 +302,6 @@ def new_nii_like(ref_img, data, affine=None, copy_header=True):
return nii


def new_gii_like(ref_img, data, copy_header=True, copy_meta=False):
"""
Coerces `data` into GiftiImage format like `ref_img`
Parameters
----------
ref_img : :obj:`str` or img_like
Reference image
data : (S [x T]) array_like
Data to be saved
copy_header : :obj:`bool`, optional
Whether to copy header from `ref_img` to new image. Default: True
copy_meta : :obj:`bool`, optional
Whether to copy meta from `ref_img` to new image. Default: False
Returns
-------
gii : :obj:`nibabel.gifti.gifti.GiftiImage`
GiftiImage
"""

if isinstance(ref_img, str):
ref_img = nib.load(ref_img)

if data.ndim == 1:
data = np.atleast_2d(data).T

darrays = [make_gii_darray(ref_img.darrays[n], d, copy_meta=copy_meta)
for n, d in enumerate(data.T)]
gii = nib.gifti.GiftiImage(header=ref_img.header if copy_header else None,
extra=ref_img.extra,
meta=ref_img.meta if copy_meta else None,
labeltable=ref_img.labeltable,
darrays=darrays)

return gii


def make_gii_darray(ref_array, data, copy_meta=False):
"""
Converts `data` into GiftiDataArray format like `ref_array`
Parameters
----------
ref_array : :obj:`str` or img_like
Reference array
data : (S,) array_like
Data to be saved
copy_meta : :obj:`bool`, optional
Whether to copy meta from `ref_img` to new image. Default: False
Returns
-------
gii : :obj:`nibabel.gifti.gifti.GiftiDataArray`
Output data array instance
"""

if not isinstance(ref_array, nib.gifti.GiftiDataArray):
raise TypeError('Provided reference is not a GiftiDataArray.')
darray = nib.gifti.GiftiDataArray(data,
intent=ref_array.intent,
datatype=data.dtype,
encoding=ref_array.encoding,
endian=ref_array.endian,
coordsys=ref_array.coordsys,
ordering=ref_array.ind_ord,
meta=ref_array.meta if copy_meta else None)

return darray


def unmask(data, mask):
"""
Unmasks `data` using non-zero entries of `mask`
Expand Down

0 comments on commit 9d39a27

Please sign in to comment.