def draw_example_meta_graph(output_path):
    _, _, ints = load_meta_graph_necessities()
    g = IU.get_meta_graph(ints, decompose_interactions=True)
    plt.clf()
    pos = nx.spring_layout(g)
    nx.draw_networkx_nodes(g, pos=pos)
    nx.draw_networkx_edges(g, pos=pos)
    nx.draw_networkx_labels(g,
                            pos=pos,
                            labels=dict(zip(g.nodes(), g.nodes())),
                            font_size=8)
    plt.savefig(output_path)
Esempio n. 2
0
def draw_example_meta_graph(output_path):
    _, _, ints = load_meta_graph_necessities()
    g = IU.get_meta_graph(
        ints,
        decompose_interactions=True
    )
    plt.clf()
    pos = nx.spring_layout(g)
    nx.draw_networkx_nodes(g, pos=pos)
    nx.draw_networkx_edges(g, pos=pos)
    nx.draw_networkx_labels(g, pos=pos,
                            labels=dict(zip(g.nodes(), g.nodes())),
                            font_size=8)
    plt.savefig(output_path)
def extract_event_context(interactions, event_tree, undirected=False):
    span = MetaGraphStat(event_tree).time_span()
    start = span['start_time']
    end = span['end_time']

    filtered_interactions = []
    for i in interactions:
        assert 'datetime' in i
        dt = i['datetime']
        assert isinstance(dt, datetime)
        if dt >= start and dt <= end:
            filtered_interactions.append(i)
    context_dag = IU.get_meta_graph(filtered_interactions,
                                    decompose_interactions=False,
                                    remove_singleton=True,
                                    undirected=undirected)
    return context_dag
Esempio n. 4
0
def test_gen_event_with_known_tree_structure():
    event_size = 100
    participants_n = 10
    event = gen_event_with_known_tree_structure(
        event_size=event_size,
        participants=range(participants_n),
        start_time=10, end_time=110,
        event_topic_param=random_topic(10, topic_noise=0.0001)[0],
        topic_noise=1,
        alpha=1.0, tau=0.8,
        forward_proba=0.3,
        reply_proba=0.5,
        create_new_proba=0.2
    )

    for n in event.nodes_iter():
        sid, rid = event.node[n]['sender_id'], event.node[n]['recipient_ids'][0]
        assert_true(sid != rid)

    for s, t in event.edges_iter():
        sid1, rid1 = event.node[s]['sender_id'], event.node[s]['recipient_ids'][0]
        sid2, rid2 = event.node[t]['sender_id'], event.node[t]['recipient_ids'][0]
        c_type = event[s][t]['c_type']
        if c_type == 'r':
            assert_equal(sid1, rid2)
            assert_equal(sid2, rid1)
        elif c_type == 'f':
            assert_equal(rid1, sid2)
            assert_true(rid2 != sid1)
        else:
            assert_equal(sid1, sid2)

    interactions = [event.node[n] for n in event.nodes_iter()]
    g = IU.get_meta_graph(
        interactions,
        decompose_interactions=False,
        remove_singleton=True,
        given_topics=True,
        convert_time=False
    )
    assert_equal(1, len(get_roots(g)))
    assert_equal(event_size, len(interactions))
    
    assert_true(nx.is_arborescence(event))
Esempio n. 5
0
    def test_make_artificial_data(self):
        events, all_interactions, params = make_artificial_data(**self.params)
        assert_equal(self.params['n_events'],
                     len(params))
        assert_equal(
            self.params['n_events'],
            len(events)
        )
        assert_equal(
            self.params['event_size_mu'] * self.params['n_events'] +
            self.params['n_noisy_interactions'],
            len(all_interactions)
        )
        for i in all_interactions:
            assert_true('message_id' in i)
            # make sure it's jsonable
            assert_true(isinstance(i['topics'], list))

        # all ids are unique
        all_ids = list(itertools.chain(*[e.nodes() for e in events]))
        assert_equal(len(all_ids), len(set(all_ids)))

        for e in events:
            # make sure nodes are relabeled
            for n in e.nodes_iter():
                assert_equal(n, e.node[n]['message_id'])

            interactions = [e.node[n] for n in e.nodes_iter()]
            assert_equal(len(interactions),
                         IU.get_meta_graph(
                             interactions,
                             decompose_interactions=False,
                             remove_singleton=True,
                             given_topics=True).number_of_nodes())
            for i in interactions:
                assert_true(isinstance(i['topics'], list))

        for i in all_interactions:
            assert_true(i['sender_id'].startswith('u-'))
