Skip to main content

conservation-anomaly

Spectral anomaly detection using the conservation ratio of graph Laplacians.

What It Does

Every graph has a Laplacian matrix whose eigenvalues encode structural properties. The conservation ratio (CR) measures how well a graph preserves its structural integrity:

CR = λ₂ / (λₙ - λ₂)

where λ₂ is the Fiedler value (algebraic connectivity) and λₙ is the largest eigenvalue. When CR drops significantly, something structurally anomalous has happened.

Installation

pip install conservation-anomaly

Quick Start

Python API

from conservation_anomaly import AnomalyDetector
import networkx as nx

# Fit a baseline (normal) graph
baseline = nx.complete_graph(10)
detector = AnomalyDetector(threshold=0.3)
detector.fit(baseline)

# Detect anomalies in new graphs
results = detector.detect([nx.path_graph(10), nx.cycle_graph(10)])
for r in results:
    print(f"Graph {r['index']}: CR={r['cr']}, anomaly={r['is_anomaly']}")

# Explain what changed
explanation = detector.explain(nx.path_graph(10))
print("Top contributing nodes:", explanation["node_contributions"])
print("Partition changes:", explanation["partition_changes"])

CLI

# Analyze a graph's conservation ratio
conservation-anomaly analyze graph.csv

# Detect anomalies against a baseline
conservation-anomaly detect baseline.json timeseries.csv --threshold 0.3

# Compare two graphs
conservation-anomaly compare graph1.json graph2.json

# Compute Fiedler partition with visualization
conservation-anomaly fiedler graph.csv --visualize --output partition.png

Input Formats

  • CSV: Adjacency matrix (comma-separated, one row per line)
  • JSON: Edge list with optional node list
    {
      "nodes": [0, 1, 2, 3],
      "edges": [
        {"source": 0, "target": 1, "weight": 1.0},
        {"source": 1, "target": 2, "weight": 0.5}
      ]
    }
    
  • Pickle: NetworkX Graph object

How It Works

  1. Conservation Ratio: Computed from the Laplacian eigenvalue spectrum. Higher values indicate more "conserved" (well-connected, regular) structure.

  2. Anomaly Detection: Compare a graph's CR against a fitted baseline. A significant drop triggers an anomaly flag.

  3. Explanation via Fiedler Analysis: The Fiedler vector (eigenvector for λ₂) reveals the graph's natural bipartition. By comparing Fiedler vectors between baseline and anomalous graphs, we identify which nodes and edges contributed to the structural change.

  4. Alignment Coefficient: Measures how similarly two graphs partition their nodes using cosine similarity of Fiedler vectors.

License

MIT

Metadata

Release files for conservation-anomaly 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for conservation-anomaly 0.1.0
File Size Uploaded Provenance Actions
conservation_anomaly-0.1.0.tar.gz 11.2 kB Without provenance Details for conservation_anomaly-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for conservation-anomaly 0.1.0
File Interpreter ABI Platform Actions
conservation_anomaly-0.1.0-py3-none-any.whl Python 3 none any Details for conservation_anomaly-0.1.0-py3-none-any.whl

Total release size: 21.5 kB

Release files / conservation_anomaly-0.1.0.tar.gz

Download URL conservation_anomaly-0.1.0.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9f4668bbe2847ea3438622e6ac114a88370fe28341ddc8fa82afbce76634b021
BLAKE2b-256 checksum
How to use checksums
6d83d446869939167ed24efb3cfc61d6a4a56082c838f82510093d0b1b178faf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / conservation_anomaly-0.1.0-py3-none-any.whl

Download URL conservation_anomaly-0.1.0-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
97907a9f49486bff026a9ad1470dc7442908f1096d5bcb4b4e9922bfd79a1c01
BLAKE2b-256 checksum
How to use checksums
020abb527f90f1ea1faf5e32287f2dde47baa0f3e5ea40670d74ba55b7ff53b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Security for conservation-anomaly 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page