Skip to main content

Universal Hyperbolic Geometry Library

Project description

UHG-Based Anomaly Detection

This repository contains an implementation of anomaly detection using Universal Hyperbolic Geometry (UHG) principles. The code leverages the UHG library to perform geometric operations in hyperbolic space.

Overview

Universal Hyperbolic Geometry (UHG) provides a powerful framework for representing complex hierarchical data. This implementation uses UHG principles to detect anomalies in network traffic data by embedding the data in hyperbolic space and identifying points that deviate from the normal patterns.

Key Features

  • UHG-based graph neural network for anomaly detection
  • Efficient implementation of UHG operations using the UHG library
  • Support for large-scale datasets through batched processing
  • Visualization tools for analyzing embeddings and anomaly scores

Installation

  1. Clone this repository:
git clone https://github.com/yourusername/uhg-anomaly-detection.git
cd uhg-anomaly-detection
  1. Install the required dependencies:
pip install -r requirements.txt

Usage

Basic Usage

from uhg_anomaly_detection_refactored import load_and_preprocess_data, create_graph_data, UHGGraphNN, UHGAnomalyLoss

# Load and preprocess data
features, labels, feature_info = load_and_preprocess_data("your_data.csv")

# Create graph data
graph_data = create_graph_data(features, labels, k=5)

# Create model
model = UHGGraphNN(
    in_channels=features.shape[1],
    hidden_channels=64,
    embedding_dim=32,
    num_layers=2,
    dropout=0.2
)

# Create loss function
criterion = UHGAnomalyLoss(spread_weight=0.1, quad_weight=1.0, margin=1.0)

# Create optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-5)

# Train the model
model.train()
for epoch in range(100):
    optimizer.zero_grad()
    z = model(graph_data.x, graph_data.edge_index)
    loss = criterion(z, graph_data.edge_index)
    loss.backward()
    optimizer.step()
    print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}")

# Generate embeddings
model.eval()
with torch.no_grad():
    embeddings = model(graph_data.x, graph_data.edge_index).cpu().numpy()

Using UHG Utilities Directly

The uhg_utils.py file provides a clean interface to the UHG library. You can use these utilities in your own code:

from uhg_utils import uhg_quadrance, uhg_spread, uhg_cross_ratio, to_uhg_space

# Convert points to UHG space
points = to_uhg_space(your_data)

# Compute quadrance (UHG distance) between points
distance = uhg_quadrance(points[0], points[1])

# Compute spread (UHG angle) between lines
spread = uhg_spread(line1, line2)

# Compute cross-ratio of four points
cr = uhg_cross_ratio(p1, p2, p3, p4)

File Structure

  • uhg_utils.py: Utility functions that provide a clean interface to the UHG library
  • uhg_anomaly_detection_refactored.py: Main implementation of UHG-based anomaly detection
  • requirements.txt: List of required dependencies

UHG Library Integration

This implementation uses the UHG library for core geometric operations. The uhg_utils.py file serves as a bridge between the application code and the UHG library, providing a clean interface and handling edge cases.

Key UHG operations used:

  • quadrance: Computes the squared distance between points in hyperbolic space
  • spread: Computes the squared angle between lines in hyperbolic space
  • cross_ratio: Computes the cross-ratio of four points, a projective invariant
  • normalize_points: Normalizes points according to UHG conventions
  • join_points: Computes the line joining two points
  • meet_line_point: Computes the intersection of a line and a point

References

  • Norman J. Wildberger, "Universal Hyperbolic Geometry I: Trigonometry", Geometriae Dedicata, 2013
  • Norman J. Wildberger, "Universal Hyperbolic Geometry II: A pictorial overview", KoG, 2013
  • Norman J. Wildberger, "Universal Hyperbolic Geometry III: First Steps in Projective Triangle Geometry", KoG, 2014

License

MIT License

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

uhg-0.3.4.tar.gz (154.3 kB view details)

Uploaded Source

Built Distribution

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

uhg-0.3.4-py3-none-any.whl (77.7 kB view details)

Uploaded Python 3

File details

Details for the file uhg-0.3.4.tar.gz.

File metadata

  • Download URL: uhg-0.3.4.tar.gz
  • Upload date:
  • Size: 154.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for uhg-0.3.4.tar.gz
Algorithm Hash digest
SHA256 87bc70936c81deb0389f6448b810a5e583cbabcc1d01fe3f0e5c30dc2555e112
MD5 d3eeaf0c59930397ee749605ce50cfb4
BLAKE2b-256 afe515d1b0ef9554a567b8f36155479b3b610da99b43f66ccd80cdeaaa102ae5

See more details on using hashes here.

File details

Details for the file uhg-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: uhg-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 77.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for uhg-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e56e99e49290100cbb3603bfd070e6588c4b2cb29dca4f69a3b3cf1ff3222819
MD5 08c2b3016bfb27275b4eda1b242f8316
BLAKE2b-256 e75a7f7e86f8d81a60a2071ca15b3fc4827a8e1f9a6c68babe8c33e28c977d6c

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