TSPulseAnomalyDetector
Anomaly detector wrapping IBM TSPulse via granite-tsfm.
Quickstart
from sktime.detection.tspulse import TSPulseAnomalyDetector
estimator = TSPulseAnomalyDetector(model_path: str='ibm-granite/granite-timeseries-tspulse-r1', revision: str='main', mask_type: str='user', prediction_mode=None, aggregation_length: int=64, aggr_function: str='max', smoothing_length: int=8, least_significant_scale: float=0.01, least_significant_score: float=0.1, batch_size: int=128, predictive_score_smoothing: bool=False, anomaly_threshold: float | None=0.6, anomaly_percentile: float | None=None, config: dict | None=None)Parameters(14)
- model_pathstr, default=”ibm-granite/granite-timeseries-tspulse-r1”
- Hugging Face model id or local path.
- revisionstr, default=”main”
- Model revision on the Hugging Face Hub.
- mask_typestr, default=”user”
Patch-masking strategy during reconstruction.
"user"applies masking from an optionalpast_observed_mask(the default for anomaly scoring). Other supported values include"block","hybrid","var_hybrid", and"random".- prediction_modestr or list of str, optional
Score type(s) computed on each context window before they are merged. Supported values:
"time": mean squared error between the input and the model’s time-domain reconstruction over the lastaggregation_lengthpoints of the context window."fft": same error using the reconstruction from the frequency (FFT) branch."forecast": mean squared error between the next observed value and the model’s one-step forecast.
When
None(default), uses["time", "fft"]. A single string is wrapped in a one-element list. With several types, each produces its own score sequence; seesmoothing_lengthandaggr_function.- aggregation_lengthint, default=64
Length of the context suffix used for patchwise stitched reconstruction when scoring with
"time"or"fft". Also controls boundary padding when scores are aligned back to the full series length.- aggr_functionstr, default=”max”
How to merge score sequences from different entries in
prediction_modeat each time point. One of"max","min", or"mean".- smoothing_lengthint, default=8
Moving-average window applied to each score sequence listed in
prediction_modeafter boundary alignment, beforeaggr_functionmerges them. One-step"forecast"scores skip this smoothing unlesspredictive_score_smoothingisTrue.- least_significant_scalefloat, default=0.01
Value in
(0, 1). Sets a variance-based floor on raw errors before scores are rescaled: deviations smaller than this fraction of the squared differences in the (standardized) input are treated as insignificant.- least_significant_scorefloat, default=0.1
Minimum scale factor applied when normalizing significant errors to the
[0, 1]score range returned by the pipeline.- batch_sizeint, default=128
- Batch size for pipeline inference.
- predictive_score_smoothingbool, default=False
If
True, applysmoothing_lengthsmoothing to"forecast"scores as well; ifFalse, forecast scores are left unsmoothed.- anomaly_thresholdfloat, optional, default=0.6
Scores strictly above this value are flagged as anomalies in
predict. IfNone,anomaly_percentileis used instead.- anomaly_percentilefloat, optional, default=None
Percentile of the score vector on
Xused as the detection threshold whenanomaly_thresholdisNone. Exactly one ofanomaly_thresholdandanomaly_percentilemust be set.- configdict, optional, default=None
Extra keyword arguments forwarded to
TSPulseForReconstruction.from_pretrained.Any key you supply overrides the built-in default for that key, except for
num_input_channels, which is always set from the number of columns inXatfittime.If
configisNone, the following default override is applied:ignore_mismatched_sizes=True: allows loading when channel or head shapes differ from the checkpoint
Other commonly useful overrides (see
TSPulseConfigin granite-tsfm):context_length: history length per window (checkpoint default often 512 for r1 models)patch_length/patch_stride: patch size and stride for the encoderdecoder_mode: channel mixing in the decoder ("mix_channel"or"common_channel")scaling: input normalization ("revin","mean","std", orNone)mask_ratio: fraction of patches masked whenmask_typeis not"user"fft_time_consistent_masking: ifTrue, masked series is used for the FFT branch during training-style maskingreconstruction_type:"patchwise"or"full"reconstructionprediction_length: horizon for the forecast head when usingprediction_mode="forecast"
Examples
>>> from sktime.detection.tspulse import TSPulseAnomalyDetector
>>> import pandas as pd
>>> import numpy as np
>>> idx = pd. date_range ("2020-01-01", periods = 200, freq = "D")
>>> X = pd. DataFrame (np. random. randn (200, 1), index = idx, columns = ["value" ])
>>> detector = TSPulseAnomalyDetector ()
>>> detector. fit (X)
>>> detector. predict (X)
>>> detector. predict_scores (X)References
- [1 ] https://github.com/ibm-granite/granite-tsfm/blob/main/notebooks/hfdemo/tspulse_anomaly_detection.ipynb [2 ] https://arxiv.org/abs/2505.13033