Processing Context#
The ProcessingContext dataclass carries all
per-subject state (patient ID, config, paths, intermediate results) through
the pipeline.
Processing context for the thesis framework.
Provides a context object that carries information about the current processing state, patient data, paths, and configuration through the pipeline.
- class thesis.core.context.ProcessingContext[source]#
Bases:
objectContext object for medical imaging processing pipelines.
Carries all relevant information about a processing run, including patient ID, data paths, configuration, and intermediate results.
- Variables:
patient_id – Unique patient identifier
config – Configuration for this processing run
data_dir – Shared assets base (config
paths.assets_dir): templates, atlases, ROIs. Not a parent of input_dir/output_dir.input_dir – Per-patient input data directory (
inputs_dir/<patient_id>)output_dir – Per-patient output directory (
output_dir/<patient_id>)working_dir – Temporary working/scratch directory
metadata – Additional metadata
results – Dictionary to store intermediate results
Example
>>> ctx = ProcessingContext( ... patient_id="DTI_LDF001", ... config=my_config, ... data_dir=Path("./data") ... ) >>> ctx.add_result("registration", transform_matrix) >>> transform = ctx.get_result("registration")
- Parameters:
- config: PipelineConfig#
- add_result(key, value)[source]#
Store a processing result.
Example
>>> ctx.add_result("brain_mask", mask_path)
- get_result(key, default=None)[source]#
Retrieve a processing result.
- Parameters:
- Return type:
- Returns:
Result data or default
Example
>>> mask_path = ctx.get_result("brain_mask")
- add_metadata(key, value)[source]#
Add metadata to the context.
Example
>>> ctx.add_metadata("acquisition_date", "2025-01-15")
- get_input_path(filename)[source]#
Get full path for an input file.
- Parameters:
filename (
str) – Input filename- Return type:
- Returns:
Full path to input file
- Raises:
ValueError – If a relative filename escapes input_dir.
Example
>>> t1_path = ctx.get_input_path("DTI_LDF001_T1.nii.gz")
- get_output_path(filename, subdir=None)[source]#
Get full path for an output file.
- Parameters:
- Return type:
- Returns:
Full path to output file
- Raises:
ValueError – If a relative path escapes output_dir.
Example
>>> reg_path = ctx.get_output_path("T1_registered.nii.gz", "registration")
- get_working_path(filename)[source]#
Get full path for a temporary working file.
Example
>>> temp_path = ctx.get_working_path("temp_mask.nii.gz")
- cleanup_working_dir()[source]#
Remove all files from the working directory.
Use with caution - this deletes temporary files.
- Return type:
- list_input_files(pattern='*')[source]#
List files in the input directory.
- Parameters:
pattern (
str) – Glob pattern for matching files- Return type:
- Returns:
List of matching file paths
Example
>>> nii_files = ctx.list_input_files("*.nii.gz")
- __init__(patient_id, config, data_dir, input_dir=None, output_dir=None, working_dir=None, metadata=<factory>, results=<factory>)#
- thesis.core.context.create_context(patient_id, config, data_dir=None, **kwargs)[source]#
Factory function to create a processing context.
- Parameters:
patient_id (
str) – Patient identifierconfig (
PipelineConfig) – Configuration objectdata_dir (
Optional[Path]) – Shared assets base (reads configpaths.assets_dirif not provided)**kwargs – Additional arguments passed to ProcessingContext
- Return type:
- Returns:
Initialized ProcessingContext
Example
>>> config = load_config("default") >>> ctx = create_context("DTI_LDF001", config)