16
16
from nipype .utils .filemanip import split_filename
17
17
import re
18
18
19
+
19
20
class Dcm2niiInputSpec (CommandLineInputSpec ):
20
21
source_names = InputMultiPath (File (exists = True ), argstr = "%s" , position = - 1 ,
21
22
copyfile = False , mandatory = True , xor = ['source_dir' ])
@@ -37,13 +38,15 @@ class Dcm2niiInputSpec(CommandLineInputSpec):
37
38
convert_all_pars = traits .Bool (True , argstr = '-v' , usedefault = True )
38
39
reorient_and_crop = traits .Bool (False , argstr = '-x' , usedefault = True )
39
40
41
+
40
42
class Dcm2niiOutputSpec (TraitedSpec ):
41
43
converted_files = OutputMultiPath (File (exists = True ))
42
44
reoriented_files = OutputMultiPath (File (exists = True ))
43
45
reoriented_and_cropped_files = OutputMultiPath (File (exists = True ))
44
46
bvecs = OutputMultiPath (File (exists = True ))
45
47
bvals = OutputMultiPath (File (exists = True ))
46
48
49
+
47
50
class Dcm2nii (CommandLine ):
48
51
"""Uses MRICRON's dcm2nii to convert dicom files
49
52
@@ -55,14 +58,12 @@ class Dcm2nii(CommandLine):
55
58
>>> converter.inputs.source_names = ['functional_1.dcm', 'functional_2.dcm']
56
59
>>> converter.inputs.gzip_output = True
57
60
>>> converter.inputs.output_dir = '.'
58
- >>> converter.cmdline #doctest: +ELLIPSIS
61
+ >>> converter.cmdline
59
62
'dcm2nii -a y -c y -b config.ini -v y -d y -e y -g y -i n -n y -o . -p y -x n -f n functional_1.dcm'
60
- >>> converter.run() # doctest: +SKIP
61
63
"""
62
64
63
- input_spec = Dcm2niiInputSpec
64
- output_spec = Dcm2niiOutputSpec
65
-
65
+ input_spec = Dcm2niiInputSpec
66
+ output_spec = Dcm2niiOutputSpec
66
67
_cmd = 'dcm2nii'
67
68
68
69
def _format_arg (self , opt , spec , val ):
@@ -81,12 +82,14 @@ def _format_arg(self, opt, spec, val):
81
82
return super (Dcm2nii , self )._format_arg (opt , spec , val )
82
83
83
84
def _run_interface (self , runtime ):
84
-
85
+ self . _config_created = False
85
86
new_runtime = super (Dcm2nii , self )._run_interface (runtime )
86
87
(self .output_files ,
87
88
self .reoriented_files ,
88
89
self .reoriented_and_cropped_files ,
89
90
self .bvecs , self .bvals ) = self ._parse_stdout (new_runtime .stdout )
91
+ if self ._config_created :
92
+ os .remove ('config.ini' )
90
93
return new_runtime
91
94
92
95
def _parse_stdout (self , stdout ):
@@ -99,22 +102,21 @@ def _parse_stdout(self, stdout):
99
102
last_added_file = None
100
103
for line in stdout .split ("\n " ):
101
104
if not skip :
102
- file = None
105
+ out_file = None
103
106
if line .startswith ("Saving " ):
104
- file = line [len ("Saving " ):]
107
+ out_file = line [len ("Saving " ):]
105
108
elif line .startswith ("GZip..." ):
106
- #for gzipped outpus files are not absolute
109
+ # for gzipped outpus files are not absolute
107
110
if isdefined (self .inputs .output_dir ):
108
111
output_dir = self .inputs .output_dir
109
112
else :
110
113
output_dir = self ._gen_filename ('output_dir' )
111
- file = os .path .abspath (os .path .join (output_dir ,
112
- line [len ("GZip..." ):]))
114
+ out_file = os .path .abspath (os .path .join (output_dir , line [len ("GZip..." ):]))
113
115
elif line .startswith ("Number of diffusion directions " ):
114
116
if last_added_file :
115
117
base , filename , ext = split_filename (last_added_file )
116
- bvecs .append (os .path .join (base ,filename + ".bvec" ))
117
- bvals .append (os .path .join (base ,filename + ".bval" ))
118
+ bvecs .append (os .path .join (base , filename + ".bvec" ))
119
+ bvals .append (os .path .join (base , filename + ".bval" ))
118
120
elif re .search ('.*-->(.*)' , line ):
119
121
val = re .search ('.*-->(.*)' , line )
120
122
val = val .groups ()[0 ]
@@ -123,11 +125,11 @@ def _parse_stdout(self, stdout):
123
125
else :
124
126
output_dir = self ._gen_filename ('output_dir' )
125
127
val = os .path .join (output_dir , val )
126
- file = val
128
+ out_file = val
127
129
128
- if file :
129
- files .append (file )
130
- last_added_file = file
130
+ if out_file :
131
+ files .append (out_file )
132
+ last_added_file = out_file
131
133
continue
132
134
133
135
if line .startswith ("Reorienting as " ):
@@ -156,11 +158,11 @@ def _gen_filename(self, name):
156
158
if name == 'output_dir' :
157
159
return os .getcwd ()
158
160
elif name == 'config_file' :
161
+ self ._config_created = True
159
162
config_file = "config.ini"
160
163
f = open (config_file , "w" )
161
164
# disable interactive mode
162
165
f .write ("[BOOL]\n ManualNIfTIConv=0\n " )
163
166
f .close ()
164
167
return config_file
165
168
return None
166
-
0 commit comments