Migrate from ADTK to PySAD¶
ADTK applies rule-based and statistical detectors to a whole pandas Series or DataFrame, or to a rolling window. It does not learn from one point at a time. PySAD scores each point when it arrives, then learns from it. No new dependency is needed: PySAD runs on numpy, scipy, and scikit-learn.
Scope and architectural differences¶
Whole series versus one point: ADTK validates a series with
validate_seriesand runsfit_detecton the whole series or on rolling windows. PySAD takes one vector of shape(num_features,)perfit_score_partialcall.Labels versus scores: ADTK detectors return boolean anomalies directly. PySAD detectors return scores. Chain a postprocessor or a probability calibrator and a threshold to turn scores into alerts (see Turn scores into alerts).
Aggregation on labels: ADTK combines boolean results with
AndAggregation,OrAggregationandMajorityVoteAggregation. PySAD combines detector pipelines with the&(AND) and|(OR) operators, which build aModelEnsemble, or combines scores with a score ensembler.Pipeline: ADTK chains detectors with
DetectorPipeline. PySAD chains transformers, detectors and postprocessors with|.Evaluation: ADTK summarizes anomalies with
summarize_anomalies. PySAD measures prequential AUROC, AUPR, precision and recall withevaluate_stream().
Use ADTK if you already have the whole series and want rule-based detectors without training. Use PySAD if points arrive one at a time, each point needs a score when it arrives, or you need calibration, ensembling, and prequential evaluation.
API mapping¶
ADTK |
PySAD equivalent |
Notes |
|---|---|---|
|
Stream a DataFrame or array one row at a time. |
|
|
|
ADTK flags values outside fixed bounds. PySAD calibrates scores first, then thresholds them. |
|
Sliding robust scale replaces rolling volatility. |
|
|
Labels the top quantile of recent scores. |
|
|
Removes seasonality, then applies the generalized extreme studentized deviate test. |
|
|
No direct equivalent |
ADTK flags a value equal to the previous value. Compare each point with the previous point in your loop. |
|
Detects values that break the daily or weekly cycle. |
|
|
|
Or |
|
Express custom rules as a transformer on each point. |
|
|
|
|
|
|
PySAD pipelines run |
|
|
Prequential metrics on labeled streams. |
Replace ThresholdAD and SeasonalAD¶
ADTK validates the series and runs two detectors on it:
import pandas as pd
from adtk.data import validate_series
from adtk.detector import ThresholdAD, SeasonalAD
series = validate_series(pd.read_csv("metrics.csv", index_col=0, parse_dates=True))
threshold_ad = ThresholdAD(high=15.0, low=5.0)
seasonal_ad = SeasonalAD(window_size=24, side="both")
anomalies = threshold_ad.fit_detect(series) + seasonal_ad.fit_detect(series)
The PySAD equivalent scores one point at a time, calibrates the scores, and thresholds them:
import numpy as np
import pandas as pd
from pysad.models import SeasonalHybridESD
from pysad.transform.postprocessing import ThresholdFilter
from pysad.transform.probability_calibration import ConformalProbabilityCalibrator
from pysad.utils import PandasStreamer
df = pd.read_csv("metrics.csv", index_col=0, parse_dates=True)
streamer = PandasStreamer()
model = SeasonalHybridESD(period=24, window_size=24 * 7, direction="both", alpha=0.001)
calibrator = ConformalProbabilityCalibrator(window_size=200)
alerts = ThresholdFilter(threshold=0.99)
for step, x in enumerate(streamer.iter(df.values)):
score = model.fit_score_partial(x)
calibrated = calibrator.fit_transform_partial(score)
alert = alerts.transform_partial(calibrated)
if alert == 1 and step >= 200:
print(f"alert at step {step}")
step >= 200 suppresses alerts while the model and calibrator warm up.
Replace InterQuartileRangeAD on a rolling window¶
ADTK flags values outside the rolling interquartile range:
from adtk.detector import InterQuartileRangeAD
iqr_ad = InterQuartileRangeAD(window_size=50)
anomalies = iqr_ad.fit_detect(series)
PySAD keeps a sliding robust scale with MedianAbsoluteDeviation and labels the most extreme scores:
import numpy as np
from pysad.models import MedianAbsoluteDeviation
from pysad.transform.postprocessing import ThresholdFilter
model = MedianAbsoluteDeviation(window_size=50)
alerts = ThresholdFilter(threshold=0.99)
for x in stream:
score = model.fit_score_partial(x)
alert = alerts.transform_partial(score)
Replace label aggregation¶
ADTK combines detector results on boolean labels:
from adtk.aggregator import AndAggregation, OrAggregation
anomalies = AndAggregation().aggregate([threshold_ad, seasonal_ad])
PySAD combines whole detector pipelines:
from pysad.models import LODA, OnlineIsolationForest
from pysad.transform.preprocessing import StandardScaler
ensemble = (
(StandardScaler() | LODA(random_state=1))
& (StandardScaler() | OnlineIsolationForest(num_trees=50, window_size=256, random_state=1))
)
& raises an alert only when every branch raises one. Use | to raise an alert when any branch raises one. Append calibration and a threshold with |:
from pysad.transform.postprocessing import ThresholdFilter
from pysad.transform.probability_calibration import ConformalProbabilityCalibrator
pipeline = ensemble | ConformalProbabilityCalibrator(window_size=200) | ThresholdFilter(threshold=0.99)
Evaluate on labeled data¶
ADTK summarizes detected anomalies after the run. PySAD evaluates prequentially, scoring each point before learning from it:
import numpy as np
from pysad.evaluation import AUROCMetric, PrecisionMetric, evaluate_stream
rng = np.random.default_rng(0)
X = rng.normal(size=(400, 2))
y = np.zeros(400, dtype=int)
y[300:310] = 1
metrics = evaluate_stream(
model,
X,
y,
{"auroc": AUROCMetric(), "precision": PrecisionMetric()},
)
Next steps¶
Calibrate scores and pick an alert threshold in Turn scores into alerts.
Run a PyOD batch detector on sliding windows in Run a PyOD model on a stream.
Stream CSV files and DataFrames in Stream your own data.