-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtc.py
267 lines (224 loc) · 9.89 KB
/
vtc.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""Read, write, create Brainvoyager VTC file format."""
import struct
import numpy as np
from bvbabel.utils import read_variable_length_string
from bvbabel.utils import write_variable_length_string
# =============================================================================
def read_vtc(filename):
"""Read Brainvoyager VTC file.
Parameters
----------
filename : string
Path to file.
Returns
-------
header : dictionary
Pre-data and post-data headers.
data : 3D numpy.array
Image data.
"""
print("staring vtc")
header = dict()
with open(filename, 'rb') as f:
# Expected binary data: short int (2 bytes)
data, = struct.unpack('<h', f.read(2))
header["File version"] = data
# Expected binary data: variable-length string
data = read_variable_length_string(f)
header["Source FMR name"] = data
# Expected binary data: short int (2 bytes)
data, = struct.unpack('<h', f.read(2))
header["Protocol attached"] = data
if header["Protocol attached"] > 0:
# Expected binary data: variable-length string
data = read_variable_length_string(f)
header["Protocol name"] = data
else:
header["Protocol name"] = ""
# Expected binary data: short int (2 bytes)
data, = struct.unpack('<h', f.read(2))
header["Current protocol index"] = data
data, = struct.unpack('<h', f.read(2))
header["Data type (1:short int, 2:float)"] = data
#new add
header["datatype"] = data
header["get_data_dtype"] = data
#
data, = struct.unpack('<h', f.read(2))
header["Nr time points"] = data
data, = struct.unpack('<h', f.read(2))
header["VTC resolution relative to VMR (1, 2, or 3)"] = data
# print("here is the reslut :\n"+str(data))
data, = struct.unpack('<h', f.read(2))
header["XStart"] = data
data, = struct.unpack('<h', f.read(2))
header["XEnd"] = data
data, = struct.unpack('<h', f.read(2))
header["YStart"] = data
data, = struct.unpack('<h', f.read(2))
header["YEnd"] = data
data, = struct.unpack('<h', f.read(2))
header["ZStart"] = data
data, = struct.unpack('<h', f.read(2))
header["ZEnd"] = data
# Expected binary data: char (1 byte)
data, = struct.unpack('<B', f.read(1))
header["L-R convention (0:unknown, 1:radiological, 2:neurological)"] = data
data, = struct.unpack('<B', f.read(1))
header["Reference space (0:unknown, 1:native, 2:ACPC, 3:Tal)"] = data
# Expected binary data: char (4 bytes)
data, = struct.unpack('<f', f.read(4))
header["TR (ms)"] = data/1000
# ---------------------------------------------------------------------
# Read VTC data
# ---------------------------------------------------------------------
# NOTE(Users Guide 2.3): Each data element (intensity value) is
# represented in 2 bytes (unsigned short) or 4 bytes (float) as
# specified in "data type" entry. The data is organized in four loops:
# DimZ
# DimY
# DimX
# DimT
#
# The axes terminology follows the internal BrainVoyager (BV) format.
# The mapping to Talairach axes is as follows:
# BV (X front -> back) [axis 2 after np.reshape] = Y in Tal space
# BV (Y top -> bottom) [axis 1 after np.reshape] = Z in Tal space
# BV (Z left -> right) [axis 0 after np.reshape] = X in Tal space
# Prepare dimensions of VTC data array
VTC_resolution = header["VTC resolution relative to VMR (1, 2, or 3)"]
DimX = (header["XEnd"] - header["XStart"]) // VTC_resolution
DimY = (header["YEnd"] - header["YStart"]) // VTC_resolution
DimZ = (header["ZEnd"] - header["ZStart"]) // VTC_resolution
DimT = header["Nr time points"]
data_img = np.zeros(DimZ * DimY * DimX * DimT)
# print('VTC_resolution:'+str(VTC_resolution))
# print('XStart:'+str(header["XStart"]))
# print('XEnd:'+str(header["XEnd"]))
# print('YStart:'+str(header["YStart"]))
# print('YEnd:'+str(header["YEnd"]))
# print('ZStart:'+str(header["ZStart"]))
# print('ZEnd:'+str(header["ZEnd"]))
# print('data_img shape:'+str(data_img.shape))
if header["Data type (1:short int, 2:float)"] == 1:
for i in range(data_img.size):
data_img[i], = struct.unpack('<h', f.read(2))
elif header["Data type (1:short int, 2:float)"] == 2:
for i in range(data_img.size):
data_img[i], = struct.unpack('<f', f.read(4))
else:
raise("Unrecognized VTC data_img type.")
data_img = np.reshape(data_img, (DimZ, DimY, DimX, DimT))
# data_img = np.reshape(data_img, (DimY, DimZ, DimX, DimT))
data_img = np.transpose(data_img, (0, 2, 1, 3)) # BV to Tal
data_img = data_img[::-1, ::-1, ::-1, :] # Flip BV axes
# print(data_img)
return header, data_img
# =============================================================================
def write_vtc(filename, header, data_img):
"""Protocol to write Brainvoyager VTC file.
Parameters
----------
filename : string
Path to file.
header : dictionary
Pre-data and post-data headers.
data_img : 3D numpy.array
Image data.
"""
with open(filename, 'wb') as f:
# Expected binary data: short int (2 bytes)
data = header["File version"]
f.write(struct.pack('<h', data))
# Expected binary data: variable-length string
data = header["Source FMR name"]
write_variable_length_string(f, data)
# Expected binary data: short int (2 bytes)
data = header["Protocol attached"]
f.write(struct.pack('<h', data))
if header["Protocol attached"] > 0:
# Expected binary data: variable-length string
data = header["Protocol name"]
write_variable_length_string(f, data)
# Expected binary data: short int (2 bytes)
data = header["Current protocol index"]
f.write(struct.pack('<h', data))
data = header["Data type (1:short int, 2:float)"]
f.write(struct.pack('<h', data))
data = header["Nr time points"]
f.write(struct.pack('<h', data))
data = header["VTC resolution relative to VMR (1, 2, or 3)"]
f.write(struct.pack('<h', data))
data = header["XStart"]
f.write(struct.pack('<h', data))
data = header["XEnd"]
f.write(struct.pack('<h', data))
data = header["YStart"]
f.write(struct.pack('<h', data))
data = header["YEnd"]
f.write(struct.pack('<h', data))
data = header["ZStart"]
f.write(struct.pack('<h', data))
data = header["ZEnd"]
f.write(struct.pack('<h', data))
# Expected binary data: char (1 byte)
data = header["L-R convention (0:unknown, 1:radiological, 2:neurological)"]
f.write(struct.pack('<B', data))
data = header["Reference space (0:unknown, 1:native, 2:ACPC, 3:Tal)"]
f.write(struct.pack('<B', data))
# Expected binary data: char (4 bytes)
data = header["TR (ms)"]
f.write(struct.pack('<f', data))
# ---------------------------------------------------------------------
# Write VTC data
# ---------------------------------------------------------------------
data_img = data_img[::-1, ::-1, ::-1, :] # Flip BV axes
data_img = np.transpose(data_img, (0, 2, 1, 3)) # Tal to BV
data_img = np.reshape(data_img, data_img.size)
if header["Data type (1:short int, 2:float)"] == 1:
for i in range(data_img.size):
f.write(struct.pack('<h', data_img[i]))
elif header["Data type (1:short int, 2:float)"] == 2:
for i in range(data_img.size):
f.write(struct.pack('<f', data_img[i]))
else:
raise("Unrecognized VTC data_img type.")
def generate_vtc():
"""Generate Brainvoyager VTC file with default values."""
header = dict()
# Expected binary data: short int (2 bytes)
header["File version"] = 3
# Expected binary data: variable-length string
header["Source FMR name"] = ""
# Expected binary data: short int (2 bytes)
header["Protocol attached"] = 0
# if header["Protocol attached"] > 0:
# # Expected binary data: variable-length string
# data = header["Protocol name"]
# write_variable_length_string(f, data)
# Expected binary data: short int (2 bytes)
header["Current protocol index"] = 0
# NOTE: float vtc does not seem to work in BV so I make it short int
header["Data type (1:short int, 2:float)"] = 1
header["Nr time points"] = 10
header["VTC resolution relative to VMR (1, 2, or 3)"] = 1
header["XStart"] = 100
header["XEnd"] = 200
header["YStart"] = 100
header["YEnd"] = 200
header["ZStart"] = 100
header["ZEnd"] = 200
# Expected binary data: char (1 byte)
header["L-R convention (0:unknown, 1:radiological, 2:neurological)"] = 1
header["Reference space (0:unknown, 1:native, 2:ACPC, 3:Tal)"] = 1
# Expected binary data: char (4 bytes)
header["TR (ms)"] = 1
# -------------------------------------------------------------------------
# Create data
dims = [100, 100, 100, 10]
data = np.random.random(np.prod(dims)) * 225 # 225 for BV visualization
data = data.reshape(dims)
data = data.astype(np.short) # NOTE: float vtc does not seem to work in BV
return header, data
if __name__ == "__main__":
read_vtc('/Users/gangxinli/Desktop/Internship/Neuro/Neuro_ISC/Data/22Aug/sub-sid000005_task-movie_run-01_bold_SCCAI_3DMCTS_THPGLMF3c_256_trilin_2x0.9_MNI.vtc')