|
7 | 7 | import os
|
8 | 8 | import re
|
9 | 9 | from copy import deepcopy
|
| 10 | +import itertools as it |
| 11 | +from glob import iglob |
10 | 12 |
|
11 | 13 | from ..utils.filemanip import split_filename
|
12 | 14 | from .base import (CommandLine, CommandLineInputSpec, InputMultiPath, traits,
|
@@ -410,52 +412,36 @@ def _run_interface(self, runtime):
|
410 | 412 | # may use return code 1 despite conversion
|
411 | 413 | runtime = super(Dcm2niix, self)._run_interface(
|
412 | 414 | 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) |
419 | 417 | return runtime
|
420 | 418 |
|
421 | 419 | 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 = [], [], [], [] |
428 | 421 | 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 |
453 | 436 |
|
454 | 437 | def _list_outputs(self):
|
455 | 438 | outputs = self.output_spec().get()
|
456 | 439 | outputs['converted_files'] = self.output_files
|
457 | 440 | outputs['bvecs'] = self.bvecs
|
458 | 441 | outputs['bvals'] = self.bvals
|
459 |
| - if self.inputs.bids_format: |
460 |
| - outputs['bids'] = self.bids |
| 442 | + outputs['bids'] = self.bids |
461 | 443 | 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