Esempio n. 6
0
def random_events(n_events, event_size_mu, event_size_sigma,
                  n_total_participants, participant_mu, participant_sigma,
                  min_time, max_time, event_duration_mu, event_duration_sigma,
                  n_topics, topic_scaling_factor, topic_noise,
                  alpha, tau,
                  forward_proba,
                  reply_proba,
                  create_new_proba,
                  taboo_topics=set(),
                  accumulate_taboo=False):
    # add main events
    events = []
    taboo_topics = set(taboo_topics)
    
    for i in xrange(n_events):
        # randomly select a topic and add some noise to it
        event = []

        event_topic_param, topic_id = random_topic(
            n_topics,
            topic_noise,
            taboo_topics
        )

        if accumulate_taboo:
            taboo_topics.add(topic_id)

        print('event_topic_param:', event_topic_param)
        event_size = 0
        while event_size <= 0:
            event_size = int(round(
                np.random.normal(event_size_mu, event_size_sigma)
            ))
        assert event_size > 0

        # randomly select participants
        n_participants = 0
        while n_participants <= 2:
            n_participants = int(round(
                np.random.normal(participant_mu, participant_sigma)
            ))
        assert n_participants > 2
        
        participants = np.random.permutation(
            n_total_participants
        )[:n_participants]
        print('participants:', participants)

        # event timespan
        start_time = np.random.uniform(min_time, max_time - event_duration_mu)
        end_time = start_time + np.random.normal(event_duration_mu,
                                                 event_duration_sigma)
        if end_time > max_time:
            end_time = max_time

        event = gen_event_with_known_tree_structure(
            event_size, participants, start_time, end_time,
            event_topic_param,
            topic_noise,
            alpha, tau,
            forward_proba,
            reply_proba,
            create_new_proba
        )

        # some checking
        g = IU.get_meta_graph(
            [event.node[n] for n in event.nodes_iter()],
            decompose_interactions=False,
            remove_singleton=True,
            given_topics=True,
            convert_time=False)
        n_interactions_in_mg = g.number_of_nodes()

        if n_interactions_in_mg == len(event):
            roots = [n
                     for n, d in g.in_degree(g.nodes_iter()).items()
                     if d == 0]
            if len(roots) > 1:
                print(roots)
                for r in roots:
                    print(event[r])
                print("WARNING: roots number {}".format(len(roots)))
                raise
        else:
            print(
                'invalid meta graph. {} < {}'.format(
                    n_interactions_in_mg,
                    len(event)
                ))
            raise
        events.append(event)

    return events, taboo_topics
Esempio n. 7
0
def random_events(n_events,
                  event_size_mu,
                  event_size_sigma,
                  n_total_participants,
                  participant_mu,
                  participant_sigma,
                  min_time,
                  max_time,
                  event_duration_mu,
                  event_duration_sigma,
                  n_topics,
                  topic_scaling_factor,
                  topic_noise,
                  alpha,
                  tau,
                  forward_proba,
                  reply_proba,
                  create_new_proba,
                  taboo_topics=set(),
                  accumulate_taboo=False):
    # add main events
    events = []
    taboo_topics = set(taboo_topics)

    for i in xrange(n_events):
        # randomly select a topic and add some noise to it
        event = []

        event_topic_param, topic_id = random_topic(n_topics, topic_noise,
                                                   taboo_topics)

        if accumulate_taboo:
            taboo_topics.add(topic_id)

        print('event_topic_param:', event_topic_param)
        event_size = 0
        while event_size <= 0:
            event_size = int(
                round(np.random.normal(event_size_mu, event_size_sigma)))
        assert event_size > 0

        # randomly select participants
        n_participants = 0
        while n_participants <= 2:
            n_participants = int(
                round(np.random.normal(participant_mu, participant_sigma)))
        assert n_participants > 2

        participants = np.random.permutation(
            n_total_participants)[:n_participants]
        print('participants:', participants)

        # event timespan
        start_time = np.random.uniform(min_time, max_time - event_duration_mu)
        end_time = start_time + np.random.normal(event_duration_mu,
                                                 event_duration_sigma)
        if end_time > max_time:
            end_time = max_time

        event = gen_event_with_known_tree_structure(event_size, participants,
                                                    start_time, end_time,
                                                    event_topic_param,
                                                    topic_noise, alpha, tau,
                                                    forward_proba, reply_proba,
                                                    create_new_proba)

        # some checking
        g = IU.get_meta_graph([event.node[n] for n in event.nodes_iter()],
                              decompose_interactions=False,
                              remove_singleton=True,
                              given_topics=True,
                              convert_time=False)
        n_interactions_in_mg = g.number_of_nodes()

        if n_interactions_in_mg == len(event):
            roots = [
                n for n, d in g.in_degree(g.nodes_iter()).items() if d == 0
            ]
            if len(roots) > 1:
                print(roots)
                for r in roots:
                    print(event[r])
                print("WARNING: roots number {}".format(len(roots)))
                raise
        else:
            print('invalid meta graph. {} < {}'.format(n_interactions_in_mg,
                                                       len(event)))
            raise
        events.append(event)

    return events, taboo_topics