|
1 | 1 | import logging
|
2 | 2 | import os.path as op
|
3 | 3 | import sys
|
| 4 | +from glob import glob |
4 | 5 |
|
5 | 6 | from . import __version__, __packagename__
|
6 | 7 | from .bids import populate_bids_templates, tuneup_bids_json_files, populate_intended_for
|
@@ -113,11 +114,42 @@ def process_extra_commands(outdir, command, files, dicom_dir_template,
|
113 | 114 | if heuristic:
|
114 | 115 | heuristic = load_heuristic(heuristic)
|
115 | 116 | kwargs = getattr(heuristic, 'POPULATE_INTENDED_FOR_OPTS', {})
|
| 117 | + if not subjs: |
| 118 | + subjs = [ |
| 119 | + # search outdir for 'sub-*'; if it is a directory (not a regular file), remove |
| 120 | + # the initial 'sub-': |
| 121 | + op.basename(s)[len('sub-'):] for s in glob(op.join(outdir, 'sub-*')) if op.isdir(s) |
| 122 | + ] |
| 123 | + # read the subjects from the participants.tsv file to compare: |
| 124 | + participants_tsv = op.join(outdir, 'participants.tsv') |
| 125 | + if op.lexists(participants_tsv): |
| 126 | + with open(participants_tsv, 'r') as f: |
| 127 | + # read header line and find index for 'participant_id': |
| 128 | + participant_id_index = f.readline().split('\t').index('participant_id') |
| 129 | + # read all participants, removing the initial 'sub-': |
| 130 | + known_subjects = [ |
| 131 | + l.split('\t')[participant_id_index][len('sub-'):] for l in f.readlines() |
| 132 | + ] |
| 133 | + if not set(subjs) == set(known_subjects): |
| 134 | + # issue a warning, but continue with the 'subjs' list (the subjects for |
| 135 | + # which there is data): |
| 136 | + lgr.warning( |
| 137 | + "'participants.tsv' contents are not identical to subjects found " |
| 138 | + "in the BIDS dataset %s", outdir |
| 139 | + ) |
| 140 | + |
116 | 141 | for subj in subjs:
|
117 |
| - session_path = op.join(outdir, 'sub-' + subj) |
| 142 | + subject_path = op.join(outdir, 'sub-' + subj) |
118 | 143 | if session:
|
119 |
| - session_path = op.join(session_path, 'ses-' + session) |
120 |
| - populate_intended_for(session_path, **kwargs) |
| 144 | + session_paths = [op.join(subject_path, 'ses-' + session)] |
| 145 | + else: |
| 146 | + # check to see if the data for this subject is organized by sessions; if not |
| 147 | + # just use the subject_path |
| 148 | + session_paths = [ |
| 149 | + s for s in glob(op.join(subject_path, 'ses-*')) if op.isdir(s) |
| 150 | + ] or [subject_path] |
| 151 | + for session_path in session_paths: |
| 152 | + populate_intended_for(session_path, **kwargs) |
121 | 153 | else:
|
122 | 154 | raise ValueError("Unknown command %s" % command)
|
123 | 155 | return
|
|
0 commit comments