Skip to main content

Implementation of the hierarchical Leiden community detection algorithms.

Project description

Louvain and Leiden Algorithms

A python implementation of the Louvain and Leiden community detection algorithms.


Table of Contents

About

This project is an implementation of the Louvain and Leiden algorithms for community detection in graphs.
The implementation was conducted and tested using Python version 3.10.12.

Getting Started

In order to try out this implementation, do the following:

  1. Check out this repository, or download the latest version and unzip it somewhere.
  2. Install the dependencies, for example using pip install -r requirements.txt.
  3. Run jupyter notebook and explore the available notebooks.

Dependencies

To use this implementation, you only need the NetworkX graph library – it was implemented and tested with version 3.0:

networkx==3.0

For running the notebooks and working with the example datasets, you will need the following packages in addition to NetworkX:

jupyter_core==5.2.0
notebook==6.5.2
pandas==1.5.3
matplotlib==3.7.0

Lastly, to run the tests, generate code coverage information and run static analysis, the following packages are required:

pytest==7.2.1
pytest-cov==4.0.0
black==23.1.0
mypy==1.0.1
ruff==0.0.290

The python dependencies are specified in the requirements.txt file and can be installed (preferrably in a virtual environment) using the command pip install -r requirements.txt.
Alternatively, if you're running nix and if you are so inclined, you can also run nix-shell to install the dependencies.

Caligraphic Symbols in the Source Code

To keep the Python code as close as possible to the pseudocode and the notation used, Unicode symbols are used for some identifiers. The Python specification allows certain Unicode symbols to be used in identifiers, for example Greek symbols such as $\theta$, or special variants of Latin symbols such as $\mathcal{H}$ and $\mathcal{P}$. This functionality is described in more detail in the Python Enhancement Proposal 3131. In particular, one can replace uses of the special variants such as 𝓗 by their "normal" variants, such as H here. If you cannot use the unicode version of the code, for example due to rendering issues, you may also use the ascii branch instead.

Sample Notebooks

For demonstration purposes of this library, the following notebooks are included in this repository:

Tests and Static Analysis

Running the tests

This project has unit tests for almost all individual utility functions and the quality functions. In addition to that, there are tests which execute the community detection algorithms on known graphs and check the results.

The tests are located in the tests directory and can be executed running the pytest tool as follows from a shell in the repository root, generating branch coverage information in the process:

pytest --cov --cov-report=html

The coverage information will be placed in a directory called htmlcov.

Running static code analysis

Apart from the tests, this project employs a number of static analysis tools:

Linter: ruff

The tool ruff is used to lint the code and look for any problems with the code. A variety of checks is configured, among them lints which check for public functions having documentation blocks in the correct format, all functions being annotated with type information, etc.
To check that the code satisfies the linting rules, run the following command:

ruff .

It will list problems it has found in the code. If no lint finds an issue, then this command produces no output.

Formatter: black

The black formatter ensures that the python code is formatted according to its code style. To reformat the code according to the code style, run the following command:

black .

You should see the line 15 files left unchanged. when running it on a fresh checkout.

Type checker: mypy

Python is a dynamically typed language, meaning that the type of a variable may change over its lifetime and that type checking is done at runtime.
The code uses type hints for all functions, in order to add static types, which can be checked using a type checker, such as mypy. The type hints serve as further documentation of a function's usage and allow for a type checker to verify the types of variables and functions in a program, so that it may calls to functions with arguments of an incorrect type, for example.
To check this project using mypy, run the following command:

mypy .

You should see the message Success: no issues found in 15 source files.

Sources

The following papers were used as sources for the implementation:

The notebooks in this repository use the following datasets:

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

hierarchical_leiden-0.1.0.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

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

hierarchical_leiden-0.1.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file hierarchical_leiden-0.1.0.tar.gz.

File metadata

  • Download URL: hierarchical_leiden-0.1.0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.25.2 CPython/3.13.5 Linux/6.11.0-1015-azure

File hashes

Hashes for hierarchical_leiden-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b957aea829ed3f5877ef0f6e04628af523dca53e875c3cf9f81412c6ffe38223
MD5 7e96cba94b62addb2d956eb1189d0f0b
BLAKE2b-256 9b5d38556a08b45b15b08ea6a6ad98aa3030a71fba5afb976a9e5fbe1ea72ce6

See more details on using hashes here.

File details

Details for the file hierarchical_leiden-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hierarchical_leiden-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.25.2 CPython/3.13.5 Linux/6.11.0-1015-azure

File hashes

Hashes for hierarchical_leiden-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 65bbb75ecd5747280fadbbda0a0c9f93c09eb82dfb8a08ec3ccfa48dd84fc0b3
MD5 e82b7c153358fbbf362f724636b4b915
BLAKE2b-256 7f45fda8f42b87f8995cec863a51026b69260192eabab9a93971387213960925

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