def main1():
    C, P, T1, T2 = ('CEO', 'PM', 'T1', 'T2')
    p = 'progress'
    s = 'suggetion'
    f = 'football'
    correct_edge_to_color = {
        ('a', 'b'): 'red',
        ('b', 'c'): 'red',
        ('c', 'd'): 'red',
        ('e', 'f'): 'green'
    }

    interactions = [('a', C, [P], p, 1), ('b', P, [T1, T2], p, 2),
                    ('c', T1, [P], p, 3), ('d', P, [C], p, 4),
                    ('e', T2, [P], s, 3), ('f', P, [C], p, 5),
                    ('g', T2, [T1], f, 4)]
    new_interactions = []
    for msg_id, sender, recs, topic, time in interactions:
        new_interactions.append(
            {
                'sender_id': sender,
                'recipient_ids': recs,
                'datetime': time,
                'message_id': msg_id
            }, )

    node_names, sources, targets, time_stamps = InteractionsUtil.unzip_interactions(
        new_interactions)
    graph = convert_to_meta_graph(node_names, sources, targets, time_stamps)
    # nx.write_dot(graph, 'tmp/illustration.dot')
    print """digraph {
    node [fontsize=20];
"""
    for u, v in graph.edges():
        print "{} -> {}[color={}];".format(
            u,
            v,
            # correct_edge_to_color.get((u, v), 'gray')
            'black')
    print "}"

    df = pd.DataFrame(new_interactions,
                      columns=['sender_id', 'recipient_ids', 'datetime'],
                      index=[i[0] for i in interactions])
    df = df.rename(columns={
        'sender_id': 'sender',
        'recipient_ids': 'recipients',
        'datetime': 'time'
    })

    mapping = {1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri'}
    df['time'] = df['time'].map(lambda t: mapping[t])
    df.to_latex('tmp/example.tex')
示例#2
0
def main():
    K, M, C, H = 'K', 'M', 'C', 'H'
    interactions = [
        {'sender_id': K, 'recipient_ids': (M, C), 'datetime': 1, 'message_id': 'K->(M, C): code(1)'},
        {'sender_id': M, 'recipient_ids': [K], 'datetime': 3, 'message_id': 'M->K: read(3)'},
        {'sender_id': K, 'recipient_ids': [M], 'datetime': 4, 'message_id': 'K->M: read(3)'},
        {'sender_id': C, 'recipient_ids': [H], 'datetime': 2, 'message_id': 'C->H: eat(2)'},
        {'sender_id': H, 'recipient_ids': [C], 'datetime': 3, 'message_id': 'H->C: eat(2)'},
    ]
    InteractionsUtil.decompose_interactions(interactions)
    node_names, sources, targets, time_stamps = InteractionsUtil.unzip_interactions(
        InteractionsUtil.decompose_interactions(interactions)
    )
    graph = convert_to_meta_graph(node_names, sources, targets, time_stamps)

    print graph.edges()
def main():
    K, M, C, H = 'K', 'M', 'C', 'H'
    interactions = [
        {
            'sender_id': K,
            'recipient_ids': (M, C),
            'datetime': 1,
            'message_id': 'K->(M, C): code(1)'
        },
        {
            'sender_id': M,
            'recipient_ids': [K],
            'datetime': 3,
            'message_id': 'M->K: read(3)'
        },
        {
            'sender_id': K,
            'recipient_ids': [M],
            'datetime': 4,
            'message_id': 'K->M: read(3)'
        },
        {
            'sender_id': C,
            'recipient_ids': [H],
            'datetime': 2,
            'message_id': 'C->H: eat(2)'
        },
        {
            'sender_id': H,
            'recipient_ids': [C],
            'datetime': 3,
            'message_id': 'H->C: eat(2)'
        },
    ]
    InteractionsUtil.decompose_interactions(interactions)
    node_names, sources, targets, time_stamps = InteractionsUtil.unzip_interactions(
        InteractionsUtil.decompose_interactions(interactions))
    graph = convert_to_meta_graph(node_names, sources, targets, time_stamps)

    print graph.edges()
示例#4
0
    def get_meta_graph(cls, interactions,
                       undirected=False,
                       preprune_secs=None,
                       decompose_interactions=True,
                       remove_singleton=True,
                       given_topics=False,
                       apply_pagerank=False,
                       convert_time=True):
        """
        Return the meta graph together with temporally sorted interactions
        
        Decompose interactions if requested
        """
        if decompose_interactions:
            if undirected:
                raise ValueError('Non-sense to deompose for undirected graph')

            logger.info("decomposing and cleaning interactions...")
            interactions = cls.decompose_interactions(
                cls.clean_interactions(
                    interactions,
                    undirected=undirected,
                    convert_time=convert_time
                )
            )
        else:
            logger.info("cleaning interactions...")
            interactions = cls.clean_interactions(
                interactions,
                undirected=undirected,
                convert_time=convert_time
            )

        if not undirected:
            logger.info('processing **directed** interactions')
            g = convert_to_meta_graph(*cls.unzip_interactions(interactions),
                                      preprune_secs=preprune_secs)
        else:
            logger.info('processing **undirected** interactions')
            g = convert_to_meta_graph_undirected(
                *cls.unzip_interactions_undirected(interactions),
                preprune_secs=preprune_secs
            )
        for i in interactions:
            n = i['message_id']
            if decompose_interactions:
                g.node[n]['message_id'] = i['original_message_id']
            else:
                g.node[n]['message_id'] = i['message_id']

            if not given_topics:
                g.node[n]['body'] = i['body']
                g.node[n]['subject'] = i['subject']
            else:
                g.node[n]['topics'] = i['topics']
                    
            g.node[n]['datetime'] = i['datetime']

            if 'hashtags' in i:
                g.node[n]['hashtags'] = i['hashtags']

            g.node[n][cls.VERTEX_REWARD_KEY] = 1

            if decompose_interactions:
                g.node[n]['peers'] = i['peers']

            if undirected:
                g.node[n]['participant_ids'] = i['participant_ids']
            else:
                g.node[n]['sender_id'] = i['sender_id']
                g.node[n]['recipient_ids'] = i['recipient_ids']
        
        if remove_singleton:
            for n in g.nodes():
                if g.degree(n) == 0:
                    g.remove_node(n)

        # override reward scores
        if apply_pagerank:
            logger.info('Appling pagerank to get node rewark')
            g = cls.add_rewards_to_nodes_using_pagerank(g, interactions)
        
        return g
示例#5
0
def main1():
    C, P, T1, T2 = ('CEO', 'PM', 'T1', 'T2')
    p = 'progress'
    s = 'suggetion'
    f = 'football'
    correct_edge_to_color = {
        ('a', 'b'): 'red',
        ('b', 'c'): 'red',
        ('c', 'd'): 'red',
        ('e', 'f'): 'green'
    }

    interactions = [
        ('a', C, [P], p, 1),
        ('b', P, [T1, T2], p, 2),
        ('c', T1, [P], p, 3),
        ('d', P, [C], p, 4),
        ('e', T2, [P], s, 3),
        ('f', P, [C], p, 5),
        ('g', T2, [T1], f, 4)
    ]
    new_interactions = []
    for msg_id, sender, recs, topic, time in interactions:
        new_interactions.append(
            {'sender_id': sender,
             'recipient_ids': recs,
             'datetime': time,
             'message_id': msg_id},
        )
    
    node_names, sources, targets, time_stamps = InteractionsUtil.unzip_interactions(
        new_interactions
    )
    graph = convert_to_meta_graph(node_names, sources, targets, time_stamps)
    # nx.write_dot(graph, 'tmp/illustration.dot')
    print """digraph {
    node [fontsize=20];
"""
    for u, v in  graph.edges():
        print "{} -> {}[color={}];".format(
            u, v,
            # correct_edge_to_color.get((u, v), 'gray')
            'black'
        )
    print "}"
    
    df = pd.DataFrame(new_interactions,
                      columns=['sender_id', 'recipient_ids', 'datetime'],
                      index=[i[0] for i in interactions])
    df = df.rename(columns={'sender_id': 'sender',
                       'recipient_ids': 'recipients',
                       'datetime': 'time'})

    mapping = {
        1: 'Mon',
        2: 'Tue',
        3: 'Wed',
        4: 'Thu',
        5: 'Fri'
    }
    df['time'] = df['time'].map(lambda t: mapping[t])
    df.to_latex('tmp/example.tex')