-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompute_covs.py
59 lines (49 loc) · 1.76 KB
/
compute_covs.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
import os.path as op
import numpy as np
from joblib import Parallel, delayed
import mne
import config as cfg
from library import preprocessing as pp
def _compute_cov(file_raw):
subject = pp.get_subject(file_raw)
raw = mne.io.read_raw_fif(file_raw)
rawc, reject = pp.clean_raw(raw, subject)
events = mne.make_fixed_length_events(
rawc, id=3000, start=0, duration=pp.duration)
epochs = mne.Epochs(
rawc, events, event_id=3000, tmin=0, tmax=pp.duration, proj=True,
baseline=None, reject=reject, preload=False, decim=1)
epochs.drop_bad()
clean_events = events[epochs.selection]
picks = mne.pick_types(rawc.info, meg=True)
covs = []
for fb in pp.fbands:
rf = rawc.copy().load_data().filter(fb[0], fb[1])
ec = mne.Epochs(
rf, clean_events, event_id=3000, tmin=0, tmax=pp.duration,
proj=True, baseline=None, reject=None, preload=False, decim=1,
picks=picks)
cov = mne.compute_covariance(ec, method='oas', rank=None)
covs.append(cov.data)
out = dict(subject=subject, kind='rest', n_events=len(events),
n_events_good=len(clean_events), covs=np.array(covs))
return out
def _run_all(file_raw):
mne.utils.set_log_level('warning')
subject = pp.get_subject(file_raw)
print(subject)
error = 'None'
result = dict()
try:
result = _compute_cov(file_raw)
except Exception as err:
error = repr(err)
print(error)
out = dict(error=error)
out.update(result)
return out
out = Parallel(n_jobs=40)(
delayed(_run_all)(file_raw=file_raw)
for file_raw in cfg.files_raw)
fname_covs = op.join(cfg.derivative_path, 'covs_allch_oas.h5')
mne.externals.h5io.write_hdf5(fname_covs, out, overwrite=True)