TSLib Changepoints
Use this skill after time-series data preparation when the user wants THUML Time-Series-Library for changepoint-like process-change events.
Important limitation: official TSLib documentation and code do not expose a native changepoint detection task or segmentation API. Use the documented anomaly_detection task to detect anomalous event intervals, then derive event starts/ends as changepoint candidates outside TSLib.
Minimum Install
git clone https://github.com/thuml/Time-Series-Library.git
cd Time-Series-Library
conda create -n tslib python=3.11
conda activate tslib
pip install torch==2.5.1 --index-url https://download.pytorch.org/whl/cu121
pip install -r requirements.txt
Adjust the PyTorch/CUDA command to the local GPU. Some optional models require extra dependencies, as documented in the upstream README.
Data Contract
- Supported anomaly loaders are
PSM, MSL, SMAP, SMD, and SWAT.
- Use
--task_name anomaly_detection, --features M, --seq_len WINDOW, and --pred_len 0.
- Training data should represent normal behavior; test data carries anomaly labels.
seq_len is the sliding reconstruction window. Keep it smaller than train/validation/test lengths.
anomaly_ratio is a prior anomaly percentage used to set the reconstruction-error threshold.
- TSLib returns point anomaly predictions and applies event-level adjustment when labels are available.
- For changepoints, convert contiguous predicted anomaly intervals to starts, ends, or both according to the task definition.
Read references/tslib-data-workflow.md before adapting custom datasets or turning anomaly intervals into changepoints.
Core Pattern
python -u run.py \
--task_name anomaly_detection \
--is_training 1 \
--root_path ./dataset/PSM \
--model_id PSM \
--model TimesNet \
--data PSM \
--features M \
--seq_len 100 \
--pred_len 0 \
--d_model 64 \
--d_ff 64 \
--e_layers 2 \
--enc_in 25 \
--c_out 25 \
--top_k 3 \
--anomaly_ratio 1 \
--batch_size 128 \
--train_epochs 3
For inference from a saved checkpoint, use the same setting arguments with --is_training 0.
Model Choice
Exp_Basic scans all files in models/, but official anomaly scripts are dataset-specific. Prefer models with scripts under scripts/anomaly_detection/<DATASET>/.
Documented anomaly scripts include:
PSM: Autoformer, DLinear, KANAD, TimesNet, Transformer.
SMAP: Autoformer, KANAD, TimesNet, Transformer.
SMD: Autoformer, KANAD, TimesNet, Transformer.
SWAT: Autoformer, KANAD, TimesNet, Transformer.
MSL: Autoformer, Crossformer, DLinear, ETSformer, FEDformer, FiLM, Informer, KANAD, LightTS, MICN, Pyraformer, Reformer, TimesNet, Transformer, iTransformer.
Do not claim a model is validated for changepoint detection unless a project-specific experiment proves that anomaly intervals map to the desired process changes.
Evaluation and Outputs
Exp_Anomaly_Detection trains with MSE reconstruction loss.
- Test scoring uses per-time-step reconstruction error averaged over channels.
- Threshold is
np.percentile(concat(train_energy, test_energy), 100 - anomaly_ratio).
- Metrics printed and appended to
result_anomaly_detection.txt: Accuracy, Precision, Recall, F-score.
utils.tools.adjustment(gt, pred) expands a detected point inside a labeled anomaly segment to the full segment; this is event-level adjustment, not raw point precision.
- No built-in changepoint precision/recall, delay, or segmentation metric is documented.
Anti-Leakage Rules
- Do not tune
anomaly_ratio, seq_len, model, dimensions, or threshold on final test labels.
- The official threshold combines train and test energy. Treat this as benchmark behavior; for deployment, set thresholds from train/validation or prior normal data only.
- Fit normalization only on training history. Official anomaly loaders use train data for
StandardScaler.
- Convert anomaly intervals to changepoint timestamps only after model scoring; never use ground-truth event boundaries to postprocess predictions in production.
- Validate with temporal splits or rolling deployment simulation, not random split.
Common Errors
- Calling TSLib a native changepoint detector; it documents anomaly detection, not changepoint segmentation.
- Using unsupported
--data custom for anomaly detection without writing a loader.
- Using
--features S for official anomaly scripts; official examples use multivariate --features M.
- Mismatching
enc_in/c_out with the number of channels.
- Setting
seq_len greater than available train/test rows.
- Forgetting that event-level adjustment can inflate point metrics.
- Treating
test_label files as available in production.
References
- Read
references/tslib-data-workflow.md for loader file formats, intervals, and leakage.
- Read
references/tslib-api-map.md for run.py, anomaly experiment internals, scripts, metrics, and limitations.
- Read
references/official-sources.md for official sources consulted.
- Use
scripts/validate_tslib_anomaly_inputs.py to sanity-check dataset files, labels, dimensions, and key run arguments.
Ready Checklist
- Task framing says anomaly-event detection or changepoint proxy, not native segmentation.
- Dataset is one of
PSM, MSL, SMAP, SMD, SWAT, or a custom loader has been implemented.
seq_len, enc_in, c_out, anomaly_ratio, model, and script source are documented.
- Metrics distinguish raw point predictions from event-level adjusted scores.
- Thresholding and preprocessing avoid future/test leakage for non-benchmark use.
1---2name: changepoint-time-series-library3description: Use THUML Time-Series-Library for process-change event detection through its documented anomaly_detection task after validating prepared multivariate time-series data, including supported PSM/MSL/SMAP/SMD/SWAT loaders, run.py scripts, reconstruction-error thresholds, anomaly_ratio, event-level adjustment, Accuracy/Precision/Recall/F-score, and leakage-safe conversion of anomaly intervals to changepoint-like starts or ends.4---56# TSLib Changepoints78Use this skill after time-series data preparation when the user wants THUML Time-Series-Library for changepoint-like process-change events.910Important limitation: official TSLib documentation and code do **not** expose a native changepoint detection task or segmentation API. Use the documented `anomaly_detection` task to detect anomalous event intervals, then derive event starts/ends as changepoint candidates outside TSLib.1112## Minimum Install1314```bash15git clone https://github.com/thuml/Time-Series-Library.git16cd Time-Series-Library17conda create -n tslib python=3.1118conda activate tslib19pip install torch==2.5.1 --index-url https://download.pytorch.org/whl/cu12120pip install -r requirements.txt21```2223Adjust the PyTorch/CUDA command to the local GPU. Some optional models require extra dependencies, as documented in the upstream README.2425## Data Contract2627- Supported anomaly loaders are `PSM`, `MSL`, `SMAP`, `SMD`, and `SWAT`.28- Use `--task_name anomaly_detection`, `--features M`, `--seq_len WINDOW`, and `--pred_len 0`.29- Training data should represent normal behavior; test data carries anomaly labels.30- `seq_len` is the sliding reconstruction window. Keep it smaller than train/validation/test lengths.31- `anomaly_ratio` is a prior anomaly percentage used to set the reconstruction-error threshold.32- TSLib returns point anomaly predictions and applies event-level adjustment when labels are available.33- For changepoints, convert contiguous predicted anomaly intervals to starts, ends, or both according to the task definition.3435Read `references/tslib-data-workflow.md` before adapting custom datasets or turning anomaly intervals into changepoints.3637## Core Pattern3839```bash40python -u run.py \41 --task_name anomaly_detection \42 --is_training 1 \43 --root_path ./dataset/PSM \44 --model_id PSM \45 --model TimesNet \46 --data PSM \47 --features M \48 --seq_len 100 \49 --pred_len 0 \50 --d_model 64 \51 --d_ff 64 \52 --e_layers 2 \53 --enc_in 25 \54 --c_out 25 \55 --top_k 3 \56 --anomaly_ratio 1 \57 --batch_size 128 \58 --train_epochs 359```6061For inference from a saved checkpoint, use the same setting arguments with `--is_training 0`.6263## Model Choice6465`Exp_Basic` scans all files in `models/`, but official anomaly scripts are dataset-specific. Prefer models with scripts under `scripts/anomaly_detection/<DATASET>/`.6667Documented anomaly scripts include:6869- `PSM`: `Autoformer`, `DLinear`, `KANAD`, `TimesNet`, `Transformer`.70- `SMAP`: `Autoformer`, `KANAD`, `TimesNet`, `Transformer`.71- `SMD`: `Autoformer`, `KANAD`, `TimesNet`, `Transformer`.72- `SWAT`: `Autoformer`, `KANAD`, `TimesNet`, `Transformer`.73- `MSL`: `Autoformer`, `Crossformer`, `DLinear`, `ETSformer`, `FEDformer`, `FiLM`, `Informer`, `KANAD`, `LightTS`, `MICN`, `Pyraformer`, `Reformer`, `TimesNet`, `Transformer`, `iTransformer`.7475Do not claim a model is validated for changepoint detection unless a project-specific experiment proves that anomaly intervals map to the desired process changes.7677## Evaluation and Outputs7879- `Exp_Anomaly_Detection` trains with MSE reconstruction loss.80- Test scoring uses per-time-step reconstruction error averaged over channels.81- Threshold is `np.percentile(concat(train_energy, test_energy), 100 - anomaly_ratio)`.82- Metrics printed and appended to `result_anomaly_detection.txt`: Accuracy, Precision, Recall, F-score.83- `utils.tools.adjustment(gt, pred)` expands a detected point inside a labeled anomaly segment to the full segment; this is event-level adjustment, not raw point precision.84- No built-in changepoint precision/recall, delay, or segmentation metric is documented.8586## Anti-Leakage Rules8788- Do not tune `anomaly_ratio`, `seq_len`, model, dimensions, or threshold on final test labels.89- The official threshold combines train and test energy. Treat this as benchmark behavior; for deployment, set thresholds from train/validation or prior normal data only.90- Fit normalization only on training history. Official anomaly loaders use train data for `StandardScaler`.91- Convert anomaly intervals to changepoint timestamps only after model scoring; never use ground-truth event boundaries to postprocess predictions in production.92- Validate with temporal splits or rolling deployment simulation, not random split.9394## Common Errors9596- Calling TSLib a native changepoint detector; it documents anomaly detection, not changepoint segmentation.97- Using unsupported `--data custom` for anomaly detection without writing a loader.98- Using `--features S` for official anomaly scripts; official examples use multivariate `--features M`.99- Mismatching `enc_in`/`c_out` with the number of channels.100- Setting `seq_len` greater than available train/test rows.101- Forgetting that event-level adjustment can inflate point metrics.102- Treating `test_label` files as available in production.103104## References105106- Read `references/tslib-data-workflow.md` for loader file formats, intervals, and leakage.107- Read `references/tslib-api-map.md` for `run.py`, anomaly experiment internals, scripts, metrics, and limitations.108- Read `references/official-sources.md` for official sources consulted.109- Use `scripts/validate_tslib_anomaly_inputs.py` to sanity-check dataset files, labels, dimensions, and key run arguments.110111## Ready Checklist112113- Task framing says anomaly-event detection or changepoint proxy, not native segmentation.114- Dataset is one of `PSM`, `MSL`, `SMAP`, `SMD`, `SWAT`, or a custom loader has been implemented.115- `seq_len`, `enc_in`, `c_out`, `anomaly_ratio`, model, and script source are documented.116- Metrics distinguish raw point predictions from event-level adjusted scores.117- Thresholding and preprocessing avoid future/test leakage for non-benchmark use.