• Fize Jacques's avatar
    Changes : · 00a85358
    Fize Jacques authored
     - Add doc
     - Add automatic annotation for similarity
     - Add similarity matrix reader
     - Add objectify
     - Debug Transform
     -
    00a85358
generate_similarity_matrix.py 2.79 KiB
# coding = utf-8
import glob
# from gmatch4py.bag_of_cliques import BagOfCliques
from gmatch4py.helpers.reader import import_dir
from gmatch4py.base import Base
from gmatch4py.ged.graph_edit_dist import GraphEditDistance
from gmatch4py.ged.bipartite_graph_matching_2 import BP_2
from gmatch4py.ged.greedy_edit_distance import GreedyEditDistance
from gmatch4py.ged.hausdorff_edit_distance import HED
from gmatch4py.jaccard import Jaccard
from gmatch4py.kernels.weisfeiler_lehman import *
from gmatch4py.mcs import MCS
from gmatch4py.vertex_edge_overlap import VertexEdgeOverlap
import argparse, os, sys, re, json, logging
import datetime

logging.basicConfig(
    filename="{0}.csv".format(datetime.datetime.now().strftime("%Y_%m_%d__%H_%M_%S")),
    format="%(message)s,%(asctime)s",
    level=logging.DEBUG
)

parser = argparse.ArgumentParser()
parser.add_argument("graphs_input_dir")
parser.add_argument("matrix_output_dir")
parser.add_argument("-d", action="store_true", help="Return distance matrix")
parser.add_argument("-s", action="store_true", help="Selected graph ?")

args = parser.parse_args()
if not os.path.exists(args.graphs_input_dir):
    print("Input graph directory doesn't exist!")
    sys.exit(1)

if not os.path.exists(args.matrix_output_dir):
    print("Output matrix directory doesn't exist!")
    print("Creating directory")
    os.makedirs(args.matrix_output_dir)
    print("Directory created")

logging.info(msg="L_G,BEGIN,\"\"")
graphs = import_dir(args.graphs_input_dir)
logging.info(msg="L_G,DONE,\"\"")
# print(graphs)
selected = None
if args.s:
    selected = json.load(open("selected.json"))
# Compute matrices
for class_ in [GraphEditDistance, BP_2, GreedyEditDistance, HED, Jaccard, MCS,
               VertexEdgeOverlap]:
    logging.info(msg="C_S,BEG,\"{0}\"".format(class_.__name__))
    print("Computing the Similarity Matrix for {0}".format(class_.__name__))

    if class_ in (GraphEditDistance, BP_2, GreedyEditDistance, HED):
        comparator = class_(1, 1, 1, 1)
    elif class_ == WeisfeleirLehmanKernel:
        comparator = class_(h=2)
    else:
        comparator = class_()
    matrix = comparator.compare(graphs, selected)
    if not args.d:
        matrix = comparator.similarity(matrix)
    else:
        matrix = comparator.distance(matrix)
    logging.info(msg="C_S,DONE,\"{0}\"".format(class_.__name__))
    output_fn = "{0}/{1}_{2}.npy".format(
        args.matrix_output_dir.rstrip("/"),
        class_.__name__,
        os.path.dirname(args.graphs_input_dir).replace("/", "_")
    )
    logging.info(msg="M_S,BEG,\"{0}\"".format(class_.__name__))
    np.save(output_fn, matrix)
    logging.info(msg="M_S,DONE,\"{0}\"".format(class_.__name__))
    print("Matrix Saved")

# json.dump(mapping_files_to_graphs,open("{0}/{1}".format(args.matrix_output_dir.rstrip("/"),"metadata.json")))
print("Done")