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 train_model(params): g_mat, tree = extract_tree(params) handlers = {} handlers["get_network"] = gn(g_mat, params["get_network_hierarchy"]) handlers["embedding_model"] = __import__( 'node_embedding.' + params["embedding_model"]["func"], fromlist=["node_embedding"]).NodeEmbedding handlers["transfer_embeddings"] = __import__( 'transfer_embeddings.' + params["transfer_embeddings"]["func"], fromlist=["transfer_embeddings"]).TransferEmbedding res_coordinates = [None] * len(tree) res_coordinates[len(tree) - 1] = np.zeros( params["embedding_model"]["embedding_size"], dtype=np.float32) res_radius = [None] * len(tree) res_radius[len(tree) - 1] = float(params["init_radius"]) dfs(len(tree) - 1, tree, handlers, params, res_radius, res_coordinates) res_path = params["train_output"] dh.symlink(res_path, os.path.join(RES_PATH, "new_train_res")) dh.append_to_file( res_path, json.dumps({ "radius": np.array(res_radius).tolist(), "coordinates": np.array(res_coordinates).tolist() })) return res_coordinates, res_radius
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!")
def metric(params): js = json.loads(open(params["metric_input"]).read()) coordinates = np.array(js["coordinates"]) radius = np.array(js["radius"]) res_path = params["metric_output"] dh.symlink(res_path, os.path.join(RES_PATH, "new_metric_res")) ret = [] for metric in params["metric_function"]: if metric["metric_func"] == "draw_circle_2D": pic_path = os.path.join(PIC_PATH, "draw_circle_" + str(int(time.time() * 1000.0)) + ".pdf") dh.symlink(pic_path, os.path.join(PIC_PATH, "new_draw_circle")) getattr(Metric, metric["metric_func"])(coordinates, radius, metric, params["num_nodes"], pic_path) else: origin_coordinates = coordinates[: params["num_nodes"]] res = getattr(Metric, metric["metric_func"])(origin_coordinates, metric) ret.append((metric["metric_func"], res)) dh.append_to_file(res_path, json.dumps(ret)) return ret