Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ docs

# Local environment
.venv
venv

# Jupyter
.ipynb_checkpoints
Expand Down
10 changes: 9 additions & 1 deletion bids2table/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ def main():
default=None,
help="List of directory names or glob patterns to exclude from indexing.",
)

parser.add_argument(
"--subject",
"-sub",
nargs="+",
type=str,
help="List of subject labels to index (default: None)",
default=None,
)
args = parser.parse_args()

log_level = ["ERROR", "WARNING", "INFO"][min(args.verbose, 2)]
Expand All @@ -75,6 +82,7 @@ def main():
workers=args.workers,
worker_id=args.worker_id,
exclude=args.exclude,
subject=args.subject,
return_table=False,
)

Expand Down
34 changes: 26 additions & 8 deletions bids2table/_b2t.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def bids2table(
workers: Optional[int] = None,
worker_id: Optional[int] = None,
return_table: bool = True,
subject: Optional[List[str]] = None,
) -> Optional[BIDSTable]:
"""
Index a BIDS dataset directory and load as a pandas DataFrame.
Expand All @@ -47,6 +48,7 @@ def bids2table(
overwrite.
return_table: whether to return the BIDS table or just build the persistent
index.
subject: optional subject label to index only a specific subject directory

Returns:
A `BIDSTable` representing the indexed dataset(s), or `None` if `return_table`
Expand All @@ -62,13 +64,29 @@ def bids2table(
if exclude is None:
exclude = []

source = Crawler(
root=root,
include=["sub-*"], # find subject dirs
skip=["sub-*"] + exclude, # but don't crawl into subject dirs
dirs_only=True,
follow_links=True,
)
if subject is not None:
subjects = [sub.lstrip("sub-") if sub.startswith("sub-") else sub for sub in subject]
logger.info(f"Indexing only subjects: {', '.join([f'sub-{sub}' for sub in subjects])}")
all_subjects = [d.name for d in root.iterdir() if d.is_dir() and d.name.startswith("sub-")]
exclude += [f"{sub}" for sub in all_subjects if sub not in [f"sub-{s}" for s in subjects]]
logger.info(f"Excluding subjects: {exclude}")
include_patterns = [f"sub-{sub}" for sub in subjects]
source = Crawler(
root=root,
include=include_patterns,
exclude=exclude,
dirs_only=True,
follow_links=True,
)
else:
logger.info("Indexing all subjects")
source = Crawler(
root=root,
include=["sub-*"], # find subject dirs
skip=["sub-*"] + exclude, # exclude specified patterns
dirs_only=True,
follow_links=True,
)
extract = partial(extract_bids_subdir, exclude=exclude, with_meta=with_meta)

if index_path is None:
Expand Down Expand Up @@ -110,4 +128,4 @@ def bids2table(
mtime_column="file__mod_time",
)
tab = BIDSTable.from_parquet(index_path) if return_table else None
return tab
return tab