-
Notifications
You must be signed in to change notification settings - Fork 3
/
compute_ggt.py
80 lines (61 loc) · 2.27 KB
/
compute_ggt.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
76
77
78
79
80
import os
import os.path as op
import numpy as np
import mne
from joblib import Parallel, delayed
import config as cfg
def _get_subjects(trans_set):
trans = 'trans-%s' % trans_set
found = os.listdir(op.join(cfg.derivative_path, trans))
if trans_set == 'halifax':
subjects = [sub[4:4 + 8] for sub in found]
elif trans_set == 'krieger':
subjects = ['CC' + sub[:6] for sub in found]
print("found", len(subjects), "coregistrations")
return subjects, [op.join(cfg.derivative_path, trans, ff) for ff in found]
subjects, trans = _get_subjects(trans_set='krieger')
trans_map = dict(zip(subjects, trans))
def _compute_GGT(subject, kind):
# compute source space
src = mne.setup_source_space(subject, spacing='oct6', add_dist=False,
subjects_dir=cfg.mne_camcan_freesurfer_path)
trans = trans_map[subject]
bem = cfg.mne_camcan_freesurfer_path + \
"/%s/bem/%s-meg-bem.fif" % (subject, subject)
# compute handle MEG data
fname = op.join(
cfg.camcan_meg_raw_path,
subject, kind, '%s_raw.fif' % kind)
raw = mne.io.read_raw_fif(fname)
mne.channels.fix_mag_coil_types(raw.info)
event_length = 5.
raw_length = raw.times[-1]
events = mne.make_fixed_length_events(
raw,
duration=event_length, start=0, stop=raw_length - event_length)
# Compute the forward and inverse
info = mne.Epochs(raw, events=events, tmin=0, tmax=event_length,
baseline=None, reject=None, preload=False,
decim=10).info
fwd = mne.make_forward_solution(info, trans, src, bem)
leadfield = fwd['sol']['data']
return {'ggt': np.dot(leadfield, leadfield.T)}
def _run_all(subject, kind='rest'):
mne.utils.set_log_level('warning')
print(subject)
error = 'None'
result = dict()
try:
result = _compute_GGT(subject, kind)
except Exception as err:
error = repr(err)
print(error)
out = dict(subject=subject, kind=kind, error=error)
out.update(result)
return out
out = Parallel(n_jobs=40)(
delayed(_run_all)(subject=subject, kind='rest')
for subject in subjects)
out_fname = op.join(
cfg.path_data, 'GGT_mne.h5')
mne.externals.h5io.write_hdf5(out_fname, out, overwrite=True)