Skip to content

Commit bbbeef4

Browse files
committed
enh: improve robustness of dcm2niix parsing
1 parent 460c1bb commit bbbeef4

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

nipype/interfaces/dcm2nii.py

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import os
88
import re
99
from copy import deepcopy
10+
import itertools as it
11+
from glob import iglob
1012

1113
from ..utils.filemanip import split_filename
1214
from .base import (CommandLine, CommandLineInputSpec, InputMultiPath, traits,
@@ -410,52 +412,36 @@ def _run_interface(self, runtime):
410412
# may use return code 1 despite conversion
411413
runtime = super(Dcm2niix, self)._run_interface(
412414
runtime, correct_return_codes=(0, 1, ))
413-
if self.inputs.bids_format:
414-
(self.output_files, self.bvecs, self.bvals,
415-
self.bids) = self._parse_stdout(runtime.stdout)
416-
else:
417-
(self.output_files, self.bvecs, self.bvals) = self._parse_stdout(
418-
runtime.stdout)
415+
(self.output_files, self.bvecs,
416+
self.bvals, self.bids) = self._parse_stdout(runtime.stdout)
419417
return runtime
420418

421419
def _parse_stdout(self, stdout):
422-
files = []
423-
bvecs = []
424-
bvals = []
425-
bids = []
426-
skip = False
427-
find_b = False
420+
outfiles, bvals, bvecs, bids = [], [], [], []
428421
for line in stdout.split("\n"):
429-
if not skip:
430-
out_file = None
431-
if line.startswith("Convert "): # output
432-
fname = str(re.search('\S+/\S+', line).group(0))
433-
out_file = os.path.abspath(fname)
434-
# extract bvals
435-
if find_b:
436-
bvecs.append(out_file + ".bvec")
437-
bvals.append(out_file + ".bval")
438-
find_b = False
439-
# next scan will have bvals/bvecs
440-
elif 'DTI gradients' in line or 'DTI gradient directions' in line or 'DTI vectors' in line:
441-
find_b = True
442-
if out_file:
443-
ext = '.nii' if self.inputs.compress == 'n' else '.nii.gz'
444-
files.append(out_file + ext)
445-
if self.inputs.bids_format:
446-
bids.append(out_file + ".json")
447-
skip = False
448-
# just return what was done
449-
if not bids:
450-
return files, bvecs, bvals
451-
else:
452-
return files, bvecs, bvals, bids
422+
if line.startswith("Convert "): # output
423+
fname = str(re.search('\S+/\S+', line).group(0))
424+
outtypes = (".nii", ".nii.gz", ".bval", ".bvec", ".json")
425+
# search for relevant files, and sort accordingly
426+
for fl in search_files(fname, outtypes):
427+
if fl.endswith(".nii") or fl.endswith(".gz"):
428+
outfiles.append(fl)
429+
elif fl.endswith(".bval"):
430+
bvals.append(fl)
431+
elif fl.endswith(".bvec"):
432+
bvecs.append(fl)
433+
elif fl.endswith(".json"):
434+
bids.append(fl)
435+
return outfiles, bvecs, bvals, bids
453436

454437
def _list_outputs(self):
455438
outputs = self.output_spec().get()
456439
outputs['converted_files'] = self.output_files
457440
outputs['bvecs'] = self.bvecs
458441
outputs['bvals'] = self.bvals
459-
if self.inputs.bids_format:
460-
outputs['bids'] = self.bids
442+
outputs['bids'] = self.bids
461443
return outputs
444+
445+
# https://stackoverflow.com/a/4829130
446+
def search_files(prefix, outtypes):
447+
return it.chain.from_iterable(iglob(prefix + outtype) for outtype in outtypes)

0 commit comments

Comments
 (0)