Repository navigation
Add participants.tsv filtering and BidsDataset.participants - #542
Merged
Merged
Conversation
generate_inputs() now reads participants.tsv from the dataset root and exposes it as BidsDataset.participants (indexed by subject label). A new participant_filter argument (and --participant-filter CLI flag) selects subjects with a pandas DataFrame.query expression on the table, e.g. "age > 30 and group == 'patient'". The selection is passed through the existing subject post-filter, so it intersects with --participant-label and combines with --exclude-participant-label. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Use universal-pathlib (already required by pybids) so participants.tsv can be read from fsspec-backed locations such as gs:// or s3://, the same way pybids handles remote dataset roots. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #542 +/- ##
==========================================
+ Coverage 95.35% 95.48% +0.12%
==========================================
Files 56 57 +1
Lines 2543 2611 +68
==========================================
+ Hits 2425 2493 +68
Misses 118 118 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Changes recommended
Existing subject filters can bypass participant-query selections, silently allowing unselected subjects to be processed.
3 open issues
What changed in this PR
Adds participant-table filtering to Snakebids input generation and exposes the full participants.tsv table through BidsDataset.participants.
Changes:
- Adds
participant_filterand CLI support for selecting subjects with pandas queries. - Introduces local and remote participant-table reading, with validation and warnings.
- Updates workflow examples, dependencies, type definitions, and tests.
| File | Description |
|---|---|
uv.lock |
Records new runtime and typing dependencies. |
typings/bids/layout/layout.pyi |
Adds the get_subjects type declaration. |
tests/test_plugins/test_snakemake.py |
Updates expected configuration with participant_filter. |
tests/test_participants.py |
Tests table reading, querying, and filtering integration. |
tests/test_bidsapp.py |
Updates plugin setup for the new argument. |
src/snakebids/project_template/{{name_slug}}/workflow/Snakefile |
Passes participant filters into generated workflows. |
src/snakebids/plugins/bidsargs.py |
Registers and documents the participant-filter CLI option. |
src/snakebids/core/participants.py |
Implements participant-table reading and querying. |
src/snakebids/core/input_generation.py |
Integrates participant selection and attaches table metadata. |
src/snakebids/core/datasets.py |
Adds the unfiltered participants table to BidsDataset. |
src/snakebids/bidsapp/args.py |
Adds the participant-filter configuration type. |
pyproject.toml |
Declares universal-pathlib and pandas typing dependencies. |
docs/bids_app/workflow.md |
Updates the workflow example to pass participant filters. |
Review effort: Balanced
Add a "Selecting participants" section to the running docs covering --participant-label, --exclude-participant-label and --participant-filter, including query syntax notes and known limitations. Document BidsDataset.participants in the workflow docs, and add pandas to the intersphinx mapping so DataFrame references resolve. Also report a participants.tsv that is not valid UTF-8 as a ConfigError, so it is a warning rather than a crash when no filter is requested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Proposed changes
Adds support for selecting participants based on the dataset's
participants.tsv, and exposes the table onBidsDataset.Filtering
participant_filterargument togenerate_inputs()and--participant-filterCLI flag (inBidsArgs), e.g.--participant-filter "age > 60 and group == 'patient'".pandas.DataFrame.query. The subject label (withoutsub-) can be referenced assubject.--participant-labeland combines with--exclude-participant-label. Custom-path components are filtered too.participants.tsvare excluded, with a warning listing them. A warning is also logged if no participants match. Requesting a filter on a dataset withoutparticipants.tsvraises aConfigError, as does an invalid query (the error lists the available columns).BidsDataset.participantsparticipants.tsvas a DataFrame indexed by subject label, orNoneif the file is absent. It is not filtered;participants.loc[dataset.subjects]selects the subjects in the dataset.participant_idis read as text (keeps leading zeros) andn/ais read as missing.generate_inputs()call, a malformedparticipants.tsvonly logs a warning unless a filter was requested.Remote datasets
universal-pathlib(already required by pybids), sogs://,s3://and other fsspec locations work.universal-pathlibis now a direct dependency.Other
pandas-stubsadded to the dev group, since pyright runs in strict mode.get_subjectsadded to the localBIDSLayoutstub (pybids generates it dynamically).participant_filter=config.get("participant_filter"). Existing apps need to add that line to get the CLI flag working.Checklist
New tests are in
tests/test_participants.py; remote reading is tested with fsspec's in-memory filesystem. Locally, ruff and pyright are clean. The full suite passed except tests that also fail onmainlocally (template tests needing docker, intermittent hypothesis deadline timeouts). Opened as a draft to get CI results. User docs are indocs/running_snakebids/overview.md("Selecting participants", including a Limitations section) anddocs/bids_app/workflow.md("Accessing participant metadata").Notes
Known limitations, documented rather than fixed in this PR. Both come from the existing subject post-filter on
main: the first also affects--participant-label, and the second can be triggered onmainwithgenerate_inputs(..., participant_label=[]), but the filter makes an empty selection much easier to reach:subjectfilter inpybids_inputsignore the participant selection.ValueErrorfor components with acustom_path, instead of returning an empty component.Open questions for review:
BidsDataset.participantsbe restricted to the subjects in the dataset rather than the full table?pandasis now imported directly but is only listed inpyproject.tomlunder a Python ≥3.12 marker; it should probably become an unconditional direct dependency.🤖 Generated with Claude Code