forked from MadsJensen/CAA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_ts.py
74 lines (61 loc) · 2.61 KB
/
extract_ts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 31 10:17:09 2015
@author: mje
"""
import mne
from mne.minimum_norm import (apply_inverse_epochs, read_inverse_operator)
from mne.time_frequency import cwt_morlet
import numpy as np
import sys
from my_settings import *
subject = sys.argv[1]
# Using the same inverse operator when inspecting single trials Vs. evoked
snr = 1.0 # Standard assumption for average data but using it for single trial
lambda2 = 1.0 / snr ** 2
method = "dSPM" # use dSPM method (could also be MNE or sLORETA)
freqs = np.arange(8, 13, 1)
n_cycle = freqs / 3.
conditions = ["ent/left", "ctl/left", "ent/right", "ctl/right"]
# Load data
labels = mne.read_labels_from_annot(subject, parc='PALS_B12_Brodmann',
regexp="Bro",
subjects_dir=subjects_dir)
labels_sel = [labels[6], labels[7]]
inverse_operator = read_inverse_operator(mne_folder +
"%s-inv.fif" % subject)
src = mne.read_source_spaces(subjects_dir + "%s/bem/%s-oct-6-src.fif"
% (subject, subject))
epochs = mne.read_epochs(epochs_folder +
"%s_trial_start-epo.fif" % subject)
# epochs.drop_bad_epochs(reject_params)
# epochs.resample(250, n_jobs=4)
for condition in conditions:
stcs = apply_inverse_epochs(epochs[condition],
inverse_operator,
lambda2,
method,
pick_ori="normal")
for label in labels_sel:
label_ts = []
for j in range(len(stcs)):
ts = mne.extract_label_time_course(stcs[j],
labels=label,
src=src,
mode="pca_flip")
ts = np.squeeze(ts)
ts *= np.sign(ts[np.argmax(np.abs(ts))])
label_ts.append(ts)
label_ts = np.asarray(label_ts)
tfr = cwt_morlet(label_ts, epochs.info["sfreq"], freqs,
use_fft=True, n_cycles=n_cycle)
np.save(tf_folder + "%s_%s_%s_%s_%s-tfr" % (subject, condition[:3],
condition[4:],
label.name, method),
tfr)
np.save(tf_folder + "%s_%s_%s_%s_%s-ts" % (subject, condition[:3],
condition[4:],
label.name, method),
label_ts)
del stcs
del tfr