-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_des_con_mat_4_tbss.py
64 lines (52 loc) · 1.74 KB
/
create_des_con_mat_4_tbss.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
# %%
import numpy as np
import os
stats_dir = 'data/res/tbss/stats'
# Define your subjects and their groups
subjects = {
'BOF112_BioFINDER2_190': 'healthy',
'BOF112_BioFINDER2_201': 'disease',
'BOF112_BioFINDER2_216': 'disease',
'BOF112_BioFINDER2_219': 'healthy',
}
# Sort subjects alphabetically (as TBSS requires)
sorted_subjects = sorted(subjects.keys())
# Create design matrix
# 1 0 for healthy
# 0 1 for disease
design_mat = np.zeros((len(subjects), 2))
for i, subject in enumerate(sorted_subjects):
if subjects[subject] == 'healthy':
design_mat[i, 0] = 1
else:
design_mat[i, 1] = 1
# Create contrast matrix
# 1 -1 for healthy > disease
# -1 1 for disease > healthy
contrast_mat = np.array([[1, -1],
[-1, 1]])
# %%
# Write design.mat
with open(os.path.join(stats_dir, 'design.mat'), 'w') as f:
f.write('/NumWaves\t{}\n'.format(design_mat.shape[1]))
f.write('/NumPoints\t{}\n'.format(design_mat.shape[0]))
f.write('/Matrix\n')
np.savetxt(f, design_mat, fmt='%d', delimiter='\t')
# Write design.con
with open(os.path.join(stats_dir, 'design.con'), 'w') as f:
f.write('/ContrastName1\tHealthy>Disease\n')
f.write('/ContrastName2\tDisease>Healthy\n')
f.write('/NumWaves\t{}\n'.format(contrast_mat.shape[1]))
f.write('/NumContrasts\t{}\n'.format(contrast_mat.shape[0]))
f.write('/Matrix\n')
np.savetxt(f, contrast_mat, fmt='%d', delimiter='\t')
print("Created design.mat and design.con files")
# %%
# Print contents of created files
print("\ndesign.mat contents:")
with open(os.path.join(stats_dir, 'design.mat'), 'r') as f:
print(f.read())
print("\ndesign.con contents:")
with open(os.path.join(stats_dir, 'design.con'), 'r') as f:
print(f.read())
# %%