Shape changes: ClaSP and FLUSS#

When the segments differ in the shape of repeating patterns rather than in their statistics, subsequence methods work best. FLUSS (Gharghabi et al., 2017) counts the nearest-neighbour arcs of the matrix profile that cross every position: few crossings mean a boundary. ClaSP (Ermshaus et al., 2023) scores every split by how well a k-nearest-neighbour classifier can tell the subsequences of the two sides apart.

import matplotlib.pyplot as plt
import numpy as np

from mvtsa import segmentation as sg
from mvtsa import viz

rng = np.random.default_rng(3)
t = np.arange(1200)
x = (
    np.r_[
        np.sin(2 * np.pi * t[:400] / 40),  # sine
        2 * (t[400:800] % 40) / 40 - 1,  # sawtooth
        np.sign(np.sin(2 * np.pi * t[800:] / 40)),
    ]
    + 0.1 * rng.normal(size=1200)
)  # square
cps = [400, 800]

ClaSP profile#

clasp = sg.ClaSPSegmenter(width=40, n_segments=3)
seg = clasp.segment(x)
print("ClaSP:", seg.change_points, "| width", clasp.width_)
fig, axes = plt.subplots(3, 1, figsize=(7, 5.5), sharex=True)
viz.plot_segmentation(x, seg, truth=cps, ax=axes[0])
viz.plot_profile(
    clasp.profile_, seg.change_points, ax=axes[1], label="ClaSP score"
)
plot 03 clasp fluss
ClaSP: [397 816] | width 40

<Axes: xlabel='time', ylabel='ClaSP score'>

FLUSS corrected arc curve#

fluss = sg.FLUSSSegmenter(width=40, n_segments=3)
print("FLUSS:", fluss.segment(x).change_points)
viz.plot_profile(
    fluss.cac_, fluss.segmentation_.change_points, ax=axes[2], label="CAC"
)
FLUSS: [396 816]

<Axes: xlabel='time', ylabel='CAC'>

Parameter study: the subsequence width#

width="auto" estimates the period from the autocorrelation.

for w in (10, 20, 40, 80, "auto"):
    c = sg.ClaSPSegmenter(width=w, n_segments=3)
    print(f"width {w!s:5s}: {c.segment(x).change_points}")
plt.show()
width 10   : [390 811]
width 20   : [392 794]
width 40   : [397 816]
width 80   : [814]
width auto : [397 816]

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

Gallery generated by Sphinx-Gallery