stc to 3d numpy array

Hi Rezvan:

       stc.data is 1d vector of the vertex on the surface right?
Can I map surface vertex back to volume position in 3d numpy array?

Tsung-Min

Hi Tsung-Min,

stc.data is 1d vector of the vertex on the surface right?

It is but the surface on which it will be plotted lives in 3D euclidean
space.

Can I map surface vertex back to volume position in 3d numpy array?

Yes you can, you need some sort of projection scheme, please find a sample
example script below, it will generate .nii.gz from stc file which you can
read in python using nibabel

import nibabel as nib
img = nib.load('Your_nii_gz_file.nii.gz')

This assumes you have freesurfer installed on the system

However if you already have 3D source space then whole problem become as
simple as
img = stc.as_volume(src) # where src is the 3d source space

HTH

Sheraz

import mne
import numpy as np
from scipy.io import loadmat
from pyimpress import utils
import os
labels_mat_path =
'/autofs/cluster/transcend/Javeria/dropbox/Dropbox/labels.mat'
labels_path = '/autofs/cluster/fusion/Sheraz/rs/452/'
subjects_dir = '/cluster/transcend/MRI/WMA/recons/'
labels = loadmat(labels_mat_path)['labels']
labels = [labels_path + str(label[0][0][1:-1]) for label in labels]
bn = loadmat('/cluster/transcend/Javeria/dropbox/Dropbox/beta_bet_neg.mat')
bp = loadmat('/cluster/transcend/Javeria/dropbox/Dropbox/beta_bet_pos.mat')
gn = loadmat('/cluster/transcend/Javeria/dropbox/Dropbox/gamma_bet_neg.mat')
gp = loadmat('/cluster/transcend/Javeria/dropbox/Dropbox/gamma_bet_pos.mat')

bp = np.array(bp['vertl']) + np.array(bp['vertr']) +
np.array(bn['vertl'])*-1 + np.array(bn['vertr'])*-1
stc = utils.get_stc(labels,bp)
stc = stc.morph('fsaverage',smooth=2,grade=7)
stc.save('/cluster/transcend/Javeria/dropbox/Dropbox/b_pos','w')

gp = np.array(gp['vertl']) + np.array(gp['vertr']) +
np.array(gn['vertl'])*-1 + np.array(gn['vertr'])*-1
stc = utils.get_stc(labels,gp)
stc = stc.morph('fsaverage',smooth=2,grade=7)
stc.save('/cluster/transcend/Javeria/dropbox/Dropbox/g_pos','w')

c1 = 'mri_surf2surf --srcsurfval
/cluster/transcend/Javeria/dropbox/Dropbox/b_pos-lh.w ' \
     '--trgsurfval /cluster/transcend/Javeria/dropbox/Dropbox/b_pos-lh.mgz
' \
     '--srcsubject fsaverage --src_type w --trgsubject fsaverage --hemi lh'

c2 = 'mri_surf2surf --srcsurfval
/cluster/transcend/Javeria/dropbox/Dropbox/b_pos-rh.w ' \
     '--trgsurfval /cluster/transcend/Javeria/dropbox/Dropbox/b_pos-rh.mgz
' \
     '--srcsubject fsaverage --src_type w --trgsubject fsaverage --hemi rh'

c3 = 'mri_surf2vol --hemi lh --surf white --o
/cluster/transcend/Javeria/dropbox/Dropbox/b_pos-lh.nii.gz ' \
     '--identity fsaverage ' \
     '--template /cluster/transcend/MRI/WMA/recons/fsaverage/mri/T1.mgz ' \
     '--surfval /cluster/transcend/Javeria/dropbox/Dropbox/b_pos-lh.mgz
--fillribbon --fill-projfrac -1 1 0.05'