Ejemplo n.º 1
0
def main_old():

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--operation',
                        type=str,
                        default="all",
                        help="[all | init | train | metric | draw]")
    parser.add_argument('--conf', type=str, default="default")
    args = parser.parse_args()
    params = dh.load_json_file(os.path.join(CONF_PATH, args.conf + ".json"))

    metric_path_pre = os.path.join(RES_PATH, args.conf)
    if os.path.exists(metric_path_pre) == False:
        os.mkdir(metric_path_pre)
    output_path = os.path.join(metric_path_pre, dh.get_time_str())
    metric_path = output_path + "_metric"

    def metric(embeddings):
        if "metrics" not in params:
            return
        for metric in params["metrics"]:
            res = getattr(Metric, metric["func"])(embeddings, metric)
            dh.append_to_file(metric_path, str(res) + "\n")
            print res

    dh.symlink(metric_path, os.path.join(metric_path_pre, "new_metric"))

    if "drawers" in params:
        draw_path = output_path + "_draw"
        if os.path.exists(draw_path) == False:
            os.mkdir(draw_path)
    draw_cnt = [0]

    def draw(embeddings):
        if "drawers" not in params:
            return
        for drawer in params['drawers']:
            getattr(Metric, drawer["func"])(embeddings, drawer, draw_path,
                                            draw_cnt[0])
        draw_cnt[0] += 1

    if args.operation == "all":
        G, embeddings, weights = __import__("init." + params["init"]["func"],
                                            fromlist=["init"]).init(
                                                params["init"], metric,
                                                output_path, draw)
        __import__("dynamic_loop." + params["main_loop"]["func"],
                   fromlist=["dynamic_loop"]).loop(params["main_loop"], G,
                                                   embeddings, weights, metric,
                                                   output_path, draw)
    elif args.operation == "init":
        G, embeddings, weights = __import__("init." + params["init"]["func"],
                                            fromlist=["init"]).init(
                                                params["init"], metric,
                                                output_path, draw)
    elif args.operation == "draw":
        pass
    else:
        print "Not Support!"
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--operation',
                        type=str,
                        default="all",
                        help="[all | init | train]")
    parser.add_argument('--conf', type=str, default="amherst0.25")
    args = parser.parse_args()
    params = dh.load_json_file(os.path.join(CONF_PATH, args.conf + ".json"))
    fw = open('my_embedding.txt', 'w')
    metric_path_pre = os.path.join(RES_PATH, args.conf)
    if os.path.exists(metric_path_pre) == False:
        os.mkdir(metric_path_pre)
    output_path = os.path.join(metric_path_pre, dh.get_time_str())
    print(output_path)
    metric_path = output_path + "_metric"

    def metric(embeddings):
        if "metrics" not in params:
            return
        for metric in params["metrics"]:
            print("[] Start node classification...")
            res = getattr(Metric, metric["func"])(embeddings, metric)
            dh.append_to_file(metric_path, str(res) + "\n")
            print("[+] Metric: " + str(res))

    dh.symlink(metric_path, os.path.join(metric_path_pre, "new_metric"))

    if args.operation == "all":
        G, embeddings, weights = __import__("init." + params["init"]["func"],
                                            fromlist=["init"]).init(
                                                params["init"], metric,
                                                output_path)
        __import__("dynamic_loop." + params["main_loop"]["func"],
                   fromlist=["dynamic_loop"]).loop(params["main_loop"], G,
                                                   embeddings, weights, metric,
                                                   output_path)
        print(embeddings)
    elif args.operation == "init":
        G, embeddings, weights = __import__("init." + params["init"]["func"],
                                            fromlist=["init"]).init(
                                                params["init"], metric,
                                                output_path)
    else:
        print("Not Support!")