@@ -516,6 +516,8 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
516
516
bids_files = (sorted (res .outputs .bids )
517
517
if len (res .outputs .bids ) == len (res_files )
518
518
else [None ] * len (res_files ))
519
+ # preload since will be used in multiple spots
520
+ bids_metas = [load_json (b ) for b in bids_files if b ]
519
521
520
522
### Do we have a multi-echo series? ###
521
523
# Some Siemens sequences (e.g. CMRR's MB-EPI) set the label 'TE1',
@@ -529,19 +531,17 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
529
531
530
532
# Check for varying echo times
531
533
echo_times = sorted (list (set (
532
- load_json ( b ) .get ('EchoTime' , None )
533
- for b in bids_files
534
+ b .get ('EchoTime' , None )
535
+ for b in bids_metas
534
536
if b
535
537
)))
536
538
537
539
is_multiecho = len (echo_times ) > 1
538
540
539
541
### Loop through the bids_files, set the output name and save files
540
- for fl , suffix , bids_file in zip (res_files , suffixes , bids_files ):
542
+ for fl , suffix , bids_file , bids_meta in zip (res_files , suffixes , bids_files , bids_metas ):
541
543
542
544
# TODO: monitor conversion duration
543
- if bids_file :
544
- fileinfo = load_json (bids_file )
545
545
546
546
# set the prefix basename for this specific file (we'll modify it,
547
547
# and we don't want to modify it for all the bids_files):
@@ -550,7 +550,7 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
550
550
# _sbref sequences reconstructing magnitude and phase generate
551
551
# two NIfTI files IN THE SAME SERIES, so we cannot just add
552
552
# the suffix, if we want to be bids compliant:
553
- if bids_file and this_prefix_basename .endswith ('_sbref' ) \
553
+ if bids_meta and this_prefix_basename .endswith ('_sbref' ) \
554
554
and len (suffixes ) > len (echo_times ):
555
555
if len (suffixes ) != len (echo_times )* 2 :
556
556
lgr .warning (
@@ -559,9 +559,9 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
559
559
len (suffixes ), len (echo_times )
560
560
)
561
561
# Check to see if it is magnitude or phase reconstruction:
562
- if 'M' in fileinfo .get ('ImageType' ):
562
+ if 'M' in bids_meta .get ('ImageType' ):
563
563
mag_or_phase = 'magnitude'
564
- elif 'P' in fileinfo .get ('ImageType' ):
564
+ elif 'P' in bids_meta .get ('ImageType' ):
565
565
mag_or_phase = 'phase'
566
566
else :
567
567
mag_or_phase = suffix
@@ -590,12 +590,12 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
590
590
# (Note: it can be _sbref and multiecho, so don't use "elif"):
591
591
# For multi-echo sequences, we have to specify the echo number in
592
592
# the file name:
593
- if bids_file and is_multiecho :
593
+ if bids_meta and is_multiecho :
594
594
# Get the EchoNumber from json file info. If not present, use EchoTime
595
- if 'EchoNumber' in fileinfo . keys () :
596
- echo_number = fileinfo ['EchoNumber' ]
595
+ if 'EchoNumber' in bids_meta :
596
+ echo_number = bids_meta ['EchoNumber' ]
597
597
else :
598
- echo_number = echo_times .index (fileinfo ['EchoTime' ]) + 1
598
+ echo_number = echo_times .index (bids_meta ['EchoTime' ]) + 1
599
599
600
600
supported_multiecho = ['_bold' , '_phase' , '_epi' , '_sbref' , '_T1w' , '_PDT2' ]
601
601
# Now, decide where to insert it.
0 commit comments