Skip to content

Commit 665d901

Browse files
authored
Merge pull request #376 from darrencl/generate-optional-jsons
Generate optional jsons
2 parents c760a4b + f2fbaf5 commit 665d901

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

heudiconv/bids.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,27 @@ def add_participant_record(studydir, subject, age, sex):
240240
known_subjects = {l.split('\t')[0] for l in f.readlines()}
241241
if participant_id in known_subjects:
242242
return
243+
else:
244+
# Populate particpants.json (an optional file to describe column names in
245+
# participant.tsv). This auto generation will make BIDS-validator happy.
246+
participants_json = op.join(studydir, 'participants.json')
247+
if not op.lexists(participants_json):
248+
save_json(participants_json,
249+
OrderedDict([
250+
("participant_id", OrderedDict([
251+
("Description", "Participant identifier")])),
252+
("age", OrderedDict([
253+
("Description", "Age in years (TODO - verify) as in the initial"
254+
" session, might not be correct for other sessions")])),
255+
("sex", OrderedDict([
256+
("Description", "self-rated by participant, M for male/F for "
257+
"female (TODO: verify)")])),
258+
("group", OrderedDict([
259+
("Description", "(TODO: adjust - by default everyone is in "
260+
"control group)")])),
261+
]),
262+
sort_keys=False,
263+
indent=2)
243264
# Add a new participant
244265
with open(participants_tsv, 'a') as f:
245266
f.write(
@@ -311,7 +332,8 @@ def save_scans_key(item, bids_files):
311332

312333
def add_rows_to_scans_keys_file(fn, newrows):
313334
"""
314-
Add new rows to file fn for scans key filename
335+
Add new rows to file fn for scans key filename and generate accompanying json
336+
descriptor to make BIDS validator happy.
315337
316338
Parameters
317339
----------
@@ -334,6 +356,25 @@ def add_rows_to_scans_keys_file(fn, newrows):
334356
os.unlink(fn)
335357
else:
336358
fnames2info = newrows
359+
# Populate _scans.json (an optional file to describe column names in
360+
# _scans.tsv). This auto generation will make BIDS-validator happy.
361+
scans_json = '.'.join(fn.split('.')[:-1] + ['json'])
362+
if not op.lexists(scans_json):
363+
save_json(scans_json,
364+
OrderedDict([
365+
("filename", OrderedDict([
366+
("Description", "Name of the nifti file")])),
367+
("acq_time", OrderedDict([
368+
("LongName", "Acquisition time"),
369+
("Description", "Acquisition time of the particular scan")])),
370+
("operator", OrderedDict([
371+
("Description", "Name of the operator")])),
372+
("randstr", OrderedDict([
373+
("LongName", "Random string"),
374+
("Description", "md5 hash of UIDs")])),
375+
]),
376+
sort_keys=False,
377+
indent=2)
337378

338379
header = ['filename', 'acq_time', 'operator', 'randstr']
339380
# prepare all the data rows

heudiconv/tests/test_heuristics.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,13 @@ def test_notop(tmpdir, bidsoptions):
165165
runner(args)
166166

167167
assert op.exists(pjoin(tmppath, 'Halchenko/Yarik/950_bids_test4'))
168-
for fname in ['CHANGES', 'dataset_description.json', 'participants.tsv', 'README']:
168+
for fname in [
169+
'CHANGES',
170+
'dataset_description.json',
171+
'participants.tsv',
172+
'README',
173+
'participants.json'
174+
]:
169175
if 'notop' in bidsoptions:
170176
assert not op.exists(pjoin(tmppath, 'Halchenko/Yarik/950_bids_test4', fname))
171177
else:

heudiconv/tests/test_main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from os.path import join as opj
2323
from six.moves import StringIO
2424
import stat
25+
import os.path as op
2526

2627

2728
@patch('sys.stdout', new_callable=StringIO)
@@ -205,6 +206,7 @@ def _check_rows(fn, rows):
205206
assert dates == sorted(dates)
206207

207208
_check_rows(fn, rows)
209+
assert op.exists(opj(tmpdir.strpath, 'file.json'))
208210
# add a new one
209211
extra_rows = {
210212
'a_new_file.nii.gz': ['2016adsfasd23', '', 'fasadfasdf'],

0 commit comments

Comments
 (0)