Skip to content
Snippets Groups Projects
base.py 3.09 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jarrod Pas's avatar
    Jarrod Pas committed
    from collections import defaultdict
    from itertools import product
    
    import networkx as nx
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    from kids.cache import cache
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    from pydyton.core import TickProcess
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    class EpochCommunity(TickProcess):
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        def __init__(self, epoch, **kwargs):
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            super().__init__(epoch)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
            self.graph = nx.Graph()
            self.old_graph = None
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            self.community = {}
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
        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 = {}
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            edges_to_keep = []
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            self.cache_clear()
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
            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):
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            if node not in self.community:
                self.community[node] = frozenset([node])
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            return self.community[node]
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        def cache_clear(self):
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            self.get_ld.cache_clear()
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            self.get_lp.cache_clear()
            self.get_ui.cache_clear()
            self.get_gp.cache_clear()
            self.get_ncf.cache_clear()
            self.get_cbc.cache_clear()
    
        @cache
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        def get_ld(self, x):
            ''''''
            g = self.old_graph
            if x not in g:
                return []
            c_x = self[x]
            return [
                g[x][y]['duration']
                for y in c_x
                if y in g[x] and g[x][y]['duration'] > 0
            ]
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        @cache
        def get_lp(self, x):
            '''local popularity of a node'''
            return sum(self.get_ld(x))
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        @cache
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        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]
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            return sum([
                edge['duration']
                for other, edge in edges.items()
                if other not in community
            ])
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        @cache
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        def get_ui(self, x):
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            '''unique interactions with a node'''
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            return len(self.get_ld(x))
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        @cache
        def get_cbc(self, c_x, c_y):
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            ''''''
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            if c_x == c_y:
    
                return float('inf')
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            g = self.old_graph
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            return sum([
    
    Jarrod Pas's avatar
    Jarrod Pas committed
                g[x][y]['duration']
                for x, y in product(c_x, c_y)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
                if x in g and y in g[x]
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            ])
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        @cache
        def get_ncf(self, x, c_y):
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            ''''''
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            g = self.old_graph
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            if x not in c_y or x not in g:
    
    Jarrod Pas's avatar
    Jarrod Pas committed
                return 0
            return sum([
                g[x][y]['duration']
                for y in c_y
                if y in g[x]
            ])