-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmdm.py
More file actions
155 lines (130 loc) · 4.98 KB
/
Copy pathmdm.py
File metadata and controls
155 lines (130 loc) · 4.98 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""Read BrainVoyager MDM file format."""
# =============================================================================
def read_mdm(filename):
"""Read BrainVoyager MDM file.
Parameters
----------
filename : string
Path to file.
Returns
-------
header : dictionary
Multi subjects design matrix (MDM) header.
data : list
Each element contains a dictionary that contains the information of
a single study.
"""
# Read non-empty lines of the input text file
with open(filename, 'r') as f:
lines = [r for r in (line.strip() for line in f) if r]
# MDM header
header = dict()
header_rows = 7 # Nr of rows without empty lines
for line in lines[0:header_rows]:
content = line.split(":")
content = [i.strip() for i in content]
if content[1].isdigit():
header[content[0]] = int(content[1])
else:
header[content[0]] = content[1]
# -----------------------------------------------------------------------------
# MDM data
data = list()
studies_row = 7
for line in lines[studies_row:]:
temp = dict()
content = line.split('" "')
content = [i.strip('"') for i in content]
temp["PathNameData"] = content[-2]
temp["PathNameSDM"] = content[-1]
if header["TypeOfFunctionalData"] == 'MTC':
temp["PathNameSSM"] = content[-3]
data.append(temp)
return header, data
def write_mdm(filename, header, data_mdm):
"""Protocol to write BrainVoyager MDM file.
Parameters
----------
filename : string
Path to file.
header : dictionary
Multi subjects design matrix (MDM) header.
data_mdm : list
Each element contains a dictionary that contains the information of
a single study.
"""
with open(filename, 'w') as f:
f.write("\n")
data = header["FileVersion"]
f.write("FileVersion: {}\n".format(data))
data = header["TypeOfFunctionalData"]
f.write("TypeOfFunctionalData: {}\n".format(data))
f.write("\n")
data = header["RFX-GLM"]
f.write("RFX-GLM: {}\n".format(data))
f.write("\n")
data = header["PSCTransformation"]
f.write("PSCTransformation: {}\n".format(data))
data = header["zTransformation"]
f.write("zTransformation: {}\n".format(data))
data = header["SeparatePredictors"]
f.write("SeparatePredictors: {}\n".format(data))
f.write("\n")
data = header["NrOfStudies"]
f.write("NrOfStudies: {}\n".format(data))
# ---------------------------------------------------------------------
# Write data
nr_rows = header["NrOfStudies"]
for i in range(nr_rows):
if header["TypeOfFunctionalData"] == 'MTC':
f.write('\"{}\" \"{}\" \"{}\"'.format(data_mdm[i]["PathNameSSM"], data_mdm[i]["PathNameData"], data_mdm[i]["PathNameSDM"]))
else:
f.write('\"{}\" \"{}\"'.format(data_mdm[i]["PathNameData"], data_mdm[i]["PathNameSDM"]))
f.write("\n")
def create_mdm(surface_data=False):
"""Create BrainVoyager MDM file with default values.
Parameters
----------
surface_data : bool
'False': an MDM file for VTC data is created
'True': an MDM file for MTC data is created
Returns
-------
header : dictionary
Multi subjects design matrix (MDM) header.
data : list
Each element contains a dictionary that contains the information of
a single study.
Notes on header values
--------
FFX (fixed effects) : RFX-GLM = 0, SeparatePredictors = 0
SPST (separate studies) : RFX-GLM = 0, SeparatePredictors = 1
SPSB (separate subjects): RFX-GLM = 0, SeparatePredictors = 2
RFX (random effects): RFX-GLM = 1, SeparatePredictors = 2
PT (percent signal change transformation of voxel time courses): PSCTransformation = 1, zTransformation = 0
ZT (z-transformation of voxel time courses):
ZTB (z-transformation using only baseline segments): PSCTransformation = 0, zTransformation = 1
"""
header = dict()
header["FileVersion"] = 3
if surface_data:
header["TypeOfFunctionalData"] = 'MTC'
else:
header["TypeOfFunctionalData"] = 'VTC'
header["RFX-GLM"] = 1
header["PSCTransformation"] = 1
header["zTransformation"] = 0
header["SeparatePredictors"] = 2
header["NrOfStudies"] = 1
# -------------------------------------------------------------------------
# Create random predictors as data
data = list()
temp = dict()
temp["PathNameSDM"] = "/Path/To/SDMfile"
if header["TypeOfFunctionalData"] == 'MTC':
temp["PathNameSSM"] = "/Path/To/SSMfile"
temp["PathNameData"] = "/Path/To/MTCfile"
else:
temp["PathNameData"] = "/Path/To/VTCfile"
data.append(temp)
return header, data