Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from mne.stats import (spatio_temporal_cluster_1samp_test,
summarize_clusters_stc, ttest_1samp_no_p)
sys.path.append(op.join('..', '..', 'processing'))
from library.config import (meg_dir, subjects_dir, fsaverage_vertices,
exclude_subjects, N_JOBS, l_freq, random_state) # noqa: E402
faces = list()
scrambled = list()
for subject_id in range(1, 20):
if subject_id in exclude_subjects:
continue
subject = "sub%03d" % subject_id
print("processing subject: %s" % subject)
data_path = op.join(meg_dir, subject)
stc = mne.read_source_estimate(
op.join(data_path, 'mne_dSPM_inverse_morph_highpass-%sHz-faces_eq'
% (l_freq,)))
faces.append(stc.magnitude().crop(None, 0.8).data.T)
stc = mne.read_source_estimate(
op.join(data_path, 'mne_dSPM_inverse_morph_highpass-%sHz-scrambled_eq'
% (l_freq,)))
scrambled.append(stc.magnitude().crop(None, 0.8).data.T)
tstep = stc.tstep
###############################################################################
# Set up our contrast and initial p-value threshold
X = np.array(faces, float) - np.array(scrambled, float)
fsaverage_src = mne.read_source_spaces(
op.join(subjects_dir, 'fsaverage', 'bem', 'fsaverage-5-src.fif'))
connectivity = spatial_src_connectivity(fsaverage_src)
def load_stc():
stc_fnames = glob.glob(op.join(
mu.get_user_fol(), '?eg', 'clusters', '{}-lh.stc'.format(MEGPanel.clusters_labels.stc_name)))
if len(stc_fnames) == 0:
print('load_stc: Cannot find {}!'.format(stc_fnames))
elif len(stc_fnames) > 1:
print('load_stc: Too many stcs!')
print(stc_fnames)
else:
MEGPanel.stc = mne.read_source_estimate(stc_fnames[0])
# labels = glob.glob(op.join(mu.get_user_fol(), 'meg', 'clusters', MEGPanel.clusters_labels.stc_name, '*.label'))
# MEGPanel.valid_vertices = []
# for label in labels:
# l = mne.read_label(label)
# MEGPanel.valid_vertices.extend(l.vertices.tolist())
if mu.max_stc(MEGPanel.stc) < 1e-4:
MEGPanel.stc._data *= np.power(10, 9) # from Amp to nAmp
MEGPanel.max_stc = get_max_stc_t(MEGPanel.stc, MEGPanel.clusters_labels.time)
def invoke(self, context, event=None):
if ColoringMakerPanel.stc_file_chosen:
if ColoringMakerPanel.stc is None:
ColoringMakerPanel.stc = mne.read_source_estimate(get_stc_full_fname())
max_vert, bpy.context.scene.frame_current = ColoringMakerPanel.stc.get_peak(
time_as_index=True, vert_as_index=True, mode=bpy.context.scene.meg_peak_mode)
print(max_vert, bpy.context.scene.frame_current)
plot_stc(ColoringMakerPanel.stc, bpy.context.scene.frame_current,
threshold=bpy.context.scene.coloring_lower_threshold, save_image=False)
elif ColoringMakerPanel.activity_map_chosen:
#todo: implement
pass
# activity_map_coloring('MEG')
else:
print('ColorMegMax: Both stc_file_chosen and activity_map_chosen are False!')
return {"FINISHED"}
fname = op.join(subject_dir, 'mne_dSPM_inverse-%s' % cond)
stc = mne.read_source_estimate(fname, subject)
brain = stc.plot(subject=subject, subjects_dir=subjects_dir, views=['ven'],
hemi='both', initial_time=0.17, time_unit='s')
return brain
brain = plot_stc('faces')
###############################################################################
# Faces - scrambled
brain = plot_stc('contrast')
###############################################################################
# LCMV Faces - scrambled
fname = op.join(subject_dir, 'mne_LCMV_inverse-contrast')
stc = mne.read_source_estimate(fname, subject)
stc.plot(subject=subject, subjects_dir=subjects_dir, views=['ven'],
hemi='both', initial_time=0.17, time_unit='s')
###############################################################################
# BEM
mne.viz.plot_bem(subject, subjects_dir)
def plot_stc(cond):
fname = op.join(subject_dir, 'mne_dSPM_inverse-%s' % cond)
stc = mne.read_source_estimate(fname, subject)
brain = stc.plot(subject=subject, subjects_dir=subjects_dir, views=['ven'],
hemi='both', initial_time=0.17, time_unit='s')
return brain
window_name = utils.namebase(window_fname)
fig_fname = op.join(figures_fol, '{}-amplitude.jpg'.format(window_name))
# if op.isfile(fig_fname) and not overwrite:
# print('{} already exist'.format(fig_fname))
# break
plt.figure()
all_files_found = True
for modality in modalities:
modality_fol = op.join(MMVT_DIR, subject, 'eeg' if modality == 'eeg' else 'meg')
stc_fname = op.join(modality_fol, '{}-epilepsy-{}-{}-{}_amplitude-zvals-lh.stc'.format(
subject, inverse_method, modality, window_name))
if not op.isfile(stc_fname):
print('Can\'t find {}!'.format(stc_fname))
all_files_found = False
break
stc = mne.read_source_estimate(stc_fname)
data = np.max(stc.data[:, :max_t], axis=0) if max_t > 0 else np.max(stc.data, axis=0)
plt.plot(data.T)
if all_files_found:
plt.title('{} amplitude'.format(window_name))
plt.legend(modalities)
print('Saving {} amplitude'.format(window_name))
plt.savefig(fig_fname, dpi=300)
plt.close()
def plot_stc(cond):
fname = op.join(subject_dir, 'mne_dSPM_inverse-%s' % cond)
stc = mne.read_source_estimate(fname, subject)
brain = stc.plot(subject=subject, subjects_dir=subjects_dir, views=['ven'],
hemi='both', initial_time=0.17, time_unit='s')
return brain
def plot_stc(cond):
fname = op.join(meg_dir, subject, 'mne_dSPM_inverse-%s' % cond)
stc = mne.read_source_estimate(fname, subject)
brain = stc.plot(subject=subject, subjects_dir=subjects_dir, views=['fro'],
hemi='both', time_viewer=True)
brain.set_data_time_index(165)
###############################################################################
def plot_stc(cond):
fname = op.join(meg_dir, subject, 'mne_dSPM_inverse-%s' % cond)
stc = mne.read_source_estimate(fname, subject)
brain = stc.plot(subject=subject, subjects_dir=subjects_dir, views=['ven'],
hemi='both', initial_time=0.17, time_unit='s')
return brain
def get_stat_stc_over_conditions(events, stat, stcs_conds=None, inverse_method='dSPM', smoothed=False,
morph_to_subject='', stc_t=-1):
stcs = {}
stc_template = STC_HEMI if not smoothed else STC_HEMI_SMOOTH
for cond_ind, cond in enumerate(events.keys()):
if stcs_conds is None:
# Reading only the rh, the lh will be read too
input_fname = stc_template.format(cond=cond, method=inverse_method, hemi='lh')
if morph_to_subject != '':
input_fname = '{}-{}-lh.stc'.format(input_fname[:-len('-lh.stc')], morph_to_subject)
if stc_t != -1:
input_fname = '{}-t{}-lh.stc'.format(input_fname[:-len('-lh.stc')], stc_t)
if op.isfile(input_fname):
print('Reading {}'.format(input_fname))
stc = mne.read_source_estimate(input_fname)
else:
print('No such file {}!'.format(input_fname))
return None
else:
stc = stcs_conds[cond]
for hemi in HEMIS:
data = stc.rh_data if hemi == 'rh' else stc.lh_data
if hemi not in stcs:
stcs[hemi] = np.zeros((data.shape[0], data.shape[1], len(events)))
stcs[hemi][:, :, cond_ind] = data
for hemi in HEMIS:
if stat == STAT_AVG:
# Average over the conditions
stcs[hemi] = stcs[hemi].mean(2)
elif stat == STAT_DIFF:
# Calc the diff of the conditions