def run_with_context(interactions_path, candidate_tree_path, dirname=None, to_original_graph=False, undirected=False):
    if not os.path.exists(dirname):
        os.makedirs(dirname)

    try:
        interactions = json.load(open(interactions_path))
    except ValueError as e:
        print(e)
        interactions = load_json_by_line(interactions_path)

    interactions = IU.clean_interactions(interactions, undirected=undirected)

    output_path = get_output_path(candidate_tree_path, dirname)

    K = 5
    events = detect_events_given_path(candidate_tree_path, K)

    contexted_events = []
    for e in events:
        context_dag = extract_event_context(interactions, e, undirected=undirected)

        if to_original_graph:
            context_dag = convert_to_original_graph(context_dag)
            e = convert_to_original_graph(e)

        contexted_events.append(add_subgraph_specific_attributes_to_graph(context_dag, [(e, {"event": True})]))
    d3_events = [to_d3_graph(ce) for ce in contexted_events]

    print("writing to {}".format(output_path))
    json_dump(d3_events, output_path)
def run(candidate_tree_path,
        k,
        id2people,
        id2interaction,
        dirname=None,
        to_original_graph=False):

    if dirname and not os.path.exists(dirname):
        os.makedirs(dirname)

    output_path = get_output_path(candidate_tree_path, dirname)

    events = detect_events_given_path(candidate_tree_path, k)
    
    # add people and content
    for e in events:
        root = get_roots(e)[0]
        for n in e.nodes_iter():
            e.node[n]['sender'] = id2people[e.node[n]['sender_id']]
            e.node[n]['recipients'] = [id2people[id_]
                                       for id_ in e.node[n]['recipient_ids']]
            # print(id2interaction[n])
            e.node[n]['subject'] = id2interaction[n]['subject']
            e.node[n]['body'] = id2interaction[n]['body']

            for f in ('retweet_count', 'favorite_count'):
                e.node[n][f] = id2interaction[n].get(f)
            
            e.node[n]['body'] = id2interaction[n]['body']
            e.node[n]['root'] = (n == root)
            e.node[n]['datetime'] = str(e.node[n]['datetime'])

        # # some simple clustering
        # assignment = greedy_clustering_on_graph(e)
        # for n in e.nodes_iter():
        #     e.node[n]['cluster_label'] = assignment[n]
            
    if to_original_graph:
        events = map(convert_to_original_graph,
                     events)
        # import pdb; pdb.set_trace()

    d3_events = [to_d3_graph(e)
                 for e in events]

    json_dump(d3_events, output_path)
def run_with_context(interactions_path,
                     candidate_tree_path,
                     dirname=None,
                     to_original_graph=False,
                     undirected=False):
    if not os.path.exists(dirname):
        os.makedirs(dirname)

    try:
        interactions = json.load(open(interactions_path))
    except ValueError as e:
        print(e)
        interactions = load_json_by_line(interactions_path)

    interactions = IU.clean_interactions(interactions,
                                         undirected=undirected)

    output_path = get_output_path(candidate_tree_path, dirname)

    K = 5
    events = detect_events_given_path(candidate_tree_path, K)

    contexted_events = []
    for e in events:
        context_dag = extract_event_context(
            interactions, e,
            undirected=undirected
        )

        if to_original_graph:
            context_dag = convert_to_original_graph(context_dag)
            e = convert_to_original_graph(e)

        contexted_events.append(
            add_subgraph_specific_attributes_to_graph(
                context_dag, [(e, {'event': True})])
        )
    d3_events = [to_d3_graph(ce)
                 for ce in contexted_events]
    
    print('writing to {}'.format(output_path))
    json_dump(d3_events, output_path)