Metadata-Version: 2.4
Name: pyHypergrid
Version: 1.0.0
Summary: N-dimensional histogram library for multivariate data — binning, rebinning, comparison, and visualization.
Project-URL: Repository, https://github.com/Clems6323/hypergrid
License: MIT License
        
        Copyright (c) 2026 Clement Linares
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Requires-Python: >=3.10
Requires-Dist: matplotlib>=3.7
Requires-Dist: numpy>=1.24
Requires-Dist: scipy>=1.10
Provides-Extra: dev
Requires-Dist: pandas>=1.5; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Provides-Extra: docs
Requires-Dist: mkdocs-jupyter>=0.24; extra == 'docs'
Requires-Dist: mkdocs-material>=9.0; extra == 'docs'
Requires-Dist: mkdocs>=1.5; extra == 'docs'
Requires-Dist: mkdocstrings[python]>=0.24; extra == 'docs'
Provides-Extra: stats
Requires-Dist: pandas>=1.5; extra == 'stats'
Provides-Extra: umap
Requires-Dist: umap-learn>=0.5; extra == 'umap'
Description-Content-Type: text/markdown

# Hypergrid

<p align="center">
  <img src="docs/hypergrid_logo.png" alt="Hypergrid" width="300"/>
</p>

<p align="center">
  <a href="https://github.com/Clems6323/hypergrid/actions/workflows/build-test-release.yml">
    <img src="https://github.com/Clems6323/hypergrid/actions/workflows/build-test-release.yml/badge.svg" alt="CI"/>
  </a>
  <a href="https://pypi.org/project/pyHypergrid/">
    <img src="https://img.shields.io/pypi/v/pyHypergrid" alt="PyPI version"/>
  </a>
  <a href="https://pypi.org/project/pyHypergrid/">
    <img src="https://img.shields.io/pypi/pyversions/pyHypergrid" alt="Python versions"/>
  </a>
  <a href="https://clems6323.github.io/hypergrid/">
    <img src="https://img.shields.io/badge/docs-online-blue" alt="Documentation"/>
  </a>
</p>

N-dimensional histogram library for multivariate data.

Hypergrid bins arbitrary-dimensional data into a grid, then lets you update it incrementally, reproject it onto new edges, compare two grids statistically, and visualize the distribution — including UMAP projections and temporal drift.

## Installation

```bash
# pip
pip install pyHypergrid

# uv
uv add pyHypergrid
```

Optional extras:

```bash
# pip — with pandas (describe()) and/or UMAP support
pip install "pyHypergrid[stats]"
pip install "pyHypergrid[umap]"
pip install "pyHypergrid[stats,umap]"

# uv
uv add "pyHypergrid[stats]"
uv add "pyHypergrid[umap]"
uv add "pyHypergrid[stats,umap]"
```

## Quick start

```python
import numpy as np
from hypergrid import DenseHypergrid, SparseHypergrid, AdaptiveHypergrid, compute_edges

data = np.random.randn(5000, 3)

# Auto-compute edges with Freedman-Diaconis rule
edges = compute_edges(data)

# Dense backend (good for low-dim, mostly populated grids)
grid = DenseHypergrid(edges)
grid.fit(data)

# Sparse backend (good for high-dim or sparse data)
grid = SparseHypergrid(edges)
grid.fit(data)

# Incremental update
grid.update(np.random.randn(500, 3))
```

## Adaptive grid

Automatically rebins when too much data falls outside the current boundaries:

```python
from hypergrid import AdaptiveHypergrid

grid = AdaptiveHypergrid(drift_threshold=0.05, buffer_size=5000)
grid.fit(data_batch_1)
grid.update(data_batch_2)   # rebins if >5% overflow
```

## Comparing grids

```python
grid1 = DenseHypergrid(edges); grid1.fit(data1)
grid2 = DenseHypergrid(edges); grid2.fit(data2)

grid1.compare(grid2, method="js")           # Jensen-Shannon divergence
grid1.compare(grid2, method="wasserstein")  # Earth Mover's Distance
grid1.compare(grid2, method="l1")           # Total variation
grid1.compare(grid2, method="kl")           # KL divergence
```

## Visualization

```python
grid.plot_all_marginals()
grid.plot_joint(dim_x=0, dim_y=1)
grid.plot_top_bins(k=20)

grid.plot_umap(n_samples=3000)
grid1.compare_umap(grid2)
grid1.compare_marginal(grid2, dim=0)
```

## Temporal tracking

```python
from hypergrid import DenseHypergrid, TemporalHypergrid

base = DenseHypergrid(edges)
tgrid = TemporalHypergrid(base, decay=0.99, snapshot_interval=1000)

for batch in stream:
    tgrid.update(batch)

tgrid.plot_evolution(method="js")
tgrid.plot_temporal_umap()
```

## Rebinning

```python
new_edges = compute_edges(new_data)
rebinned = grid.rebin_to(new_edges)   # returns dict {index: count}
```

## Architecture

```
BaseHypergrid  (ABC)
  └─ BaseTensorHypergrid  (+ RebinMixin, ComparisonMixin, EmbeddingMixin, VisualizationMixin)
       ├─ DenseTensorHypergrid   — numpy array backend
       ├─ SparseTensorHypergrid  — bounds-checked sparse dict
       ├─ StaticHypergrid        — pluggable storage backend
       └─ AdaptiveHypergrid      — auto-rebinning on drift

TemporalHypergrid  — wraps any hypergrid, adds decay + snapshots
```

## Storage backends

| Class | Backend | Best for |
|---|---|---|
| `DenseTensorHypergrid` | numpy array | Low-dim, mostly full grids |
| `SparseTensorHypergrid` | sparse dict (bounds-checked) | High-dim or sparse data |
| `StaticHypergrid` | pluggable (default: DictStorage) | Custom backends |

## License

MIT
