Skip to navigation

Run Kumo Anomaly

View as MarkdownOpen in Claude
Runs in Kumo-TS

Kumo Anomaly is part of the Kumo-TS library, not SDM. Install the Kumo-Anomaly package (Python 3.12 or later) before running the example.

Use perform_anomaly_analysis_with_diffusion to score every time step of a series and flag anomalous steps. For what the model is and how it works, see Kumo Anomaly.

PropertyValue
Entry pointperform_anomaly_analysis_with_diffusion(df, threshold_strategy="scs")
PackageKumo-TS Kumo-Anomaly 1.0.1, Python 3.12 or later
InputA pandas DataFrame of numeric columns, one row per time step
OutputThe input columns plus Anomaly (0 or 1) and MAE (anomaly score)
DevicesCPU or GPU; uses several GPUs automatically when available

Example

Run this script with uv run from the Kumo-TS/Kumo-Anomaly directory. The first run downloads the weights into the working directory. The input holds numeric columns only, and the script injects an anomaly into pressure at steps 300 to 309. The main guard runs the model when you execute the script directly.

import numpy as np
import pandas as pd
from sdk.anomaly_analysis import perform_anomaly_analysis_with_diffusion
from sdk.inference_ad import set_seed
steps = 500
t = np.arange(steps)
rng = np.random.default_rng(0)
df = pd.DataFrame({
"temperature": 20 + 2 * np.sin(2 * np.pi * t / 50) + rng.normal(scale=0.2, size=steps),
"pressure": 5 + 0.5 * np.sin(2 * np.pi * t / 50 + 0.5) + rng.normal(scale=0.05, size=steps),
})
df.loc[300:309, "pressure"] += 3.0
if __name__ == "__main__":
set_seed(0)
result = perform_anomaly_analysis_with_diffusion(df=df, threshold_strategy="scs")
print(result.columns.tolist())
print("Flagged steps:", result.index[result["Anomaly"] == 1].tolist())
print("Highest MAE steps:", sorted(result["MAE"].nlargest(10).index.tolist()))
Expected output
['temperature', 'pressure', 'Anomaly', 'MAE']
Flagged steps: [300, 301, 302, 303, 304, 305, 306, 307, 308, 309]
Highest MAE steps: [300, 301, 302, 304, 305, 306, 307, 308, 309, 407]

All ten injected steps (300 to 309) are flagged, and nine of the ten highest scores fall on them.

Read the Output

ColumnMeaning
MAEReconstruction error. Higher means more likely anomalous. Rank by it when you want a fixed alert budget.
Anomaly1 for flagged steps, 0 otherwise, from the chosen threshold strategy.
threshold_strategyMethod
"scs"Segmented Confidence Sequences: thresholds scores within adaptive segments of the series.
"macs"Multi-Scale Adaptive Confidence Segments: variance-weighted score bounds.

Explain an Anomaly

Pass sdk_config=ADDiffusionConfig(report_path="report.pdf", timestamp_column="timestamp", ground_truth_column="GT") to write a PDF of signals, flags, and scores. Timestamp and label columns named there are kept in the output but excluded from the model’s features.

Add explain=True to rank the features of each flagged step by reconstruction error. TopContributors lists the features with the largest errors (up to explanation_top_k, default 3), and ContributionShares gives each one’s share of the step’s total error. The ranking describes where the model’s reconstruction failed, not what caused the anomaly. This code continues the Example above with the same df.

from sdk.anomaly_analysis import ADDiffusionConfig, perform_anomaly_analysis_with_diffusion
from sdk.inference_ad import set_seed
if __name__ == "__main__":
set_seed(0)
result = perform_anomaly_analysis_with_diffusion(
df=df,
threshold_strategy="scs",
sdk_config=ADDiffusionConfig(explain=True, report_path="kumo-anomaly-report.pdf"),
)
flagged = result[result["Anomaly"] == 1]
print(flagged[["MAE", "TopContributors", "ContributionShares"]].round(3))
Expected output
MAE TopContributors ContributionShares
300 0.698 ["pressure","temperature"] [0.727367,0.272633]
301 0.771 ["pressure","temperature"] [0.684312,0.315688]
302 0.768 ["pressure","temperature"] [0.648471,0.351529]
303 0.586 ["pressure","temperature"] [0.663143,0.336857]
304 0.699 ["pressure","temperature"] [0.515667,0.484333]
305 0.724 ["pressure","temperature"] [0.561017,0.438983]
306 0.798 ["pressure","temperature"] [0.552463,0.447537]
307 0.853 ["pressure","temperature"] [0.551744,0.448256]
308 0.905 ["pressure","temperature"] [0.593804,0.406196]
309 0.845 ["pressure","temperature"] [0.507871,0.492129]

In this two-sensor example, pressure has the largest share of reconstruction error at every flagged step. With other inputs, the top contributor can differ from the sensor containing the fault, including a correlated sensor. For more information, see Explainability and reporting.

Tune Compute and Reproducibility

  • nsample (default 15) sets the diffusion samples per window. Lower runs faster with noisier scores.
  • Diffusion sampling uses the SDK’s default seed (42) on every call. sdk.inference_ad.set_seed(...) seeds only the global Python, NumPy, and PyTorch random number generators in your own process.

Inputs and Limits

  • Numeric columns only. Other columns raise The following columns contain non-numeric values: [...]; drop them or declare them in ADDiffusionConfig.
  • At least as many rows as the model’s PCA dimension.
  • Weight files go to model_path and model_config_path, which default to the working directory.

Fine-Tuning

Fine-tune on normal windows from your own CSV with examples/finetune_example.py.

Learn More