Python bindings for anomaly detection using Markov models
Project description
Anomaly Grid Python
Python bindings for anomaly detection using Markov models. Train on sequential data to detect unusual patterns.
Installation
From PyPI
pip install anomaly-grid-py
From Source
For development or latest features:
# Clone the repository
git clone https://github.com/abimael10/anomaly-grid-py
cd anomaly-grid-py
# Set up development environment
./setup.sh
source venv/bin/activate
# Build the package
maturin develop
Note: Requires Rust toolchain for building. Dependencies are downloaded automatically.
Quick Start
import anomaly_grid_py
# Create detector for web server log analysis
detector = anomaly_grid_py.AnomalyDetector(max_order=3)
# Train with normal web server patterns
normal_logs = [
"GET", "/", "200", "GET", "/login", "200", "POST", "/login", "302",
"GET", "/dashboard", "200", "GET", "/profile", "200", "POST", "/logout", "302"
] * 50 # 50 user sessions
detector.train(normal_logs)
# Detect suspicious activity
suspicious_activity = [
"GET", "/", "200", "GET", "/admin", "403", "GET", "/admin/users", "403",
"POST", "/admin/delete", "403", "GET", "/../etc/passwd", "404"
]
anomalies = detector.detect(suspicious_activity, threshold=0.1)
print(f"🚨 Detected {len(anomalies)} suspicious patterns")
for anomaly in anomalies[:3]: # Show first 3
print(f"Alert: '{anomaly.sequence}' (confidence: {anomaly.anomaly_strength:.1%})")
Detailed Example
See example.py for a complete working example:
python example.py
API Reference
AnomalyDetector
The main class for anomaly detection.
Constructor
AnomalyDetector(max_order=3): Create a new detector with specified maximum order
Methods
train(events): Train the detector with a list of eventsdetect(events, threshold=0.1): Detect anomalies in a sequenceget_performance_metrics(): Get performance metrics as a dictionarymax_order(): Get the maximum order of the detector
AnomalyInfo
Information about an anomaly detection result.
Properties
position: Position in the sequence (int)sequence: The sequence window that was analyzed (string)likelihood: Likelihood of the sequence under the model (float)anomaly_strength: Anomaly strength score [0,1] (float)is_anomaly: Whether this sequence is considered an anomaly (bool)
Development
Building from Source
# Install development dependencies
pip install maturin pytest
# On Linux, also install patchelf
pip install patchelf # Linux only
# Build in development mode
maturin develop
# Run tests
pytest tests/
Development Dependencies
For a complete development environment:
# Install all development dependencies
pip install -e .[dev]
# Or install specific dependency groups
pip install -e .[test] # Testing dependencies
pip install -e .[docs] # Documentation dependencies
Project Structure
anomaly-grid-py/
├── .github/workflows/ # CI/CD configuration
├── docs/ # Documentation
├── python/anomaly_grid_py/ # Python module
├── src/lib.rs # PyO3 bindings
├── tests/ # Test suite
├── build.sh # Build script
├── setup.sh # Environment setup
├── example.py # Usage example
├── pyproject.toml # Python package config
├── Cargo.toml # Rust extension config
├── CHANGELOG.md # Version history
└── LICENSE # MIT license
Running Tests
# Run all tests
pytest tests/
# Run specific test file
pytest tests/test_anomaly_detector.py
Code Quality
This project includes configuration for several code quality tools:
- Black: Code formatting
- Ruff: Linting and code analysis
- MyPy: Type checking
- Pre-commit: Git hooks for quality checks
# Install pre-commit hooks (if pre-commit is installed)
pre-commit install
# Run all quality checks (if tools are installed)
pre-commit run --all-files
Use Cases
Suitable for sequential data analysis:
- Log Analysis: HTTP requests, application events, system logs
- User Behavior: Login patterns, navigation sequences, action flows
- Network Traffic: Connection patterns, protocol sequences
- Sensor Data: IoT readings, equipment status changes
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG.md for version history and changes.
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
Built Distributions
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 anomaly_grid_py-0.1.0.tar.gz.
File metadata
- Download URL: anomaly_grid_py-0.1.0.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
228a5514d66b05c0b919cc38b0d06c83cd47be2467c969a120c95648cd56ebb8
|
|
| MD5 |
b2c9929300b4d1ac6d5ccf1867109247
|
|
| BLAKE2b-256 |
484e07b3655dc045fbfd27fb56fa1c2493b735156ebc450a20336256f74b1026
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 168.1 kB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5ca5896ec8c486fa4a590803534bf76efe55c05770d27b5763c88019c737fae
|
|
| MD5 |
9c704fb77fe2c75c8189eb2da661d594
|
|
| BLAKE2b-256 |
aa6f34c9d0414d3676779a9ddf48e1f99f87b5d5312f911ea0c3882c32e7af86
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-win32.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-win32.whl
- Upload date:
- Size: 163.0 kB
- Tags: CPython 3.8+, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7fbea03abdfcca834c2979b16cffd2f33ffd1227ad2e09fea74f80b9cd0cfd1
|
|
| MD5 |
92fa2d2f14fe5df9dc43aa925b8e5cef
|
|
| BLAKE2b-256 |
2b4fe62f92443f8e55de5a1cae9c1d4633592968930d6e147f60aa7e3aa2d577
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 316.7 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ba8deff23d786ebb5b8764c57abd266adaf4b8f32a80cb3e3aeeb02f2008059
|
|
| MD5 |
9a659b59bf903daa18d642d73eee36ca
|
|
| BLAKE2b-256 |
dd15115590d79ffa2f935610254a2d7f7bd5bd6ae88de5ca15c801882f50d8d0
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
- Upload date:
- Size: 359.3 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ s390x
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f609a3cd7eff39bdd53287edb815c979176c159df537bea579a5515e57db43fd
|
|
| MD5 |
94090afbf70921e4b719511b2e829744
|
|
| BLAKE2b-256 |
e0d048ce6390880a6972189b58184b0e5715680243191ceb0ad43b8fc0aeb754
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
- Upload date:
- Size: 446.8 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ppc64le
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52f76117ae3c453666463b516b4241798244cfbda1506239df36745d14a1b580
|
|
| MD5 |
3bc24ff7f81f15a2f1771e8c359bed41
|
|
| BLAKE2b-256 |
da7ed6533f854753ded2b0d1bd116560bc2d691455029201daf80c43a48beb9f
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
- Upload date:
- Size: 321.0 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARMv7l
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4769d28affd206cbd2cad5baf50c233fb1d281ec66b73933caf1b61dd173a7ec
|
|
| MD5 |
ee8b3f277256f5655990c064641a29ee
|
|
| BLAKE2b-256 |
8986983ac78f0d2de07cb292b59739425bb62e61fc1089ad9d6322d35140437e
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 314.0 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5e69ea8a8c1443d6be856e22ba5ccb65bf9c636c2156ce31fefd7d32ff16582
|
|
| MD5 |
017d63583ea57cfb5cd40ab826310eac
|
|
| BLAKE2b-256 |
623bdc6234777f88f649ae6c5e6a46935a41324b7bba3badc90ae676dfc85d1d
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 340.9 kB
- Tags: CPython 3.8+, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a695f3cdb0064acad82b99bc285d45aa3ad45dbf619ad26d003266e766d054a4
|
|
| MD5 |
2f5941a4ce25befe873de960381eb939
|
|
| BLAKE2b-256 |
4e2ce57465fd3b97bae61c76ee273e624d31149b9c560165703892409c7feb19
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 276.1 kB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b701e98dcdb6ddfcecf965e4b4137be7c1a9452ab05c65e6dfe48c9faee2d9e
|
|
| MD5 |
6f8f0f76fd553a60f50cbf9e2e249421
|
|
| BLAKE2b-256 |
62fc8560d1e15369ebd5bc5abd976ee7e7f6044eb072c177071646a1e7072f2b
|
File details
Details for the file anomaly_grid_py-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: anomaly_grid_py-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 283.6 kB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d169ee833b3a48f15908161afcff02e45f92bfa51528c84622086daf9aa3a5a0
|
|
| MD5 |
af826f5d28b383b2088c25fd6ffd30e7
|
|
| BLAKE2b-256 |
4b5f8094eea2e501f26f0601439bfb824ce9455cd0170c3aad84e42427bf64a5
|