dataenginex.ml¶
ML training, model registry, drift detection, and model serving.
dataenginex.ml
¶
ML training, model registry, drift detection, serving, scheduling, and metrics.
Public API::
from dataenginex.ml import (
BaseTrainer, SklearnTrainer, TrainingResult,
ModelRegistry, ModelArtifact, ModelStage,
DriftDetector, DriftReport,
DriftScheduler, DriftMonitorConfig, DriftCheckResult,
ModelServer, PredictionRequest, PredictionResponse,
model_prediction_total, model_prediction_latency_seconds,
model_drift_psi, model_drift_alerts_total,
)
DriftDetector
¶
Detect distribution drift between a reference and current dataset.
PSI thresholds (industry standard): < 0.10 — no significant drift 0.10-0.25 — moderate drift > 0.25 — significant drift
Parameters¶
psi_threshold: PSI value above which drift is flagged (default 0.20). n_bins: Number of histogram bins for PSI calculation (default 10).
Source code in packages/dataenginex/src/dataenginex/ml/drift.py
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
check_feature(feature_name, reference, current)
¶
Check drift for a single numeric feature.
Source code in packages/dataenginex/src/dataenginex/ml/drift.py
check_dataset(reference, current)
¶
Check drift across all shared features in two datasets.
Parameters¶
reference:
Mapping of feature_name → values for the reference period.
current:
Mapping of feature_name → values for the current period.
Source code in packages/dataenginex/src/dataenginex/ml/drift.py
DriftReport
dataclass
¶
Outcome of a drift check for a single feature.
Attributes:
| Name | Type | Description |
|---|---|---|
feature_name |
str
|
Name of the feature that was checked. |
psi |
float
|
Population Stability Index value. |
drift_detected |
bool
|
Whether drift exceeds the configured threshold. |
severity |
str
|
Drift severity — |
reference_mean |
float | None
|
Mean of the reference distribution. |
current_mean |
float | None
|
Mean of the current distribution. |
reference_std |
float | None
|
Standard deviation of reference distribution. |
current_std |
float | None
|
Standard deviation of current distribution. |
details |
dict[str, Any]
|
Extra context (bins, threshold, etc.). |
checked_at |
datetime
|
Timestamp of the drift check. |
Source code in packages/dataenginex/src/dataenginex/ml/drift.py
to_dict()
¶
Serialize the drift report to a plain dictionary.
Source code in packages/dataenginex/src/dataenginex/ml/drift.py
ModelArtifact
dataclass
¶
Registry entry for a model version.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Model name (e.g. |
version |
str
|
Semantic version string. |
stage |
ModelStage
|
Current lifecycle stage. |
artifact_path |
str
|
File path to the serialised model. |
metrics |
dict[str, float]
|
Training/evaluation metrics. |
parameters |
dict[str, Any]
|
Hyper-parameters used for training. |
description |
str
|
Free-text description. |
created_at |
datetime
|
When the artifact was registered. |
promoted_at |
datetime | None
|
When the artifact was last promoted. |
tags |
list[str]
|
Arbitrary labels for filtering. |
Source code in packages/dataenginex/src/dataenginex/ml/registry.py
to_dict()
¶
Serialize the model artifact metadata to a plain dictionary.
Source code in packages/dataenginex/src/dataenginex/ml/registry.py
ModelRegistry
¶
JSON-file-backed model registry.
Parameters¶
persist_path: Path to a JSON file for persistence (optional).
Source code in packages/dataenginex/src/dataenginex/ml/registry.py
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 171 172 173 174 175 176 177 178 179 180 181 182 | |
register(artifact)
¶
Register a new model version.
Source code in packages/dataenginex/src/dataenginex/ml/registry.py
get(name, version)
¶
Return the artifact for name at version, or None.
get_latest(name)
¶
Return the most recently registered version of name.
Source code in packages/dataenginex/src/dataenginex/ml/registry.py
get_production(name)
¶
Return the model currently in production stage.
Source code in packages/dataenginex/src/dataenginex/ml/registry.py
list_models()
¶
list_versions(name)
¶
promote(name, version, target_stage)
¶
Promote a model version to a new stage.
If promoting to production, any existing production model is
automatically archived.
Source code in packages/dataenginex/src/dataenginex/ml/registry.py
ModelStage
¶
DriftCheckResult
dataclass
¶
Aggregated result of a drift check across all features of a model.
Attributes:
| Name | Type | Description |
|---|---|---|
model_name |
str
|
Name of the model checked. |
reports |
list[DriftReport]
|
Per-feature drift reports. |
drift_detected |
bool
|
|
max_psi |
float
|
Highest PSI score across all features. |
checked_at |
datetime
|
Timestamp of the check. |
Source code in packages/dataenginex/src/dataenginex/ml/scheduler.py
to_dict()
¶
Serialize to a plain dictionary.
Source code in packages/dataenginex/src/dataenginex/ml/scheduler.py
DriftMonitorConfig
dataclass
¶
Configuration for monitoring a single model's data drift.
Attributes:
| Name | Type | Description |
|---|---|---|
model_name |
str
|
Name of the model being monitored. |
reference_data |
dict[str, list[float]]
|
Mapping of feature_name → reference distribution values. |
psi_threshold |
float
|
PSI value above which drift is flagged (default 0.20). |
check_interval_seconds |
float
|
Seconds between consecutive checks (default 300). |
n_bins |
int
|
Number of histogram bins for PSI calculation (default 10). |
Source code in packages/dataenginex/src/dataenginex/ml/scheduler.py
DriftScheduler
¶
Background scheduler for periodic model drift checks.
Runs a daemon thread that iterates registered monitors and
invokes DriftDetector when each monitor's interval has elapsed.
Results are published to Prometheus gauges and counters.
Parameters¶
tick_seconds:
How often the scheduler loop wakes up to check deadlines
(default 5.0). Lower values give more precise timing
at the cost of CPU.
Source code in packages/dataenginex/src/dataenginex/ml/scheduler.py
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 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 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 | |
is_running
property
¶
Whether the scheduler thread is alive.
registered_models
property
¶
Names of all registered models.
register(config, data_fn)
¶
Register a model for periodic drift monitoring.
Parameters¶
config:
Monitor configuration (thresholds, interval, reference data).
data_fn:
Callable returning current feature data as
dict[str, list[float]].
Raises¶
ValueError:
If config.reference_data is empty.
Source code in packages/dataenginex/src/dataenginex/ml/scheduler.py
unregister(model_name)
¶
Remove a model from drift monitoring.
Parameters¶
model_name: Name of the model to unregister.
Raises¶
KeyError: If the model is not registered.
Source code in packages/dataenginex/src/dataenginex/ml/scheduler.py
start()
¶
Start the background monitoring thread.
Raises¶
RuntimeError: If the scheduler is already running.
Source code in packages/dataenginex/src/dataenginex/ml/scheduler.py
stop(timeout=10.0)
¶
Stop the background monitoring thread.
Parameters¶
timeout:
Seconds to wait for the thread to join (default 10.0).
Source code in packages/dataenginex/src/dataenginex/ml/scheduler.py
get_last_result(model_name)
¶
Return the most recent drift check result for a model.
run_check(model_name)
¶
Manually trigger a drift check for one model.
Parameters¶
model_name: Name of a registered model to check.
Raises¶
KeyError: If the model is not registered.
Returns¶
DriftCheckResult: Aggregated result with per-feature reports.
Source code in packages/dataenginex/src/dataenginex/ml/scheduler.py
ModelServer
¶
Registry-aware model server.
Loads a model from the ModelRegistry and serves predictions via
the predict method.
Parameters¶
registry:
A ModelRegistry instance (from dataenginex.ml.registry).
Source code in packages/dataenginex/src/dataenginex/ml/serving.py
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 | |
load_model(name, version, model)
¶
Register a model object for serving.
Parameters¶
name:
Model name matching registry entries.
version:
Model version.
model:
Any object with a predict(X) method.
Source code in packages/dataenginex/src/dataenginex/ml/serving.py
predict(request)
¶
Run inference for request.
Source code in packages/dataenginex/src/dataenginex/ml/serving.py
PredictionRequest
dataclass
¶
Input to the serving layer.
Attributes:
| Name | Type | Description |
|---|---|---|
model_name |
str
|
Name of the model to invoke. |
version |
str | None
|
Model version ( |
features |
list[dict[str, Any]]
|
List of feature dicts — each dict is one sample. |
request_id |
str
|
Caller-provided request ID for tracing. |
Source code in packages/dataenginex/src/dataenginex/ml/serving.py
PredictionResponse
dataclass
¶
Output from the serving layer.
Attributes:
| Name | Type | Description |
|---|---|---|
model_name |
str
|
Name of the model that produced predictions. |
version |
str
|
Version of the model used. |
predictions |
list[Any]
|
List of prediction values. |
latency_ms |
float
|
Inference latency in milliseconds. |
request_id |
str
|
Echoed request ID for tracing. |
served_at |
datetime
|
Timestamp of the prediction. |
Source code in packages/dataenginex/src/dataenginex/ml/serving.py
to_dict()
¶
Serialize the prediction response to a plain dictionary.
Source code in packages/dataenginex/src/dataenginex/ml/serving.py
BaseTrainer
¶
Bases: ABC
Abstract base class for model trainers.
Source code in packages/dataenginex/src/dataenginex/ml/training.py
SklearnTrainer
¶
Bases: BaseTrainer
scikit-learn model trainer.
Works with any sklearn estimator (or pipeline) that implements
fit, predict, and score.
Parameters¶
model_name:
Name used in model registry.
version:
Semantic version string.
estimator:
An sklearn estimator instance (e.g. RandomForestClassifier()).
Source code in packages/dataenginex/src/dataenginex/ml/training.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 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 | |
train(X_train, y_train, **params)
¶
Fit the estimator on X_train/y_train and return metrics.
Source code in packages/dataenginex/src/dataenginex/ml/training.py
evaluate(X_test, y_test)
¶
Score the fitted model on X_test/y_test and return metrics.
Source code in packages/dataenginex/src/dataenginex/ml/training.py
predict(X)
¶
Generate predictions for X using the fitted estimator.
save(path)
¶
Pickle the fitted model and its metadata to path.
Source code in packages/dataenginex/src/dataenginex/ml/training.py
load(path)
¶
Load a pickled model from path and mark as fitted.
Source code in packages/dataenginex/src/dataenginex/ml/training.py
TrainingResult
dataclass
¶
Outcome of a model training run.
Attributes:
| Name | Type | Description |
|---|---|---|
model_name |
str
|
Name of the trained model. |
version |
str
|
Semantic version of this training run. |
metrics |
dict[str, float]
|
Training metrics (e.g. |
parameters |
dict[str, Any]
|
Hyper-parameters used for training. |
duration_seconds |
float
|
Wall-clock training time. |
artifact_path |
str | None
|
Path where the model artifact is saved. |
trained_at |
datetime
|
Timestamp of training completion. |
Source code in packages/dataenginex/src/dataenginex/ml/training.py
to_dict()
¶
Serialize the training result to a plain dictionary.