-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_mark_HT.py
59 lines (52 loc) · 1.99 KB
/
hand_mark_HT.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
import mne
import numpy as np
from os import listdir
from os.path import join
import re
def get_ecog_channels(reg, chans):
chs = []
for ch in raw.ch_names:
match = re.match(reg, ch)
if match:
chs.append(ch)
return chs
root_dir = "/home/jev/hdd/epi/"
proc_dir = join(root_dir, "proc") # where the files are
proc_files = listdir(proc_dir)
subjs = ["1001", "1002", "1003", "1004", "1005", "2001", "3001", "3002"]
conds = ["Stim", "Sham"]
# search in these regions, in this order, for valid channels
reg_priority = ["HT", "HC", "HH", "H", "AMY"]
hemis = ["L", "R"]
magd_hemis = ["1.", "2."]
overwrite = False
for subj in subjs:
for cond in conds:
infile = f"f_EPI_{subj}_{cond}-raw.fif"
outfile = "HT_" + infile
# check if it already exists
if outfile in proc_files and not overwrite:
print(f"{outfile} already exists. Skipping...")
continue
try:
raw = mne.io.Raw(join(proc_dir, infile), preload=True)
except:
continue
hem_orig = magd_hemis if "200" in subj else hemis
for hemi_idx in range(2):
for rp in reg_priority:
chs = get_ecog_channels(f"{rp}{hem_orig[hemi_idx]}", raw.ch_names)
if len(chs):
break
if not len(chs):
print("Could not find a valid region")
continue
this_raw = raw.copy().pick_channels(chs)
actual_bads = this_raw.info["bads"].copy()
this_raw.plot(block=True)
hc_picks = list(set(this_raw.info["bads"]) - set(actual_bads))
raw = mne.set_bipolar_reference(raw, *hc_picks, ch_name=f"H{hemis[hemi_idx]}")
raw.set_channel_types({f"H{hemis[hemi_idx]}":"misc"})
raw.pick_types(eeg=True, misc=True, eog=True, emg=True, ecog=False)
raw.set_channel_types({f"H{hemi}":"ecog" for hemi in hemis if f"H{hemi}" in raw.ch_names})
raw.save(join(proc_dir, outfile))