forked from ThomWorm/Pandemics_Graph_Viz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hierarchy_pos.py
119 lines (97 loc) · 5.32 KB
/
hierarchy_pos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import networkx as nx
def hierarchy_pos(G, root=None, width=1., vert_gap = 0.2, vert_loc = 0, leaf_vs_root_factor = 0.5):
'''
If the graph is a tree this will return the positions to plot this in a
hierarchical layout.
Based on Joel's answer at https://stackoverflow.com/a/29597209/2966723,
but with some modifications.
We include this because it may be useful for plotting transmission trees,
and there is currently no networkx equivalent (though it may be coming soon).
There are two basic approaches we think of to allocate the horizontal
location of a node.
- Top down: we allocate horizontal space to a node. Then its ``k``
descendants split up that horizontal space equally. This tends to result
in overlapping nodes when some have many descendants.
- Bottom up: we allocate horizontal space to each leaf node. A node at a
higher level gets the entire space allocated to its descendant leaves.
Based on this, leaf nodes at higher levels get the same space as leaf
nodes very deep in the tree.
We use use both of these approaches simultaneously with ``leaf_vs_root_factor``
determining how much of the horizontal space is based on the bottom up
or top down approaches. ``0`` gives pure bottom up, while 1 gives pure top
down.
:Arguments:
**G** the graph (must be a tree)
**root** the root node of the tree
- if the tree is directed and this is not given, the root will be found and used
- if the tree is directed and this is given, then the positions will be
just for the descendants of this node.
- if the tree is undirected and not given, then a random choice will be used.
**width** horizontal space allocated for this branch - avoids overlap with other branches
**vert_gap** gap between levels of hierarchy
**vert_loc** vertical location of root
**leaf_vs_root_factor**
xcenter: horizontal location of root
'''
if not nx.is_tree(G):
raise TypeError('cannot use hierarchy_pos on a graph that is not a tree')
if root is None:
if isinstance(G, nx.DiGraph):
root = next(iter(nx.topological_sort(G))) #allows back compatibility with nx version 1.11
else:
root = random.choice(list(G.nodes))
def _hierarchy_pos(G, root, leftmost, width, leafdx = 0.2, vert_gap = 0.2, vert_loc = 0,
xcenter = 0.5, rootpos = None,
leafpos = None, parent = None):
'''
see hierarchy_pos docstring for most arguments
pos: a dict saying where all nodes go if they have been assigned
parent: parent of this branch. - only affects it if non-directed
'''
if rootpos is None:
rootpos = {root:(xcenter,vert_loc)}
else:
rootpos[root] = (xcenter, vert_loc)
if leafpos is None:
leafpos = {}
children = list(G.neighbors(root))
leaf_count = 0
if not isinstance(G, nx.DiGraph) and parent is not None:
children.remove(parent)
if len(children)!=0:
rootdx = width/len(children)
nextx = xcenter - width/2 - rootdx/2
for child in children:
nextx += rootdx
rootpos, leafpos, newleaves = _hierarchy_pos(G,child, leftmost+leaf_count*leafdx,
width=rootdx, leafdx=leafdx,
vert_gap = vert_gap, vert_loc = vert_loc-vert_gap,
xcenter=nextx, rootpos=rootpos, leafpos=leafpos, parent = root)
leaf_count += newleaves
leftmostchild = min((x for x,y in [leafpos[child] for child in children]))
rightmostchild = max((x for x,y in [leafpos[child] for child in children]))
leafpos[root] = ((leftmostchild+rightmostchild)/2, vert_loc)
else:
leaf_count = 1
leafpos[root] = (leftmost, vert_loc)
# pos[root] = (leftmost + (leaf_count-1)*dx/2., vert_loc)
# print(leaf_count)
return rootpos, leafpos, leaf_count
xcenter = width/2.
if isinstance(G, nx.DiGraph):
leafcount = len([node for node in nx.descendants(G, root) if G.out_degree(node)==0])
elif isinstance(G, nx.Graph):
leafcount = len([node for node in nx.node_connected_component(G, root) if G.degree(node)==1 and node != root])
rootpos, leafpos, leaf_count = _hierarchy_pos(G, root, 0, width,
leafdx=width*1./leafcount,
vert_gap=vert_gap,
vert_loc = vert_loc,
xcenter = xcenter)
pos = {}
for node in rootpos:
pos[node] = (leaf_vs_root_factor*leafpos[node][0] + (1-leaf_vs_root_factor)*rootpos[node][0], leafpos[node][1])
# pos = {node:(leaf_vs_root_factor*x1+(1-leaf_vs_root_factor)*x2, y1) for ((x1,y1), (x2,y2)) in (leafpos[node], rootpos[node]) for node in rootpos}
xmax = max(x for x,y in pos.values())
for node in pos:
pos[node]= (pos[node][0]*width/xmax, pos[node][1])
return pos