Skip to content

Commit

Permalink
Merge pull request #420 from cbinyu/nipy
Browse files Browse the repository at this point in the history
Set the  field 'TaskName' in the json file for multiecho func data
  • Loading branch information
yarikoptic authored Mar 27, 2020
2 parents 74d9140 + 7a5f13e commit 7d2c526
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions heudiconv/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from math import nan
import shutil
import sys
import re

from .utils import (
read_config,
Expand Down Expand Up @@ -323,6 +324,9 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
% (outname)
)

# add the taskname field to the json file(s):
add_taskname_to_infofile( bids_outfiles )

if len(bids_outfiles) > 1:
lgr.warning("For now not embedding BIDS and info generated "
".nii.gz itself since sequence produced "
Expand Down Expand Up @@ -637,3 +641,32 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
except TypeError as exc: ##catch lists
raise TypeError("Multiple BIDS sidecars detected.")
return bids_outfiles


def add_taskname_to_infofile(infofiles):
"""Add the "TaskName" field to json files corresponding to func images.
Parameters
----------
infofiles : list with json filenames or single filename
Returns
-------
"""

# in case they pass a string with a path:
if not isinstance(infofiles, list):
infofiles = [infofiles]

for infofile in infofiles:
meta_info = load_json(infofile)
try:
meta_info['TaskName'] = (re.search('(?<=_task-)\w+',
op.basename(infofile))
.group(0).split('_')[0])
except AttributeError:
lgr.warning("Failed to find task field in {0}.".format(infofile))
continue

# write to outfile
save_json(infofile, meta_info)

0 comments on commit 7d2c526

Please sign in to comment.