-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate_cm_datatype.py
executable file
·78 lines (65 loc) · 2.79 KB
/
generate_cm_datatype.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
#!/usr/bin/env python3
import json
import os
import shutil
import glob
with open('config.json') as config_file:
config = json.load(config_file)
# input_csv = "output_makemat/out_parc_correlation_connMatdf.csv"
input_csv_name = "output_makemat/out_*_connMatdf.csv"
input_csv = glob.glob(input_csv_name)[0]
#parse key file
catalog = dict()
with open(config["key"]) as key_file:
key_lines = key_file.readlines();
for line in key_lines:
tokens = line.split("\t")
label = tokens[0]
parcellation = tokens[2]
name_comp = tokens[3].split(" ") #== rh.R_V4t_ROI.label \n
name = name_comp[1]
catalog[parcellation] = {"name": name, "label": label, "voxel_value": int(parcellation)}
labels = [ {"name": "self-loop", "desc": "index(x,x) is the diagonal"} ]
#parse the csv
with open(input_csv) as cm_csv:
cm_lines = cm_csv.readlines()
parcs = cm_lines[0].split(",")
for parc in parcs[1:-14]:
try:
rec = catalog[parc.strip()]
labels.append(rec)
except:
print("no %s in key.txt" % parc)
#https://github.com/faskowit/app-fmri-2-mat/issues/5
#last 14 are for 14 freesurferaseg (https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/AnatomicalROI/FreeSurferColorLUT)
f14 = [
{"name": "Freesurfer Aseg / Left thalamus", "label": "10"},
{"name": "Freesurfer Aseg / Left caudate", "label": "11"},
{"name": "Freesurfer Aseg / Left putamen", "label": "12"},
{"name": "Freesurfer Aseg / Left pallidum", "label": "13"},
{"name": "Freesurfer Aseg / Left hippocampus", "label": "17"},
{"name": "Freesurfer Aseg / Left amygdala", "label": "18"},
{"name": "Freesurfer Aseg / Left accumbens", "label": "26"},
{"name": "Freesurfer Aseg / Right thalamus", "label": "49"},
{"name": "Freesurfer Aseg / Right caudate", "label": "50"},
{"name": "Freesurfer Aseg / Right putamen", "label": "51"},
{"name": "Freesurfer Aseg / Right pallidum", "label": "52"},
{"name": "Freesurfer Aseg / Right hippocampus", "label": "53"},
{"name": "Freesurfer Aseg / Right amygdala", "label": "54"},
{"name": "Freesurfer Aseg / Right accumbens", "label": "58"},
]
idx=0
for parc in parcs[-14:]:
labels.append({"name": f14[idx]["name"], "label": f14[idx]["label"], "voxel_value": int(parc)})
idx+=1
if not os.path.exists("cm/csv"):
os.makedirs("cm/csv")
with open("cm/csv/correlation.csv", "w") as cm_csv:
lines = cm_lines[1:]
csv = []
for line in lines:
line = line.strip().split(",")[1:]
csv.append(",".join(line))
cm_csv.write("\n".join(csv))
with open("cm/label.json", "w") as label_file:
json.dump(labels, label_file, indent=4)