Skip to content

Commit a6c52b1

Browse files
committed
BF: add _ses- suffix to _scans files
1 parent a6b125f commit a6c52b1

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

bin/heudiconv

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,15 @@ def add_rows_to_scans_keys_file(fn, newrows):
10151015
writer.writerow([key] + fnames2info[key])
10161016

10171017

1018+
def _find_subj_ses(f_name):
1019+
"""Given a path to the bids formatted filename parse out subject/session"""
1020+
# we will allow the match at either directories or within filename
1021+
# assuming that bids layout is "correct"
1022+
regex = re.compile('sub-(?P<subj>[a-zA-Z0-9]*)([/_]ses-(?P<ses>[a-zA-Z0-9]*))?')
1023+
res = regex.search(f_name).groupdict()
1024+
return res.get('subj'), res.get('ses', None)
1025+
1026+
10181027
def save_scans_key(items, outname_bids_files):
10191028
"""
10201029
Parameters
@@ -1027,23 +1036,38 @@ def save_scans_key(items, outname_bids_files):
10271036
10281037
"""
10291038
rows = dict()
1039+
# we will need to deduce subject and session from the bids_filename
1040+
# and if there is a conflict, we would just blow since this function
1041+
# should be invoked only on a result of a single item conversion as far
1042+
# as I see it, so should have the same subject/session
1043+
subj, ses = None, None
1044+
10301045
for item, outname_bids_file in zip(items, outname_bids_files):
10311046
# get filenames
10321047
f_name = '/'.join(outname_bids_file.split('/')[-2:])
10331048
f_name = f_name.replace('json', 'nii.gz')
10341049
rows[f_name] = get_formatted_scans_key_row(item)
1050+
subj_, ses_ = _find_subj_ses(f_name)
1051+
if subj and subj_ != subj:
1052+
raise ValueError(
1053+
"We found before subject %s but now deduced %s from %s"
1054+
% (subj, subj_, f_name)
1055+
)
1056+
subj = subj_
1057+
if ses and ses_ != ses:
1058+
raise ValueError(
1059+
"We found before session %s but now deduced %s from %s"
1060+
% (ses, ses_, f_name)
1061+
)
1062+
ses = ses_
10351063
# where should we store it?
10361064
output_dir = dirname(dirname(outname_bids_file))
1037-
# get subject info
1038-
subj_pattern = re.compile('(sub-[a-zA-Z0-9]*)')
1039-
subj = subj_pattern.findall(f_name)
1040-
assert(len(subj) >= 1)
1041-
subj = subj[0]
1042-
10431065
# save
1066+
ses = '_ses-%s' % ses if ses else ''
10441067
add_rows_to_scans_keys_file(
1045-
pjoin(output_dir, '{0}_scans.tsv'.format(subj)),
1046-
rows)
1068+
pjoin(output_dir, '{0}{1}_scans.tsv'.format(subj, ses)),
1069+
rows
1070+
)
10471071

10481072

10491073
def tuneup_bids_json_files(json_files):

tests/test_main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,8 @@ def _check_rows(fn, rows):
157157
heudiconv.add_rows_to_scans_keys_file(fn, extra_rows)
158158
_check_rows(fn, extra_rows)
159159

160+
def test__find_subj_ses():
161+
assert heudiconv._find_subj_ses('950_bids_test4/sub-phantom1sid1/fmap/sub-phantom1sid1_acq-3mm_phasediff.json') == ('phantom1sid1', None)
162+
assert heudiconv._find_subj_ses('sub-s1/ses-s1/fmap/sub-s1_ses-s1_acq-3mm_phasediff.json') == ('s1', 's1')
163+
assert heudiconv._find_subj_ses('sub-s1/ses-s1/fmap/sub-s1_ses-s1_acq-3mm_phasediff.json') == ('s1', 's1')
164+
assert heudiconv._find_subj_ses('fmap/sub-01-fmap_acq-3mm_acq-3mm_phasediff.nii.gz') == ('01', None)

0 commit comments

Comments
 (0)