1
# This file allows separating the most CPU intensive routines from the
2
# main code. This allows them to be optimized with Cython. If you
3
# don't have Cython, this will run normally. However, if you use
4
# Cython, you'll get speed boosts from 10-100x automatically.
6
# The only catch is that IF YOU MODIFY THIS FILE, YOU MUST ALSO MODIFY
7
# fa2util.pxd TO REFLECT ANY CHANGES IN FUNCTION DEFINITIONS!
9
# Copyright 2016 Max Shinn <mws41@cam.ac.uk>
11
# Available under the GPLv3
15
# This will substitute for the nLayout object
26
# This is not in the original java code, but it makes it easier to
36
# Here are some functions from ForceFactor.java
37
# =============================================
39
# Repulsion function. `n1` and `n2` should be nodes. This will
40
# adjust the dx and dy values of `n1` (and optionally `n2`). It does
41
# not return anything.
42
def linRepulsion(n1, n2, coefficient=0):
45
distance2 = xDist*xDist + yDist*yDist # Distance squared
48
factor = coefficient * n1.mass * n2.mass / distance2
49
n1.dx += xDist * factor
50
n1.dy += yDist * factor
51
n2.dx -= xDist * factor
52
n2.dy -= yDist * factor
54
# Gravity repulsion function. For some reason, gravity was included
55
# within the linRepulsion function in the original gephi java code,
56
# which doesn't make any sense (considering a. gravity is unrelated to
57
# nodes repelling each other, and b. gravity is actually an
59
def linGravity(n, g, coefficient=0):
62
distance = sqrt(xDist*xDist + yDist*yDist)
65
factor = coefficient * n.mass * g / distance
66
n.dx -= xDist * factor
67
n.dy -= yDist * factor
69
# Strong gravity force function. `n` should be a node, and `g`
70
# should be a constant by which to apply the force.
71
def strongGravity(n, g, coefficient=0):
75
if xDist != 0 and yDist != 0:
76
factor = coefficient * n.mass * g
77
n.dx -= xDist * factor
78
n.dy -= yDist * factor
80
# Attraction function. `n1` and `n2` should be nodes. This will
81
# adjust the dx and dy values of `n1` (and optionally `n2`). It does
82
# not return anything.
83
def linAttraction(n1, n2, e, coefficient=0):
86
factor = -coefficient * e
87
n1.dx += xDist * factor
88
n1.dy += yDist * factor
89
n2.dx -= xDist * factor
90
n2.dy -= yDist * factor
92
def apply_repulsion(nodes, coefficient):
93
for i in range(0, len(nodes)):
95
linRepulsion(nodes[i], nodes[j], coefficient)
97
def apply_gravity(nodes, gravity, scalingRatio):
98
for i in range(0, len(nodes)):
99
linGravity(nodes[i], gravity/scalingRatio, scalingRatio)
101
def apply_attraction(nodes, edges, coefficient, edgeWeightInfluence):
102
# Optimization, since usually edgeWeightInfluence is 0 or 1, and pow is slow
103
if edgeWeightInfluence == 0:
105
linAttraction(nodes[edge.node1], nodes[edge.node2], 1, coefficient)
106
elif edgeWeightInfluence == 1:
108
linAttraction(nodes[edge.node1], nodes[edge.node2], 1, edge.weight)
111
linAttraction(nodes[edge.node1], nodes[edge.node2], pow(edge.weight, edgeWeightInfluence), coefficient)
117
print("Using compiled version of cython")
119
print("Warning: uncompiled. Compile with cython for extra speed")
121
print("No cython detected")