1- #!/usr/bin/env python3
1+ #!/usr/bin/env python
22#
33# BrainBrowser: Web-based Neurological Visualization Tools
44# (https://brainbrowser.cbrain.mcgill.ca)
55#
6- # Copyright (C) 2011 McGill University
6+ # Copyright (C) 2011 McGill University
77#
88# This program is free software: you can redistribute it and/or modify
99# it under the terms of the GNU Affero General Public License as
1919# along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
2121# Author: Jon Pipitone <jon@pipitone.ca>
22+ # Author: Robert D. Vincent <robert.d.vincent@mcgill.ca>
23+ # (Made it work with Python 2.7, supported multiple voxel types)
2224
25+ from __future__ import print_function
2326import sys
2427import shutil
2528import os .path
2629import argparse
2730import subprocess
2831import json
2932
30- required_minc_cmdline_tools = ['mincinfo' , 'minctoraw' ]
33+ required_minc_cmdline_tools = ['mincinfo' , 'minctoraw' ]
3134
32- def console_error (message , exit_code ):
35+ def console_error (message , exit_code ):
3336 print (message , file = sys .stderr )
34- sys .exit (1 )
37+ sys .exit (exit_code )
3538
3639def console_log (message ):
3740 print (message )
3841
39- def check_minc_tools_installed ():
42+ def which (pgm ):
43+ path = os .getenv ('PATH' )
44+ for p in path .split (os .path .pathsep ):
45+ p = os .path .join (p , pgm )
46+ if os .path .exists (p ) and os .access (p , os .X_OK ):
47+ return p
48+
49+ def check_minc_tools_installed ():
4050 for tool in required_minc_cmdline_tools :
41- if not shutil . which (tool ):
51+ if not which (tool ):
4252 console_error (
4353 "minc2volume-viewer.py requires that the MINC tools be installed.\n "
4454 "Visit http://www.bic.mni.mcgill.ca/ServicesSoftware/MINC for info." , 1 )
4555
46- def cmd (command ):
56+ def cmd (command ):
4757 return subprocess .check_output (command .split (), universal_newlines = True ).strip ()
4858
49- def get_space (mincfile , space ):
59+ def get_space (mincfile , space ):
5060 header = {
5161 "start" : float (cmd ("mincinfo -attval {}:start {}" .format (space ,mincfile ))),
5262 "space_length" : float (cmd ("mincinfo -dimlength {} {}" .format (space ,mincfile ))),
@@ -60,45 +70,63 @@ def get_space(mincfile, space):
6070
6171 return header
6272
63- def make_header (mincfile , headerfile ):
73+ def make_header (mincfile , datatype , headerfile ):
6474 header = {}
6575
76+ header ["datatype" ] = datatype ;
77+
6678 # find dimension order
6779 order = cmd ("mincinfo -attval image:dimorder {}" .format (mincfile ))
6880 order = order .split ("," )
6981
7082 if len (order ) < 3 or len (order ) > 4 :
7183 order = cmd ("mincinfo -dimnames {}" .format (mincfile ))
7284 order = order .split (" " )
73-
85+
7486 header ["order" ] = order
7587
76- if len (order ) == 4 :
88+ if len (order ) == 4 :
7789 time_start = cmd ("mincinfo -attval time:start {}" .format (mincfile ))
7890 time_length = cmd ("mincinfo -dimlength time {}" .format (mincfile ))
79-
80- header ["time" ] = { "start" : float (time_start ),
91+
92+ header ["time" ] = { "start" : float (time_start ),
8193 "space_length" : float (time_length ) }
8294
8395 # find space
8496 header ["xspace" ] = get_space (mincfile ,"xspace" )
8597 header ["yspace" ] = get_space (mincfile ,"yspace" )
86- header ["xspace " ] = get_space (mincfile ,"xspace " )
98+ header ["zspace " ] = get_space (mincfile ,"zspace " )
8799
88100 if len (order ) > 3 :
89101 header ["time" ] = get_space (mincfile ,"time" )
90102
91103 # write out the header
92104 open (headerfile ,"w" ).write (json .dumps (header ))
93105
94- def make_raw (mincfile , rawfile ):
106+ def make_raw (mincfile , datatype , rawfile ):
95107 raw = open (rawfile , "wb" )
96- raw .write (
97- subprocess .check_output (["minctoraw" ,"-byte" ,"-unsigned" ,"-normalize" ,mincfile ]))
98-
99- def main (filename ):
108+ args = ["-normalize" , mincfile ]
109+ opts = {
110+ "int8" : ["-byte" , "-signed" ],
111+ "uint8" : ["-byte" , "-unsigned" ],
112+ "int16" : ["-short" , "-signed" ],
113+ "uint16" : ["-short" , "-unsigned" ],
114+ "int32" : ["-int" , "-signed" ],
115+ "uint32" : ["-int" , "-unsigned" ],
116+ "int32" : ["-int" , "-signed" ],
117+ "uint32" : ["-int" , "-unsigned" ],
118+ "float32" :["-float" ],
119+ "float64" :["-double" ],
120+ }[datatype ]
121+ for opt in opts :
122+ args .insert (0 , opt )
123+ args .insert (0 , "minctoraw" )
124+ raw .write (
125+ subprocess .check_output (args ))
126+
127+ def main (filename , datatype ):
100128 if not os .path .isfile (filename ):
101- console_error ("File {} does not exist." .format (filename ))
129+ console_error ("File {} does not exist." .format (filename ), 1 )
102130
103131 check_minc_tools_installed ()
104132
@@ -109,14 +137,19 @@ def main(filename):
109137 console_log ("Processing file: {}" .format (filename ))
110138
111139 console_log ("Creating header file: {}" .format (headername ))
112- make_header (filename , headername )
140+ make_header (filename , datatype , headername )
113141
114142 console_log ("Creating raw data file: {}" .format (rawname ))
115- make_raw (filename , rawname )
143+ make_raw (filename , datatype , rawname )
116144
117- if __name__ == '__main__' :
145+ if __name__ == '__main__' :
118146 parser = argparse .ArgumentParser ()
119- parser .add_argument ("filename" )
147+ parser .add_argument ("filename" , help = "the minc file to convert" )
148+ parser .add_argument ("-T" , dest = "datatype" ,
149+ choices = ["int8" , "int16" , "int32" ,
150+ "uint8" , "uint16" , "uint32" ,
151+ "float32" , "float64" ],
152+ action = "store" , default = "uint8" ,
153+ help = "set the voxel data type of the output" )
120154 args = parser .parse_args ()
121-
122- main (args .filename )
155+ main (args .filename , args .datatype )
0 commit comments