Skip to content

Enh/cleanup dcm2nii #1132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Next release
* FIX: Change N4BiasFieldCorrection to use short tag for dimensionality (backward compatible) (https://github.com/nipy/nipype/pull/1096)
* ENH: Added -newgrid input to Warp in AFNI (3dWarp wrapper) (https://github.com/nipy/nipype/pull/1128)
* FIX: Fixed AFNI Copy interface to use positional inputs as required (https://github.com/nipy/nipype/pull/1131)
* 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)

Release 0.10.0 (October 10, 2014)
============
Expand Down
38 changes: 20 additions & 18 deletions nipype/interfaces/dcm2nii.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from nipype.utils.filemanip import split_filename
import re


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


class Dcm2niiOutputSpec(TraitedSpec):
converted_files = OutputMultiPath(File(exists=True))
reoriented_files = OutputMultiPath(File(exists=True))
reoriented_and_cropped_files = OutputMultiPath(File(exists=True))
bvecs = OutputMultiPath(File(exists=True))
bvals = OutputMultiPath(File(exists=True))


class Dcm2nii(CommandLine):
"""Uses MRICRON's dcm2nii to convert dicom files

Expand All @@ -55,14 +58,12 @@ class Dcm2nii(CommandLine):
>>> converter.inputs.source_names = ['functional_1.dcm', 'functional_2.dcm']
>>> converter.inputs.gzip_output = True
>>> converter.inputs.output_dir = '.'
>>> converter.cmdline #doctest: +ELLIPSIS
>>> converter.cmdline
'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'
>>> converter.run() # doctest: +SKIP
"""

input_spec=Dcm2niiInputSpec
output_spec=Dcm2niiOutputSpec

input_spec = Dcm2niiInputSpec
output_spec = Dcm2niiOutputSpec
_cmd = 'dcm2nii'

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

def _run_interface(self, runtime):

self._config_created = False
new_runtime = super(Dcm2nii, self)._run_interface(runtime)
(self.output_files,
self.reoriented_files,
self.reoriented_and_cropped_files,
self.bvecs, self.bvals) = self._parse_stdout(new_runtime.stdout)
if self._config_created:
os.remove('config.ini')
return new_runtime

def _parse_stdout(self, stdout):
Expand All @@ -99,22 +102,21 @@ def _parse_stdout(self, stdout):
last_added_file = None
for line in stdout.split("\n"):
if not skip:
file = None
out_file = None
if line.startswith("Saving "):
file = line[len("Saving "):]
out_file = line[len("Saving "):]
elif line.startswith("GZip..."):
#for gzipped outpus files are not absolute
# for gzipped outpus files are not absolute
if isdefined(self.inputs.output_dir):
output_dir = self.inputs.output_dir
else:
output_dir = self._gen_filename('output_dir')
file = os.path.abspath(os.path.join(output_dir,
line[len("GZip..."):]))
out_file = os.path.abspath(os.path.join(output_dir, line[len("GZip..."):]))
elif line.startswith("Number of diffusion directions "):
if last_added_file:
base, filename, ext = split_filename(last_added_file)
bvecs.append(os.path.join(base,filename + ".bvec"))
bvals.append(os.path.join(base,filename + ".bval"))
bvecs.append(os.path.join(base, filename + ".bvec"))
bvals.append(os.path.join(base, filename + ".bval"))
elif re.search('.*-->(.*)', line):
val = re.search('.*-->(.*)', line)
val = val.groups()[0]
Expand All @@ -123,11 +125,11 @@ def _parse_stdout(self, stdout):
else:
output_dir = self._gen_filename('output_dir')
val = os.path.join(output_dir, val)
file = val
out_file = val

if file:
files.append(file)
last_added_file = file
if out_file:
files.append(out_file)
last_added_file = out_file
continue

if line.startswith("Reorienting as "):
Expand Down Expand Up @@ -156,11 +158,11 @@ def _gen_filename(self, name):
if name == 'output_dir':
return os.getcwd()
elif name == 'config_file':
self._config_created = True
config_file = "config.ini"
f = open(config_file, "w")
# disable interactive mode
f.write("[BOOL]\nManualNIfTIConv=0\n")
f.close()
return config_file
return None