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
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
61 62 63 64 65 66 67 68 69 70 71 72 73 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 | |
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.
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.