Skip to content

Commit d26215e

Browse files
committed
NF: Automatically reshape FreeSurfer ico7 niftis
1 parent e8aef7d commit d26215e

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

nibabel/nifti1.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -698,16 +698,26 @@ def get_data_shape(self):
698698
Allows for freesurfer hack for large vectors described in
699699
https://github.com/nipy/nibabel/issues/100 and
700700
https://code.google.com/p/fieldtrip/source/browse/trunk/external/freesurfer/save_nifti.m?spec=svn5022&r=5022#77
701+
702+
Allows for freesurfer hack for 7th order icosahedron surface described
703+
in
704+
https://github.com/nipy/nibabel/issues/309
705+
https://code.google.com/p/fieldtrip/source/browse/trunk/external/freesurfer/load_nifti.m?r=8776#86
706+
https://code.google.com/p/fieldtrip/source/browse/trunk/external/freesurfer/save_nifti.m?r=8776#50
701707
'''
702708
shape = super(Nifti1Header, self).get_data_shape()
703709
# Apply freesurfer hack for vector
704-
if shape != (-1, 1, 1): # Normal case
710+
if shape == (-1, 1, 1):
711+
vec_len = int(self._structarr['glmin'])
712+
if vec_len == 0:
713+
raise HeaderDataError('-1 in dim[1] but 0 in glmin; '
714+
'inconsistent freesurfer type header?')
715+
return (vec_len, 1, 1)
716+
# Apply freesurfer hack for ico7 surface
717+
elif shape == (27307, 1, 6):
718+
return (163842, 1, 1)
719+
else: # Normal case
705720
return shape
706-
vec_len = int(self._structarr['glmin'])
707-
if vec_len == 0:
708-
raise HeaderDataError('-1 in dim[1] but 0 in glmin; inconsistent '
709-
'freesurfer type header?')
710-
return (vec_len, 1, 1)
711721

712722
def set_data_shape(self, shape):
713723
''' Set shape of data
@@ -725,12 +735,22 @@ def set_data_shape(self, shape):
725735
Applies freesurfer hack for large vectors described in
726736
https://github.com/nipy/nibabel/issues/100 and
727737
https://code.google.com/p/fieldtrip/source/browse/trunk/external/freesurfer/save_nifti.m?spec=svn5022&r=5022#77
738+
739+
Allows for freesurfer hack for 7th order icosahedron surface described
740+
in
741+
https://github.com/nipy/nibabel/issues/309
742+
https://code.google.com/p/fieldtrip/source/browse/trunk/external/freesurfer/load_nifti.m?r=8776#86
743+
https://code.google.com/p/fieldtrip/source/browse/trunk/external/freesurfer/save_nifti.m?r=8776#50
728744
'''
729-
# Apply freesurfer hack for vector
730745
hdr = self._structarr
731746
shape = tuple(shape)
732-
if (len(shape) == 3 and shape[1:] == (1, 1) and
733-
shape[0] > np.iinfo(hdr['dim'].dtype.base).max): # Freesurfer case
747+
748+
# Apply freesurfer hack for ico7 surface
749+
if shape == (163842, 1, 1):
750+
shape = (27307, 1, 6)
751+
# Apply freesurfer hack for vector
752+
elif (len(shape) == 3 and shape[1:] == (1, 1) and
753+
shape[0] > np.iinfo(hdr['dim'].dtype.base).max):
734754
try:
735755
hdr['glmin'] = shape[0]
736756
except OverflowError:

0 commit comments

Comments
 (0)