I am trying to get to grips with the GIfTI format and would like to use python code for reading, manipulating and viewing them. One of the most widespread GIfTI images (I think) is one of the ones supplied with SPM:
~SPM/canonical/cortex_20484.surf.gii
First I tried to load it in nilearn:
import nilearn.surface;
filename='canonical/cortex_20484.surf.gii ';
nilearn.surface.load_surf_data('filename');
but that returns:
In [55]: nilearn.surface.load_surf_data(filename)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-55-b71237af107f> in <module>()
----> 1 nilearn.surface.load_surf_data(filename)
/usr/local/lib/python2.7/dist-packages/nilearn/surface/surface.pyc in load_surf_data(surf_data)
546 data = np.zeros((len(gii.darrays[0].data), len(gii.darrays)))
547 for arr in range(len(gii.darrays)):
--> 548 data[:, arr] = gii.darrays[arr].data
549 data = np.squeeze(data)
550 except IndexError:
ValueError: could not broadcast input array from shape (40960,3) into shape (40960)
If I try to do it bare-metal (ish) with nibabel:
import nibabel;
filename='/usr/share/data/spm8/canonical/cortex_20484.surf.gii';
img_in=nibabel.load(filename);
# for 'intent' see https://nifti.nimh.nih.gov/pub/dist/src/niftilib/nifti1.h
da0=img_in.darrays[0]; # intent: 1009 (NIFTI_INTENT_TRIANGLE), length: 40960 - faces
da1=img_in.darrays[1]; # intent: 1008 (NIFTI_INTENT_POINTSET), length: 20484 - vertices
da0.data.shape # (40960, 3)
da1.data.shape # (20484, 3) -- why are these numbers different?!
img_in.get_arrays_from_intent(1009)[0].data.shape # (40960,3)
img_in.get_arrays_from_intent(1008)[0].data.shape # (20484,3)
then the darrays variable is the only numeric data I can find. And it consists of 2 ndarrays with different dimensions. That doesn’t make sense to me - is there anyone to whom it does?