ML API¶
Datasets & loaders¶
edl_ml.ml.dataset ¶
Torch dataset helpers for the capacitance surrogate.
StandardScalerTensor
dataclass
¶
Simple mean/std standardiser stored as tensors.
Fitting is performed on the training set so no test information leaks into the normalisation statistics.
Source code in src/edl_ml/ml/dataset.py
fit
classmethod
¶
Fit mean/std on the given 2D array and return a scaler.
Source code in src/edl_ml/ml/dataset.py
inverse_transform ¶
CapacitanceDataset ¶
Bases: Dataset[tuple[Tensor, Tensor]]
PyTorch dataset wrapping a long-format capacitance DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame produced by
:func: |
required |
x_scaler
|
StandardScalerTensor
|
Fitted scaler for the input features. Required. |
required |
y_scaler
|
StandardScalerTensor
|
Fitted scaler for the target. Required. |
required |
Source code in src/edl_ml/ml/dataset.py
LoaderBundle
dataclass
¶
Container for the three dataloaders and the fitted scalers.
Attributes:
| Name | Type | Description |
|---|---|---|
train, val, test |
DataLoaders for each split. |
|
x_scaler, y_scaler |
Scalers fitted on the training split only. |
Source code in src/edl_ml/ml/dataset.py
build_loaders ¶
build_loaders(train_df: DataFrame, val_df: DataFrame, test_df: DataFrame, batch_size: int = 256, num_workers: int = 0, seed: int | None = 0) -> LoaderBundle
Fit scalers on train_df and wrap each split in a DataLoader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_df
|
DataFrame
|
Splits returned by
:func: |
required |
val_df
|
DataFrame
|
Splits returned by
:func: |
required |
test_df
|
DataFrame
|
Splits returned by
:func: |
required |
batch_size
|
int
|
Mini-batch size. |
256
|
num_workers
|
int
|
DataLoader workers. Keep at 0 on macOS MPS. |
0
|
seed
|
int | None
|
Seed for the training shuffle generator. |
0
|
Source code in src/edl_ml/ml/dataset.py
Model¶
edl_ml.ml.model ¶
Torch MLP surrogate for the Gouy-Chapman-Stern solver.
CapacitanceMLP ¶
Bases: Module
Fully connected regressor predicting the scaled capacitance.
The network consumes a feature vector of physical parameters concatenated with the electrode potential and emits a single scalar (the standardised differential capacitance at that potential).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
MLPConfig
|
Hyperparameters. |
required |
Source code in src/edl_ml/ml/model.py
MLPConfig
dataclass
¶
Hyperparameters of the capacitance MLP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Number of input features. Equal to |
6
|
hidden_dims
|
tuple[int, ...]
|
Sequence of hidden-layer widths. |
(128, 128, 64)
|
activation
|
str
|
One of |
'silu'
|
dropout
|
float
|
Probability for the dropout applied after each hidden layer. |
0.05
|
use_batch_norm
|
bool
|
Whether to insert a BatchNorm layer between linear and activation. |
False
|
Source code in src/edl_ml/ml/model.py
Training¶
edl_ml.ml.train ¶
Training loop for the capacitance surrogate with optional MLflow logging.
TrainConfig
dataclass
¶
Training hyperparameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Standard optimiser/loop controls. |
0.001
|
weight_decay
|
float
|
Standard optimiser/loop controls. |
0.001
|
max_epochs
|
float
|
Standard optimiser/loop controls. |
0.001
|
batch_size
|
float
|
Standard optimiser/loop controls. |
0.001
|
patience
|
int
|
Early-stopping patience in epochs of unimproved validation loss. |
20
|
grad_clip
|
float
|
Gradient-norm clip threshold; set to 0 to disable. |
1.0
|
device
|
str
|
|
'auto'
|
seed
|
int
|
Seed for torch RNGs. |
0
|
mlflow_experiment
|
str | None
|
If not |
None
|
mlflow_tracking_uri
|
str | None
|
Optional MLflow tracking URI; defaults to local |
None
|
Source code in src/edl_ml/ml/train.py
TrainReport
dataclass
¶
Summary of a training run.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
CapacitanceMLP
|
The trained model (loaded with the best validation checkpoint). |
best_val_loss |
float
|
Minimum validation loss observed. |
train_losses, val_losses |
Per-epoch training and validation mean squared error on the scaled target. |
|
test_metrics |
dict[str, float]
|
Dictionary of metrics evaluated on the test set in unscaled units
(µF/cm²): |
x_scaler, y_scaler |
Scalers used during training; persisted alongside the model for deployment-time inference. |
Source code in src/edl_ml/ml/train.py
train_model ¶
train_model(loaders: LoaderBundle, model_config: MLPConfig, train_config: TrainConfig, *, checkpoint_path: Path | str | None = None) -> TrainReport
Train the capacitance MLP and return a :class:TrainReport.
Supports optional MLflow logging. A best-on-validation checkpoint is kept
in memory and optionally serialised to checkpoint_path. Early
stopping triggers when the validation loss fails to improve for
train_config.patience consecutive epochs.
Source code in src/edl_ml/ml/train.py
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 | |
Hyperparameter search¶
edl_ml.ml.tune ¶
Optuna hyperparameter search for the capacitance surrogate.
TuneConfig
dataclass
¶
Controls for the Optuna study.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_trials
|
int
|
Number of hyperparameter trials. |
40
|
timeout_seconds
|
float | None
|
Optional wall-clock cap. |
None
|
max_epochs
|
int
|
Upper bound on training epochs inside each trial. |
150
|
patience
|
int
|
Early-stopping patience inside each trial. |
15
|
study_name
|
str
|
Optuna study identifiers. |
'edl-ml-surrogate'
|
storage
|
str
|
Optuna study identifiers. |
'edl-ml-surrogate'
|
direction
|
str
|
|
'minimize'
|
seed
|
int
|
RNG seed controlling the TPE sampler. |
0
|
Source code in src/edl_ml/ml/tune.py
run_optuna_study ¶
run_optuna_study(loaders: LoaderBundle, input_dim: int, tune_config: TuneConfig, base_train_config: TrainConfig | None = None) -> Any
Launch an Optuna TPE study optimising validation MSE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loaders
|
LoaderBundle
|
Pre-built data loaders (train/val/test + scalers). |
required |
input_dim
|
int
|
Number of input features. |
required |
tune_config
|
TuneConfig
|
Study configuration. |
required |
base_train_config
|
TrainConfig | None
|
Template training config; only learning-rate / weight-decay are overridden by the sampler. |
None
|
Returns:
| Type | Description |
|---|---|
Study
|
The completed study; best trial accessible via |
Source code in src/edl_ml/ml/tune.py
Explanations¶
edl_ml.ml.explain ¶
SHAP and permutation-based explanations for the capacitance surrogate.
ShapResult
dataclass
¶
Output of a SHAP explanation run.
Attributes:
| Name | Type | Description |
|---|---|---|
values |
NDArray[float64]
|
SHAP values in unscaled capacitance units, shape
|
features |
NDArray[float64]
|
Corresponding raw feature values, shape |
feature_names |
tuple[str, ...]
|
Names of the features in column order. |
base_value |
float
|
The expected model output (mean over the background set) in the same
units as |
Source code in src/edl_ml/ml/explain.py
permutation_feature_importance ¶
permutation_feature_importance(model: CapacitanceMLP, features: NDArray[float64], targets: NDArray[float64], x_scaler: StandardScalerTensor, y_scaler: StandardScalerTensor, *, n_repeats: int = 20, seed: int = 0) -> NDArray[np.float64]
Return mean permutation feature importance (increase in MAE per feature).
Useful as a lightweight, no-external-dependency alternative to SHAP. Each
feature column is independently shuffled n_repeats times and the mean
increase in mean-absolute error (µF/cm²) is recorded.
Source code in src/edl_ml/ml/explain.py
shap_explain ¶
shap_explain(model: CapacitanceMLP, background: NDArray[float64], samples: NDArray[float64], x_scaler: StandardScalerTensor, y_scaler: StandardScalerTensor, *, nsamples: int = 200) -> ShapResult
Compute KernelSHAP values for the surrogate on raw feature space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
CapacitanceMLP
|
Trained :class: |
required |
background
|
NDArray[float64]
|
Background dataset, shape |
required |
samples
|
NDArray[float64]
|
Samples to explain, shape |
required |
x_scaler
|
StandardScalerTensor
|
Scalers used when training |
required |
y_scaler
|
StandardScalerTensor
|
Scalers used when training |
required |
nsamples
|
int
|
Number of coalitions used by KernelSHAP per sample. |
200
|
Returns:
| Type | Description |
|---|---|
ShapResult
|
Results in unscaled output units. |