utils
tit.pre.utils ¶
Utility helpers for the tit.pre package.
Provides subject discovery, BIDS directory scaffolding, dataset-description management, subprocess execution with cancellation support, and shared exception classes.
Public API¶
discover_subjects Return sorted subject IDs found in a BIDS project tree. check_m2m_exists Check whether a SimNIBS m2m directory exists.
See Also¶
tit.pre : Package-level overview and convenience re-exports.
PreprocessError ¶
Bases: RuntimeError
Raised when a preprocessing step fails.
See Also¶
PreprocessCancelled : Raised specifically when cancelled by a stop event.
PreprocessCancelled ¶
Bases: RuntimeError
Raised when a preprocessing run is cancelled via a stop event.
See Also¶
PreprocessError : General preprocessing failure.
CommandRunner ¶
CommandRunner(stop_event: Event | None = None)
Run subprocesses with cancellation and streaming log output.
Wraps subprocess.Popen to stream stdout/stderr line-by-line to a
logger while honouring a threading.Event for cancellation.
Parameters¶
stop_event : threading.Event or None, optional
Event that, when set, causes any running command to be terminated.
A fresh event is created if None.
Attributes¶
stop_event : threading.Event The cancellation event.
See Also¶
PreprocessCancelled : Exception raised when a command is cancelled.
Source code in tit/pre/utils.py
request_stop ¶
terminate_all ¶
run ¶
Execute cmd and stream its output to logger.
Parameters¶
cmd : sequence of str Command and arguments. logger : logging.Logger Logger that receives each output line at INFO level. cwd : str or None, optional Working directory for the subprocess. env : dict or None, optional Environment variables for the subprocess.
Returns¶
int Process exit code.
Raises¶
PreprocessCancelled
If stop_event is set before or during execution.
ValueError
If cmd is empty.
Source code in tit/pre/utils.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
discover_subjects ¶
Return sorted, deduplicated subject IDs found in a BIDS project tree.
Returns an empty list when project_dir is None (project not
configured).
Discovery order:
sourcedata/sub-*/T1w/orT2w/-- any subdir, NIfTI, DICOM, or supported DICOM archive (.zip,.tar,.tar.gz,.tgz).sourcedata/sub-*/*.tgz(compressed bundles at top level).sub-*/anat/*T1w*.nii[.gz]or*T2w*.nii[.gz]at project root.
Parameters¶
project_dir : str BIDS project root directory.
Returns¶
list[str]
Sorted list of subject identifiers (without the sub- prefix).
See Also¶
check_m2m_exists : Check whether a subject's m2m directory exists.
Source code in tit/pre/utils.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
check_m2m_exists ¶
Return True if the SimNIBS m2m directory already exists.
Checks for
<project_dir>/derivatives/SimNIBS/sub-<subject_id>/m2m_<subject_id>.
Parameters¶
project_dir : str
BIDS project root directory.
subject_id : str
Subject identifier without the sub- prefix.
Returns¶
bool
True if the m2m directory exists on disk.
See Also¶
discover_subjects : Find all subject IDs in a BIDS project. run_charm : Generate the m2m head mesh.
Source code in tit/pre/utils.py
ensure_subject_dirs ¶
Create the standard BIDS directory scaffold for a subject.
Scaffolds a dicom/ folder for every supported modality so the drop
location for each one is discoverable without reading the docs.
Raises ValueError for a subject id that is not one
(:func:tit.paths.validate_subject_id). This function creates directories
from an id that may have come from a config file or a script argument, and a
../../.. id used to scaffold anat/ outside the project entirely.
Source code in tit/pre/utils.py
ensure_bidsignore ¶
ensure_bidsignore(project_dir: str) -> None
Create or extend the project's root .bidsignore.
CT has no place in BIDS yet (BEP024 is still a draft), so
anat/sub-{id}_ct.nii.gz would otherwise raise a validator error.
Existing lines are preserved -- users curate this file by hand.
Source code in tit/pre/utils.py
ensure_dataset_descriptions ¶
Create or update dataset_description.json for each dataset.
Source code in tit/pre/utils.py
build_logger ¶
build_logger(step_name: str, subject_id: str, project_dir: str, *, log_file: str | None = None, console: bool = True) -> Logger
Create a named logger with a file handler for a preprocessing step.