Utils
bootstrap
Bootstrap resampling helpers shared by the metric and visualisation modules.
draw_bootstrap_indices
Draw all bootstrap resample indices up front, sequentially, so the random draw sequence (and thus reproducibility for a given rng state) is independent of how the resamples are later computed or dispatched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rng
|
Generator
|
Random generator the resamples are drawn from. |
required |
n_samples
|
int
|
Number of samples in the data being resampled. |
required |
n_bootstraps
|
int
|
Number of resamples to draw. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Integer array of shape (n_bootstraps, n_samples) whose rows are the resample indices, drawn with replacement. |
Source code in medpipe/utils/bootstrap.py
run_bootstraps
run_bootstraps(
worker: Callable[..., Any],
bootstrap_indices: NDArray,
*args: Any,
n_jobs: int | None,
backend: Literal["threading", "loky"],
) -> list[Any]
Apply worker(idx, *args) to every bootstrap resample, in parallel
when n_jobs allows it, and drop resamples the worker rejected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worker
|
callable
|
Module-level function taking a resample's indices followed by |
required |
bootstrap_indices
|
ndarray
|
Resample indices, one row per resample, as returned by |
required |
*args
|
Any
|
Extra positional arguments forwarded unchanged to every |
()
|
n_jobs
|
int or None
|
Number of parallel jobs (the display section's |
required |
backend
|
('threading', 'loky')
|
Joblib backend. Benchmarks showed numpy-bound resamples (ROC/PR curves) gain from threads, since process start-up and pickling outweigh their millisecond cost, while resamples dominated by Python-level loops (predictive-distribution intervals, spline fitting) only gain from processes. |
"threading"
|
Returns:
| Type | Description |
|---|---|
list
|
The non- |
Source code in medpipe/utils/bootstrap.py
io
I/O utilities module.
This module provides helper functions for reading from and writing to files, handling various common I/O tasks.
DataLoaderRegistry
Registry managing file extension mappings to DataFrame reader functions.
Source code in medpipe/utils/io.py
register
classmethod
Decorator to register a custom file reader for an extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extension
|
str
|
File extension the reader handles, with or without the leading dot (e.g., '.rds' or 'rds'). Matching is case-insensitive. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
decorator |
Callable[[Callable], Callable]
|
Decorator registering a function that accepts a file path and returns a DataFrame. |
Source code in medpipe/utils/io.py
get
classmethod
Retrieve the reader function for a given file extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extension
|
str
|
File extension, with or without the leading dot. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., DataFrame]
|
Reader function registered for the extension. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no reader is registered for the extension. |
Source code in medpipe/utils/io.py
list_registered
classmethod
List all supported file extensions.
Returns:
| Type | Description |
|---|---|
list of str
|
Registered file extensions, each with a leading dot. |
register_data_loader
Register a custom file reader function for a specific file extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extension
|
str
|
File extension starting with a dot (e.g., '.feather', '.rds'). |
required |
loader_func
|
Callable[..., Any]
|
Function accepting a file path and returning a Pandas DataFrame. |
required |
Source code in medpipe/utils/io.py
load_data
Reads a supported data file and returns its contents as a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_file
|
str or Path
|
Path to the data file to load. |
required |
**kwargs
|
Any
|
Additional keyword arguments passed to the underlying Pandas reader function
(e.g., |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
data |
DataFrame
|
Loaded tabular data. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data_file is not a str or Path. |
FileNotFoundError
|
If data_file does not exist. |
IsADirectoryError
|
If data_file is a directory rather than a file. |
ValueError
|
If data_file has an unsupported file extension. |
Source code in medpipe/utils/io.py
read_classifier_toml_configuration
read_classifier_toml_configuration(
config_file: str | Path,
) -> MedpipeClassifierConfig
Reads a MedPipe TOML configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_file
|
str | Path
|
Path to the configuration file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
config |
MedpipeClassifierConfig
|
Configuration for the pipeline. |
Source code in medpipe/utils/io.py
read_regressor_toml_configuration
read_regressor_toml_configuration(
config_file: str | Path,
) -> MedpipeRegressorConfig
Reads a MedPipe regression TOML configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_file
|
str | Path
|
Path to the configuration file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
config |
MedpipeRegressorConfig
|
Configuration for the regression pipeline. |
Source code in medpipe/utils/io.py
logger
medpipe.utils.logger
Provides centralized logging configuration for the medpipe package. Supports routing high-level logs to the console and detailed debugging logs to specific experiment artifact directories.
CompactProgressFilter
Bases: Filter
Filter that passes only high-level workflow milestone messages during compact mode.
Source code in medpipe/utils/logger.py
filter
Decide whether a log record is emitted in compact mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True for records of level WARNING or above and for messages
containing one of |
Source code in medpipe/utils/logger.py
set_verbosity
Globally configure the console log verbosity level and filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose
|
str, bool, or int
|
|
"compact"
|
Source code in medpipe/utils/logger.py
get_console_logger
Initialize and return the base console logger for the package with configurable verbosity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the sub-logger instance. |
"medpipe"
|
verbose
|
str, bool, or int
|
Console verbosity setting ("compact", "info", "debug", "quiet") |
"compact"
|
Returns:
| Type | Description |
|---|---|
Logger
|
Configured standard library Logger instance. |
Source code in medpipe/utils/logger.py
add_file_handler
add_file_handler(
logger: Logger,
log_dir: str | Path,
filename: str = "execution.log",
level: int = DEBUG,
) -> None
Attach a FileHandler to an existing logger to archive detailed run logs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
The logger instance to attach the handler to. |
required |
log_dir
|
str or Path
|
The directory where the log file should be saved (e.g., an artifact folder). |
required |
filename
|
str
|
The name of the log file. |
"execution.log"
|
level
|
int
|
The logging level threshold for the file output. |
logging.DEBUG
|
Source code in medpipe/utils/logger.py
registry
BaseRegistry
Abstract base class for creating component registries.
Every subclass automatically gets its own isolated _registry dict
and _fallback_modules list via __init_subclass__, so state never
leaks between subclasses even if they don't redeclare either
attribute themselves.
The registry is generic over T, the type of item stored in it
(e.g., instances or class types).
Source code in medpipe/utils/registry.py
register
classmethod
Decorator to register a custom item into the specific subclass registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str or None
|
The lookup key for the item. Defaults to the item's name attribute. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
decorator |
Callable[[T], T]
|
Decorator function registering the item. |
Source code in medpipe/utils/registry.py
get
classmethod
Retrieve a registered item, checking fallback modules if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name identifier of the registered item. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The registered instance or class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested item is not found in the registry or fallbacks. |
Source code in medpipe/utils/registry.py
list_registered
classmethod
Return a list of string keys registered in this custom registry.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of registered item lookup keys. |
reproducibility
medpipe.utils.reproducibility
Provides utilities for capturing runtime environment metadata, hashing configurations and datasets, and managing artifact serialization for experiment reproducibility.
ArtifactManager
Manages the creation of experiment artifact directories and metadata persistence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_artifact_dir
|
str or Path
|
Root directory where experiment runs will be saved. |
"artifacts"
|
Attributes:
| Name | Type | Description |
|---|---|---|
base_dir |
Path
|
Resolved base directory path. |
Methods:
| Name | Description |
|---|---|
create_run_directory |
Create a new versioned directory for storing experiment artifacts. |
save_json |
Saves object as a JSON file. |
saved_resolved_config |
Persist the resolved configuration dictionary as a JSON file. |
save_toml_config |
Copy the original TOML configuration file into the artifact directory. |
save_env_state |
Capture and persist the environment state to |
Source code in medpipe/utils/reproducibility.py
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 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 | |
create_run_directory
Create a new versioned directory for storing experiment artifacts.
Returns:
| Type | Description |
|---|---|
Path
|
Path to the newly created run directory (e.g., |
Source code in medpipe/utils/reproducibility.py
save_json
Saves object as a JSON file.
The destination directory is created if it does not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to save. |
required |
destination_dir
|
str or Path
|
Directory where the file will be written. |
required |
filename
|
str
|
Name of the file to save. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the saved JSON file. |
Source code in medpipe/utils/reproducibility.py
save_resolved_config
Persist the resolved configuration dictionary as a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Fully resolved configuration to save. |
required |
destination_dir
|
str or Path
|
Directory where |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the saved JSON file. |
Source code in medpipe/utils/reproducibility.py
save_toml_config
Copy the original TOML configuration file into the artifact directory.
The destination directory is created if it does not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_path
|
str or Path
|
Path to the original TOML configuration file. |
required |
destination_dir
|
str or Path
|
Directory where |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the copied TOML file. |
Source code in medpipe/utils/reproducibility.py
save_env_state
save_env_state(
destination_dir: str | Path,
config: dict[str, Any],
dataset_path: str | Path | None = None,
) -> Path
Capture and persist the environment state to env_state.json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
destination_dir
|
str or Path
|
Directory where |
required |
config
|
dict
|
Fully resolved configuration dictionary. |
required |
dataset_path
|
str or Path
|
Path to the dataset file to hash and log. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the saved JSON file. |
Source code in medpipe/utils/reproducibility.py
compute_file_hash
Compute the cryptographic hash of a file on disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str or Path
|
Path to the file to be hashed. |
required |
algorithm
|
str
|
Hashing algorithm to use (e.g., 'md5', 'sha256'). |
"sha256"
|
Returns:
| Type | Description |
|---|---|
str
|
Hexadecimal hash string representing the file contents. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified |
Source code in medpipe/utils/reproducibility.py
compute_config_hash
Generate a deterministic SHA-256 hash from a configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Fully resolved configuration dictionary. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The SHA-256 hash representing the config state. |
Source code in medpipe/utils/reproducibility.py
get_git_commit_hash
Retrieve the current Git commit hash of the repository.
Returns:
| Type | Description |
|---|---|
str or None
|
The 40-character SHA-1 commit hash, or None if Git is not installed or the code is not running inside a Git repository. |
Source code in medpipe/utils/reproducibility.py
capture_environment_state
capture_environment_state(
config: dict[str, Any],
dataset_path: str | Path | None = None,
) -> dict[str, Any]
Collect system, Python, package, and data metadata for reproducibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
Fully resolved configuration to save. |
required |
dataset_path
|
str or Path
|
Path to the dataset file to hash. If None, dataset_hash is omitted. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary containing environment and runtime metadata. |
Source code in medpipe/utils/reproducibility.py
validation
Validation functions module.
This module provides functions for validating files and paths, raising the appropriate exception when a check fails.
Functions: - file_checks: Checks if the file is correct. - path_checks: Checks if the path is correct.
file_checks
Performs checks to ensure that a file and extension are correct.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str | Path
|
File to check. |
required |
extension
|
str | list[str]
|
Extension or list of extensions of the file to check. |
required |
exists
|
bool
|
Flag to indicate if the file should exists. |
True
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If file is not a str or a Path. If extension is not a str or list[str]. |
FileNotFoundError
|
If file does not exist. |
IsADirectoryError
|
If file is a directory. |
ValueError
|
If file extension is not correct. |
Source code in medpipe/utils/validation.py
path_checks
Performs checks to ensure that a path is correct and creates it if it does not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to check. |
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If path is not a str or Path. |
FileNotFoundError
|
If path does not exist. |
NotADirectoryError
|
If path is not a directory. |