Skip to content

Commit 6046dc0

Browse files
committed
RF+ENH: avoid loading the same bids json twice, rename fileinfo -> bids_meta
for consistency
1 parent 2f3a7d2 commit 6046dc0

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

heudiconv/convert.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
516516
bids_files = (sorted(res.outputs.bids)
517517
if len(res.outputs.bids) == len(res_files)
518518
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]
519521

520522
### Do we have a multi-echo series? ###
521523
# 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
529531

530532
# Check for varying echo times
531533
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
534536
if b
535537
)))
536538

537539
is_multiecho = len(echo_times) > 1
538540

539541
### 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):
541543

542544
# TODO: monitor conversion duration
543-
if bids_file:
544-
fileinfo = load_json(bids_file)
545545

546546
# set the prefix basename for this specific file (we'll modify it,
547547
# 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
550550
# _sbref sequences reconstructing magnitude and phase generate
551551
# two NIfTI files IN THE SAME SERIES, so we cannot just add
552552
# 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') \
554554
and len(suffixes) > len(echo_times):
555555
if len(suffixes) != len(echo_times)*2:
556556
lgr.warning(
@@ -559,9 +559,9 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
559559
len(suffixes), len(echo_times)
560560
)
561561
# 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'):
563563
mag_or_phase = 'magnitude'
564-
elif 'P' in fileinfo.get('ImageType'):
564+
elif 'P' in bids_meta.get('ImageType'):
565565
mag_or_phase = 'phase'
566566
else:
567567
mag_or_phase = suffix
@@ -590,12 +590,12 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
590590
# (Note: it can be _sbref and multiecho, so don't use "elif"):
591591
# For multi-echo sequences, we have to specify the echo number in
592592
# the file name:
593-
if bids_file and is_multiecho:
593+
if bids_meta and is_multiecho:
594594
# 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']
597597
else:
598-
echo_number = echo_times.index(fileinfo['EchoTime']) + 1
598+
echo_number = echo_times.index(bids_meta['EchoTime']) + 1
599599

600600
supported_multiecho = ['_bold', '_phase', '_epi', '_sbref', '_T1w', '_PDT2']
601601
# Now, decide where to insert it.

0 commit comments

Comments
 (0)