-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathconvert_bruker
executable file
·124 lines (103 loc) · 2.91 KB
/
convert_bruker
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
#!/usr/bin/env python
import sys, os.path
if len (sys.argv) != 3:
print ("usage: convert_bruker 2dseq header.mih")
sys.exit (0)
#if os.path.basename (sys.argv[1]) != '2dseq':
#print ("expected '2dseq' file as first argument")
#sys.exit (1)
if not sys.argv[2].endswith ('.mih'):
print ("expected .mih suffix as the second argument")
sys.exit (1)
with open (os.path.join (os.path.dirname (sys.argv[1]), 'reco')) as f:
lines = f.read().split ('##$');
with open (os.path.join (os.path.dirname (sys.argv[1]), '../../acqp')) as f:
lines += f.read().split ('##$');
with open (os.path.join (os.path.dirname (sys.argv[1]), '../../method')) as f:
lines += f.read().split ('##$');
for line in lines:
line = line.lower();
if line.startswith ('reco_size='):
mat_size = line.splitlines()[1].split()
print ('mat_size', mat_size)
elif line.startswith ('nslices='):
nslices = line.split('=')[1].split()[0]
print ('nslices', nslices)
elif line.startswith ('acq_time_points='):
nacq = len (line.split('\n',1)[1].split())
print ('nacq', nacq)
elif line.startswith ('reco_wordtype='):
wtype = line.split('=')[1].split()[0]
print ('wtype', wtype)
elif line.startswith ('reco_byte_order='):
byteorder = line.split('=')[1].split()[0]
print ('byteorder', byteorder)
elif line.startswith ('pvm_spatresol='):
res = line.splitlines()[1].split()
print ('res', res)
elif line.startswith ('pvm_spackarrslicedistance='):
slicethick = line.splitlines()[1].split()[0]
print ('slicethick', slicethick)
elif line.startswith ('pvm_dweffbval='):
bval = line.split('\n',1)[1].split()
print ('bval', bval)
elif line.startswith ('pvm_dwgradvec='):
bvec = line.split('\n',1)[1].split()
print ('bvec', bvec)
with open (sys.argv[2], 'w') as f:
f.write ('mrtrix image\ndim: ' + mat_size[0] + ',' + mat_size[1])
if len(mat_size) > 2:
f.write (',' + str(mat_size[2]))
else:
try:
nslices
f.write (',' + str(nslices))
except:
pass
try:
nacq
f.write (',' + str(nacq))
except:
pass
f.write ('\nvox: ' + str(res[0]) + ',' + str(res[1]));
if len(res) > 2:
f.write (',' + str(res[2]));
else:
try:
slicethick
f.write (',' + str(slicethick))
except:
pass
try:
nacq
f.write (',')
except:
pass
f.write ('\ndatatype: ')
if wtype == '_16bit_sgn_int':
f.write ('int16')
elif wtype == '_32bit_sgn_int':
f.write ('int32')
if byteorder=='littleendian':
f.write ('le')
else:
f.write ('be')
f.write ('\nlayout: +0,+1')
try:
nslices
f.write (',+2')
except:
pass
try:
nacq
f.write (',+3')
except:
pass
f.write ('\nfile: ' + sys.argv[1] + '\n')
try:
bvec
bval
for n in range (0, len (bval)):
f.write ('dw_scheme: ' + bvec[3*n] + ',' + bvec[3*n+1] + ',' + str(-float(bvec[3*n+2])) + ',' + bval[n] + '\n')
except:
pass