Metadata-Version: 2.4
Name: codebethe
Version: 0.1.0
Summary: Community detection in sparse static and time-evolving graphs with the Bethe-Hessian (Python port of CoDeBetHe.jl)
Author-email: "Lorenzo Dall'Amico, Nicolas Tremblay" <lorenzo.dallamico@isi.it>
Project-URL: Repository, https://github.com/lorenzodallamico/CoDeBetHe.jl
Project-URL: Documentation, https://lorenzodallamico.github.io/CoDeBetHe.jl/
Keywords: community detection,spectral clustering,Bethe-Hessian,stochastic block model,temporal graphs
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: scikit-learn
Dynamic: license-file

# CoDeBetHe (Python)

##### **Co**mmunity **De**tection with the **Bet**he **He**ssian

Pure-Python implementation (NumPy/SciPy, no Julia required) of the spectral community detection algorithms of
[CoDeBetHe.jl](https://github.com/lorenzodallamico/CoDeBetHe.jl):

* Dall'Amico, Couillet, Tremblay - *Revisiting the Bethe-Hessian: improved community detection in sparse heterogeneous graphs* (NeurIPS 2019)
* Dall'Amico, Couillet, Tremblay - *A unified framework for spectral clustering in sparse graphs* (JMLR 2021)
* Dall'Amico, Couillet, Tremblay - *Community detection in sparse time-evolving graphs with a dynamical Bethe-Hessian* (NeurIPS 2020)

If you use this package, please cite the references above.

```bash
pip install codebethe
```

## Static graphs

```python
import numpy as np
from codebethe import matrix_C, create_label_vector, adjacency_matrix_DCSBM, community_detection

n, k, c = 5000, 3, 8.
pi_v = np.ones(k) / k
ℓ = create_label_vector(n, k, pi_v)
θ = np.random.uniform(3, 10, n) ** 4; θ /= θ.mean()        # heterogeneous degrees
A = adjacency_matrix_DCSBM(matrix_C(2., c, 0., pi_v), c, ℓ, θ)

res = community_detection(A)          # k is estimated if not given
res.ℓ, res.k, res.modularity, res.ζ   # labels (0-based), number of classes, modularity, ζ_p
community_detection(A, k=3, ℓ=ℓ).overlap
```

## Time-evolving graphs

```python
from codebethe import adjacency_matrix_DDCSBM, dynamic_community_detection

T, η = 5, 0.7                         # number of snapshots, label persistence
AT, ℓ_T = adjacency_matrix_DDCSBM(T, matrix_C(2., c, 0., pi_v), c, η, θ, pi_v)
res = dynamic_community_detection(AT, η, k, ℓ_T=ℓ_T)    # approx_embedding=True for large n T
res.ℓ, res.overlap, res.modularity    # T x n labels, per-time overlap and modularity
```

## Differences with the Julia package

* Labels are **0-based** (`0, ..., k-1`); the Julia package uses `1, ..., k`.
* Function names are kept (`community_detection_optimal_BH`, `dynamic_community_detection_BH`), with the shorter
  aliases `community_detection` and `dynamic_community_detection`. `CD_BH` replaces the former PyJulia bridge.
* Results are returned as dataclasses (`StaticResult`, `DynamicResult`); `overlap` is `None` when no ground truth is given.
* Random functions accept a `seed`.

## Authors

[Lorenzo Dall'Amico](https://lorenzodallamico.github.io/), Nicolas Tremblay

## License

GNU Affero General Public License v3 (see `LICENSE`), as the original Julia package.
