@@ -76,6 +76,9 @@ def populate_bids_templates(path, defaults={}):
76
76
# 'Manufacturer', 'SliceTiming', ''}
77
77
for fpath in find_files ('.*_task-.*\_bold\.json' , topdir = path ,
78
78
exclude_vcs = True , exclude = "/\.(datalad|heudiconv)/" ):
79
+ #
80
+ # According to BIDS spec I think both _task AND _acq (may be more? _rec, _dir, ...?) should be retained?
81
+ # TODO: if we are to fix it, then old ones (without _acq) should be removed first
79
82
task = re .sub ('.*_(task-[^_\.]*(_acq-[^_\.]*)?)_.*' , r'\1' , fpath )
80
83
json_ = load_json (fpath )
81
84
if task not in tasks :
@@ -95,17 +98,33 @@ def populate_bids_templates(path, defaults={}):
95
98
lgr .debug ("Generating %s" , events_file )
96
99
with open (events_file , 'w' ) as f :
97
100
f .write ("onset\t duration\t trial_type\t response_time\t stim_file\t TODO -- fill in rows and add more tab-separated columns if desired" )
101
+
98
102
# extract tasks files stubs
99
103
for task_acq , fields in tasks .items ():
100
104
task_file = op .join (path , task_acq + '_bold.json' )
101
- # do not touch any existing thing, it may be precious
102
- if not op .lexists (task_file ):
103
- lgr .debug ("Generating %s" , task_file )
104
- fields ["TaskName" ] = ("TODO: full task name for %s" %
105
- task_acq .split ('_' )[0 ].split ('-' )[1 ])
106
- fields ["CogAtlasID" ] = "TODO"
107
- with open (task_file , 'w' ) as f :
108
- f .write (json_dumps_pretty (fields , indent = 2 , sort_keys = True ))
105
+ # Since we are pulling all unique fields we have to possibly
106
+ # rewrite this file to guarantee consistency.
107
+ # See https://github.com/nipy/heudiconv/issues/277 for a usecase/bug
108
+ # when we didn't touch existing one.
109
+ # But the fields we enter (TaskName and CogAtlasID) might need need
110
+ # to be populated from the file if it already exists
111
+ placeholders = {
112
+ "TaskName" : ("TODO: full task name for %s" %
113
+ task_acq .split ('_' )[0 ].split ('-' )[1 ]),
114
+ "CogAtlasID" : "TODO" ,
115
+ }
116
+ if op .lexists (task_file ):
117
+ j = load_json (task_file )
118
+ # Retain possibly modified placeholder fields
119
+ for f in placeholders :
120
+ if f in j :
121
+ placeholders [f ] = j [f ]
122
+ act = "Regenerating"
123
+ else :
124
+ act = "Generating"
125
+ lgr .debug ("%s %s" , act , task_file )
126
+ fields .update (placeholders )
127
+ save_json (fields , indent = 2 , sort_keys = True , pretty = True )
109
128
110
129
111
130
def tuneup_bids_json_files (json_files ):
0 commit comments