forked from drqm/working_memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPR6m_decoding_source_localize_patterns.py
75 lines (66 loc) · 2.61 KB
/
APR6m_decoding_source_localize_patterns.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
75
proj_name = 'MINDLAB2020_MEG-AuditoryPatternRecognition'
wdir = '/projects/' + proj_name + '/scratch/working_memory/'
scripts_dir = '/projects/' + proj_name + '/scripts/working_memory/'
import sys
sys.path.append(scripts_dir)
import mne
import os.path as op
from stormdb.access import Query
from sys import argv
import pickle
import numpy as np
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
proj_name = 'MINDLAB2020_MEG-AuditoryPatternRecognition'
wdir = '/projects/' + proj_name + '/scratch/working_memory/'
data_dir = wdir + 'averages/data/'
mode = 'patterns'
suffix = 'task_sensor_lf_0.05_hf_None_tstep_0.025_twin_0.05'
qr = Query(proj_name)
subs = qr.get_subjects()
scodes = np.arange(11,91)
modes = ['patterns','filters']
# if len(argv) > 1:
# scode = int(argv[1])
# if len(argv) > 2:
# mode = argv[2]
for scode in scodes:
sub = subs[scode-1]
print('processing subject ', sub)
for mode in modes:
try:
print(mode)
cfname = op.join(data_dir,sub,sub + '_' + mode + '_' + suffix + '.p')
times_fname = op.join(data_dir,sub,sub + '_times_' + suffix + '.p')
#inv_fname = op.join(data_dir,sub,sub + '_evoked_inverse_lf_0.05_hf_40_tstep_0.025_twin_0.05.p')
inv_fname = op.join(data_dir,sub,sub + '_decoding_inv_task_lf_0.05_hf_None_grad.p')
print('loading data')
cfile = open(cfname,'rb')
p = pickle.load(cfile)
cfile.close()
times_file = open(times_fname,'rb')
times = pickle.load(times_file)
times_file.close()
inv_file = open(inv_fname,'rb')
inv = pickle.load(inv_file)
inv_file.close()
for cp in p:
p[cp].times = times[cp[0:-1]]
if ('manipulation1' in p.keys()) and ('maintenance1' in p.keys()):
p['interaction'] = mne.combine_evoked([p['manipulation1'],p['maintenance1']],weights=[1,-1])
print('localizing')
sources = {}
for e in p:
sources[e] = mne.beamformer.apply_lcmv(p[e],inv)
sources[e].tmin = times[cp[0:-1]][0]
sources[e].tmax = times[cp[0:-1]][-1]
sources[e].tstep = np.diff(times[cp[0:-1]])[0]
print(sources[e].data.shape)
# Saving
src_fname = op.join(data_dir,sub,sub + '_{}_sources_{}_localized.p'.format(mode,suffix))
src_file = open(src_fname,'wb')
pickle.dump(sources,src_file)
src_file.close()
except Exception as e:
print(e)
continue