Skip to main content

Synthetic anomaly injection for time series with auto baseline estimation.

Project description

sanin

image

A lightweight package for injecting synthetic anomalies into time series without requiring prior metadata.
It internally estimates level, trend, and seasonality, then applies user-selected anomalies.

Local Installation

pip install -e .

Uso rápido

import numpy as np, pandas as pd
from sanin import AnomalyInjector, AnomalyType

# Example series
t = np.arange(1000)
y = 100 + 0.01*t + 5*np.sin(2*np.pi*t/50) + np.random.normal(0, 1, size=len(t))
s = pd.Series(y, index=pd.date_range("2024-01-01", periods=len(t), freq="min"))

inj = AnomalyInjector(random_state=123)

# 1) Positive spikes in 5 points
y1, rep1, mask1 = inj.inject(s, AnomalyType.SPIKE, severity=1.0, n_points=5, return_mask=True)

# 2) Level shift starting at a point
y2, rep2, mask2 = inj.inject(s, AnomalyType.LEVEL_SHIFT, severity=2.0, return_mask=True)

# 3) Variance change within a window
y3, rep3, mask3 = inj.inject(s, AnomalyType.VARIANCE_CHANGE, severity=1.5, return_mask=True)

Anomaly Types

  • SPIKE (point anomaly, +)
  • DROP (point anomaly, −)
  • LEVEL_SHIFT (step change in level)
  • VARIANCE_CHANGE (increased noise in a window)
  • TREND_DRIFT (slope change)
  • SEASON_AMP_CHANGE (seasonal amplitude change in a window)
  • FLATLINE (sensor stuck at constant value)
  • MISSING (segment with NaNs)
  • STUCK_HIGH, STUCK_LOW (saturation at high/low value)
  • BLACKOUT (zeroing or fixed value in a window)

Each injection also returns a report with affected indices and resolved parameters.

Design

  • No heavy dependencies: uses numpy/pandas.
  • Internal decomposition:
    • level = robust median;
    • trend = moving average over ~10% of the length;
    • seasonality = mean by phase with period estimated via autocorrelation;
    • residual = observed - (level + trend + seasonality).
  • Parameters have robust defaults derived from the series itself.

Tests

pytest -q

License

MIT

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

sanin-1.0.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

sanin-1.0.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file sanin-1.0.0.tar.gz.

File metadata

  • Download URL: sanin-1.0.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sanin-1.0.0.tar.gz
Algorithm Hash digest
SHA256 438fb1f376c0f0c5d1446f7564670f6d0368a6ab8996b6330a1cd614fea675bb
MD5 8830941c6e0cad27b30bdd8c7e329fbb
BLAKE2b-256 ff46b6d76d433e691272ce1e27217aecfd0505ed6de07bf78ceab3abc69d6c73

See more details on using hashes here.

Provenance

The following attestation bundles were made for sanin-1.0.0.tar.gz:

Publisher: publish.yml on Mpaape/sanin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sanin-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sanin-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sanin-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb03ac817dc2790896a771680d55d192b618cc0fc7ec6e61bad5cbefdfa304dd
MD5 e7360dd9c9495964fc78c758d266a3c2
BLAKE2b-256 e408f90e27b2ba23682483851b72ac533b3b662378350fe2d0f5301bf69d63d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for sanin-1.0.0-py3-none-any.whl:

Publisher: publish.yml on Mpaape/sanin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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