Skip to content
Snippets Groups Projects
kclique.py 823 B
Newer Older
  • Learn to ignore specific revisions
  • Jarrod Pas's avatar
    Jarrod Pas committed
    import networkx as nx
    
    from .base import EpochCommunity
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    # TODO: this is probably bugged, performs terribly for bubble
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    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()
    
    Jarrod Pas's avatar
    Jarrod Pas committed
                g = self.next_epoch(self.env.now)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
                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