Skip to main content

Community detection via Louvain/Leiden + Genetic Algorithm

Project description

TAU Community Detection

PyPI License: MIT Python 3.10+ Downloads Build Status Code style: black

tau-community-detection implements TAU, an evolutionary community detection algorithm that couples genetic search with Leiden refinements. It is designed for scalable graph clustering with a simple drop-in run_clustering() API, sensible defaults, and multiprocessing support.


Highlights

  • Evolutionary search: Maintains a population of candidate partitions and applies crossover/mutation tailored for graph clustering.
  • Leiden optimization: Refines every candidate with Leiden to ensure modularity gains.
  • Multiprocessing aware: Utilises worker pools for population optimization.
  • Deterministic options: Accepts a user-specified random seed for reproducibility.
  • Simple API: Use tau.run_clustering(graph) for the default workflow, or drop down to TauClustering and TauConfig when you need advanced control.

Installation

The project targets Python 3.10 or newer.

pip install tau-community-detection

To work from a clone, install the package in editable mode inside a virtual environment:

git clone https://github.com/HillelCharbit/community_TAU.git
cd community_TAU
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pip install -e .

Quick Start (Python API)

import networkx as nx
import tau_community_detection as tau

g = nx.erdos_renyi_graph(n=1000, p=0.01, seed=42)

# Zero-friction default usage
clustering = tau.run_clustering(g)
print(f"Modularity: {clustering.modularity:.4f}")
print(f"Communities: {len(clustering)}")

# Only override the knobs you care about
clustering = tau.run_clustering(
  g,
  resolution_parameter=0.8,
  random_seed=42,
  verbose=True,
  population_size=100,
  max_generations=50,
)

run_clustering() returns an igraph.VertexClustering object, so the usual modularity and membership attributes are available immediately.

For advanced tuning, pass additional TauConfig fields directly as keyword arguments without having to instantiate TauConfig yourself:

clustering = tau.run_clustering(
  g,
  stopping_generations=5,
  stopping_jaccard=0.95,
  elite_fraction=0.2,
)

Graph input

To optimize for very large graphs or when using many worker processes, it is recommended to pass a file path (e.g., to an .graph, .ncol, or .edgelist file) directly to run_clustering() or TauClustering rather than a pre-loaded graph object. This allows efficient memory sharing.

Supported input:

  • File path to a graph in common NetworkX or igraph format (auto-detects weighting and structure).
  • Already-loaded networkx.Graph or igraph.Graph objects.

By default, the loader auto-detects whether the graph is weighted based on the file or graph structure. You can override this by setting TauConfig(is_weighted=True/False) when constructing TauClustering, or by passing the appropriate weight settings into run_clustering().

See the Quick Start section above for usage examples.


Configuration

All algorithm hyper-parameters live on the TauConfig dataclass. The high-level run_clustering() wrapper accepts the most common ones directly, while TauConfig remains available for advanced workflows. Key fields include:

  • worker_count: number of parallel processes (defaults to CPU count, capped by population size).
  • population_size: number of partitions maintained per generation (default: 60 in run_clustering()).
  • max_generations: upper bound on evolutionary iterations (default: 20 in run_clustering()).
  • verbose: set to True for progress logging (default: False).
  • stopping_generations / stopping_jaccard: convergence checks based on membership stability.
  • random_seed: makes runs reproducible across processes.

See src/tau_community_detection/config.py for the complete list.


Development

pip install -r requirements-dev.txt
make lint
make test

To build local distributions:

make build

Continuous Integration

  • GitHub Actions run lint, tests, and package builds on pushes and pull requests.
  • Set the CODECOV_TOKEN secret to upload coverage reports.

Publishing

  1. Bump the version in setup.cfg/pyproject.toml and commit.
  2. Tag the release with git tag vX.Y.Z && git push --tags.
  3. Run the Publish Package workflow (defaults to TestPyPI). For PyPI, supply the pypi input and ensure PYPI_API_TOKEN is set. Use TEST_PYPI_API_TOKEN for dry runs.

Reference & Citation

If you use TAU in your research, please cite the original algorithm paper:

From Leiden to Tel-Aviv University (TAU): exploring clustering solutions via a genetic algorithm Gal Gilad and Roded Sharan. PNAS Nexus, Volume 2, Issue 6, June 2023. DOI: 10.1093/pnasnexus/pgad180

BibTeX:

@article{gilad2023tau,
  title={From Leiden to Tel-Aviv University (TAU): exploring clustering solutions via a genetic algorithm},
  author={Gilad, Gal and Sharan, Roded},
  journal={PNAS Nexus},
  volume={2},
  number={6},
  pages={pgad180},
  year={2023},
  publisher={Oxford University Press}
}

License & Versioning

Current Version: 1.3.0 License: This project is licensed under the MIT License.

See the Changelog for a detailed history of changes and updates.

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

tau_community_detection-1.3.0.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

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

tau_community_detection-1.3.0-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file tau_community_detection-1.3.0.tar.gz.

File metadata

  • Download URL: tau_community_detection-1.3.0.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tau_community_detection-1.3.0.tar.gz
Algorithm Hash digest
SHA256 db9eb6c09c6b70f624ae430938e7b095ac60c28dd7873e71573024c06c329786
MD5 4175730fda42f6999e313cd6a4d5de4d
BLAKE2b-256 5b6f7fd08465b1d6874afda25eec8d2e9d1b43dbfbf5138a9b010498cfd1227c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tau_community_detection-1.3.0.tar.gz:

Publisher: publish.yml on HillelCharbit/TAU

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tau_community_detection-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tau_community_detection-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ebe92371930726fe1487213c4ab20f376550e09947865a902651e511418a118
MD5 fd03a80834b066fc08ea3b695f81d739
BLAKE2b-256 9b90e143a6776d8e9e4841dc3ac6a167154251a846d2554a1aab6ca9e8241a4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tau_community_detection-1.3.0-py3-none-any.whl:

Publisher: publish.yml on HillelCharbit/TAU

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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