Spectral anomaly detection using conservation ratio of graph Laplacians
Project description
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
-
Conservation Ratio: Computed from the Laplacian eigenvalue spectrum. Higher values indicate more "conserved" (well-connected, regular) structure.
-
Anomaly Detection: Compare a graph's CR against a fitted baseline. A significant drop triggers an anomaly flag.
-
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.
-
Alignment Coefficient: Measures how similarly two graphs partition their nodes using cosine similarity of Fiedler vectors.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file conservation_anomaly-0.1.0.tar.gz.
File metadata
- Download URL: conservation_anomaly-0.1.0.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f4668bbe2847ea3438622e6ac114a88370fe28341ddc8fa82afbce76634b021
|
|
| MD5 |
9efca80d5801611d1a27564610f9925a
|
|
| BLAKE2b-256 |
6d83d446869939167ed24efb3cfc61d6a4a56082c838f82510093d0b1b178faf
|
File details
Details for the file conservation_anomaly-0.1.0-py3-none-any.whl.
File metadata
- Download URL: conservation_anomaly-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97907a9f49486bff026a9ad1470dc7442908f1096d5bcb4b4e9922bfd79a1c01
|
|
| MD5 |
edd40d08fb2c1ab1cf43ac03ab722806
|
|
| BLAKE2b-256 |
020abb527f90f1ea1faf5e32287f2dde47baa0f3e5ea40670d74ba55b7ff53b7
|