Getting started#
This quick guide shows how to install tsseg, load a time series and run a
segmentation detector. The instructions assume Python 3.10+ and a virtual
environment.
Installation#
From PyPI, in a virtual environment:
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install tsseg
Detectors built on heavier libraries need an extra, such as tsseg[torch], or
tsseg[all] for all of them (see Installation).
From source, for development:
git clone https://github.com/fchavelli/tsseg.git
cd tsseg
python -m venv .venv
source .venv/bin/activate
pip install -e ".[all,dev]"
make install runs these steps in .venv. With conda, make install-conda
creates the tsseg-env environment from environment.yml instead.
Basic usage#
Change point detection with PeltDetector, which needs no optional
dependency and does not need to know the number of change points:
import numpy as np
from tsseg.algorithms import PeltDetector
# Toy signal: three segments with different means
rng = np.random.default_rng(42)
seg_lengths = [200, 150, 250]
segments = [
rng.normal(mu, 0.2, length) for mu, length in zip([-0.5, 0.8, 0.0], seg_lengths)
]
series = np.concatenate(segments)[:, None] # shape (n_points, n_channels)
change_points = PeltDetector(pen_scale="bic").fit_predict(series)
print(change_points) # first index of each new segment: [200 350]
State detection returns one label per time point instead. With
GaussianHMMDetector, the number of states is chosen by BIC:
from tsseg.algorithms import GaussianHMMDetector
states = GaussianHMMDetector(random_state=0).fit_predict(series)
print(np.unique(states, return_counts=True))
Most detectors expose the fit, predict, and fit_predict methods and share
metadata tags (see Detectors).
Guided (semi-supervised) usage#
When the number of change points or states is known, detectors with the
capability:semi_supervised tag take it in their constructor (the parameter
name depends on the detector: n_cps, n_segments, n_states, …):
from tsseg.algorithms import DynpDetector
print(DynpDetector.get_class_tag("capability:semi_supervised")) # True
change_points = DynpDetector(n_cps=2).fit_predict(series)
states = GaussianHMMDetector(n_states=3, random_state=0).fit_predict(series)
Important: For the detectors shown here, y does not supply the number of
change points or states. Set n_cps or n_states in the constructor to guide
segmentation. For unsupervised use, choose a detector with the
capability:unsupervised tag set to True, such as
PeltDetector(pen_scale="bic") or
GaussianHMMDetector(n_states=None, random_state=0). DynpDetector always
uses a specified count (n_cps=1 by default); omitting it does not make the
detector unsupervised.
Running the test suite#
To ensure your environment is healthy (the full suite takes a long time; select
tests with -k):
python -m pytest tests/
python -m pytest tests/algorithms -k PeltDetector
make test, make lint and make docs run the same commands as the test,
lint and documentation jobs of the CI (select tests with
make test PYTEST_ARGS="-k PeltDetector").