Utilities Module¶
Helper functions and utilities for leech.
Overview¶
The util module provides various helper functions for model loading, metrics computation, and more.
Model Loading¶
load_model_from_checkpoint
¶
load_model_from_checkpoint(checkpoint_path: Path, device: str = 'cuda', checkpoint_name: str = 'model_best.pt') -> tuple[nn.Module, dict]
Load a trained model from checkpoint directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint_path
|
Path
|
Path to checkpoint directory (contains config.json and .pt files) |
required |
device
|
str
|
Device to load model on |
'cuda'
|
checkpoint_name
|
str
|
Name of checkpoint file (default: model_best.pt) |
'model_best.pt'
|
Returns:
| Type | Description |
|---|---|
tuple[Module, dict]
|
Tuple of (model, config_dict) |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If config.json or checkpoint file not found |
ValueError
|
If config is invalid |
Source code in src/leech/model_loading.py
Metrics Computation¶
compute_metrics
¶
Compute classification metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
ndarray
|
True labels (binary) |
required |
y_pred
|
ndarray
|
Predicted labels (binary) |
required |
y_prob
|
ndarray
|
Predicted probabilities (0-1) |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with metrics: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If input arrays are empty or have mismatched lengths |
Source code in src/leech/metrics.py
save_metrics
¶
Save metrics to JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
dict
|
Dictionary of metrics |
required |
output_path
|
Path
|
Output file path |
required |
Source code in src/leech/metrics.py
print_metrics
¶
Pretty print metrics to console using Rich tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
dict
|
Dictionary of metrics |
required |
Source code in src/leech/metrics.py
Reproducibility¶
setup_random_seed
¶
Setup random seed for reproducibility and optionally save to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Random seed value, or None to generate one |
required |
output_dir
|
Path | None
|
Directory to save seed.txt file, or None to skip saving |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The seed value used |