Ejemplo n.º 1
0
def run_experiments(data, ground_truth, network_num):
    all_res = []
    # Timerank with one connection - default q
    mutu4 = Muturank_new(data.graphs,
                         threshold=1e-6,
                         alpha=0.85,
                         beta=0.85,
                         connection='one',
                         clusters=len(ground_truth),
                         default_q=True)
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu4.dynamic_coms,
                             "Timerank with one connection - default q",
                             mutu4.tfs,
                             eval="dynamic",
                             duration=mutu4.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu4.dynamic_coms,
                             "Timerank with one connection - default q",
                             mutu4.tfs,
                             eval="sets",
                             duration=mutu4.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu4.dynamic_coms,
                             "Timerank with one connection - default q",
                             mutu4.tfs,
                             eval="per_tf",
                             duration=mutu4.duration))
    f = open(results_file, 'a')
    f.write(
        tabulate(all_res, headers="keys", tablefmt="fancy_grid").encode('utf8')
        + "\n")
    f.close()
    # Timerank with all connections - default q
    mutu5 = Muturank_new(data.graphs,
                         threshold=1e-6,
                         alpha=0.85,
                         beta=0.85,
                         connection='all',
                         clusters=len(ground_truth),
                         default_q=True)
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu5.dynamic_coms,
                             "Timerank with all connections - default q",
                             mutu5.tfs,
                             eval="dynamic",
                             duration=mutu5.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu5.dynamic_coms,
                             "Timerank with all connections - default "
                             "q",
                             mutu5.tfs,
                             eval="sets",
                             duration=mutu5.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu5.dynamic_coms,
                             "Timerank with all connections - default "
                             "q",
                             mutu5.tfs,
                             eval="per_tf",
                             duration=mutu5.duration))
    f = open(results_file, 'a')
    f.write(
        tabulate(all_res, headers="keys", tablefmt="fancy_grid").encode('utf8')
        + "\n")
    f.close()
    # Timerank with next connection - default q
    mutu6 = Muturank_new(data.graphs,
                         threshold=1e-6,
                         alpha=0.85,
                         beta=0.85,
                         connection='next',
                         clusters=len(ground_truth),
                         default_q=True)
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu6.dynamic_coms,
                             "Timerank with next connection - default q",
                             mutu6.tfs,
                             eval="dynamic",
                             duration=mutu6.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu6.dynamic_coms,
                             "Timerank with next connection - default q",
                             mutu6.tfs,
                             eval="sets",
                             duration=mutu6.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu6.dynamic_coms,
                             "Timerank with next connection - default q",
                             mutu6.tfs,
                             eval="per_tf",
                             duration=mutu6.duration))
    f = open(results_file, 'a')
    f.write(
        tabulate(all_res, headers="keys", tablefmt="fancy_grid").encode('utf8')
        + "\n")
    f.close()
    # NNTF
    fact = TensorFact(data.graphs,
                      num_of_coms=len(ground_truth),
                      threshold=1e-4,
                      seeds=10,
                      overlap=False)
    all_res.append(
        evaluate.get_results(ground_truth,
                             fact.dynamic_coms,
                             "NNTF",
                             mutu6.tfs,
                             eval="dynamic",
                             duration=fact.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             fact.dynamic_coms,
                             "NNTF",
                             mutu6.tfs,
                             eval="sets",
                             duration=fact.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             fact.dynamic_coms,
                             "NNTF",
                             mutu6.tfs,
                             eval="per_tf",
                             duration=fact.duration))
    f = open(results_file, 'a')
    f.write(
        tabulate(all_res, headers="keys", tablefmt="fancy_grid").encode('utf8')
        + "\n")
    f.close()
    with open(results_file, 'a') as f:
        f.write("NNTF\n")
        f.write("Error: " + str(fact.error) + "Seed: " + str(fact.best_seed) +
                "\n")
        f.write("A\n")
        pprint.pprint(fact.A, stream=f, width=150)
        f.write("B\n")
        pprint.pprint(fact.B, stream=f, width=150)
        f.write("C\n")
        pprint.pprint(fact.C, stream=f, width=150)
        pprint.pprint(fact.dynamic_coms, stream=f, width=150)
    # GED
    import sys
    sys.path.insert(0, '../GED/')
    import preprocessing, Tracker
    start_time = time.time()
    from ged import GedWrite, ReadGEDResults
    ged_data = GedWrite(data)
    graphs = preprocessing.getGraphs(ged_data.fileName)
    tracker = Tracker.Tracker(graphs)
    tracker.compare_communities()
    #outfile = 'tmpfiles/ged_results.csv'
    outfile = './results/GED-events-handdrawn-' + str(network_num) + '.csv'
    with open(outfile, 'w') as f:
        for hypergraph in tracker.hypergraphs:
            hypergraph.calculateEvents(f)
    ged_time = str(datetime.timedelta(seconds=int(time.time() - start_time)))
    print "--- %s seconds ---" % (ged_time)
    ged = ReadGEDResults.ReadGEDResults(file_coms=ged_data.fileName,
                                        file_output=outfile)
    with open(results_file, 'a') as f:
        f.write("GED\n")
        pprint.pprint(ged.dynamic_coms, stream=f, width=150)
    all_res.append(
        evaluate.get_results(ground_truth,
                             ged.dynamic_coms,
                             "GED",
                             mutu6.tfs,
                             eval="dynamic",
                             duration=ged_time))
    all_res.append(
        evaluate.get_results(ground_truth,
                             ged.dynamic_coms,
                             "GED",
                             mutu6.tfs,
                             eval="sets",
                             duration=ged_time))
    f = open(results_file, 'a')
    f.write(
        tabulate(all_res, headers="keys", tablefmt="fancy_grid").encode('utf8')
        + "\n")
    f.close()
    #all_res.append(evaluate.get_results(ground_truth, ged.dynamic_coms, "GED", mutu6.tfs, eval="per_tf"))
    # Run Timerank - One connection
    mutu1 = Muturank_new(data.graphs,
                         threshold=1e-6,
                         alpha=0.85,
                         beta=0.85,
                         connection='one',
                         clusters=len(ground_truth),
                         default_q=False)
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu1.dynamic_coms,
                             "Timerank with one connection",
                             mutu1.tfs,
                             eval="dynamic",
                             duration=mutu1.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu1.dynamic_coms,
                             "Timerank with one connection",
                             mutu1.tfs,
                             eval="sets",
                             duration=mutu1.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu1.dynamic_coms,
                             "Timerank with one connection",
                             mutu1.tfs,
                             eval="per_tf",
                             duration=mutu1.duration))
    f = open(results_file, 'a')
    f.write(
        tabulate(all_res, headers="keys", tablefmt="fancy_grid").encode('utf8')
        + "\n")
    f.close()
    muturank_res = OrderedDict()
    muturank_res["tf/node"] = ['t' + str(tf) for tf in mutu1.tfs_list]
    for i, node in enumerate(mutu1.node_ids):
        muturank_res[node] = [
            mutu1.p_new[tf * len(mutu1.node_ids) + i]
            for tf in range(mutu1.tfs)
        ]
    f = open(results_file, 'a')
    f.write("ONE CONNECTION\n")
    f.write(
        tabulate(muturank_res, headers="keys", tablefmt="fancy_grid").encode(
            'utf8') + "\n")
    f.write(
        tabulate(zip(['t' + str(tf) for tf in mutu1.tfs_list], mutu1.q_new),
                 headers="keys",
                 tablefmt="fancy_grid").encode('utf8') + "\n")
    f.close()

    # Timerank with all connections
    mutu2 = Muturank_new(data.graphs,
                         threshold=1e-6,
                         alpha=0.85,
                         beta=0.85,
                         connection='all',
                         clusters=len(ground_truth),
                         default_q=False)
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu2.dynamic_coms,
                             "Timerank with all connections",
                             mutu2.tfs,
                             eval="dynamic",
                             duration=mutu2.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu2.dynamic_coms,
                             "Timerank with all connections",
                             mutu2.tfs,
                             eval="sets",
                             duration=mutu2.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu2.dynamic_coms,
                             "Timerank with all connections",
                             mutu2.tfs,
                             eval="per_tf",
                             duration=mutu2.duration))
    f = open(results_file, 'a')
    f.write(
        tabulate(all_res, headers="keys", tablefmt="fancy_grid").encode('utf8')
        + "\n")
    f.close()
    muturank_res = OrderedDict()
    muturank_res["tf/node"] = ['t' + str(tf) for tf in mutu2.tfs_list]
    for i, node in enumerate(mutu2.node_ids):
        muturank_res[node] = [
            mutu2.p_new[tf * len(mutu2.node_ids) + i]
            for tf in range(mutu2.tfs)
        ]
    f = open(results_file, 'a')
    f.write("ALL CONNECTIONS\n")
    f.write(
        tabulate(muturank_res, headers="keys", tablefmt="fancy_grid").encode(
            'utf8') + "\n")
    f.write(
        tabulate(zip(['t' + str(tf) for tf in mutu2.tfs_list], mutu2.q_new),
                 headers="keys",
                 tablefmt="fancy_grid").encode('utf8') + "\n")
    f.close()
    # Timerank with next connection
    mutu3 = Muturank_new(data.graphs,
                         threshold=1e-6,
                         alpha=0.85,
                         beta=0.85,
                         connection='next',
                         clusters=len(ground_truth),
                         default_q=False)
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu3.dynamic_coms,
                             "Timerank with next connection",
                             mutu3.tfs,
                             eval="dynamic",
                             duration=mutu3.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu3.dynamic_coms,
                             "Timerank with next connection",
                             mutu3.tfs,
                             eval="sets",
                             duration=mutu3.duration))
    all_res.append(
        evaluate.get_results(ground_truth,
                             mutu3.dynamic_coms,
                             "Timerank with next connection",
                             mutu3.tfs,
                             eval="per_tf",
                             duration=mutu3.duration))
    f = open(results_file, 'a')
    f.write(
        tabulate(all_res, headers="keys", tablefmt="fancy_grid").encode('utf8')
        + "\n")
    f.close()
    muturank_res = OrderedDict()
    muturank_res["tf/node"] = ['t' + str(tf) for tf in mutu3.tfs_list]
    for i, node in enumerate(mutu3.node_ids):
        muturank_res[node] = [
            mutu3.p_new[tf * len(mutu3.node_ids) + i]
            for tf in range(mutu3.tfs)
        ]
    f = open(results_file, 'a')
    f.write("NEXT CONNECTION\n")
    f.write(
        tabulate(muturank_res, headers="keys", tablefmt="fancy_grid").encode(
            'utf8') + "\n")
    f.write(
        tabulate(zip(['t' + str(tf) for tf in mutu3.tfs_list], mutu3.q_new),
                 headers="keys",
                 tablefmt="fancy_grid").encode('utf8') + "\n")
    f.write("GROUND TRUTH\n")
    pprint.pprint(ground_truth, stream=f, width=150)
    f.write("ONE CONNECTION\n")
    pprint.pprint(mutu1.dynamic_coms, stream=f, width=150)
    f.write("ALL CONNECTIONS\n")
    pprint.pprint(mutu2.dynamic_coms, stream=f, width=150)
    f.write("NEXT CONNECTION\n")
    pprint.pprint(mutu3.dynamic_coms, stream=f, width=150)
    f.close()
    return all_res
Ejemplo n.º 2
0
                                 event_type='dissolving')
        for key in self.Fcandidates.keys():
            Dhypergraph.add_edge(pastTFcommunities[-1],
                                 self.Fcandidates[key],
                                 event_type='forming')
        return Dhypergraph

    def analyze_results(self, ):
        events_names = [
            'merging', 'splitting', 'growing', 'shrinking', 'continuing',
            'dissolving', 'forming', 'no_event'
        ]
        events = [e['resulted_event'] for e in self.results]
        for name in events_names:
            print(name, events.count(name))


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('Usage: Tracker.py <inputfile.json>')
        print('Exiting with code 1')
        exit(1)
    start_time = time.time()
    graphs = preprocessing.getGraphs(sys.argv[1])
    tracker = Tracker(graphs)
    tracker.compare_communities()
    with open('ged_results.csv', 'w') as f:
        for hypergraph in tracker.hypergraphs:
            hypergraph.calculateEvents(f)
    print("--- %s seconds ---" % (time.time() - start_time))