## ForceAtlas2 for Python
A port of Gephi's Force Atlas 2 layout algorithm to Python 2 and Python 3 (with a wrapper for NetworkX and igraph). This is the fastest python implementation available with most of the features complete. It also supports Barnes Hut approximation for maximum speedup.
ForceAtlas2 is a very fast layout algorithm for force-directed graphs. It's used to spatialize a **weighted undirected** graph in 2D (Edge weight defines the strength of the connection). The implementation is based on this [paper](http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0098679) and the corresponding [gephi-java-code](https://github.com/gephi/gephi/blob/master/modules/LayoutPlugin/src/main/java/org/gephi/layout/plugin/forceAtlas2/ForceAtlas2.java). Its really quick compared to the fruchterman reingold algorithm (spring layout) of networkx and scales well to high number of nodes (>10000).
<p align="center" text-align="center">
<b>Spatialize a random Geometric Graph</b>
</p>
<p align="center">
<img width="460" height="300" src="https://raw.githubusercontent.com/bhargavchippada/forceatlas2/master/examples/geometric_graph.png" alt="Geometric Graph">
</p>
## Installation
Install from pip:
pip install fa2
To build and install run from source:
python setup.py install
**Cython is highly recommended if you are buidling from source as it will speed up by a factor of 10-100x depending on the graph**
### Dependencies
- numpy (adjacency matrix as complete matrix)
- scipy (adjacency matrix as sparse matrix)
- tqdm (progressbar)
- Cython (10-100x speedup)
- networkx (To use the NetworkX wrapper function, you obviously need NetworkX)
- python-igraph (To use the igraph wrapper)
<p align="center" text-align="center">
<b>Spatialize a 2D Grid</b>
</p>
<p align="center">
<img width="460" height="300" src="https://raw.githubusercontent.com/bhargavchippada/forceatlas2/master/examples/grid_graph.png" alt="Grid Graph">
</p>
## Usage
from fa2 import ForceAtlas2
Create a ForceAtlas2 object with the appropriate settings. ForceAtlas2 class contains three important methods:
```python
forceatlas2 (G, pos, iterations)
# G is a graph in 2D numpy ndarray format (or) scipy sparse matrix format. You can set the edge weights (> 0) in the matrix
# pos is a numpy array (Nx2) of initial positions of nodes
# iterations is num of iterations to run the algorithm
# returns a list of (x,y) pairs for each node's final position
```
```python
forceatlas2_networkx_layout(G, pos, iterations)
# G is a networkx graph. Edge weights can be set (if required) in the Networkx graph
# pos is a dictionary, as in networkx
# iterations is num of iterations to run the algorithm
# returns a dictionary of node positions (2D X-Y tuples) indexed by the node name
```
```python
forceatlas2_igraph_layout(G, pos, iterations, weight_attr)
# G is an igraph graph
# pos is a numpy array (Nx2) or list of initial positions of nodes (see that the indexing matches igraph node index)
# iterations is num of iterations to run the algorithm
# weight_attr denotes the weight attribute's name in G.es, None by default
# returns an igraph layout
```
Below is an example usage. You can also see the feature settings of ForceAtlas2 class.
```python
import networkx as nx
from fa2 import ForceAtlas2
import matplotlib.pyplot as plt
G = nx.random_geometric_graph(400, 0.2)
forceatlas2 = ForceAtlas2(
# Behavior alternatives
outboundAttractionDistribution=True, # Dissuade hubs
linLogMode=False, # NOT IMPLEMENTED
adjustSizes=False, # Prevent overlap (NOT IMPLEMENTED)
edgeWeightInfluence=1.0,
# Performance
jitterTolerance=1.0, # Tolerance
barnesHutOptimize=True,
barnesHutTheta=1.2,
multiThreaded=False, # NOT IMPLEMENTED
# Tuning
scalingRatio=2.0,
strongGravityMode=False,
gravity=1.0,
# Log
verbose=True)
positions = forceatlas2.forceatlas2_networkx_layout(G, pos=None, iterations=2000)
nx.draw_networkx_nodes(G, positions, node_size=20, with_labels=False, node_color="blue", alpha=0.4)
nx.draw_networkx_edges(G, positions, edge_color="green", alpha=0.05)
plt.axis('off')
plt.show()
# equivalently
import igraph
G = igraph.Graph.TupleList(G.edges(), directed=False)
layout = forceatlas2.forceatlas2_igraph_layout(G, pos=None, iterations=2000)
igraph.plot(G, layout).show()
```
You can also take a look at forceatlas2.py file for understanding the ForceAtlas2 class and its functions better.
## Features Completed
- **barnesHutOptimize**: Barnes Hut optimization, n<sup>2</sup> complexity to n.ln(n)
- **gravity**: Attracts nodes to the center. Prevents islands from drifting away
- **Dissuade Hubs**: Distributes attraction along outbound edges. Hubs attract less and thus are pushed to the borders
- **scalingRatio**: How much repulsion you want. More makes a more sparse graph
- **strongGravityMode**: A stronger gravity view
- **jitterTolerance**: How much swinging you allow. Above 1 discouraged. Lower gives less speed and more precision
- **verbose**: Shows a progressbar of iterations completed. Also, shows time taken for different force computations
- **edgeWeightInfluence**: How much influence you give to the edges weight. 0 is "no influence" and 1 is "normal"
## Documentation
You will find all the documentation in the source code
## Contributors
Contributions are highly welcome. Please submit your pull requests and become a collaborator.
## Copyright
Copyright (C) 2017 Bhargav Chippada bhargavchippada19@gmail.com.
Licensed under the GNU GPLv3.
The files are heavily based on the java files included in Gephi, git revision 2b9a7c8 and Max Shinn's port to python of the algorithm. Here I include the copyright information from those files:
Copyright 2008-2011 Gephi
Authors : Mathieu Jacomy <mathieu.jacomy@gmail.com>
Website : http://www.gephi.org
Copyright 2011 Gephi Consortium. All rights reserved.
Portions Copyrighted 2011 Gephi Consortium.
The contents of this file are subject to the terms of either the
GNU General Public License Version 3 only ("GPL") or the Common
Development and Distribution License("CDDL") (collectively, the
"License"). You may not use this file except in compliance with
the License.
<https://github.com/mwshinn/forceatlas2-python>
Copyright 2016 Max Shinn <mws41@cam.ac.uk>
Available under the GPLv3
Also, thanks to Eugene Bosiakov <https://github.com/bosiakov/fa2l>
Raw data
{
"_id": null,
"home_page": "https://github.com/bhargavchippada/forceatlas2",
"name": "fa2",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "forceatlas2,networkx,force-directed-graph,force-layout,graph",
"author": "Bhargav Chippada",
"author_email": "bhargavchippada19@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/48/d1/aa67d917628d29b83f3413790155da575865f3eab8b6e577c5871ef987d8/fa2-0.3.5.tar.gz",
"platform": "",
"description": "## ForceAtlas2 for Python\n\nA port of Gephi's Force Atlas 2 layout algorithm to Python 2 and Python 3 (with a wrapper for NetworkX and igraph). This is the fastest python implementation available with most of the features complete. It also supports Barnes Hut approximation for maximum speedup.\n\nForceAtlas2 is a very fast layout algorithm for force-directed graphs. It's used to spatialize a **weighted undirected** graph in 2D (Edge weight defines the strength of the connection). The implementation is based on this [paper](http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0098679) and the corresponding [gephi-java-code](https://github.com/gephi/gephi/blob/master/modules/LayoutPlugin/src/main/java/org/gephi/layout/plugin/forceAtlas2/ForceAtlas2.java). Its really quick compared to the fruchterman reingold algorithm (spring layout) of networkx and scales well to high number of nodes (>10000).\n\n<p align=\"center\" text-align=\"center\">\n <b>Spatialize a random Geometric Graph</b>\n</p>\n<p align=\"center\">\n <img width=\"460\" height=\"300\" src=\"https://raw.githubusercontent.com/bhargavchippada/forceatlas2/master/examples/geometric_graph.png\" alt=\"Geometric Graph\">\n</p>\n\n## Installation\n\nInstall from pip:\n\n pip install fa2\n\nTo build and install run from source:\n\n python setup.py install\n\n**Cython is highly recommended if you are buidling from source as it will speed up by a factor of 10-100x depending on the graph**\n\n### Dependencies\n\n- numpy (adjacency matrix as complete matrix)\n- scipy (adjacency matrix as sparse matrix)\n- tqdm (progressbar)\n- Cython (10-100x speedup)\n- networkx (To use the NetworkX wrapper function, you obviously need NetworkX)\n- python-igraph (To use the igraph wrapper)\n\n<p align=\"center\" text-align=\"center\">\n <b>Spatialize a 2D Grid</b>\n</p>\n<p align=\"center\">\n <img width=\"460\" height=\"300\" src=\"https://raw.githubusercontent.com/bhargavchippada/forceatlas2/master/examples/grid_graph.png\" alt=\"Grid Graph\">\n</p>\n\n## Usage\n\nfrom fa2 import ForceAtlas2\n\nCreate a ForceAtlas2 object with the appropriate settings. ForceAtlas2 class contains three important methods:\n```python\nforceatlas2 (G, pos, iterations)\n# G is a graph in 2D numpy ndarray format (or) scipy sparse matrix format. You can set the edge weights (> 0) in the matrix\n# pos is a numpy array (Nx2) of initial positions of nodes\n# iterations is num of iterations to run the algorithm\n# returns a list of (x,y) pairs for each node's final position\n```\n```python\nforceatlas2_networkx_layout(G, pos, iterations)\n# G is a networkx graph. Edge weights can be set (if required) in the Networkx graph\n# pos is a dictionary, as in networkx\n# iterations is num of iterations to run the algorithm\n# returns a dictionary of node positions (2D X-Y tuples) indexed by the node name\n```\n```python\nforceatlas2_igraph_layout(G, pos, iterations, weight_attr)\n# G is an igraph graph\n# pos is a numpy array (Nx2) or list of initial positions of nodes (see that the indexing matches igraph node index)\n# iterations is num of iterations to run the algorithm\n# weight_attr denotes the weight attribute's name in G.es, None by default\n# returns an igraph layout\n```\nBelow is an example usage. You can also see the feature settings of ForceAtlas2 class.\n\n```python\nimport networkx as nx\nfrom fa2 import ForceAtlas2\nimport matplotlib.pyplot as plt\n\nG = nx.random_geometric_graph(400, 0.2)\n\nforceatlas2 = ForceAtlas2(\n # Behavior alternatives\n outboundAttractionDistribution=True, # Dissuade hubs\n linLogMode=False, # NOT IMPLEMENTED\n adjustSizes=False, # Prevent overlap (NOT IMPLEMENTED)\n edgeWeightInfluence=1.0,\n\n # Performance\n jitterTolerance=1.0, # Tolerance\n barnesHutOptimize=True,\n barnesHutTheta=1.2,\n multiThreaded=False, # NOT IMPLEMENTED\n\n # Tuning\n scalingRatio=2.0,\n strongGravityMode=False,\n gravity=1.0,\n\n # Log\n verbose=True)\n\npositions = forceatlas2.forceatlas2_networkx_layout(G, pos=None, iterations=2000)\nnx.draw_networkx_nodes(G, positions, node_size=20, with_labels=False, node_color=\"blue\", alpha=0.4)\nnx.draw_networkx_edges(G, positions, edge_color=\"green\", alpha=0.05)\nplt.axis('off')\nplt.show()\n\n# equivalently\nimport igraph\nG = igraph.Graph.TupleList(G.edges(), directed=False)\nlayout = forceatlas2.forceatlas2_igraph_layout(G, pos=None, iterations=2000)\nigraph.plot(G, layout).show()\n```\nYou can also take a look at forceatlas2.py file for understanding the ForceAtlas2 class and its functions better.\n\n## Features Completed\n\n- **barnesHutOptimize**: Barnes Hut optimization, n<sup>2</sup> complexity to n.ln(n)\n- **gravity**: Attracts nodes to the center. Prevents islands from drifting away\n- **Dissuade Hubs**: Distributes attraction along outbound edges. Hubs attract less and thus are pushed to the borders\n- **scalingRatio**: How much repulsion you want. More makes a more sparse graph\n- **strongGravityMode**: A stronger gravity view\n- **jitterTolerance**: How much swinging you allow. Above 1 discouraged. Lower gives less speed and more precision\n- **verbose**: Shows a progressbar of iterations completed. Also, shows time taken for different force computations\n- **edgeWeightInfluence**: How much influence you give to the edges weight. 0 is \"no influence\" and 1 is \"normal\"\n\n## Documentation\n\nYou will find all the documentation in the source code\n\n## Contributors\n\nContributions are highly welcome. Please submit your pull requests and become a collaborator.\n\n## Copyright\n\n Copyright (C) 2017 Bhargav Chippada bhargavchippada19@gmail.com.\n Licensed under the GNU GPLv3.\n\nThe files are heavily based on the java files included in Gephi, git revision 2b9a7c8 and Max Shinn's port to python of the algorithm. Here I include the copyright information from those files:\n\n Copyright 2008-2011 Gephi\n Authors : Mathieu Jacomy <mathieu.jacomy@gmail.com>\n Website : http://www.gephi.org\n Copyright 2011 Gephi Consortium. All rights reserved.\n Portions Copyrighted 2011 Gephi Consortium.\n The contents of this file are subject to the terms of either the\n GNU General Public License Version 3 only (\"GPL\") or the Common\n Development and Distribution License(\"CDDL\") (collectively, the\n \"License\"). You may not use this file except in compliance with\n the License.\n\n <https://github.com/mwshinn/forceatlas2-python>\n Copyright 2016 Max Shinn <mws41@cam.ac.uk>\n Available under the GPLv3\n\n Also, thanks to Eugene Bosiakov <https://github.com/bosiakov/fa2l>\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "The fastest ForceAtlas2 algorithm for Python (and NetworkX)",
"version": "0.3.5",
"split_keywords": [
"forceatlas2",
"networkx",
"force-directed-graph",
"force-layout",
"graph"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "5da1eb976b569df3b32b2104b597daf8",
"sha256": "f4712aa75a857df35bf023e02e48739a6243a29ef95649cfc7050e18e7e8c15c"
},
"downloads": -1,
"filename": "fa2-0.3.5-cp37-cp37m-win_amd64.whl",
"has_sig": false,
"md5_digest": "5da1eb976b569df3b32b2104b597daf8",
"packagetype": "bdist_wheel",
"python_version": "cp37",
"requires_python": null,
"size": 151220,
"upload_time": "2019-01-25T14:49:33",
"upload_time_iso_8601": "2019-01-25T14:49:33.047629Z",
"url": "https://files.pythonhosted.org/packages/18/6a/8762da08a7cd4e7fdd25d51793fe6b65a90d37c17450f5d514eda84655b7/fa2-0.3.5-cp37-cp37m-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "3f6e4bd14b8a69e6c330b58fc66c4e95",
"sha256": "9a12e0a7dc228999c605beaae751424ec4a4f60281f44683c0744a72fc9141ef"
},
"downloads": -1,
"filename": "fa2-0.3.5.tar.gz",
"has_sig": false,
"md5_digest": "3f6e4bd14b8a69e6c330b58fc66c4e95",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 435436,
"upload_time": "2019-01-25T14:49:35",
"upload_time_iso_8601": "2019-01-25T14:49:35.297315Z",
"url": "https://files.pythonhosted.org/packages/48/d1/aa67d917628d29b83f3413790155da575865f3eab8b6e577c5871ef987d8/fa2-0.3.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2019-01-25 14:49:35",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "bhargavchippada",
"github_project": "forceatlas2",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "fa2"
}