Experiment tracking and model cards#

Comparing settings is an experiment, and experiments should leave a trace. The ExperimentTracker logs parameters, metrics, figures and models of every run to a local folder (and to MLflow when it is installed and requested). A model card documents what a model is for, how it was evaluated and where its limits are (Mitchell et al., 2019).

import tempfile
from pathlib import Path

import matplotlib.pyplot as plt

import mvtsa as mv
from mvtsa import mlops
from mvtsa import segmentation as sg
from mvtsa.evaluation import evaluate_segmentation

X, cps, _ = mv.datasets.make_correlation_switching(
    n_segments=5, length=150, n_variables=4, random_state=7
)
workdir = Path(tempfile.mkdtemp())

Tracked runs#

tracker = mlops.ExperimentTracker(
    workdir / "runs", experiment="pca-segmentation", use_mlflow=False
)
for q in (1, 2, 3):
    with tracker.start_run(f"q={q}") as run:
        model = sg.PCASegmenter(n_segments=5, n_components=q)
        seg = model.segment(X)
        run.log_params(mlops.to_config(model)["params"])
        run.log_metrics(
            {
                k: v
                for k, v in evaluate_segmentation(
                    cps, seg, len(X), margin=8
                ).items()
                if k in ("f1", "covering", "hausdorff")
            }
        )
        fig = mv.viz.plot_segmentation(X, seg, truth=cps).figure
        run.log_figure(fig, "segmentation.png")
        plt.close(fig)
runs = tracker.runs()
print(runs[["name", "param.n_components", "metric.f1", "metric.covering"]])
fig, ax = plt.subplots(figsize=(4.5, 3))
ax.bar(runs["name"], runs["metric.covering"], color="#285780")
ax.set_ylabel("covering")
ax.set_ylim(0, 1.05)
ax.set_title("tracked runs", fontsize=9, loc="left")
tracked runs
  name  param.n_components  metric.f1  metric.covering
0  q=2                   2       1.00         1.000000
1  q=3                   3       0.75         0.974167
2  q=1                   1       1.00         1.000000

Text(0.0, 1.0, 'tracked runs')

A model card#

card = mlops.model_card(
    sg.PCASegmenter(n_segments=5, n_components=1),
    name="PCA segmentation of unit 7",
    purpose="Detect changes of the correlation structure of four process variables.",
    data_description="Synthetic data with four correlation switches.",
    metrics={"f1": 1.0, "covering": 1.0},
    limitations="Blind to pure mean shifts; assumes one latent variable.",
    path=workdir / "MODEL_CARD.md",
)
print(card)
plt.show()
# Model card: PCA segmentation of unit 7

*Generated 2026-11-29 with mvtsa 1.0.1, numpy 2.5.5, scipy 1.18.1, sklearn 1.9.1, python 3.12.16.*

## Model
**Class:** `mvtsa.segmentation.latent.PCASegmenter`

| Parameter | Value |
|---|---|
| `n_segments` | `5` |
| `n_components` | `1` |
| `statistic` | `Q` |
| `search` | `bottomup` |
| `initial_size` | `10` |
| `aggregate` | `sum` |
| `scale` | `True` |
| `lags` | `0` |

## Intended use
Detect changes of the correlation structure of four process variables.

## Data
Synthetic data with four correlation switches.

## Evaluation
| Metric | Value |
|---|---|
| f1 | 1 |
| covering | 1 |

## Limitations and monitoring
Blind to pure mean shifts; assumes one latent variable.

Total running time of the script: (0 minutes 0.218 seconds)

Gallery generated by Sphinx-Gallery