Skip to content

Instantly share code, notes, and snippets.

@SherazKhan
Created September 14, 2018 17:40
Show Gist options
  • Save SherazKhan/3809d7c99a1096e14690dccf43c08da7 to your computer and use it in GitHub Desktop.
Save SherazKhan/3809d7c99a1096e14690dccf43c08da7 to your computer and use it in GitHub Desktop.
import mne
import numpy as np
def labels2stc(labels,labels_data,stc):
stc_new = stc.copy()
stc_new.data.fill(0)
for index,label in enumerate(labels):
if labels_data.ndim==1:
if isinstance(label, str):
temp = stc.in_label(mne.read_label(label))
else:
emp = stc.in_label(label)
temp.data.fill(labels_data[index])
stc_new += temp.expand(stc.vertices)
else:
if isinstance(label, str):
lab = mne.read_label(label)
label_name = label
else:
lab = label
label_name = lab.name
ver = np.intersect1d(lab.vertices, stc.vertices)
if 'rh.' in label_name:
ver=ver+len(stc.vertices[0])
stc_data = np.tile(labels_data[index,:][:,np.newaxis],len(ver)).T
stc_new.data[ver, :] = stc_data
return stc_new
def get_stc(labels,data,tmin=0,tstep=1):
stc_vertices = [np.uint32(np.arange(10242)), np.uint32(np.arange(10242))]
if data.ndim==1:
stc_data = np.ones((20484,1), dtype=np.float32)
else:
stc_data = np.ones((20484, data.shape[1]), dtype=np.float32)
stc = mne.SourceEstimate(stc_data, vertices=stc_vertices, tmin=tmin, tstep=tstep, subject='fsaverage')
stc_new = labels2stc(labels, data, stc)
return stc_new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment