Note
Go to the end to download the full example code or to run this example in your browser via Binder.
From analysis to a deployable model#
An analysis that should run every day has to be reproducible: the same
configuration, the same preprocessing and the same learned regimes. The
AnalysisPipeline standardizes the data, segments it,
learns recurring regimes from the segments of the training data and labels
new data with them. Every object can be written to a JSON or YAML
configuration; fitted models are saved with checksums, library versions and
a fingerprint of the training data, and a registry keeps their versions and
stages.
import tempfile
from pathlib import Path
import mvtsa as mv
from mvtsa import mlops
from mvtsa import segmentation as sg
# One process with three recurring regimes: the first half is the training
# record, the second half arrives later.
X, cps, states = mv.datasets.make_regime_process(
n_regimes=3, n_segments=16, length=150, random_state=0
)
half = cps[7]
X_train, X_new, cps_new = X[:half], X[half:], cps[8:] - half
workdir = Path(tempfile.mkdtemp())
Configuration as code#
pipe = mlops.AnalysisPipeline(
sg.BottomUp("normal", n_segments=8, initial_size=15), n_regimes=3
)
cfg = mlops.to_config(pipe)
print(mlops.save_config(pipe, workdir / "pipeline.yaml").read_text())
same = mlops.from_config(cfg) # an identical, unfitted pipeline
class: mvtsa.mlops.pipeline.AnalysisPipeline
params:
segmenter:
class: mvtsa.segmentation.search.BottomUp
params:
cost: normal
n_segments: 8
penalty: null
max_cost: null
min_size: null
initial_size: 15
merge_criterion: increase
cost_params: null
n_regimes: 3
standardize: zscore
descriptor: gaussian
metric: gaussian_w2
min_regime_length: 10
Fit, save, register, promote#
pipe.fit(X_train)
seg = pipe.predict(X_new)
print(
"regimes of the new data:",
seg.labels.tolist(),
"| true regimes:",
states[8:].tolist(),
)
reg = mlops.ModelRegistry(workdir / "registry")
v1 = reg.register(
"regimes",
pipe,
data=X_train,
metrics={"covering": 0.97},
tags={"plant": "demo"},
)
reg.promote("regimes", v1, "production")
print(reg.list()[["name", "version", "stage", "created"]])
production = reg.load("regimes", stage="production")
mv.viz.plot_segmentation(
X_new,
production.predict(X_new),
truth=cps_new,
title="production model on new data",
)

regimes of the new data: [1, 0, 1, 0, 1, 2, 1, 0] | true regimes: [0, 2, 0, 2, 0, 1, 0, 2]
name version stage created
0 regimes 1 production 2026-11-29T08:40:35+00:00
<Axes: title={'center': 'production model on new data'}, xlabel='time'>
Total running time of the script: (0 minutes 0.071 seconds)