Export NiLearn Surfaces

Summary of what happened:

I would like to export the surface file I created in nilearn using vol_to_surf(). I am currently trying to use nibabel’s write_geometry(), but I am not sure what components of the output from vol_to_surf() correspond to the arguments in write_geometry(). Does anyone have suggestions for how to use write_geometry(), or other functions I might consider using for exporting the surface data?

Command used (and if a helper script was used, a link to the helper script or the command generated):

import numpy as np 
import nibabel as nib
fsaverage = datasets.fetch_surf_fsaverage("fsaverage5")
...
surf_data = surface.vol_to_surf(img, # img is path to a 4D nifti of one person's resting state scan
                                surf_mesh = fsaverage["pial_left"],
                                inner_mesh = fsaverage["white_left"])

Version:

Python 3.8.9
nilearn==0.9.1
nibabel==3.2.2

Environment (Docker, Singularity, custom installation):

Installed the python packages with pip3.

What you are writing is the surface representation of img, right ?
For this I generally use:

from nibabel.gifti import read, write, GiftiDataArray, GiftiImage
gimage = GiftiImage(
                darrays=[GiftiDataArray().from_array(
                    surf_data, intent='t test')]) # or whatever this represents
write(gimage, some_path)
1 Like

In order to load data that I created in nilearn using vol_to_surf as an overlay in freeview, I needed to export it as follows:

import numpy as np
from nibabel.nifti1 import intent_codes, data_type_codes
from nibabel.gifti import GiftiDataArray, GiftiImage

gimage = GiftiImage(
    darrays=[GiftiDataArray(
        surf_data.astype(np.float32),
        intent=intent_codes.code['NIFTI_INTENT_NONE'],
        datatype=data_type_codes.code['NIFTI_TYPE_FLOAT32'])]
)
gimage.to_filename('out.gii')