Skip to content

Commit

Permalink
Merge pull request #5 from WestHealth/feature/add-setter-properties-t…
Browse files Browse the repository at this point in the history
…o-physics

Feature/add setter properties to physics
  • Loading branch information
boludo00 committed May 15, 2018
2 parents 51c29e1 + b08b806 commit 3d451a0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 43 deletions.
52 changes: 41 additions & 11 deletions pyvis/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,25 +553,55 @@ def get_edges(self):
"""
return self.edges

def barnes_hut(self):
def barnes_hut(
self,
gravity=-80000,
central_gravity=0.3,
spring_length=250,
spring_strength=0.001,
damping=0.09,
overlap=0
):
"""
BarnesHut is a quadtree based gravity model. It is the fastest. default
and recommended solver for non-heirarchical layouts.
"""
self.options.physics.use_barnes_hut()
self.options.physics.use_barnes_hut(locals())

def repulsion(self):
def repulsion(
self,
node_distance=100,
central_gravity=0.2,
spring_length=200,
spring_strength=0.05,
damping=0.09
):
"""
Set the physics attribute of the entire network to repulsion.
When called, it sets the solver attribute of physics to repulsion.
"""
self.options.physics.use_repulsion()

def hrepulsion(self):
self.options.physics.use_hrepulsion()

def force_atlas_2based(self):
self.options.physics.use_force_atlas_2based()
self.options.physics.use_repulsion(locals())

def hrepulsion(
self,
node_distance=120,
central_gravity=0.0,
spring_length=100,
spring_strength=0.01,
damping=0.09
):
self.options.physics.use_hrepulsion(locals())

def force_atlas_2based(
self,
gravity=-50,
central_gravity=0.01,
spring_length=100,
spring_strength=0.08,
damping=0.4,
overlap=0
):
self.options.physics.use_force_atlas_2based(locals())

def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__,
Expand All @@ -592,7 +622,7 @@ def set_edge_smooth(self, smooth_type):
:type smooth_type: string
"""

self.options.edges.smooth.enabled = True
self.options.edges.smooth.type = smooth_type

def toggle_hide_edges_on_drag(self, status):
Expand Down
66 changes: 35 additions & 31 deletions pyvis/physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ class barnesHut(object):
This is the fastest, default and recommended.
"""

def __init__(self):
self.gravitationalConstant = -80000
self.springConstant = 0.001
self.springLength = 250
def __init__(self, params):
self.gravitationalConstant = params["gravity"]
self.centralGravity = params["central_gravity"]
self.springLength = params["spring_length"]
self.springConstant = params["spring_strength"]
self.damping = params["damping"]
self.avoidOverlap = params["overlap"]


class forceAtlas2Based(object):
"""
Expand All @@ -33,39 +37,39 @@ class forceAtlas2Based(object):
masses have a multiplier based on the amount of connected edges
plus one.
"""
def __init__(self):
self.gravitationalConstant = -50
self.centralGravity = 0.01
self.springLength = 100
self.springConstant = 0.08
self.damping = 0.4
self.avoidOverlap = 0
def __init__(self, params):
self.gravitationalConstant = params["gravity"]
self.centralGravity = params["central_gravity"]
self.springLength = params["spring_length"]
self.springConstant = params["spring_strength"]
self.damping = params["damping"]
self.avoidOverlap = params["overlap"]

class Repulsion(object):
"""
The repulsion model assumes nodes have a simplified field
around them. Its force lineraly decreases from 1
(at 0.5*nodeDistace and smaller) to 0 (at 2*nodeDistance)
"""
def __init__(self):
self.nodeDistance = 500
self.centralGravity = 0.2
self.springLength = 2000
self.springConstant = 0.0000005
self.damping = 0.09
def __init__(self, params):
self.nodeDistance = params['node_distance']
self.centralGravity = params['central_gravity']
self.springLength = params['spring_length']
self.springConstant = params['spring_strength']
self.damping = params['damping']

class hierarchicalRepulsion(object):
"""
This model is based on the repulsion solver but the levels
are taken into accound and the forces
are normalized.
"""
def __init__(self):
self.nodeDistance = 120
self.centralGravity = 0.0
self.springLength = 100
self.springConstant = 0.01
self.damping = 0.09
def __init__(self, params):
self.nodeDistance = params['node_distance']
self.centralGravity = params['central_gravity']
self.springLength = params['spring_length']
self.springConstant = params['spring_strength']
self.damping = params['damping']

class Stabilization(object):
"""
Expand All @@ -88,17 +92,17 @@ def __init__(self):
self.enabled = True
self.stabilization = self.Stabilization()

def use_barnes_hut(self):
self.barnesHut = self.barnesHut()
def use_barnes_hut(self, params):
self.barnesHut = self.barnesHut(params)

def use_force_atlas_2based(self):
self.forceAtlas2Based = self.forceAtlas2Based()
def use_force_atlas_2based(self, params):
self.forceAtlas2Based = self.forceAtlas2Based(params)

def use_repulsion(self):
self.repulsion = self.Repulsion()
def use_repulsion(self, params):
self.repulsion = self.Repulsion(params)

def use_hrepulsion(self):
self.hierarchicalRepulsion = self.hierarchicalRepulsion()
def use_hrepulsion(self, params):
self.hierarchicalRepulsion = self.hierarchicalRepulsion(params)

def toggle_stabilization(self, status):
self.stabilization.toggle_stabilization(status)
Expand Down
2 changes: 1 addition & 1 deletion pyvis/templates/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
position: relative;
overflow: hidden;
}
</style>
</style>

</head>

Expand Down

0 comments on commit 3d451a0

Please sign in to comment.