-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemFuncGraph.py
105 lines (90 loc) · 3.88 KB
/
MemFuncGraph.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
import math
import random
import matplotlib.pyplot as pplt
import networkx
import numpy as np
class MemFuncGraph(networkx.Graph):
def __init__(self, graphsize=30, graph=None, exp=0.0):
networkx.Graph.__init__(self)
if graph is None:
self.b = networkx.barabasi_albert_graph(graphsize, 3)
else:
self.b = graph
# self.b = networkx.grid_graph([5,5])
# self.b = networkx.cycle_graph(size)
for n in self.b.nodes_iter():
self.add_node(2 * n, type='f')
self.add_node(2 * n + 1, type='m')
self.add_edge(2 * n, 2 * n + 1, weight=1.0)
for n, nbr in self.b.edges_iter():
n_f_0 = (self.degree(2 * n) == 1)
n_m_0 = (self.degree(2 * n + 1) == 1)
nbr_f_0 = (self.degree(2 * nbr) == 1)
nbr_m_0 = (self.degree(2 * nbr + 1) == 1)
if n_f_0:
n_target = 2 * n # (n,'f')
nbr_target = 2 * nbr + 1 # (nbr,'m')
elif nbr_f_0:
n_target = 2 * n + 1 # (n, 'm')
nbr_target = 2 * nbr # (nbr, 'f')
elif n_m_0:
n_target = 2 * n + 1 # (n,'m')
nbr_target = 2 * nbr # (nbr,'f')
elif nbr_m_0:
n_target = 2 * n # (n,'f')
nbr_target = 2 * nbr + 1 # (nbr,'m')
elif random.random() < 0.5:
n_target = 2 * n # (n,'f')
nbr_target = 2 * nbr + 1 # (nbr,'m')
else:
n_target = 2 * n + 1 # (n, 'm')
nbr_target = 2 * nbr # (nbr, 'f')
if exp != 0.0:
w = np.random.exponential(scale=1.0) ** exp / math.factorial(int(math.ceil(exp)))
else:
w = 1.0
self.add_edge(n_target, nbr_target, weight=w)
def draw(self):
placement = True
shapeMap = dict()
shapeMap['f'] = 'o'
shapeMap['m'] = 's'
colorMap = dict()
colorMap['f'] = 'r'
colorMap['m'] = 'y'
for np in self.nodes(data="True"):
if 'loc' not in np[1]:
placement = False
if not placement:
nodePos = networkx.layout.spring_layout(self)
else:
nodePos = dict()
for n in self.nodes():
nodePos[n] = self.node[n]['loc']
pplt.axis('equal')
pplt.axis('off')
# For each node class...
for t in ['f', 'm']:
# ...filter and draw the subset of nodes with the same symbol in the positions that are now known through the use of the layout.
nl = [sNode[0] for sNode in filter(lambda x: x[1]["type"] == t, self.nodes(data=True))]
networkx.draw_networkx_nodes(self, nodePos,
node_color=colorMap[t],
node_shape=shapeMap[t],
node_size=10,
linewidths=0.5,
nodelist=nl)
# for aShape in nodeShapes:
# ...filter and draw the subset of nodes with the same symbol in the positions that are now known through the use of the layout.
# networkx.draw_networkx_nodes(G, nodePos, node_shape=aShape, nodelist=[sNode[0] for sNode in
# filter(lambda x: x[1]["s"] == aShape,
# G.nodes(data=True))])
# Finally, draw the edges between the nodes
networkx.draw_networkx_edges(self, nodePos, width=0.15)
if __name__ == '__main__':
import matplotlib.pyplot as plt
inst = MemFuncGraph(size=12)
# networkx.draw(inst.b)
inst.draw()
plt.show()
networkx.draw(inst)
plt.show()