.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/10_mlops/plot_39_registry_monitoring.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_10_mlops_plot_39_registry_monitoring.py: Registry, model card and drift monitoring ========================================= An equation in production needs the same care as any model. This example registers a soft-sensor equation with its data fingerprint and validity domain (:class:`~eqdisco.mlops.ModelRegistry`), writes its model card (:class:`~eqdisco.mlops.ModelCard`; Mitchell et al., 2019), and then monitors a stream of new data in which the process slowly drifts: the population stability index of the inputs, the share of samples outside the training box, and a Page--Hinkley test on the residuals (:class:`~eqdisco.mlops.DriftMonitor`). .. GENERATED FROM PYTHON SOURCE LINES 13-27 .. code-block:: Python import tempfile import matplotlib.pyplot as plt import numpy as np import eqdisco as eq from eqdisco.mlops import DriftMonitor, ModelCard, ModelRegistry rng = np.random.default_rng(1) X = np.column_stack([rng.uniform(300, 360, 300), rng.uniform(1, 5, 300)]) y = 0.02 * X[:, 0] * np.sqrt(X[:, 1]) + rng.normal(0, 0.05, 300) model = eq.SymbolicRegressor(population_size=200, generations=20, functions=("add", "sub", "mul", "sqrt"), feature_names=["T", "p"], random_state=0).fit(X, y) .. GENERATED FROM PYTHON SOURCE LINES 28-30 Registry and model card ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-36 .. code-block:: Python reg = ModelRegistry(tempfile.mkdtemp()) v = reg.register("quality-sensor", model.equation_, X=X, y=y, tags={"unit": "reactor 2"}) reg.promote("quality-sensor", v, "production") card = ModelCard(model.equation_, "quality-sensor", intended_use="estimate product quality every minute", X=X, y=y) print(reg.list()) print(card.to_markdown()[:600]) .. rst-class:: sphx-glr-script-out .. code-block:: none name version ... metric_aic metric_bic 0 quality-sensor 1 ... -898.916268 -887.80492 [1 rows x 11 columns] # Model card: quality-sensor *Created 2026-11-29T08:17:12+00:00* ## Model $$y = 0.01403 \sqrt{T p \left(\sqrt{T} + 2 T - p\right)} - 0.04318$$ - Method: SymbolicRegressor - Variables: T, p - Complexity: 17 nodes, 2 parameters ## Intended use estimate product quality every minute ## Performance | metric | value | |---|---| | mse | 0.00286764 | | rmse | 0.0535504 | | mae | 0.0425805 | | r2 | 0.999449 | | aic | -898.916 | | bic | -887.805 | ## Validity domain (training data range) | variable | min | max | |---|---|---| | T | 300.349 | 359.942 | | p | 1.00823 | 4.96956 | Training sample .. GENERATED FROM PYTHON SOURCE LINES 37-39 Monitoring a drifting process ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 39-66 .. code-block:: Python monitor = DriftMonitor(reg.load("quality-sensor", stage="production"), X, y) reports = [] for week in range(12): drift = max(0, week - 5) * 6.0 # the temperature rises after week 5 Xw = np.column_stack([rng.uniform(300, 360, 100) + drift, rng.uniform(1, 5, 100)]) yw = 0.02 * Xw[:, 0] * np.sqrt(Xw[:, 1]) * (1 + 0.004 * max(0, week - 5)) + rng.normal(0, 0.05, 100) reports.append(monitor.check(Xw, yw)) print([r["status"] for r in reports]) fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) psi = [r["features"]["T"]["psi"] for r in reports] out = [r["outside_domain"] for r in reports] sdr = [r["residual_sd_ratio"] for r in reports] weeks = np.arange(12) colors = {"ok": "#009E73", "warning": "#E69F00", "retrain": "#D55E00"} axs[0].bar(weeks, psi, color=[colors[r["status"]] for r in reports]) thr_w, thr_r = reports[0]["psi_thresholds"] # 95 % and 99 % points for 300 vs 100 samples axs[0].axhline(thr_w, color="#E69F00", ls="--", lw=0.8) axs[0].axhline(thr_r, color="#D55E00", ls="--", lw=0.8) axs[0].set_xlabel("week") axs[0].set_ylabel("PSI of T (bar color: status)") axs[1].plot(weeks, out, "o-", color="#285780", label="share outside training box") axs[1].plot(weeks, sdr, "s-", color="#D55E00", label="residual sd / reference") axs[1].set_xlabel("week") axs[1].legend(frameon=False, fontsize=8) fig.tight_layout() eq.viz.plot_drift(reports[-1]) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/10_mlops/images/sphx_glr_plot_39_registry_monitoring_001.png :alt: plot 39 registry monitoring :srcset: /auto_examples/10_mlops/images/sphx_glr_plot_39_registry_monitoring_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/10_mlops/images/sphx_glr_plot_39_registry_monitoring_002.png :alt: outside training box: 56.0 % · status: retrain :srcset: /auto_examples/10_mlops/images/sphx_glr_plot_39_registry_monitoring_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'retrain', 'retrain', 'retrain', 'retrain', 'retrain', 'retrain'] .. GENERATED FROM PYTHON SOURCE LINES 67-69 Parameter study: the Page--Hinkley threshold -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 69-84 .. code-block:: Python lams = [2, 5, 10, 20, 40] delay = [] for lam in lams: mon = DriftMonitor(model.equation_, X, y, ph_lambda=lam) Xs = np.column_stack([rng.uniform(300, 360, 400), rng.uniform(1, 5, 400)]) ys = 0.02 * Xs[:, 0] * np.sqrt(Xs[:, 1]) + rng.normal(0, 0.05, 400) ys[200:] += 0.1 # a sudden offset at sample 200 alarm, at = mon.page_hinkley(ys - model.equation_.predict(Xs)) delay.append((at - 200) if alarm and at is not None and at >= 200 else np.nan) print("Page-Hinkley threshold", lams, "detection delay", delay) fig, ax = plt.subplots(figsize=(4.5, 3)) ax.plot(lams, delay, "o-", color="#285780") ax.set_xlabel("Page-Hinkley threshold $\\lambda$") ax.set_ylabel("detection delay (samples)") plt.show() .. image-sg:: /auto_examples/10_mlops/images/sphx_glr_plot_39_registry_monitoring_003.png :alt: plot 39 registry monitoring :srcset: /auto_examples/10_mlops/images/sphx_glr_plot_39_registry_monitoring_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Page-Hinkley threshold [2, 5, 10, 20, 40] detection delay [nan, 4, 6, 14, 30] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.891 seconds) .. _sphx_glr_download_auto_examples_10_mlops_plot_39_registry_monitoring.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/eqdisco/main?urlpath=lab/tree/notebooks/auto_examples/10_mlops/plot_39_registry_monitoring.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_39_registry_monitoring.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_39_registry_monitoring.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_39_registry_monitoring.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_