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

Modularize communities

parent cc0c3a3a
No related branches found
No related tags found
No related merge requests found
from .kclique import KCliqueCommunity
from .louvain import LouvainCommunity
def none(**kwargs):
return None
types = {
'kclique': KCliqueCommunity,
'louvain': LouvainCommunity,
'none': none,
}
......@@ -2,18 +2,18 @@ from collections import defaultdict
from itertools import product
import networkx as nx
from community import best_partition as louvain_partition
from .core import TickProcess
from pydyton.core import TickProcess
class EpochCommunity(TickProcess):
def __init__(self, epoch=1, **kwargs):
def __init__(self, epoch, **kwargs):
super().__init__(epoch)
self.graph = nx.Graph()
self.old_graph = None
self.community = defaultdict(frozenset)
self._cbc_memo = {}
self.__cbc_memo = {}
def set_link(self, a, b, state, now):
if a not in self.graph:
......@@ -37,7 +37,7 @@ class EpochCommunity(TickProcess):
def next_epoch(self, now):
self.community = defaultdict(frozenset)
edges_to_keep = []
self._cbc_memo = {}
self.__cbc_memo = {}
for a, b, start in self.graph.edges(data='start'):
if start > -1:
......@@ -94,6 +94,7 @@ class EpochCommunity(TickProcess):
])
def get_cbc(self, a, b):
''''''
g = self.old_graph
c_x = self[a]
c_y = self[b]
......@@ -102,8 +103,8 @@ class EpochCommunity(TickProcess):
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]
if memo in self.__cbc_memo:
return self.__cbc_memo[memo]
cbc = sum([
g[x][y]['duration']
......@@ -111,18 +112,12 @@ class EpochCommunity(TickProcess):
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
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]
......@@ -135,56 +130,3 @@ class EpochCommunity(TickProcess):
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
def process(self, network):
while True:
yield self.env.timeout(self.tick)
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)
def process(self, network):
while True:
yield self.env.timeout(self.tick)
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,
}
import networkx as nx
from .base import EpochCommunity
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, network):
while True:
yield self.tick()
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
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, network):
while True:
yield self.tick()
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
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