I am computing two separate set of sources, one in volume (beamformer) and another on surfaces (eLORETA). I’d like to export these as NIfTI volumes. Of course, for this, I understand that surface sources will need to be somehow extrapolated from vertices to voxels. I wrote a snippet of code using a kdtree spatial indexing with a nearest neighbor lookup that kinda works for that purpose, but I’d rather rely on MNE built-in functionalities as much as possible. I found that it is pretty easy to export volume sources with something like
which I think is great. I could not find the equivalence for surface sources. It does appear that some code was written as part as WIP: Plot glass brain #4496 that would accomplish most of this work (except the saving)
elif isinstance(stc, SourceEstimate): n_vertices = sum(len(v) for v in stc.vertices) offset = 0 surf_to_mri = 0. for hi, hemi in enumerate(['lh', 'rh']): ribbon = nib.load(op.join(subjects_dir, subject, 'mri', '%s.ribbon.mgz' % hemi)) xfm = ribbon.header.get_vox2ras_tkr() mri_data = ribbon.get_data() ijk = np.array(np.where(mri_data)).T xyz = apply_trans(xfm, ijk) / 1000. row_ind = np.where(mri_data.ravel()) data = np.ones(row_ind.size) rr = read_surface(op.join(subjects_dir, subject, 'surf', '%s.white' % hemi)) rr /= 1000. rr = rr[stc.vertices[hi]] col_ind = _compute_nearest(rr, xyz) + offset surf_to_mri = surf_to_mri + sparse.csr_matrix( (data, (row_ind, col_ind)), shape=(mri_data.size, n_vertices)) offset += len(stc.vertices[hi]) data = surf_to_mri.dot(stc.data[:, time_idx]) data.shape = mri_data.shape xfm = _read_talairach(subject, subjects_dir) affine = np.dot(xfm, ribbon.header.get_vox2ras()) img = nib.Nifti1Image(data, affine)
but that PR was abandoned. Is there such a functionality somewhere in MNE base code (saving surface sources as volumes)? I could not find it but MNE has many crannies and nooks so I might just have missed it!