Exemplo n.º 1
0
def runOptimizer(G):
    '''
        Set inital values before optimization
    '''

    global initial_st
    global all_pairs_sp
    global initial_cr
    # 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("Initial 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)

    # Crossings steup
    initial_cr = 1
    if compute_cr:
        initial_cr = len(crossings.count_crossings(G))

    # Save the values to a file.

    return optimize(G)
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
Exemplo n.º 3
0
# import betametrics/vertexangularresolution as vertexangularresolution


graphpath = sys.argv[1]
outputTxtFile = sys.argv[2]

input_file_name = os.path.basename(graphpath)
graph_name = input_file_name.split(".")[0]



Gpgv = pgv.AGraph(graphpath)
G = nx_read_dot(graphpath)
G = nx.Graph(G)

crossings_val = cr.count_crossings(G)
uniedgelen_val = -1;#uniedgelen.uniformity_edge_length(Gpgv)
stress_val = -1#st.stress(G)
neigpres_val = -1#neigpres.compute_neig_preservation(G)
labelsBBRatio_val = labelsmeas.labelsBBRatio(G)
totLabelsArea_val = labelsmeas.totLabelsArea(G)
bbox_val = othermeas.boundingBox(S)


crossings_str = "crossings: " + str(crossings_val)
uniformity_str = "uniformity edge length: "+  str(uniedgelen_val)
stress_str = "stress: "+ str(stress_val)
neigh_str = "neighbors preservation: " + str(neigpres_val)
bb_str = "bounding box: " + str(bbox_val)
labelsBBRatio_str = "lbls ratio: " +  str(labelsBBRatio_val)
totLabelsArea_str = "lbls area: " +  str(totLabelsArea_val)
Exemplo n.º 4
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
        # converting weights in float
        all_weights_n = nx.get_node_attributes(G, "weight")
        for nk in all_weights_n.keys():
            all_weights_n[nk] = float(all_weights_n[nk])
        nx.set_node_attributes(G, all_weights_n, "weight")

        all_weights_e = nx.get_edge_attributes(G, "weight")
        for ek in all_weights_e.keys():
            all_weights_e[ek] = float(all_weights_e[ek])
        nx.set_edge_attributes(G, all_weights_e, "weight")
        all_pairs_sp = nx.shortest_path(G, weight="weight")
    else:
        all_pairs_sp = nx.shortest_path(G)

if cr or all:
    crss = crossings.count_crossings(G, ignore_label_edge_cr=True)
    crossings_val = len(crss)
    output_line =  "CR: " + str(crossings_val)
    output_txt += output_line + "\n"
    print(output_line)
    csv_head_line += "crossings&"
    csv_line += str(crossings_val) + "&"

if ue or all:
    uniedgelen_val = uniedgelen.uniformity_edge_length(G)
    output_line = "UE: " + str(uniedgelen_val)
    output_txt += output_line + "\n"
    print(output_line)
    csv_head_line += "uniformity\_edge\_length&"
    csv_line += str(uniedgelen_val) + "&"
Exemplo n.º 8
0
v_pos = nx.get_node_attributes(subG, "pos")

for e in edges_to_be_added:

    (s1, t1) = (e[0], e[1])
    if s1 == t1:
        continue

    if e in list(nx.edges(subG)):
        continue

    single_edge_list = [e]

    crossing = crossings.count_crossings(G=subG,
                                         edge_list_H=single_edge_list,
                                         stop_when_found=True)

    if crossing > 0:
        continue

    (u, v) = e

    if u not in nx.nodes(subG) or v not in nx.nodes(subG):
        continue

    x_source = float(v_pos[u].split(",")[0])
    y_source = float(v_pos[u].split(",")[1])

    x_target = float(v_pos[v].split(",")[0])
    y_target = float(v_pos[v].split(",")[1])
Exemplo n.º 9
0
#adj_mat = torch.empty(n, n, dtype=torch.float)
#print(adj_mat)
#pos=nx.spring_layout(G)
pos = nx.random_layout(G)
print(len(pos.keys()), n, pos[0])
nx.draw(G, pos=pos)
plt.show()

positions = dict()
for i in range(0, n):
    x = pos[i][0]
    y = pos[i][1]
    v_pos = str(x) + "," + str(y)
    positions[i] = v_pos
nx.set_node_attributes(G, positions, 'pos')
cr_arr = crossings.count_crossings(G)
print(cr_arr)
for e1, e2, p, _ in cr_arr:
    u, v = e1
    if G.has_edge(u, v):
        G.remove_edge(u, v)
    G.add_edge(u, n)
    G.add_edge(v, n)
    u, v = e2
    if G.has_edge(u, v):
        G.remove_edge(u, v)
    G.add_edge(u, n)
    G.add_edge(v, n)
    x, y = p
    pos[n] = [x, y]
    n = n + 1