Skip to content

Commit 7d2c526

Browse files
authored
Merge pull request #420 from cbinyu/nipy
Set the field 'TaskName' in the json file for multiecho func data
2 parents 74d9140 + 7a5f13e commit 7d2c526

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

heudiconv/convert.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from math import nan
66
import shutil
77
import sys
8+
import re
89

910
from .utils import (
1011
read_config,
@@ -323,6 +324,9 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
323324
% (outname)
324325
)
325326

327+
# add the taskname field to the json file(s):
328+
add_taskname_to_infofile( bids_outfiles )
329+
326330
if len(bids_outfiles) > 1:
327331
lgr.warning("For now not embedding BIDS and info generated "
328332
".nii.gz itself since sequence produced "
@@ -637,3 +641,32 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
637641
except TypeError as exc: ##catch lists
638642
raise TypeError("Multiple BIDS sidecars detected.")
639643
return bids_outfiles
644+
645+
646+
def add_taskname_to_infofile(infofiles):
647+
"""Add the "TaskName" field to json files corresponding to func images.
648+
649+
Parameters
650+
----------
651+
infofiles : list with json filenames or single filename
652+
653+
Returns
654+
-------
655+
"""
656+
657+
# in case they pass a string with a path:
658+
if not isinstance(infofiles, list):
659+
infofiles = [infofiles]
660+
661+
for infofile in infofiles:
662+
meta_info = load_json(infofile)
663+
try:
664+
meta_info['TaskName'] = (re.search('(?<=_task-)\w+',
665+
op.basename(infofile))
666+
.group(0).split('_')[0])
667+
except AttributeError:
668+
lgr.warning("Failed to find task field in {0}.".format(infofile))
669+
continue
670+
671+
# write to outfile
672+
save_json(infofile, meta_info)

0 commit comments

Comments
 (0)