Save functional data as surface with nilearn

I am running functional connectivity analysis on the surface following this tutorial: Nilearn: Statistical Analysis for NeuroImaging in Python — Machine learning for NeuroImaging
I would like to use the same pipeline to analyze data that I have as volumes. I converted my volumes to the fsaverage surface using vol2surf, but I can’t find a function to save that surface after the conversion (the conversion takes a while for long acquisitions so I would want to save my files as surface .gii files, then load them in a separate notebook).
Is there a save/write function that parallels nilearn.surface.load_surf? I also tried but it doesn’t work on multi-volume data.

you can easily do that with nibabel

import nibabel as nib
 img = nib.gifti.GiftiImage(darrays=[nib.gifti.GiftiDataArray(stat_map)]), '/tmp/stat_map.gii')


Thank you! That worked, one thing I noticed though is that when saving with this function and then loading the surface again with nilearn.surface.load_surf, the data got transposed. Easy to solve with checking the shape of the loaded data, but I don’t understand exactly why it happens. Thanks anyways!

Sorry, I’m not a nibabel guru, I don’t know why there is a transposition. I think that this part of nibabel will be refactored in a near future, with a more consistent API to deal with surfaces.