def printMetrics(G): ''' Set inital values before optimization ''' global initial_st global all_pairs_sp global initial_cr global initial_ar global initial_asp # Do some preliminary stuff # Stress will be normalized considering the first value as max # To speed up ST precompute all pairs shortest paths initial_st = 1 if compute_st: initial_st = stress.stress(G, all_sp=all_pairs_sp) print("ST:", initial_st, end=" - ") if all_pairs_sp is None: all_pairs_sp = nx.shortest_path(G) # To speed up NP precompute all pairs shortest paths if compute_np: if all_pairs_sp is None: all_pairs_sp = nx.shortest_path(G) initial_np = neighbors_preservation.compute_neig_preservation( G, all_sp=all_pairs_sp) print("NP:", initial_np, end=" - ") initial_sym = 0 if compute_sym: initial_sym = ksymmetry.get_symmetric_score(G) print("Sym:", abs(initial_sym), end=" - ") initial_cr = 1 if compute_cr: initial_cr = len(crossings.count_crossings(G)) print("CR:", initial_cr, end=" - ") initial_ue = 0 if compute_ue: initial_ue = uniformity_edge_length.uniformity_edge_length(G) print("UE:", initial_ue, end=" - ") initial_ar = 1 if compute_ar: initial_ar = areafunctions.areaerror(G) print("AR:", initial_ar, end=" - ") initial_asp = 1 if compute_asp: initial_asp = areafunctions.aspectRatioerror(G) print("ASP:", initial_asp, end=" - ") print("") return
def metrics_evaluator(X): ''' Evaluates the metrics of the given layout and weights them ''' global log global G global all_pairs_sp n = nx.number_of_nodes(G) #Reshape the 1D array to a n*2 matrix X = X.reshape((n, 2)) return_val = 0.0 G = writeSPXPositiontoNetworkXGraph(G, X) ue = 0 if compute_ue: ue = uniformity_edge_length.uniformity_edge_length(G) if log % 100 == 0: print("UE:", ue, end=" - ") ue *= abs(compute_ue) st = 0 if compute_st: st = stress.stress(G, all_sp=all_pairs_sp) if log % 100 == 0: print("ST:", st, end=" - ") st *= abs(compute_st) / initial_st sym = 0 if compute_sym: sym = ksymmetry.get_symmetric_score(G) if log % 100 == 0: print("Sym:", abs(sym), end=" - ") sym = 1 - sym sym *= abs(compute_sym) np = 0 if compute_np: np = neighbors_preservation.compute_neig_preservation( G, all_sp=all_pairs_sp) if log % 100 == 0: print("NP:", abs(np), end=" - ") np = 1 - np np *= abs(compute_np) cr = 0 if compute_cr: cr = len(crossings.count_crossings(G)) if log % 100 == 0: print("cr", cr, end="-") cr *= abs(compute_cr) / initial_cr return_val = ue + st + sym + np + cr if log % 100 == 0: print("ret", return_val) write_dot(G, 'output/' + graph_name + '_running.dot') log += 1 return return_val
def metrics_evaluator(X, print_val=False): ''' Evaluates the metrics of the given layout and weights them ''' global G global all_pairs_sp # Add some additional global variables global OUTPUT_FOLDER global graph_name global cnvs, cnvs_size, cnvs_padding, draw_counter n = nx.number_of_nodes(G) #Reshape the 1D array to a n*2 matrix X = X.reshape((n, 2)) return_val = 0.0 G = writeSPXPositiontoNetworkXGraph(G, X) ue = 0 ue_count = 0 if compute_ue: ue = uniformity_edge_length.uniformity_edge_length(G) ue_count = ue # if log%100==0: # print("UE:", ue, end=" - ") ue *= abs(compute_ue) st = 0 st_count = 0 if compute_st: st = stress.stress(G, all_sp=all_pairs_sp) st_count = st # if log%100==0: # print("ST:", st, end=" - ") st *= abs(compute_st) / initial_st sym = 0 sym_count = 0 if compute_sym: G = scale_graph(G, 1000) sym = ksymmetry.get_symmetric_score(G) G = scale_graph(G, 1 / 1000) sym_count = sym # if log%100==0: # print("Sym:", abs(sym), end=" - ") sym = 1 - sym sym *= abs(compute_sym) np = 0 np_count = 0 if compute_np: np = neighbors_preservation.compute_neig_preservation( G, all_sp=all_pairs_sp) np_count = np np = 1 - np np *= abs(compute_np) cr = 0 cr_count = 0 if compute_cr: cr = len(crossings.count_crossings(G)) cr_count = cr if not initial_cr == 0: cr *= abs(compute_cr) / initial_cr else: cr = 0 ar = 0 ar_count = 0 if compute_ar: ar = areafunctions.areaerror(G) ar_count = ar ar = abs(ar - 1) ar *= abs(compute_ar) / initial_ar # Aspect ratio asp = 0 asp_count = 0 if compute_asp: asp = areafunctions.aspectRatioerror(G) asp_count = asp asp = abs(asp - 1) asp *= abs(compute_asp) / initial_asp return_val = ue + st + sym + np + cr + ar + asp if print_val: print("score: ", return_val) if mode == "GUI": if draw_counter % 100 == 0: min_x, min_y, max_x, max_y = 0, 0, 0, 0 for currVStr in nx.nodes(G): currV = G.nodes[currVStr] x = float(currV['pos'].split(",")[0]) y = float(currV['pos'].split(",")[1]) min_x = min(min_x, x) max_x = max(max_x, x) min_y = min(min_y, y) max_y = max(max_y, y) currV['pos'] = str(x) + "," + str(y) cnvs.delete("all") scl = (cnvs_size - cnvs_padding) / (max(max_y - min_y, max_x - min_x)) tx = cnvs_padding / 2 ty = cnvs_padding / 2 pos_dict = nx.get_node_attributes(G, 'pos') for edge in nx.edges(G): (s, t) = edge x_source = float(pos_dict[s].split(",")[0]) x_target = float(pos_dict[t].split(",")[0]) y_source = float(pos_dict[s].split(",")[1]) y_target = float(pos_dict[t].split(",")[1]) cnvs.create_line((x_source - min_x) * scl + tx, (y_source - min_y) * scl + ty, (x_target - min_x) * scl + tx, (y_target - min_y) * scl + ty) print((x_source - min_x) * scl, (x_target - min_x) * scl, (y_source - min_y) * scl, (y_target - min_y) * scl) cnvs.update() draw_counter += 1 return return_val
def metrics_evaluator(X, print_val=False): ''' Evaluates the metrics of the given layout and weights them ''' global G global all_pairs_sp global log n = nx.number_of_nodes(G) #Reshape the 1D array to a n*2 matrix X = X.reshape((n, 2)) return_val = 0.0 G = writeSPXPositiontoNetworkXGraph(G, X) ue = 0 ue_count = 0 if compute_ue: ue = uniformity_edge_length.uniformity_edge_length(G) ue_count = ue # if log%100==0: # print("UE:", ue, end=" - ") ue *= abs(compute_ue) st = 0 st_count = 0 if compute_st: st = stress.stress(G, all_sp=all_pairs_sp) st_count = st # if log%100==0: # print("ST:", st, end=" - ") st *= abs(compute_st) / initial_st sym = 0 sym_count = 0 if compute_sym: G = scale_graph(G, 1000) sym = ksymmetry.get_symmetric_score(G) G = scale_graph(G, 1 / 1000) sym_count = sym # if log%100==0: # print("Sym:", abs(sym), end=" - ") sym = 1 - sym sym *= abs(compute_sym) np = 0 np_count = 0 if compute_np: np = neighbors_preservation.compute_neig_preservation( G, all_sp=all_pairs_sp) np_count = np np = 1 - np np *= abs(compute_np) cr = 0 cr_count = 0 if compute_cr: cr = len(crossings.count_crossings(G)) # if log%100==0: # print("CR:", abs(cr), end=" - ") cr_count = cr cr *= abs(compute_cr) / initial_cr ar = 0 ar_count = 0 if compute_ar: ar = areafunctions.areaerror(G) ar_count = ar ar = abs(ar - 1) ar *= abs(compute_ar) / initial_ar # Aspect ratio asp = 0 asp_count = 0 if compute_asp: asp = areafunctions.aspectRatioerror(G) asp_count = asp asp = abs(asp - 1) asp *= abs(compute_asp) / initial_asp nonintv = 0 if compute_intcoo: nonintv = intcoord.nonintvalues(G) nonupward = 0 if compute_upward: nonupward = intcoord.upwardness(G) nonoverlapping = 0 if compute_nonoverlapping: nonoverlapping = intcoord.overlapping(G) upawardgrid = 0 if compute_upwardgrid: upwardgrid = intcoord.upwardgrid(G) print("Grid:", upwardgrid, end=" - ") return_val = upawardgrid # return_val = ue+st+sym+np+cr+ar+asp+nonintv+nonupward+nonoverlapping if print_val: print("score: ", return_val) log += 1 return return_val
output_txt += output_line + "\n" print(output_line) csv_head_line += "uniformity\_edge\_length&" csv_line += str(uniedgelen_val) + "&" if st or all: stress_val = stress.stress(G, weighted=weighted, all_sp=all_pairs_sp) output_line = "ST: " + str(stress_val) output_txt += output_line + "\n" print(output_line) csv_head_line += "stress&" csv_line += str(stress_val) + "&" if np or all: neigpres_val = neigpres.compute_neig_preservation(G, weighted=weighted, all_sp=all_pairs_sp) output_line = "NP: " + str(neigpres_val) output_txt += output_line + "\n" print(output_line) csv_head_line += "neighbors\_preservation&" csv_line += str(neigpres_val) + "&" if lblbb or all: labelsBBRatio_val = labelsmeas.labelsBBRatio(G) output_line = "lblbb: " + str(labelsBBRatio_val) output_txt += output_line + "\n" print(output_line) csv_head_line += "boundingbox\_ratio\_to\_labels&" csv_line += str(labelsBBRatio_val) + "&" if lblarea or all:
import neighbors_preservation as np s_path = sys.argv[1] g_path = sys.argv[2] outputTxtFile = sys.argv[3] input_file_name = os.path.basename(s_path) graph_name = input_file_name.split(".")[0] S = nx_read_dot(s_path) G = nx_read_dot(g_path) G = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)[0] S_induced = nx.Graph(G.subgraph(S)) np_val_S = np.compute_neig_preservation(S) np_val_SG = np.compute_neig_preservation(S, G) np_val_SGI = np.compute_neig_preservation(S, S_induced) output_txt = "Metrics for " + graph_name + "\n" output_txt += "np_S:" + str(np_val_S) + "\n" output_txt += "np_SG:" + str(np_val_SG) + "\n" output_txt += "np_SGI:" + str(np_val_SGI) + "\n" print(output_txt) csv_head_line = "filename;np;np(induced);np(complete)\n" csv_line = graph_name + ";" + str(np_val_S) + ";" + str( np_val_SGI) + ";" + str(np_val_SG) + "\n" exists = os.path.isfile(outputTxtFile)