Newer
Older
from collections import defaultdict
from itertools import product
import networkx as nx
from community import best_partition as louvain_partition
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
self.graph = nx.Graph()
self.old_graph = None
self.community = defaultdict(frozenset)
self._cbc_memo = {}
def set_link(self, a, b, state, now):
if a not in self.graph:
self.graph.add_node(a)
if b not in self.graph:
self.graph.add_node(b)
if b not in self.graph[a]:
self.graph.add_edge(a, b, { 'start': -1 })
edge = self.graph[a][b]
if state:
edge['start'] = now
if 'duration' not in edge:
edge['duration'] = 0
else:
edge['duration'] = now - edge['start']
edge['start'] = -1
def next_epoch(self, now):
self.community = defaultdict(frozenset)
edges_to_keep = []
self._cbc_memo = {}
for a, b, start in self.graph.edges(data='start'):
if start > -1:
self.set_link(a, b, False, now)
edges_to_keep.append((a, b))
self.old_graph = self.graph
self.graph = nx.Graph()
for a, b in edges_to_keep:
self.set_link(a, b, True, now)
return self.old_graph
def __getitem__(self, node):
return self.community[node]
def get_lp(self, node):
'''local popularity of a node'''
if node not in self.old_graph:
return 0
edges = self.old_graph[node]
community = self[node]
return sum([
edge['duration']
for other, edge in edges.items()
if other in community
])
def get_gp(self, node):
'''global popularity of a node'''
if node not in self.old_graph:
return 0
edges = self.old_graph[node]
community = self[node]
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
return sum([
edge['duration']
for other, edge in edges.items()
if other not in community
])
def get_ui(self, node):
'''unique interactions with a node'''
if node not in self.old_graph:
return 0
edges = self.old_graph[node]
community = self[node]
return len([
other
for other in edges
if other in community
])
def get_cbc(self, a, b):
g = self.old_graph
c_x = self[a]
c_y = self[b]
memo = (c_x, c_y)
if a not in g or b not in g or c_x == c_y:
return 0
if memo in self._cbc_memo:
return self._cbc_memo[memo]
cbc = sum([
g[x][y]['duration']
for x, y in product(c_x, c_y)
if y in g[x]
])
'''
for x in c_x:
for y in c_y:
if x in g[y]:
cbc += g[x][y]['duration']
'''
self._cbc_memo[memo] = cbc
self._cbc_memo[(memo[1], memo[0])] = cbc
return cbc
def get_ncf(self, x, b):
g = self.old_graph
c_y = self[b]
if x not in g or b not in g:
return 0
return sum([
g[x][y]['duration']
for y in c_y
if y in g[x]
])
class KCliqueCommunity(EpochCommunity):
def __init__(self, k=3, threshold=300, epoch=604800, **kwargs):
super().__init__(epoch=epoch, **kwargs)
self.k = k
self.threshold = threshold
g = self.next_epoch(env.now)
G = nx.Graph()
G.add_nodes_from(g.nodes())
for a, b, duration in g.edges(data='duration'):
if duration > self.threshold:
G.add_edge(a, b)
for community in nx.k_clique_communities(G, self.k):
for node in community:
self.community[node] = community
class LouvainCommunity(EpochCommunity):
def __init__(self, epoch=604800, **kwargs):
super().__init__(epoch=epoch, **kwargs)
g = self.next_epoch(env.now)
p = louvain_partition(g, weight='duration')
communities = defaultdict(set)
for node, c in louvain_partition(g, weight='duration').items():
communities[c].add(node)
for community in communities.values():
community = frozenset(community)
for node in community:
self.community[node] = community
def none(**kwargs):
return None
types = {
'kclique': KCliqueCommunity,
'louvain': LouvainCommunity,
'none': none,
}