diff --git a/src/brainflex/core/get_data.py b/src/brainflex/core/get_data.py index f33f035..92ecd85 100644 --- a/src/brainflex/core/get_data.py +++ b/src/brainflex/core/get_data.py @@ -181,6 +181,8 @@ def _get_freesurfer( def _write_pheno(layout: DatasetLayout, session_ids: dict[str, str]) -> None: """Copy participants.tsv to the tabular directory, with a session ID column. + Participant IDs without the ``sub-`` prefix have it prepended. + Rows with an empty participant ID and participants without BIDS imaging data are excluded. Warns if participant IDs and BIDS subjects do not fully match, and raises ValueError if none match. @@ -196,6 +198,16 @@ def _write_pheno(layout: DatasetLayout, session_ids: dict[str, str]) -> None: fieldnames = reader.fieldnames rows = [row for row in reader if (row[COL_PARTICIPANT_ID] or "").strip()] + for row in rows: + participant_id = row[COL_PARTICIPANT_ID] + if not participant_id.startswith(BIDS_SUBJECT_PREFIX): + row[COL_PARTICIPANT_ID] = BIDS_SUBJECT_PREFIX + participant_id + warnings.warn( + f"Prepended {BIDS_SUBJECT_PREFIX!r} to participant ID" + f" {participant_id!r} in {PARTICIPANTS_FILENAME}", + stacklevel=3, + ) + fieldnames = [COL_PARTICIPANT_ID, COL_SESSION_ID] + [ col for col in fieldnames if col not in (COL_PARTICIPANT_ID, COL_SESSION_ID) ] diff --git a/tests/core/test_get_data.py b/tests/core/test_get_data.py index 97d7c5b..f367d41 100644 --- a/tests/core/test_get_data.py +++ b/tests/core/test_get_data.py @@ -204,11 +204,32 @@ def test_no_matching_participants( self, mock_s3: dict[tuple[str, str], bytes], root: Path ): mock_s3[(OPENNEURO_BUCKET, f"{DATASET_ID}/participants.tsv")] = ( - b"participant_id\tage\nSZ_0113\t25\n" + b"participant_id\tage\nsub-SZ0113\t25\n" ) with pytest.raises(ValueError, match="No participant IDs"): get_data(root, DATASET_ID) + def test_participants_without_prefix( + self, mock_s3: dict[tuple[str, str], bytes], root: Path + ): + mock_s3[(OPENNEURO_BUCKET, f"{DATASET_ID}/participants.tsv")] = ( + b"participant_id\tage\n01\t20\n02\t30\nsub-03\t40\n" + ) + with pytest.warns(UserWarning) as record_warnings: + dpath_study = get_data(root, DATASET_ID) + assert [ + str(w.message) for w in record_warnings if "Prepended" in str(w.message) + ] == [ + "Prepended 'sub-' to participant ID '01' in participants.tsv", + "Prepended 'sub-' to participant ID '02' in participants.tsv", + ] + pheno = (dpath_study / "tabular" / "pheno.tsv").read_text() + assert pheno == ( + "participant_id\tsession_id\tage\n" + "sub-01\tses-unnamed\t20\n" + "sub-02\tses-unnamed\t30\n" + ) + def test_no_participants_json( self, mock_s3: dict[tuple[str, str], bytes], root: Path ):