@@ -242,7 +242,7 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
242
242
if not isinstance (outtypes , (list , tuple )):
243
243
outtypes = (outtypes ,)
244
244
245
- prefix_dirname = op .dirname (prefix + '.ext' )
245
+ prefix_dirname = op .dirname (prefix )
246
246
outname_bids = prefix + '.json'
247
247
bids_outfiles = []
248
248
lgr .info ('Converting %s (%d DICOMs) -> %s . '
@@ -442,8 +442,7 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
442
442
"""
443
443
from nipype .interfaces .base import isdefined
444
444
445
- prefix_dirname = op .dirname (prefix + '.ext' )
446
- prefix_basename = op .basename (prefix )
445
+ prefix_dirname , prefix_basename = op .split (prefix )
447
446
448
447
bids_outfiles = []
449
448
res_files = res .outputs .converted_files
@@ -475,8 +474,8 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
475
474
# Also copy BIDS files although they might need to
476
475
# be merged/postprocessed later
477
476
bids_files = sorted (res .outputs .bids
478
- if len (res .outputs .bids ) == len (res_files )
479
- else [None ] * len (res_files ))
477
+ if len (res .outputs .bids ) == len (res_files )
478
+ else [None ] * len (res_files ))
480
479
481
480
### Do we have a multi-echo series? ###
482
481
# Some Siemens sequences (e.g. CMRR's MB-EPI) set the label 'TE1',
@@ -488,23 +487,24 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
488
487
# series. To do that, the most straightforward way is to read the
489
488
# echo times for all bids_files and see if they are all the same or not.
490
489
491
- # Get the echo times while not breaking non-BIDS compliance
492
- echo_times = []
490
+ # Check for echotime information
491
+ echo_times = set ()
492
+
493
493
for bids_file in bids_files :
494
494
if bids_file :
495
- echo_times .append (load_json (bids_file ).get ('EchoTime' ))
495
+ # check for varying EchoTimes
496
+ echot = load_json (bids_file ).get ('EchoTime' , None )
497
+ if echot is not None :
498
+ echo_times .add (echot )
496
499
497
500
# To see if the echo times are the same, convert it to a set and see if
498
- # only one remains:
499
- multiecho = False
500
- if echo_times :
501
- multiecho = len (set (echo_times )) == 1
501
+ # only one remains:
502
+ is_multiecho = len (echo_times ) >= 1 if echo_times else False
502
503
503
504
### Loop through the bids_files, set the output name and save files
504
-
505
505
for fl , suffix , bids_file in zip (res_files , suffixes , bids_files ):
506
- # load the json file info:
507
- # TODO: time performance
506
+
507
+ # TODO: monitor conversion duration
508
508
if bids_file :
509
509
fileinfo = load_json (bids_file )
510
510
@@ -515,7 +515,7 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
515
515
# _sbref sequences reconstructing magnitude and phase generate
516
516
# two NIfTI files IN THE SAME SERIES, so we cannot just add
517
517
# the suffix, if we want to be bids compliant:
518
- if ( bids_file and ( this_prefix_basename .endswith ('_sbref' )) ):
518
+ if bids_file and this_prefix_basename .endswith ('_sbref' ):
519
519
# Check to see if it is magnitude or phase reconstruction:
520
520
if 'M' in fileinfo .get ('ImageType' ):
521
521
mag_or_phase = 'magnitude'
@@ -525,7 +525,7 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
525
525
mag_or_phase = suffix
526
526
527
527
# Insert reconstruction label
528
- if not (( "_rec-%s" % mag_or_phase ) in this_prefix_basename ) :
528
+ if not ("_rec-%s" % mag_or_phase ) in this_prefix_basename :
529
529
530
530
# If "_rec-" is specified, prepend the 'mag_or_phase' value.
531
531
if ('_rec-' in this_prefix_basename ):
@@ -548,23 +548,23 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
548
548
# (Note: it can be _sbref and multiecho, so don't use "elif"):
549
549
# For multi-echo sequences, we have to specify the echo number in
550
550
# the file name:
551
- if bids and multiecho :
551
+ if bids_file and is_multiecho :
552
552
# Get the EchoNumber from json file info. If not present, it's echo-1
553
553
echo_number = fileinfo .get ('EchoNumber' , 1 )
554
554
555
555
556
- supported_multiecho = ['_bold' , '_sbref' , '_T1w' ] # epi?
556
+ supported_multiecho = ['_bold' , '_epi' , ' _sbref' , '_T1w' ]
557
557
# Now, decide where to insert it.
558
558
# Insert it **before** the following string(s), whichever appears first.
559
- for imgtype in [ '_bold' , '_sbref' , '_T1w' ] :
559
+ for imgtype in supported_multiecho :
560
560
if (imgtype in this_prefix_basename ):
561
561
this_prefix_basename = this_prefix_basename .replace (
562
562
imgtype , "_echo-%d%s" % (echo_number , imgtype )
563
563
)
564
564
break
565
565
566
566
# For Scout runs with multiple NIfTI images per run:
567
- if ( bids and ( 'scout' in this_prefix_basename .lower ()) ):
567
+ if bids and 'scout' in this_prefix_basename .lower ():
568
568
# in some cases (more than one slice slab), there are several
569
569
# NIfTI images in the scout run, so distinguish them with "_acq-"
570
570
spt = this_prefix_basename .split ('_acq-Scout' , 1 )
@@ -573,7 +573,7 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
573
573
# Fallback option:
574
574
# If we have failed to modify this_prefix_basename, because it didn't fall
575
575
# into any of the options above, just add the suffix at the end:
576
- if ( this_prefix_basename == prefix_basename ) :
576
+ if this_prefix_basename == prefix_basename :
577
577
this_prefix_basename += suffix
578
578
579
579
# Finally, form the outname by stitching the directory and outtype:
@@ -586,6 +586,7 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
586
586
outname_bids_file = "%s.json" % (outname )
587
587
safe_copyfile (bids_file , outname_bids_file , overwrite )
588
588
bids_outfiles .append (outname_bids_file )
589
+
589
590
# res_files is not a list
590
591
else :
591
592
outname = "{}.{}" .format (prefix , outtype )
0 commit comments