Skip to content
Snippets Groups Projects
louvain.py 849 B
Newer Older
  • Learn to ignore specific revisions
  • Jarrod Pas's avatar
    Jarrod Pas committed
    from collections import defaultdict
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    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()
    
    Jarrod Pas's avatar
    Jarrod Pas committed
                g = self.next_epoch(self.env.now)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
                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