forceatlas2_layout

forceatlas2_layout#

graph_tool.draw.forceatlas2_layout(g, weight=None, pos=None, dim=2, mass=None, scaling=2.0, gravity=1.0, strong_gravity=False, linlog=False, dissuade_hubs=False, edge_weight_influence=1.0, jitter_tolerance=1.0, theta=1.2, max_iter=1000)[git][source]#

Calculate a force-directed layout of the graph with the ForceAtlas2 algorithm.

Parameters:
gGraph

Graph to be used (the edge directions are ignored).

weightEdgePropertyMap (optional, default: None)

Non-negative edge weights, which multiply the attraction between the endpoints. If not given, every edge has unit weight.

posVertexPropertyMap (optional, default: None)

Initial positions, which are modified in place. If not given, random positions in a square (or cube) of side \(\sqrt{V}\) are used.

dimint (optional, default: 2)

Number of dimensions.

massVertexPropertyMap (optional, default: None)

Positive vertex masses. If not given, the mass of a vertex is its degree plus one.

scalingfloat (optional, default: 2.)

Strength \(k_r\) of the repulsion, which sets the scale of the layout.

gravityfloat (optional, default: 1.)

Strength \(k_g\) of the attraction towards the origin.

strong_gravitybool (optional, default: False)

If True, the gravity grows linearly with the distance to the origin.

linlogbool (optional, default: False)

If True, the attraction grows logarithmically with the distance (the LinLog model), which separates clusters more.

dissuade_hubsbool (optional, default: False)

If True, the attraction on a vertex is divided by its mass, which pushes the vertices of high degree to the periphery.

edge_weight_influencefloat (optional, default: 1.)

Exponent \(\delta\) applied to the edge weights.

jitter_tolerancefloat (optional, default: 1.)

Tolerance of the oscillations of the vertices, which controls the trade-off between speed and precision.

thetafloat (optional, default: 1.2)

Opening angle of the Barnes-Hut approximation of the repulsion, in two dimensions. If 0, all pairs are summed exactly.

max_iterint (optional, default: 1000)

Number of iterations.

Returns:
posVertexPropertyMap

The vertex positions.

See also

sfdp_layout

Multilevel force-directed layout

fruchterman_reingold_layout

Fruchterman-Reingold spring-block layout

graphopt_layout

GraphOpt force-directed layout

drl_layout

DrL force-directed layout

Notes

In the force-directed ForceAtlas2 model [jacomy-forceatlas2-2014], a vertex \(i\) of mass \(m_i\) is repelled from every other vertex \(j\) with the force \(k_r m_i m_j / d_{ij}\), attracted to its neighbors with the force \(w_{ij}^\delta d_{ij}\) (or \(w_{ij}^\delta \log(1 + d_{ij})\) with linlog=True, and divided by \(m_i\) with dissuade_hubs=True), and attracted to the origin with the force \(k_g m_i\) (or \(k_g m_i d_i\) with strong_gravity=True), where \(d\) are the Euclidean distances. In every iteration, all vertices move at once along their total forces, \(\boldsymbol x_i \leftarrow \boldsymbol x_i + s_i\boldsymbol F_i\), with the local speed \(s_i = s / (1 + \sqrt{s\,\mathrm{swg}_i})\), where \(\mathrm{swg}_i = m_i|\boldsymbol F_i - \boldsymbol F_i'|\) is the “swinging” of the vertex, i.e. the change of its force from the previous iteration (with \(\boldsymbol F_i' = 0\) in the first one). The global speed \(s\), initially one, follows the reference implementation in Gephi: with the total swinging \(S = \sum_i \mathrm{swg}_i\), the total “traction” \(T = \sum_i m_i|\boldsymbol F_i + \boldsymbol F_i'|/2\) and the tolerance

\[\tau = \tau_J\max\left(\sqrt{0.05\sqrt V}, \min\left(10, \frac{0.05\sqrt V\,T}{V^2}\right)\right),\]

where \(\tau_J\) is jitter_tolerance, it is updated as

\[s \leftarrow s + \min\left(\frac{\tau\eta T}{S} - s, \frac{s}{2}\right),\]

where the efficiency \(\eta\), initially one, is halved before the update when \(S > 2T\) (in which case \(\tau\) is also raised to at least \(\tau_J\)), and, once \(\tau\eta T/S\) is computed, multiplied by 0.7 if \(S > \tau T\), or otherwise by 1.3 if \(s < 1000\) (with \(s\) before the update); the reductions of \(\eta\) are made only while \(\eta > 0.05\). The iterations stop after max_iter iterations, or when \(S = 0\). If pos is not given, the initial positions are drawn uniformly in a square (or cube) of side \(\sqrt V\) centered at the origin.

In two dimensions, the repulsion is computed with the Barnes-Hut approximation [barnes-hierarchical-1986], so that every iteration takes time \(O(V\log V + E)\); otherwise, or with theta=0, it takes time \(O(V^2 + E)\). The initial positions are drawn with NumPy’s random number generator.

Parallel implementation.

If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.

References

[jacomy-forceatlas2-2014]

Mathieu Jacomy, Tommaso Venturini, Sebastien Heymann and Mathieu Bastian, “ForceAtlas2, a continuous graph layout algorithm for handy network visualization designed for the Gephi software”, PLoS ONE 9, e98679 (2014), DOI: 10.1371/journal.pone.0098679 [sci-hub, @tor]

[barnes-hierarchical-1986]

Josh Barnes and Piet Hut, “A hierarchical O(N log N) force-calculation algorithm”, Nature 324, 446-449 (1986), DOI: 10.1038/324446a0 [sci-hub, @tor]

Examples

>>> g = gt.collection.data["football"]
>>> pos = gt.forceatlas2_layout(g)
>>> gt.graph_draw(g, pos=pos, vertex_fill_color=g.vp.value,
...               output="football-forceatlas2.pdf")
<...>
../_images/football-forceatlas2.png

ForceAtlas2 layout of the network of American college football games, with the vertex colors given by the conferences.#