You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wanted to get thoughts from others who may find this feature useful, and insights into possible straightforward ways to implement.
Describe the new feature or enhancement
In an effort to compare/overlay results in fMRI and EEG/MEG, several of us are seeking an easy way to export MNE source results to GIFTI (and potentially NIFTI in the future). GIFTI would allow other programs (e.g. AFNI, Freesurfer) to then work with source-space data in useful ways (e.g. constrain solutions based on fMRI, make pretty pictures).
Describe your proposed implementation
Add options to mne.SourceSpaces.save( ) or better - a new method .export( ) to allow the export of GIFTI images. For added convenience, with option for hemisphere, time-window of interest, and overwrite. Wrapper for nibabel's GIFTI implementation with nuances for writing the relevant data in.
This would be decently useful for folks trying to do EEG-fMRI simultaneous, MEG & fMRI joint analysis, or anyone interested in taking the source estimates from MNE into other softwares (FieldTrip and BrainStorm both have GIFTI imports).
I think it's worth trying. Now we need to see how to handle the fact stc
subsample the high resolution
surfaces of freesurfer. Not sure how to handle this with gifti unless we
upsample creating very
large files.
@pmolfese I think we're planning on working this into nibabel first, see discussions and hopefully join them (next meeting is a week from tomorrow IIRC!) -- it would be nice not to duplicate efforts here:
I'm interesting in joining these discussions, how might I join? I see a standing zoom room in your second link, should I simply join at the appropriate time?
@pmolfese the code is not the best. It relies on the fact that a .stc file is basically a set of concatenated Freesurfer paint (.w) files. With the old MNE-C (2.7.4, ca. 2016), I was using mne_make_movie to extract the surface at the time of interest and export it to a .w file, then freesurfer's mri_surf2vol to convert the .w file to a nifti file. For gifti I think you could use mri_surf2surf. To avoid the old code you can set ftype=w when you save the SourceEstimate in mne-python.
I will add my name to the linked discussion point too. For MNE-nirs I want to move to source reconstruction next and was planning to integrate tightly with nilearn.
@pmolfese why did you prefer to export as GIFTI? I am just curious as Im new to this topic. Some of the fNIRS software exports as NIFTI, and wondered if there was a specific reason for choosing between the two?
@rob-luke - GIFTI mostly comfort in handling source data from other software. There are programs for converting between NIFTI and GIFTI for sure. I suspect ultimately both would be useful.
@jbteves you pinged me with an email for a status update here -- so far there has been some progress at the nibabel end. I think our plan is basically to wait for nibabel to make SurfaceImage available, then allow something like stc.as_gifti or something to export a SurfaceImage class from our SourceEstimate class in MNE. (Or maybe someday SourceEstimate can be a wrapper for SurfaceImage -- but that would be more work.) In any case, I think any Gifti export would hopefully come from going STC->SurfaceImage (implemented at the MNE end) then SurfaceImage->Gifti (implemented at the nibabel end).
So I think at this point the bottleneck is at the nibabel end, maybe there is some way you could help their effort?
I think we want nibabel to do the heavy lifting on that, so we'll need to wait at the MNE end for them to have GIFTI export.
One idea of a backward and forward compatible way for us to get this very easily would be to make BaseSourceEstimate a subclass of nibabel's surface API class. If this works it might be a nice way to transition
Agreed I think the nibabel stuff has stalled (?) and in theory as long as we set the API up decently enough we're future compatible with using their code anyway. So +1 for adding a new method stc.export_gifti(fname, subject=None, subjects_dir=None) to the SourceEstimate class. Then maybe we want a mne.source_estimate.read_gifti(fname, subject=None) or so? That way you could do round-trip testing that a save-load round-trip of the Gifti you get the same data, vertices, etc. (or as much information as you can).
Feel free to open a PR to integrate the "hack" (nice name 😄 ) code into MNE
Maybe if you pass fname='something.gii' we should automatically create 'something-lh.gii' for the decimated left surf and 'something-rh.gii' with the decimated right hemi surf. Would that be a reasonable workflow? If not feel free to propose something that would work better / make more sense -- the use case isn't totally clear to me, I've never used or thought much about gifti and what it can/can't do as a format...
In Gifti (I believe) there's the geometry surface file that has the shape, and then we overlay the functional data on top of it. These correspond in MNE terms Source Space and STC, respectfully. That functional data (STC here) doesn't have shape, just the nodes that those time data map onto the geometry. For the STCs, I agree with stc.export_gifti( ) for the saving the functional data.
But for the downsampled surfaces / geometry I think we'll need to add a case to mne.write_source_spaces( ) as well. Like a format = where folks can specify either FIF or GIFTI. Can attempt to get the mne.read_source_space to also read these downsampled surfaces eventually as well.
For the STCs, I agree with stc.export_gifti( ) for the saving the functional data.
But for the downsampled surfaces / geometry I think we'll need to add a case to mne.write_source_spaces( ) as well.
Or perhaps a cleaner API would be:
stc.export_gifti(fname, src=fwd['src'])
i.e., require the user to pass a fname filename and the src source space that was used in creating the STC. (There's plenty of precedent for this in viz and morphing functions so I don't think it's too onerous.) And then export_gifti can just write out whatever file(s) with whatever information is needed to actually use with some downstream gifti tool.
Activity
welcome commentedon Apr 12, 2021
Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴🏽♂️
agramfort commentedon Apr 13, 2021
jhouck commentedon May 10, 2021
This would be great. Currently using an amalgamation of MNE-C, Freesurfer, and AFNI to get surface estimates into .nii files.
pmolfese commentedon Oct 14, 2021
Hello again. @jbteves and I finally have some bandwidth to work more on this.
@agramfort & @larsoner, can you direct us to where the subsampling of the surfaces happens?
Also @jhouck - can you point us to your code that does work-arounds?
larsoner commentedon Oct 14, 2021
@pmolfese I think we're planning on working this into
nibabelfirst, see discussions and hopefully join them (next meeting is a week from tomorrow IIRC!) -- it would be nice not to duplicate efforts here:https://nipy.discourse.group/t/collecting-use-cases/58
https://nipy.discourse.group/t/surface-api-follow-up-meetings/79/9
nipy/nibabel#1056
But to answer your question, the subsampling is here (and associated helper functions), it's not the easiest code to read:
mne-python/mne/surface.py
Lines 942 to 993 in 022f1a8
jbteves commentedon Oct 15, 2021
I'm interesting in joining these discussions, how might I join? I see a standing zoom room in your second link, should I simply join at the appropriate time?
larsoner commentedon Oct 15, 2021
@jbteves it would be good to drop a comment in https://nipy.discourse.group/t/surface-api-follow-up-meetings/79/9 to mention your interest just so that other folks know you're coming
jhouck commentedon Nov 2, 2021
@pmolfese the code is not the best. It relies on the fact that a .stc file is basically a set of concatenated Freesurfer paint (.w) files. With the old MNE-C (2.7.4, ca. 2016), I was using mne_make_movie to extract the surface at the time of interest and export it to a .w file, then freesurfer's mri_surf2vol to convert the .w file to a nifti file. For gifti I think you could use mri_surf2surf. To avoid the old code you can set ftype=w when you save the SourceEstimate in mne-python.
rob-luke commentedon Nov 3, 2021
I will add my name to the linked discussion point too. For MNE-nirs I want to move to source reconstruction next and was planning to integrate tightly with nilearn.
rob-luke commentedon Nov 3, 2021
@pmolfese why did you prefer to export as GIFTI? I am just curious as Im new to this topic. Some of the fNIRS software exports as NIFTI, and wondered if there was a specific reason for choosing between the two?
pmolfese commentedon Nov 3, 2021
@rob-luke - GIFTI mostly comfort in handling source data from other software. There are programs for converting between NIFTI and GIFTI for sure. I suspect ultimately both would be useful.
larsoner commentedon Jan 18, 2022
@jbteves you pinged me with an email for a status update here -- so far there has been some progress at the
nibabelend. I think our plan is basically to wait fornibabelto makeSurfaceImageavailable, then allow something likestc.as_giftior something to export aSurfaceImageclass from ourSourceEstimateclass in MNE. (Or maybe somedaySourceEstimatecan be a wrapper forSurfaceImage-- but that would be more work.) In any case, I think any Gifti export would hopefully come from going STC->SurfaceImage (implemented at the MNE end) then SurfaceImage->Gifti (implemented at the nibabel end).So I think at this point the bottleneck is at the nibabel end, maybe there is some way you could help their effort?
https://nipy.discourse.group/t/preliminary-notes-on-surface-representations/57
https://nipy.discourse.group/t/surface-api-follow-up-meetings/79
nipy/nibabel#1056
Looks like the next meeting is scheduled for 9AM EST Jan 28th if you want to join and discuss!
jbteves commentedon Jan 18, 2022
Alright, thanks for letting me know!
8 remaining items
jbteves commentedon Mar 7, 2022
Opened #10421 for you to take a look.
jbteves commentedon Mar 9, 2022
Should this be reopened? I think we're still missing the "write to gifti" part.
larsoner commentedon Mar 9, 2022
I think the "partially addresses #XYZ" was misread by GitHub, agreed we should reopen
jbteves commentedon Mar 9, 2022
Thanks! For the GIFTI part should I add another function, or are we wanting to wait for the surface API changes?
larsoner commentedon Mar 9, 2022
I think we want
nibabelto do the heavy lifting on that, so we'll need to wait at the MNE end for them to have GIFTI export.One idea of a backward and forward compatible way for us to get this very easily would be to make BaseSourceEstimate a subclass of nibabel's surface API class. If this works it might be a nice way to transition
pmolfese commentedon Jun 14, 2023
Wondering if we can revisit this since it's been over a year. Trying to make sure perfect doesn't stand in the way of progress.
We have a working prototype in our specific use cases that may need to be broadened a bit: https://github.com/nimh-cmn/stc2gii_hack
larsoner commentedon Jun 14, 2023
Agreed I think the nibabel stuff has stalled (?) and in theory as long as we set the API up decently enough we're future compatible with using their code anyway. So +1 for adding a new method
stc.export_gifti(fname, subject=None, subjects_dir=None)to theSourceEstimateclass. Then maybe we want amne.source_estimate.read_gifti(fname, subject=None)or so? That way you could do round-trip testing that a save-load round-trip of the Gifti you get the same data, vertices, etc. (or as much information as you can).Feel free to open a PR to integrate the "hack" (nice name 😄 ) code into MNE
pmolfese commentedon Jun 14, 2023
Thanks @larsoner! We'll also need a method to save the decimated surface as GIFTI. Any preferences on a name (and namespace) for that?
larsoner commentedon Jun 14, 2023
Maybe if you pass
fname='something.gii'we should automatically create'something-lh.gii'for the decimated left surf and'something-rh.gii'with the decimated right hemi surf. Would that be a reasonable workflow? If not feel free to propose something that would work better / make more sense -- the use case isn't totally clear to me, I've never used or thought much about gifti and what it can/can't do as a format...pmolfese commentedon Jun 14, 2023
Ah sorry I'm not typing clear enough even for me!
In Gifti (I believe) there's the geometry surface file that has the shape, and then we overlay the functional data on top of it. These correspond in MNE terms Source Space and STC, respectfully. That functional data (STC here) doesn't have shape, just the nodes that those time data map onto the geometry. For the STCs, I agree with
stc.export_gifti( )for the saving the functional data.But for the downsampled surfaces / geometry I think we'll need to add a case to
mne.write_source_spaces( )as well. Like aformat =where folks can specify either FIF or GIFTI. Can attempt to get the mne.read_source_space to also read these downsampled surfaces eventually as well.larsoner commentedon Jun 15, 2023
Or perhaps a cleaner API would be:
i.e., require the user to pass a
fnamefilename and thesrcsource space that was used in creating the STC. (There's plenty of precedent for this in viz and morphing functions so I don't think it's too onerous.) And thenexport_giftican just write out whatever file(s) with whatever information is needed to actually use with some downstream gifti tool.