def CFG(): fin1 = open("C:\\Users\\PC\\Desktop\\assignment\\Test\\2-1A.c", encoding='utf-8') pretreatefile1 = pretreate(fin1) lexfile1 = mylex(pretreatefile1) graph1, node_list1 = initgraph(lexfile1) G1 = nx.DiGraph() G1.add_nodes_from(node_list1) G1.add_edges_from(graph1) G1_edges = G1.number_of_edges() fin2 = open("C:\\Users\\PC\\Desktop\\assignment\\Test\\2-1B.c", encoding='utf-8') pretreatefile2 = pretreate(fin2) lexfile2 = mylex(pretreatefile2) graph2, node_list2 = initgraph(lexfile2) G2 = nx.DiGraph() G2.add_nodes_from(node_list2) G1.add_edges_from(graph2) minv = 0.0 for v in nx.optimize_graph_edit_distance(G1, G2): minv = v simi = (1 - ((minv - G1_edges) / G1_edges)) * 100 print("similarity: %f%%" % (simi))
def normalized_graph_edit_distance(self, graph1, graph2, structure_only): """Returns graph edit distance normalized between [0,1]. Parameters ---------- graph1 : graph graph2 : graph structure_only : whether to use node substitution cost 0 (e.g. all nodes are identical). Returns ------- float The normalized graph edit distance of G1,G2. Node substitution cost is normalized string edit distance of labels. Insertions cost 1, deletion costs 1. """ if structure_only: node_subst_cost = lambda x, y: 0 else: node_subst_cost = self.node_subst_cost_lexical approximated_distances = nx.optimize_graph_edit_distance(graph1, graph2, node_subst_cost=node_subst_cost) total_cost_graph1 = len(graph1.nodes) + len(graph1.edges) total_cost_graph2 = len(graph2.nodes) + len(graph2.edges) normalization_factor = max(total_cost_graph1, total_cost_graph2) dist = None for v in approximated_distances: dist = v return float(dist)/normalization_factor
def calc_ged(recepie1, recepie2, timeout_val=600): start_time = time.time() G1 = make_graph(recepie1) G2 = make_graph(recepie2) ged = None try: status = "OK" with timeout(Quota(timeout_val), exception=RuntimeError): for ged in nx.optimize_graph_edit_distance(G1, G2, lambda n1, n2: n1['op'] == n2['op']): pass except RuntimeError as e: status = "Timeout" except Exception as e: status = "Exception: " + str(e) return { "recepie_i": recepie1, "recepie_j": recepie2, "ged": ged, "time": time.time() - start_time, "status": status }
def thread_func(tup: tuple): i, j = tup gi, gj = graphs[i], graphs[j] ni, nj = num_cycle_nodes(gi.src_str), num_cycle_nodes(gj.src_str) repeat_lcm = ni if (not ni == nj): repeat_lcm = lcm(ni, nj) imult = repeat_lcm // ni if imult > 1: istr = repeat_oantigen((gi.name, gi.src_str), imult) gi = parse(istr) jmult = repeat_lcm // nj if jmult > 1: jstr = repeat_oantigen((gj.name, gj.src_str), jmult) gj = parse(jstr) logging.warning( f"start {gi.name}, {gj.name}.\nlcm: {repeat_lcm}, m: {repeat_lcm//ni}, {repeat_lcm//nj} ({ni},{nj})" ) fname = "rep_graphs/%s" + str(i) + "_" + str(j) + "_" + gi.name.replace( " ", "_") + "___" + gj.name.replace(" ", "_") + ".pkl" # try: # with open(fname%"g", "rb") as in_pkl: # ob = pickle.load(in_pkl) # assert(ni == ob[0][0]) # assert(nj == ob[0][1]) # assert(repeat_lcm == ob[0][2]) # assert(gi.name == ob[1].name) # assert(gj.name == ob[2].name) # assert(i == ob[4]) # assert(j == ob[5]) # logging.warning(f"deserialize {gi.name}, {gj.name} (m: {repeat_lcm//ni}, {repeat_lcm//nj})") # return (i,j,ob[3][-1]) # except: pass # with open(fname % "_g", "wb+") as outf: # pickle.dump(((ni, nj, repeat_lcm, repeat_lcm // ni, repeat_lcm // nj), gi, gj), outf) _start_time = time.time() ed = nx.optimize_graph_edit_distance(gi.g, gj.g, node_match=nmatch, edge_match=ematch) _vs = [] for v in ed: _vs.append(v) _end_time = time.time() global _gfc global _gtime logging.warning( f"finish calc {gi.name}, {gj.name}. Time: {_end_time - _gtime:.3f}. Num of finished: {_gfc}. lcm: {repeat_lcm}, multipliers: {repeat_lcm//ni}, {repeat_lcm//nj} ({ni},{nj})" ) _gfc += 1 with open(fname % "g", "wb+") as outf: pickle.dump(((ni, nj, repeat_lcm, repeat_lcm // ni, repeat_lcm // nj), gi, gj, _vs, i, j), outf) return (i, j, _vs[-1])
def transfomer(graph_1, graph_2, vals, index): graph_1.remove_nodes_from(nx.isolates(graph_1)) graph_2.remove_nodes_from(nx.isolates(graph_2)) edges_1 = [[edge[0], edge[1]] for edge in graph_1.edges()] nodes_2 = graph_2.nodes() random.shuffle(list(nodes_2)) mapper = {node: i for i, node in enumerate(nodes_2)} edges_2 = [[mapper[edge[0]], mapper[edge[1]]] for edge in graph_2.edges()] graph_1 = nx.from_edgelist(edges_1) graph_2 = nx.from_edgelist(edges_2) graph_1.remove_nodes_from(nx.isolates(graph_1)) graph_2.remove_nodes_from(nx.isolates(graph_2)) edges_1 = [[edge[0], edge[1]] for edge in graph_1.edges()] edges_2 = [[edge[0], edge[1]] for edge in graph_2.edges()] data = dict() data["graph_1"] = edges_1 data["graph_2"] = edges_2 data["labels_1"] = [str(graph_1.degree(node)) for node in graph_1] data["labels_2"] = [str(graph_2.degree(node)) for node in graph_2] nx.set_node_attributes(graph_1, 'Labels', 1) nx.set_node_attributes(graph_2, 'Labels', 2) print(nx.get_node_attributes(graph_1, "Labels")) for x in range(0, len(data["labels_1"])): graph_1.nodes[x]["Labels"] = data["labels_1"][x] for x in range(0, len(data["labels_2"])): graph_2.nodes[x]["Labels"] = data["labels_2"][x] print(nx.get_node_attributes(graph_1, "Labels")) print(nx.get_node_attributes(graph_2, "Labels")) max2 = 0 # Finding approximate GED for v in nx.optimize_graph_edit_distance(graph_1, graph_2): max2 = v break data["ged"] = max2 print("Graph Edit distance is:") print(data["ged"]) if len(data["labels_1"]) == len(nx.nodes(graph_1)) and len( data["labels_2"]) == len(nx.nodes(graph_2)): p = index while (os.path.isfile(str(p) + ".json")): p += 1 print("exists") if len(nx.nodes(graph_1)) == max(graph_1.nodes()) + 1 and len( nx.nodes(graph_2)) == max(graph_2.nodes()) + 1: with open("../dataset/test/" + str(p) + ".json", 'w+') as f: json.dump(data, f) print("Saved:") print(str(p) + ".json") f.close z = index + 1 else: z = index return z
def processInput(G_pred, G_true, _id): dists = [ x for x in nx.optimize_graph_edit_distance( G_pred, G_true, upper_bound=MAX) ] if len(dists) > 0: min_dist = np.min(dists) else: min_dist = MAX print(min_dist, _id) return min_dist, len(G_true.nodes())
def perform_experiment(model, n_data_points, fit_function, experiment_name): ''' Performs an experiment on a given model :param model: :param n_data_points: :param fit_function: :param experiment_name: :return: the optimized graph edit distance ''' data = [model.random_walk() for i in range(n_data_points)] result, best = fit_function(data) to_png(model, "{}_target".format(experiment_name)) to_png(best.dna, "{}_best".format(experiment_name)) r = min([x for x in nx.optimize_graph_edit_distance(best.dna.to_di_graph(), model.to_di_graph())]) print('edit distance') print(r) return r
def edit_distance_iterative(g1, g2, max_iter): # print("-- in edit distance\n") graph_x_1 = networkx.from_numpy_matrix(g1) graph_x_2 = networkx.from_numpy_matrix(g2) gen = networkx.optimize_graph_edit_distance(graph_x_1, graph_x_2) i = 0 min_ed = 0 last_val = 0 t1 = 0 t2 = 0 for val in gen: if i >= max_iter: break # print("new val: ", val) t1 = time.time() if i > 0: if abs(t2 - t1) >= 0.02: break t2 = t1 min_ed = val i += 1 return min_ed
def ged(self, g1, g2, verbose=True): for v in nx.optimize_graph_edit_distance( g1, g2, node_subst_cost=self.node_match): if verbose: print(v) return v
for line in f: ln = line.split(',') map[int(ln[0])] = ln[1] print(map) return map #id_map = id_name_mapping() #print(id_map) for f in files: graphs.append((f, read_json_file(f))) n = len(graphs) sim_mat = [[0] * n for i in range(n)] #calculate similarity for i in range(n): for j in range(i, n): print("Calculating similarity for ", graphs[i][0], graphs[j][0]) sim = 0.0 if i != j: #sim = nx.optimize_graph_edit_distance(graphs[i][1], graphs[j][1]) for v in nx.optimize_graph_edit_distance(graphs[i][1], graphs[j][1]): sim = v sim_mat[i][j] = sim sim_mat[j][i] = sim print("similarity for ", graphs[i][0], graphs[j][0], sim) print(sim_mat)
keep_track = [] while len(keep_track) < 1000: all_states = [0, 1, 2, 3, 4, 5, 6, 7] random.shuffle(all_states) if str(all_states) not in keep_track: keep_track.append(str(all_states)) all_seqs.append(list(all_states)) # generate final layout for each sequence for seq in all_seqs: curr_fixed_nodes_state = [] prev_fixed_nodes_state = [] gen_state(curr_fixed_nodes_state, prev_fixed_nodes_state, sample) for s in seq: prev_fixed_nodes_state = list(curr_fixed_nodes_state) curr_fixed_nodes_state.append(s) im, mks = gen_state(curr_fixed_nodes_state, prev_fixed_nodes_state, sample) if im is not None: # estimate graph estimated_graph_obj = estimate_graph(mks, real_nodes) # compute edit distance and store image dists = [x for x in nx.optimize_graph_edit_distance(true_graph_obj, estimated_graph_obj)] all_images.append(torch.tensor(np.array(im).transpose((2, 0, 1)))/255.0) all_dists.append(np.min(dists)) print('edit distance: {}'.format(np.mean(all_dists))) all_images = torch.stack(all_images) save_image(all_images, '{}/runs/output.png'.format(PREFIX), nrow=10, normalize=False) exit(0)
def load_dataset_SimGNN(ds_name): """ :return: graphs numpy array of shape (num_of_graphs) ged numpy matrix of shape (num_of_graphs, num_of_graphs) """ directory = BASE_DIR + "/data/SimGNN_graphs/{}".format(ds_name) graphs = iterate_get_graphs(directory) num_graphs = len(graphs) pickle_path = directory+"/geds.pickle" if os.path.exists(pickle_path): with open(pickle_path, 'rb') as f: geds = pickle.load(f) else: geds = np.zeros((num_graphs,num_graphs)) print("Calculating GEDs ...") for i in tqdm(range(num_graphs)): for j in range(i): k = 0 for approx in nx.optimize_graph_edit_distance( graphs[i], graphs[j], node_subst_cost=is_diff, edge_subst_cost=is_diff ): if k == 0: geds[i,j] = approx break; k += 1 if i != j: geds[j,i] = geds[i,j] #pbar.update(1) with open(pickle_path, 'wb') as f: pickle.dump(geds, f) print(geds) # normalize for i in tqdm(range(num_graphs)): n1 = graphs[i].number_of_nodes() for j in range(i): n2 = graphs[j].number_of_nodes() geds[i,j] /= ( (n1+n2)/2 ) geds[j,i] = geds[i,j] geds = np.exp(-geds) # construct dict injectively mapping node attribute dicts -> integers node_num_att = 0 node_att_map = {} for graph in graphs: nodes = list(graph.nodes.data()) for node in nodes: # each node is (node, att_dict) tuple att = copy.deepcopy(node[1]) del att['label'] # this is always unique to each node; we don't care about this att = str(sorted(att.items())) # can't hash a dict, so we use this instead if att not in node_att_map: node_att_map[att] = node_num_att node_num_att += 1 # convert graphs to numpy format npgraphs = np.empty(len(graphs), dtype=object) for i in range(len(graphs)): npgraph = nx_to_np(graphs[i], node_att_map) npgraph = np.transpose(npgraph, [1,2,0]) npgraph = normalize_graph(npgraph) npgraph = np.transpose(npgraph, [2,0,1]) npgraphs[i] = npgraph return npgraphs, geds
def compute_graph_edit_distance(graph_1, graph_2): gen = nx.optimize_graph_edit_distance(graph_1, graph_2) min_dist = np.infty for g in gen: min_dist = g return min_dist, None
for k, l in g_pred[1]: G.add_edges_from([(k, l)]) H = nx.Graph() colors_H = [] for k, label in enumerate(g_true[0]): if label >= 0: H.add_nodes_from([(k, {'label': label})]) colors_H.append(ID_COLOR[label]) for k, m, l in g_true[1]: if m >= 0: # print((k, l)) H.add_edges_from([(k, l)]) min_dist = np.min([x for x in nx.optimize_graph_edit_distance(G, H)]) edit_dist_per_node[len(H.nodes())].append(min_dist) globalIndex += 1 # # save predictions # im_pred = Image.new('RGB', (256, 256)) # dr = ImageDraw.Draw(im_pred) # bbs = gen_room_bb[i].view(-1, 4) # for nd, bb in zip(nodes[i].detach().cpu().numpy(), bbs): # x0, y0, x1, y1 = bb * 256.0 # if x0 >= 0 and y0 >= 0 and x1 >= 0 and y1 >= 0: # color = ID_COLOR[nd] # dr.rectangle((x0, y0, x1, y1), outline=color) # im_pred.save('./debug/{}_pred_bb.jpg'.format(globalIndex)) # # save predictions