Skip to content

Commit 7a5f13e

Browse files
committed
Changes to "add_taskname_to_infofile"
It handles now either a path or a list of paths of json files. The setting of the "TaskName" is removed from "embed_metadata_from_dicoms". Addresses some change requests in PR #470
1 parent 9449810 commit 7a5f13e

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

heudiconv/convert.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import shutil
66
import sys
7+
import re
78

89
from .utils import (
910
read_config,
@@ -321,9 +322,13 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
321322
% (outname)
322323
)
323324

325+
# add the taskname field to the json file(s):
326+
add_taskname_to_infofile( bids_outfiles )
327+
324328
if len(bids_outfiles) > 1:
325-
for bids_outfile in bids_outfiles:
326-
add_taskname_to_infofile( bids_outfile )
329+
lgr.warning("For now not embedding BIDS and info generated "
330+
".nii.gz itself since sequence produced "
331+
"multiple files")
327332
elif not bids_outfiles:
328333
lgr.debug("No BIDS files were produced, nothing to embed to then")
329334
elif outname:
@@ -629,28 +634,30 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
629634
return bids_outfiles
630635

631636

632-
def add_taskname_to_infofile(infofile):
637+
def add_taskname_to_infofile(infofiles):
633638
"""Add the "TaskName" field to json files corresponding to func images.
634639
635640
Parameters
636641
----------
637-
infofile : filename of json file
642+
infofiles : list with json filenames or single filename
638643
639644
Returns
640645
-------
641646
"""
642647

643-
import re
644-
645-
meta_info = load_json(infofile)
646-
647-
try:
648-
meta_info['TaskName'] = (re.search('(?<=_task-)\w+',
649-
op.basename(infofile))
650-
.group(0).split('_')[0])
651-
except AttributeError:
652-
lgr.warning("Failed to find task field in {0}.".format(infofile))
653-
return
648+
# in case they pass a string with a path:
649+
if not isinstance(infofiles, list):
650+
infofiles = [infofiles]
651+
652+
for infofile in infofiles:
653+
meta_info = load_json(infofile)
654+
try:
655+
meta_info['TaskName'] = (re.search('(?<=_task-)\w+',
656+
op.basename(infofile))
657+
.group(0).split('_')[0])
658+
except AttributeError:
659+
lgr.warning("Failed to find task field in {0}.".format(infofile))
660+
continue
654661

655-
# write to outfile
656-
save_json(infofile, meta_info)
662+
# write to outfile
663+
save_json(infofile, meta_info)

0 commit comments

Comments
 (0)