Save functional data as surface with nilearn

Hi!
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 nibabel.freesurfer.io.write_morph_data but it doesn’t work on multi-volume data.
Thanks!
Maya

Hi,
you can easily do that with nibabel

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

Best,
Bertrand

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.
Best,
Bertrand