Skip to main content

A lightweight evaluation toolkit for time-series anomaly detection

Project description

tadmetric

Lightweight evaluation metrics for time-series anomaly detection.

tadmetric helps you evaluate anomaly scores or binary predictions with a small, simple API. It includes point-wise metrics, point-adjusted metrics, point-adjusted %K, composite metrics, event-wise metrics, delay-aware metrics, and threshold search.

Installation

pip install tadmetric

Why tadmetric?

  • Minimal input: use y_true with either y_pred or y_score
  • Practical TSAD metrics in one place
  • Fast threshold search for best F1
  • Easy high-level API for benchmarking multiple metrics at once
  • Extensible metric registry for adding new custom metrics

Quick start

Discover the API quickly

import tadmetric as tm

print(tm.api_overview())
print(tm.available_metrics())
print([spec.asdict() for spec in tm.describe_metrics()])

Evaluate from anomaly scores

from tadmetric import evaluate_scores

y_true = [0, 0, 1, 1, 1, 0, 0]
y_score = [0.1, 0.2, 0.4, 0.9, 0.7, 0.1, 0.0]

result = evaluate_scores(
    y_true,
    y_score,
    threshold=0.5,
    metric_kwargs={"point_adjusted_k": {"k": 50}},
)

print(result["point"].asdict())
print(result["point_adjusted"].asdict())
print(result["point_adjusted_k"].asdict())
print(result["composite"].asdict())

Find the best threshold

from tadmetric import search_best_f1_threshold

y_true = [0, 0, 1, 1, 1, 0, 0]
y_score = [0.1, 0.2, 0.4, 0.9, 0.7, 0.1, 0.0]

best = search_best_f1_threshold(y_true, y_score, metric="composite")

print(best.threshold)
print(best.f1)
print(best.evaluation["composite"].asdict())

Reuse large score arrays across repeated evaluations

from tadmetric import ScoreEvaluationContext

ctx = ScoreEvaluationContext.from_scores(y_true, y_score)

at_05 = ctx.evaluate(threshold=0.5)
at_07 = ctx.evaluate(
    threshold=0.7,
    metric_kwargs={"point_adjusted_k": {"k": 30}},
)

Included metrics

Core precision / recall / F1

  • Point-wise: point_precision, point_recall, point_f1
  • Point-adjusted: point_adjusted_precision, point_adjusted_recall, point_adjusted_f1
  • Point-adjusted %K: point_adjusted_k_precision, point_adjusted_k_recall, point_adjusted_k_f1
  • Composite: composite_precision, composite_recall, composite_f1
  • Event-wise: event_precision, event_recall, event_f1

Delay-aware metrics

  • time_to_detect
  • mean_time_to_detect
  • median_time_to_detect
  • missed_detection_rate

Thresholding and utilities

  • threshold_by_quantile
  • threshold_by_topk
  • threshold_by_best_f1
  • search_best_f1_threshold
  • apply_hysteresis
  • binary_to_intervals
  • intervals_to_binary
  • merge_intervals
  • scores_to_binary

Curve-based metrics

  • precision_recall_curve
  • roc_curve
  • auc_pr
  • auc_roc

High-level API

For most workflows, these are the main entry points:

  • evaluate
  • evaluate_scores
  • ScoreEvaluationContext
  • available_metrics
  • describe_metrics
  • api_overview
  • register_metric

Metric semantics

  • Point-wise metrics treat each timestamp independently.
  • Event-wise metrics treat each contiguous anomaly region as one event.
  • Point-adjusted metrics credit an event if it is detected at least once.
  • Point-adjusted %K metrics credit an event when at least K% of the event is detected.
  • Composite metrics use time-wise precision and event-based recall.
  • Interval semantics are half-open: [start, end).
  • Event matching defaults to overlap > 0.
  • Zero division returns 0.0 by default.

Development

pip install -e .[dev]
pytest

Build

python -m build --no-isolation
python -m twine check dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tadmetric-0.1.2.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tadmetric-0.1.2-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file tadmetric-0.1.2.tar.gz.

File metadata

  • Download URL: tadmetric-0.1.2.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for tadmetric-0.1.2.tar.gz
Algorithm Hash digest
SHA256 37020d4ed058612bc5797e76a78af2b267f8783c852537010df17a8f0ce0439a
MD5 13ef418b354a4308cf1b78ee40b3c7a7
BLAKE2b-256 a28b1b40ea52fd735fae1b616555ed8f5f96c5cc0a1be8467a896cbc2b4b8de0

See more details on using hashes here.

File details

Details for the file tadmetric-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tadmetric-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for tadmetric-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cce0cc9e0bf3a4a1f064314b9e12ff89991940ad366f4008788cf77f30a0fa41
MD5 61e33af27c317cd9ae3c7fdfd613bc1e
BLAKE2b-256 ccb3d9fe4de6dbb206b4e5fb348ba2173444acc3c380e663cd82e693e5b3b5e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page