Skip to content
Snippets Groups Projects
Commit e26d4bfb authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Remove old community code

parent c5ff7f8a
No related branches found
No related tags found
2 merge requests!3Version 0.2,!1Full rewrite
from .kclique import KCliqueCommunity
from .louvain import LouvainCommunity
def none(**kwargs):
return None
types = {
'kclique': KCliqueCommunity,
'louvain': LouvainCommunity,
'none': none,
}
from itertools import product
import networkx as nx
from pydtn.core import TickProcess
class EpochCommunity(TickProcess):
def __init__(self, epoch, **kwargs):
super().__init__(epoch)
self.graph = nx.Graph()
self.old_graph = None
self.community = {}
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 = {}
edges_to_keep = []
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):
if node not in self.community:
self.community[node] = frozenset([node])
return self.community[node]
def get_ld(self, x):
''''''
g = self.old_graph
if x not in g:
return []
c_x = self[x]
for y in c_x:
if y in g[x] and g[x][y]['duration'] > 0:
yield g[x][y]['duration']
def get_lp(self, x):
'''local popularity of a node'''
return sum(self.get_ld(x))
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]
return sum(
edge['duration']
for other, edge in edges.items()
if other not in community
)
def get_ui(self, x):
'''unique interactions with a node'''
return len(list(self.get_ld(x)))
def get_cbc(self, c_x, c_y):
''''''
if c_x == c_y:
return float('inf')
g = self.old_graph
return sum(
g[x][y]['duration']
for x, y in product(c_x, c_y)
if x in g and y in g[x]
)
def get_ncf(self, x, c_y):
''''''
g = self.old_graph
if x not in g or x in c_y:
return 0
return sum(
g[x][y]['duration']
for y in c_y
if y in g[x]
)
import networkx as nx
from .base import EpochCommunity
# TODO: this is probably bugged, performs terribly for bubble
class KCliqueCommunity(EpochCommunity):
def __init__(self, k=3, threshold=300, epoch=604800, **kwargs):
super().__init__(epoch, **kwargs)
self.k = k
self.threshold = threshold
def process(self, **kwargs):
while True:
yield self.tick()
g = self.next_epoch(self.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
from collections import defaultdict
from community import best_partition as louvain_partition
from .base import EpochCommunity
# TODO: look into other louvain implementations
class LouvainCommunity(EpochCommunity):
def __init__(self, epoch=604800, **kwargs):
super().__init__(epoch, **kwargs)
def process(self, **kwargs):
while True:
yield self.tick()
g = self.next_epoch(self.env.now)
partitions = louvain_partition(g, weight='duration')
communities = defaultdict(set)
for node, c in partitions.items():
communities[c].add(node)
for community in communities.values():
community = frozenset(community)
for node in community:
self.community[node] = community
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment