Skip to content

Commit 3fa6f3e

Browse files
committed
Merge pull request #1132 from nipy/enh/cleanup_dcm2nii
Enh/cleanup dcm2nii
2 parents a10080b + a92e7a1 commit 3fa6f3e

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Next release
5757
* FIX: Change N4BiasFieldCorrection to use short tag for dimensionality (backward compatible) (https://github.com/nipy/nipype/pull/1096)
5858
* ENH: Added -newgrid input to Warp in AFNI (3dWarp wrapper) (https://github.com/nipy/nipype/pull/1128)
5959
* FIX: Fixed AFNI Copy interface to use positional inputs as required (https://github.com/nipy/nipype/pull/1131)
60+
* ENH: Added a check in Dcm2nii to check if nipype created the config.ini file and remove if true (https://github.com/nipy/nipype/pull/1132)
6061

6162
Release 0.10.0 (October 10, 2014)
6263
============

nipype/interfaces/dcm2nii.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from nipype.utils.filemanip import split_filename
1717
import re
1818

19+
1920
class Dcm2niiInputSpec(CommandLineInputSpec):
2021
source_names = InputMultiPath(File(exists=True), argstr="%s", position=-1,
2122
copyfile=False, mandatory=True, xor=['source_dir'])
@@ -37,13 +38,15 @@ class Dcm2niiInputSpec(CommandLineInputSpec):
3738
convert_all_pars = traits.Bool(True, argstr='-v', usedefault=True)
3839
reorient_and_crop = traits.Bool(False, argstr='-x', usedefault=True)
3940

41+
4042
class Dcm2niiOutputSpec(TraitedSpec):
4143
converted_files = OutputMultiPath(File(exists=True))
4244
reoriented_files = OutputMultiPath(File(exists=True))
4345
reoriented_and_cropped_files = OutputMultiPath(File(exists=True))
4446
bvecs = OutputMultiPath(File(exists=True))
4547
bvals = OutputMultiPath(File(exists=True))
4648

49+
4750
class Dcm2nii(CommandLine):
4851
"""Uses MRICRON's dcm2nii to convert dicom files
4952
@@ -55,14 +58,12 @@ class Dcm2nii(CommandLine):
5558
>>> converter.inputs.source_names = ['functional_1.dcm', 'functional_2.dcm']
5659
>>> converter.inputs.gzip_output = True
5760
>>> converter.inputs.output_dir = '.'
58-
>>> converter.cmdline #doctest: +ELLIPSIS
61+
>>> converter.cmdline
5962
'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
6163
"""
6264

63-
input_spec=Dcm2niiInputSpec
64-
output_spec=Dcm2niiOutputSpec
65-
65+
input_spec = Dcm2niiInputSpec
66+
output_spec = Dcm2niiOutputSpec
6667
_cmd = 'dcm2nii'
6768

6869
def _format_arg(self, opt, spec, val):
@@ -81,12 +82,14 @@ def _format_arg(self, opt, spec, val):
8182
return super(Dcm2nii, self)._format_arg(opt, spec, val)
8283

8384
def _run_interface(self, runtime):
84-
85+
self._config_created = False
8586
new_runtime = super(Dcm2nii, self)._run_interface(runtime)
8687
(self.output_files,
8788
self.reoriented_files,
8889
self.reoriented_and_cropped_files,
8990
self.bvecs, self.bvals) = self._parse_stdout(new_runtime.stdout)
91+
if self._config_created:
92+
os.remove('config.ini')
9093
return new_runtime
9194

9295
def _parse_stdout(self, stdout):
@@ -99,22 +102,21 @@ def _parse_stdout(self, stdout):
99102
last_added_file = None
100103
for line in stdout.split("\n"):
101104
if not skip:
102-
file = None
105+
out_file = None
103106
if line.startswith("Saving "):
104-
file = line[len("Saving "):]
107+
out_file = line[len("Saving "):]
105108
elif line.startswith("GZip..."):
106-
#for gzipped outpus files are not absolute
109+
# for gzipped outpus files are not absolute
107110
if isdefined(self.inputs.output_dir):
108111
output_dir = self.inputs.output_dir
109112
else:
110113
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..."):]))
113115
elif line.startswith("Number of diffusion directions "):
114116
if last_added_file:
115117
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"))
118120
elif re.search('.*-->(.*)', line):
119121
val = re.search('.*-->(.*)', line)
120122
val = val.groups()[0]
@@ -123,11 +125,11 @@ def _parse_stdout(self, stdout):
123125
else:
124126
output_dir = self._gen_filename('output_dir')
125127
val = os.path.join(output_dir, val)
126-
file = val
128+
out_file = val
127129

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
131133
continue
132134

133135
if line.startswith("Reorienting as "):
@@ -156,11 +158,11 @@ def _gen_filename(self, name):
156158
if name == 'output_dir':
157159
return os.getcwd()
158160
elif name == 'config_file':
161+
self._config_created = True
159162
config_file = "config.ini"
160163
f = open(config_file, "w")
161164
# disable interactive mode
162165
f.write("[BOOL]\nManualNIfTIConv=0\n")
163166
f.close()
164167
return config_file
165168
return None
166-

0 commit comments

Comments
 (0)