File: convert_bruker

package info (click to toggle)
mrtrix3 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,300 kB
  • sloc: cpp: 130,470; python: 9,603; sh: 597; makefile: 62; xml: 47
file content (146 lines) | stat: -rwxr-xr-x 4,467 bytes parent folder | download
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
#!/usr/bin/python3

# Copyright (c) 2008-2025 the MRtrix3 contributors.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Covered Software is provided under this License on an "as is"
# basis, without warranty of any kind, either expressed, implied, or
# statutory, including, without limitation, warranties that the
# Covered Software is free of defects, merchantable, fit for a
# particular purpose or non-infringing.
# See the Mozilla Public License v. 2.0 for more details.
#
# For more details, see http://www.mrtrix.org/.

# note: deal with these warnings properly when we drop support for Python 2:
# pylint: disable=unspecified-encoding

import sys, os.path

if len (sys.argv) != 3:
  sys.stderr.write("usage: convert_bruker 2dseq header.mih\n")
  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'):
  sys.stderr.write("expected .mih suffix as the second argument\n")
  sys.exit (1)



def main():

  with open (os.path.join (os.path.dirname (sys.argv[1]), 'reco')) as file_reco:
    lines = file_reco.read().split ('##$')

  with open (os.path.join (os.path.dirname (sys.argv[1]), '../../acqp')) as file_acqp:
    lines += file_acqp.read().split ('##$')

  with open (os.path.join (os.path.dirname (sys.argv[1]), '../../method')) as file_method:
    lines += file_method.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 file_out:
    file_out.write ('mrtrix image\ndim: ' + mat_size[0] + ',' + mat_size[1])
    if len(mat_size) > 2:
      file_out.write (',' + str(mat_size[2]))
    else:
      try:
        nslices #pylint: disable=pointless-statement
        file_out.write (',' + str(nslices))
      except:
        pass

    try:
      nacq #pylint: disable=pointless-statement
      file_out.write (',' + str(nacq))
    except:
      pass

    file_out.write ('\nvox: ' + str(res[0]) + ',' + str(res[1]))
    if len(res) > 2:
      file_out.write (',' + str(res[2]))
    else:
      try:
        slicethick #pylint: disable=pointless-statement
        file_out.write (',' + str(slicethick))
      except:
        pass
    try:
      nacq #pylint: disable=pointless-statement
      file_out.write (',')
    except:
      pass

    file_out.write ('\ndatatype: ')
    if wtype == '_16bit_sgn_int':
      file_out.write ('int16')
    elif wtype == '_32bit_sgn_int':
      file_out.write ('int32')

    if byteorder=='littleendian':
      file_out.write ('le')
    else:
      file_out.write ('be')

    file_out.write ('\nlayout: +0,+1')
    try:
      nslices #pylint: disable=pointless-statement
      file_out.write (',+2')
    except:
      pass
    try:
      nacq #pylint: disable=pointless-statement
      file_out.write (',+3')
    except:
      pass

    file_out.write ('\nfile: ' + sys.argv[1] + '\n')

    try:
      assert len(bvec) == 3*len(bval)
      bvec = [ bvec[n:n+3] for n in range(0,len(bvec),3) ]
      for direction, value in zip(bvec, bval):
        file_out.write ('dw_scheme: ' + direction[0] + ',' + direction[1] + ',' + str(-float(direction[2])) + ',' + value + '\n')
    except:
      pass

main()