def get_hypergraph(proto_path):
    hypergraph = None
    try:
        tmp = ExperimentalResult()
        with proto_path.open('rb') as proto:
            tmp.ParseFromString(proto.read())
        hypergraph = tmp.hypergraph
    except:
        pass
    if hypergraph is not None:
        return hypergraph
    try:
        tmp = Hypergraph()
        with proto_path.open('rb') as proto:
            tmp.ParseFromString(proto.read())
        hypergraph = tmp
    except:
        pass
    if hypergraph is not None:
        return hypergraph
    raise ValueError("Input path cannot be parsed as a hypergraph")
    metric = [m for m in result.metrics if m.experiment_name == exp_name][0]
    return set((r.node_idx, r.edge_idx) for r in metric.records if take_rec(r))


if __name__ == "__main__":
    args = ParseArgs()
    res1_path, res2_path = [Path(p) for p in args.paths]
    # must have supplied real files
    assert res1_path.is_file()
    assert res2_path.is_file()
    # must have set one but not both
    assert args.compare_correct or args.compare_wrong
    assert not (args.compare_correct and args.compare_wrong)

    result1 = ExperimentalResult()
    result2 = ExperimentalResult()

    with res1_path.open("rb") as proto:
        result1.ParseFromString(proto.read())
    with res2_path.open("rb") as proto:
        result2.ParseFromString(proto.read())

    shared_experiments = set(m.experiment_name
                             for m in result1.metrics).intersection(
                                 set(m.experiment_name
                                     for m in result2.metrics))
    assert len(shared_experiments) > 0

    for name in shared_experiments:
        recs1 = ToInterestingRecords(result1, name, args.compare_correct)
    log.info("Checking result path")
    original_result_path = Path(args.result)
    assert original_result_path.is_file()

    log.info("Checking vector text")
    vector_text_path = Path(args.vector_text)
    assert vector_text_path.is_file()

    log.info("Checking output location is writeable")
    output_path = Path(args.out)
    assert not output_path.exists()
    assert output_path.parent.is_dir()

    log.info("Parsing input result")
    try:
        result = ExperimentalResult()
        with original_result_path.open('rb') as proto:
            result.ParseFromString(proto.read())
    except:
        log.error("Invalid input proto %s", original_result_path)

    log.info("Clearing original unimportant data")
    result.embedding.Clear()
    result.embedding.method_name = "metapath2vec++"

    log.info("Parsing vector")
    with vector_text_path.open('r') as input_file:
        for line in tqdm(input_file):
            if line[0] in 'va':
                is_edge = line[0] == 'v'
                tokens = line.strip().split()
    parser.add_argument("reference_exp", type=Path)
    parser.add_argument("out_exp", type=Path)
    return parser.parse_args()


if __name__ == "__main__":
    args = ParseArgs()
    print(args)
    assert args.bine_1.is_file()
    assert args.metadata.is_file()
    assert args.reference_exp.is_file()
    assert not args.out_exp.is_file()
    assert args.out_exp.parent.is_dir()
    node_map, edge_map = LoadMetadataMaps(args.metadata)

    result_exp = ExperimentalResult()
    # Copy all
    with open(args.reference_exp, 'rb') as proto:
        result_exp.ParseFromString(proto.read())
    # Clear embedding
    result_exp.embedding.Clear()

    # Load Emb
    result_exp.embedding.method_name = "BiNE"
    load_dim = True
    with open(args.bine_1) as file:
        for line in file:
            tokens = line.split()
            idx = int(tokens[0][1:])
            vec = [float(f) for f in tokens[1:]]
            if load_dim:
  return parser.parse_args()

def test_equal_hypergraphs(a, b):
  assert len(a.node) == len(b.node)
  assert len(a.edge) == len(b.edge)
  for a_idx, a_node in a.node.items():
    assert a_idx in b.node
    b_node = b.node[a_idx]
    assert set(a_node.edges) == set(b_node.edges)
  for a_idx, a_edge in a.edge.items():
    assert a_idx in b.edge
    b_edge = b.edge[a_idx]
    assert set(a_edge.nodes) == set(b_edge.nodes)

if __name__ == "__main__":
  args = ParseArgs()
  print(args)
  assert args.ref_result.is_file()
  assert not args.out_data.is_file()
  assert not args.out_meta.is_file()
  result = ExperimentalResult()

  with open(args.ref_result, 'rb') as file:
    result.ParseFromString(file.read())

  SaveEdgeList(result.hypergraph, args.out_data, args.out_meta, args.weighted, args.only_one_side)

  # TEST WRITE CORRECT
  # recovered = LoadEdgeList(args.out_data, args.out_meta)
  # test_equal_hypergraphs(result.hypergraph, recovered)