pre
tit.pre ¶
Preprocessing APIs for TI-Toolbox.
This package provides standalone, reusable functions for every stage of the TI-Toolbox preprocessing pipeline: DICOM conversion, FreeSurfer cortical reconstruction, SimNIBS head-mesh generation (CHARM), tissue analysis, and diffusion-weighted imaging (DWI) preprocessing via QSIPrep/QSIRecon.
All heavy computation runs inside Docker containers (SimNIBS, FreeSurfer, QSIPrep/QSIRecon). The functions in this package orchestrate those containers and manage BIDS-compliant file layouts.
Public API¶
run_pipeline
Full preprocessing pipeline orchestrator (sequential or parallel).
run_dicom_to_nifti
Convert DICOM series to BIDS-compliant NIfTI files.
run_recon_all
Run FreeSurfer recon-all cortical reconstruction.
run_subcortical_segmentations
Run thalamic-nuclei and hippocampal-subfield segmentations.
run_charm
Generate a SimNIBS head mesh via the charm command.
run_tissue_analysis
Compute tissue volumes and thickness from segmented NIfTI data.
run_qsiprep
Preprocess DWI data using QSIPrep (Docker-out-of-Docker).
run_qsirecon
Reconstruct DWI data using QSIRecon (Docker-out-of-Docker).
extract_dti_tensor
Extract a DTI tensor for SimNIBS anisotropic conductivity.
discover_subjects
Discover subject IDs present in a BIDS project tree.
check_m2m_exists
Check whether a SimNIBS m2m directory already exists.
See Also¶
tit.pre.qsi : QSI subpackage for DWI preprocessing and reconstruction. tit.sim : Simulation engine that consumes preprocessing outputs.
run_pipeline ¶
run_pipeline(subject_ids: Iterable[str], *, convert_dicom: bool = False, run_recon: bool = False, parallel_recon: bool = False, parallel_cores: int | None = None, create_m2m: bool = False, run_tissue_analysis: bool = False, run_qsiprep: bool = False, run_qsirecon: bool = False, qsiprep_config: dict | None = None, qsi_recon_config: dict | None = None, extract_dti: bool = False, run_subcortical_segmentations: bool = False, debug: bool = False, stop_event: object | None = None, logger_callback: Callable | None = None, runner: CommandRunner | None = None) -> int
Run the preprocessing pipeline for one or more subjects.
Orchestrates DICOM conversion, FreeSurfer recon-all, SimNIBS CHARM, tissue analysis, QSIPrep/QSIRecon DWI preprocessing, DTI tensor extraction, and subcortical segmentation. Steps are enabled via boolean flags; disabled steps are skipped.
Parameters¶
subject_ids : iterable of str
Subject identifiers without the sub- prefix.
convert_dicom : bool, optional
Run DICOM-to-NIfTI conversion.
run_recon : bool, optional
Run FreeSurfer recon-all.
parallel_recon : bool, optional
Run recon-all in parallel across subjects.
parallel_cores : int or None, optional
Maximum number of parallel subjects for recon-all.
create_m2m : bool, optional
Run SimNIBS charm (also runs subject_atlas for .annot
files).
run_tissue_analysis : bool, optional
Run tissue-volume and thickness analysis.
run_qsiprep : bool, optional
Run QSIPrep DWI preprocessing via Docker.
run_qsirecon : bool, optional
Run QSIRecon reconstruction via Docker.
qsiprep_config : dict or None, optional
Extra configuration passed to run_qsiprep.
qsi_recon_config : dict or None, optional
Extra configuration passed to run_qsirecon.
extract_dti : bool, optional
Extract DTI tensor for SimNIBS anisotropic conductivity.
run_subcortical_segmentations : bool, optional
Run thalamic-nuclei and hippocampal-subfield segmentations.
debug : bool, optional
Enable verbose logging.
stop_event : object or None, optional
Threading event used to cancel running steps.
logger_callback : callable or None, optional
Callback used by the GUI to capture log lines.
runner : CommandRunner or None, optional
Subprocess runner used to stream command output.
Returns¶
int
0 on success, 1 on failure.
Raises¶
PreprocessError If no subjects are provided or a preprocessing step fails. PreprocessCancelled If stop_event is set during execution.
See Also¶
run_dicom_to_nifti : DICOM-to-NIfTI conversion step. run_recon_all : FreeSurfer recon-all step. run_charm : SimNIBS CHARM head-mesh step. run_tissue_analysis : Tissue analysis step. run_qsiprep : QSIPrep DWI preprocessing step. run_qsirecon : QSIRecon reconstruction step. extract_dti_tensor : DTI tensor extraction step.
Source code in tit/pre/structural.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
run_dicom_to_nifti ¶
run_dicom_to_nifti(project_dir: str, subject_id: str, *, logger, runner: CommandRunner | None = None) -> None
Convert DICOM files to BIDS-compliant NIfTI for a subject.
Looks for T1w and T2w DICOM directories under
sourcedata/sub-{subject_id}/ and converts each found modality
using dcm2niix. DICOM discovery is recursive under each
modality's dicom/ directory and includes .dcm and .dicom
files. Supported archives (.zip, .tar, .tar.gz, .tgz)
placed directly in the modality folder or its dicom/ folder are
safely extracted to dicom/extracted_archives/ before discovery.
Parameters¶
project_dir : str
BIDS project root directory.
subject_id : str
Subject identifier without the sub- prefix.
logger : logging.Logger
Logger for progress messages.
runner : CommandRunner or None, optional
Subprocess runner for streaming output.
Raises¶
PreprocessError If output NIfTI files already exist for a modality.
See Also¶
run_pipeline : Full preprocessing pipeline.
Source code in tit/pre/dicom2nifti.py
run_recon_all ¶
run_recon_all(project_dir: str, subject_id: str, *, logger, parallel: bool = False, runner: CommandRunner | None = None) -> None
Run FreeSurfer recon-all for a subject.
Runs the full recon-all -all pipeline and, upon success,
automatically runs thalamic-nuclei and hippocampal-subfield
segmentations.
Parameters¶
project_dir : str
BIDS project root.
subject_id : str
Subject identifier without the sub- prefix.
logger : logging.Logger
Logger used for progress and command output.
parallel : bool, optional
Use FreeSurfer OpenMP parallelization.
runner : CommandRunner or None, optional
Subprocess runner used to stream output.
Raises¶
PreprocessError
If no T1 file is found, the output directory already exists, or
recon-all exits with a non-zero code.
See Also¶
run_subcortical_segmentations : Standalone subcortical segmentation. run_charm : SimNIBS CHARM head-mesh generation.
Source code in tit/pre/recon_all.py
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 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
run_subcortical_segmentations ¶
run_subcortical_segmentations(project_dir: str, subject_id: str, *, logger, runner: CommandRunner | None = None) -> None
Run thalamic-nuclei and hippocampal-subfield segmentations standalone.
Resolves the FreeSurfer subjects directory from the project layout and
delegates to the internal segmentation runner. Intended for cases where
recon-all has already completed and only the subcortical step needs
to be (re-)run.
Parameters¶
project_dir : str
BIDS project root.
subject_id : str
Subject identifier without the sub- prefix.
logger : logging.Logger
Logger for progress output.
runner : CommandRunner or None, optional
Subprocess runner for streaming output.
See Also¶
run_recon_all : Full FreeSurfer recon-all (includes subcortical).
run_pipeline : Full preprocessing pipeline.
Source code in tit/pre/recon_all.py
run_charm ¶
run_charm(project_dir: str, subject_id: str, *, logger, runner: CommandRunner | None = None) -> None
Run SimNIBS charm to generate a head mesh for a subject.
Creates an m2m directory at the standard BIDS derivatives location containing the volumetric head model required for TI simulations.
Parameters¶
project_dir : str
BIDS project root.
subject_id : str
Subject identifier without the sub- prefix.
logger : logging.Logger
Logger used for progress and command output.
runner : CommandRunner or None, optional
Subprocess runner used to stream output.
Raises¶
PreprocessError
If no T1 image is found, the m2m directory already exists, or
charm exits with a non-zero code.
See Also¶
run_subject_atlas : Create atlas .annot files after CHARM.
run_recon_all : FreeSurfer cortical reconstruction.
Source code in tit/pre/charm.py
run_tissue_analysis ¶
run_tissue_analysis(project_dir: str, subject_id: str, *, tissues: Iterable[str] = DEFAULT_TISSUES, logger: Logger, runner: CommandRunner | None = None) -> dict
Run tissue analysis for a subject.
Creates a TissueAnalyzer for each requested tissue type, computes
volume and thickness statistics, and writes reports and visualizations.
Parameters¶
project_dir : str
BIDS project root.
subject_id : str
Subject identifier without the sub- prefix.
tissues : iterable of str, optional
Tissue types to analyze. Defaults to ('bone', 'csf', 'skin').
logger : logging.Logger
Logger for progress output.
runner : CommandRunner or None, optional
Not used; kept for API compatibility with the pipeline runner.
Returns¶
dict Mapping of tissue name to per-tissue analysis results (volume, thickness, voxel counts, report path).
Raises¶
PreprocessError If the segmentation labeling file does not exist.
See Also¶
TissueAnalyzer : Low-level analysis for a single tissue type. run_pipeline : Full preprocessing pipeline.
Source code in tit/pre/tissue_analyzer.py
run_qsiprep ¶
run_qsiprep(project_dir: str, subject_id: str, *, logger: Logger, output_resolution: float = QSI_DEFAULT_OUTPUT_RESOLUTION, cpus: int | None = None, memory_gb: int | None = None, omp_threads: int = QSI_DEFAULT_OMP_THREADS, image_tag: str = QSI_QSIPREP_IMAGE_TAG, skip_bids_validation: bool = True, denoise_method: str = 'dwidenoise', unringing_method: str = 'mrdegibbs', runner: CommandRunner | None = None) -> None
Run QSIPrep preprocessing for a subject's DWI data.
This function spawns a QSIPrep Docker container as a sibling to the current SimNIBS container using Docker-out-of-Docker (DooD).
Parameters¶
project_dir : str
Path to the BIDS project root directory.
subject_id : str
Subject identifier (without 'sub-' prefix).
logger : logging.Logger
Logger for status messages.
output_resolution : float, optional
Target output resolution in mm. Default: 2.0.
cpus : int, optional
Number of CPUs to allocate. Default: 8.
memory_gb : int, optional
Memory limit in GB. Default: 32.
omp_threads : int, optional
Number of OpenMP threads. Default: 1.
image_tag : str, optional
QSIPrep Docker image tag. Default from constants.QSI_QSIPREP_IMAGE_TAG.
skip_bids_validation : bool, optional
Skip BIDS validation. Default: True.
denoise_method : str, optional
Denoising method. Default: 'dwidenoise'.
unringing_method : str, optional
Unringing method. Default: 'mrdegibbs'.
runner : CommandRunner | None, optional
Command runner for subprocess execution.
Raises¶
PreprocessError If QSIPrep fails or prerequisites are not met.
Source code in tit/pre/qsi/qsiprep.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 | |
run_qsirecon ¶
run_qsirecon(project_dir: str, subject_id: str, *, logger: Logger, recon_specs: list[str] | None = None, atlases: list[str] | None = None, use_gpu: bool = False, cpus: int | None = None, memory_gb: int | None = None, omp_threads: int = QSI_DEFAULT_OMP_THREADS, image_tag: str = QSI_QSIRECON_IMAGE_TAG, skip_odf_reports: bool = True, runner: CommandRunner | None = None) -> None
Run QSIRecon reconstruction for a subject's preprocessed DWI data.
This function spawns QSIRecon Docker containers as siblings to the current SimNIBS container using Docker-out-of-Docker (DooD).
QSIRecon requires QSIPrep output as input. Multiple reconstruction specs can be run sequentially.
Parameters¶
project_dir : str
Path to the BIDS project root directory.
subject_id : str
Subject identifier (without 'sub-' prefix).
logger : logging.Logger
Logger for status messages.
recon_specs : list[str] | None, optional
List of reconstruction specifications to run. Default: ['dsi_studio_gqi'].
This default produces DTI tensors for SimNIBS anisotropic modeling.
Other specs (mrtrix_, dipy_, amico_noddi, pyafq_*, etc.) remain available.
atlases : list[str] | None, optional
List of atlases for connectivity analysis. Default: None (no connectivity).
Not needed for DTI extraction. Set to e.g. ['4S156Parcels', 'AAL116']
if connectivity matrices are desired.
use_gpu : bool, optional
Enable GPU acceleration. Default: False.
cpus : int | None, optional
Number of CPUs to allocate. None = inherit from current container.
memory_gb : int | None, optional
Memory limit in GB. None = inherit from current container.
omp_threads : int, optional
Number of OpenMP threads. Default: 1.
image_tag : str, optional
QSIRecon Docker image tag. Default from constants.QSI_QSIRECON_IMAGE_TAG.
skip_odf_reports : bool, optional
Skip ODF report generation. Default: True.
runner : CommandRunner | None, optional
Command runner for subprocess execution.
Raises¶
PreprocessError If QSIRecon fails or prerequisites are not met.
Source code in tit/pre/qsi/qsirecon.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 160 161 162 163 164 165 166 167 168 169 170 | |
extract_dti_tensor ¶
extract_dti_tensor(project_dir: str, subject_id: str, *, logger: Logger, skip_registration: bool = False) -> Path
Extract and register a DTI tensor from QSIRecon DSI Studio output.
Loads the six tensor components produced by DSI Studio GQI,
validates them, registers the tensor to the SimNIBS T1 grid
(with FSL-convention pre-compensation), saves
DTI_coregT1_tensor.nii.gz into the m2m directory, and
generates a QC report.
Parameters¶
project_dir : str
BIDS project root directory.
subject_id : str
Subject identifier (e.g. '070').
logger : logging.Logger
Logger instance for progress and diagnostic messages.
skip_registration : bool, optional
When True, copy the tensor as-is without resampling or
reorientation. Default is False.
Returns¶
pathlib.Path
Path to the saved DTI_coregT1_tensor.nii.gz.
Raises¶
tit.pre.utils.PreprocessError If required inputs are missing, the tensor already exists, or the tensor data is invalid.
Source code in tit/pre/qsi/dti_extractor.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
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
59 60 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 | |
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.