-
Notifications
You must be signed in to change notification settings - Fork 2
/
exp2json.py
87 lines (66 loc) · 3.06 KB
/
exp2json.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
81
82
83
84
85
86
import os
import json
import re
from matgrab import mat2df
from BIDS_converter.utils.organize import from_excel
from BIDS_converter.utils.utils import is_number
HOME = os.path.expanduser("~")
DUKEDIR = os.path.join(HOME, "Box", "CoganLab", "D_Data")
TASKS = ['Environmental_Sternberg', 'GlobalLocal', 'Lexical',
'LexicalDecRepDelay', 'LexicalDecRepNoDelay',
'Neighborhood_Sternberg', 'Phoneme_Sequencing', 'SentenceRep',
'timit', 'Uniqueness_Point']
def remove_from_brackets(string: str):
pattern = r'\[[^]]*\]'
found = re.findall(pattern, string)
for f in found:
string = string.replace(f, re.sub(r'\s', '', f))
return string
def sort_alphanumeric(lst):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(lst, key=alphanum_key)
def updateJsonFile(filename: str, data: dict, task: str, sub: str):
names = [n.strip() for n in data["channels"]]
try:
with open(filename, "r") as jsonFile: # Open the JSON file for reading
current = json.load(jsonFile)
except (json.JSONDecodeError, FileNotFoundError):
current = {} # Read the JSON into the buffer
# Working with buffered content
if sub not in current.keys():
current[sub] = {"default": data}
elif any(n not in current[sub]['default']['channels'] for n in names):
current[sub]['default']['channels'] = sort_alphanumeric(list(
set(current[sub]['default']['channels'] + names)))
elif any(n not in names for n in current[sub]["default"]["channels"]):
current[sub][task] = data
# sort the dictionary
myKeys = list(current.keys())
myKeys.sort()
sorted_dict = {i: current[i] for i in myKeys}
# Save our changes to JSON file
out_str = remove_from_brackets(json.dumps(sorted_dict, indent=4))
with open(filename, "w+") as jsonFile:
jsonFile.write(out_str)
for task in TASKS:
DATADIR = os.path.join(DUKEDIR, task)
for sub in [s for s in os.listdir(DATADIR) if s.startswith('D') and
is_number(s[-1])]:
for root, _, files in os.walk(os.path.join(DATADIR, sub)):
for f in files:
if f == "experiment.mat":
df = mat2df(os.path.join(root, f), "channels")
names = sort_alphanumeric(df["name"].tolist())
excel = os.path.join(os.path.dirname(
DUKEDIR), "ECoG_Task_Data", "Timestamps (MASTER).xlsx")
dtype = from_excel(excel, sub, "Type")
trig = from_excel(excel, sub, "Trigger")
if "grid" in dtype.lower():
dtype = "ecog"
elif "seeg" in dtype.lower():
dtype = "seeg"
else:
raise ValueError(f"dtype {dtype} not recognized")
data = {"channels": names, "dtype": dtype, "trigger": trig}
updateJsonFile("subjects.json", data, task, sub)