Skip to main content

Fast Gephi's ForceAtlas2 graph layout algorithm written in Python for Python.

Project description

PyForceAtlas2

PyPI version License Build Status DOI

PyForceAtlas2 is a Python implementation of the ForceAtlas2 graph layout algorithm. Originally designed for Gephi, this implementation is optimized for reproducible and high-performance network visualization in Python, supporting both NetworkX and igraph.

Features

  • High Performance: Barnes-Hut optimization for large networks.
  • Customizable: Fine-tune attraction, repulsion, gravity, and speed parameters.
  • Versatile: Seamless integration with NetworkX and igraph.
  • Reproducible: Easily set random seeds for deterministic layouts.

Installation

Install PyForceAtlas2 via pip:

pip install pyforceatlas2

Usage Example

The following example demonstrates how to compute a layout for a random geometric graph and visualize it with NetworkX and Matplotlib:

"""
Example: ForceAtlas2 Layout on a Random Geometric Graph
--------------------------------------------------------

This script generates a random geometric graph with 400 nodes,
computes a ForceAtlas2 layout, and visualizes the result.
"""

import random
import networkx as nx
import matplotlib.pyplot as plt
from pyforceatlas2 import ForceAtlas2

# Set a random seed for reproducibility.
random.seed(888)

# Generate a random geometric graph: nodes are connected if they are within radius 0.2.
G = nx.random_geometric_graph(400, 0.2)

# Initialize ForceAtlas2 with custom parameters.
forceatlas2 = ForceAtlas2(
    outbound_attraction_distribution=False,  # Do not dissuade hubs.
    edge_weight_influence=1.0,
    lin_log_mode=True,                       # Use LinLog mode for attraction forces.
    jitter_tolerance=10.0,                     # Tolerance for oscillations.
    barnes_hut_optimize=True,                  # Enable Barnes-Hut optimization.
    barnes_hut_theta=2.2,                      # Trade-off between accuracy and speed.
    scaling_ratio=2.0,                         # Controls overall repulsion strength.
    strong_gravity_mode=False,                 # Use standard gravity.
    gravity=1.0,                               # Gravitational constant.
    verbose=True,
)

# Compute the layout. Returns a dict mapping each node to an (x, y) coordinate.
positions = forceatlas2.forceatlas2_networkx_layout(G, pos=None, iterations=100)

# Visualize the graph.
nx.draw_networkx_nodes(G, positions, node_size=20, node_color="blue", alpha=0.4)
nx.draw_networkx_edges(G, positions, edge_color="green", alpha=0.05)
plt.axis("off")
plt.show()

API Overview

The main entry point is the ForceAtlas2 class:

from pyforceatlas2 import ForceAtlas2

# Initialize with desired parameters.
forceatlas2 = ForceAtlas2(
    outbound_attraction_distribution=False,
    edge_weight_influence=1.0,
    lin_log_mode=True,
    jitter_tolerance=10.0,
    barnes_hut_optimize=True,
    barnes_hut_theta=2.2,
    scaling_ratio=2.0,
    strong_gravity_mode=False,
    gravity=1.0,
    verbose=True,
)

# For a NetworkX graph:
positions = forceatlas2.forceatlas2_networkx_layout(G, pos=None, iterations=100)

# For an igraph graph:
layout_obj = forceatlas2.forceatlas2_igraph_layout(igraph_graph, pos=None, iterations=100)

References

  • ForceAtlas2 Paper:
    Jacomy, M., Venturini, T., Heymann, S., & Bastian, M. (2014).
    ForceAtlas2, a continuous graph layout algorithm for handy network visualization designed for the Gephi software.
    PLoS ONE, 9(6), e98679.
    DOI: 10.1371/journal.pone.0098679

Contributing

Contributions, bug reports, and feature requests are welcome!
Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the GNU General Public License v3.0 (GPLv3).
See the LICENSE file for details.
For more information, visit GNU GPLv3.

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

pyforceatlas2-0.0.1.tar.gz (54.1 kB view details)

Uploaded Source

Built Distribution

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

pyforceatlas2-0.0.1-py3-none-any.whl (37.6 kB view details)

Uploaded Python 3

File details

Details for the file pyforceatlas2-0.0.1.tar.gz.

File metadata

  • Download URL: pyforceatlas2-0.0.1.tar.gz
  • Upload date:
  • Size: 54.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pyforceatlas2-0.0.1.tar.gz
Algorithm Hash digest
SHA256 353835f6d0396d2d79645b14e6d7bf8f167100e79f4420049047899047c614af
MD5 0218d3ae3fac1894084a8c144dc0445d
BLAKE2b-256 e28d6cb2b8eb1f220784b5293ef4a4b524239598a200f3d703fbd40be7f49554

See more details on using hashes here.

File details

Details for the file pyforceatlas2-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyforceatlas2-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 37.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pyforceatlas2-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba3c75907f702909f2165b5bdfe190c468035afe7e664b6e57a69f2195b6dd35
MD5 5bc0ce66d8016581fcac20084c11d682
BLAKE2b-256 ba865ef2c98349394dc7fc796b61bf376a7b6d73adcd1a1d233ec33abdc5be6d

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