-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_time_series.py
More file actions
60 lines (49 loc) · 2.52 KB
/
04_time_series.py
File metadata and controls
60 lines (49 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Example 4: Time Series — detect anomalies in metric streams.
Feed WaveGuard normal time windows and check new windows for anomalies.
Each sample is an array of numbers representing a time window.
Usage:
export WAVEGUARD_API_KEY="your-key"
python 04_time_series.py
"""
import os
from waveguard import WaveGuard
api_key = os.environ.get("WAVEGUARD_API_KEY", "demo")
wg = WaveGuard(api_key=api_key)
# ── Normal time windows (stable oscillating metric) ──────────────────────
# Each sample is a 10-point window of a response-time metric (ms)
normal_windows = [
[45, 48, 42, 50, 47, 44, 49, 46, 43, 51],
[46, 49, 43, 48, 45, 47, 50, 44, 42, 48],
[44, 47, 41, 49, 46, 45, 48, 43, 44, 50],
[47, 50, 44, 51, 48, 46, 49, 45, 43, 49],
[43, 46, 40, 48, 45, 44, 47, 42, 41, 47],
]
# ── Test windows ──────────────────────────────────────────────────────────
test_windows = [
# Normal — same statistical shape
[45, 47, 43, 49, 46, 44, 48, 45, 42, 50],
# Anomaly — latency spike (sudden sustained increase)
[45, 48, 120, 350, 500, 480, 510, 490, 520, 500],
# Anomaly — flatline (metric stopped updating)
[45, 45, 45, 45, 45, 45, 45, 45, 45, 45],
# Normal — slightly different but same distribution
[48, 51, 45, 52, 49, 47, 50, 46, 44, 51],
]
# ── Scan ──────────────────────────────────────────────────────────────────
result = wg.scan(
training=normal_windows,
test=test_windows,
encoder_type="timeseries",
)
# ── Report ────────────────────────────────────────────────────────────────
labels = ["Normal window", "Latency spike", "Flatline", "Normal window"]
print("=== Time Series Anomaly Detection ===\n")
for i, (r, label) in enumerate(zip(result.results, labels)):
status = "🚨" if r.is_anomaly else "✅"
preview = str(test_windows[i][:5])[:-1] + ", ...]"
print(f" {status} Window {i + 1}: {label}")
print(f" Data: {preview}")
print(f" Score: {r.score:.2f} | Confidence: {r.confidence:.0%}")
print()
print(f"Detected: {result.summary.anomalies_found} anomalies out of {result.summary.total_test_samples} windows")