Beispiel #1
0
    def translate_graph(nodes_data, edges_data):
        N = len(nodes_data)  # number of nodes
        a = {n[0]: i for i, n in enumerate(nodes_data)}
        g2 = rag.RAG()
        # generating the edges with the new numeration
        for e in edges_data:
            g2.add_edge(a[e[0]], a[e[1]], weight=e[2]['weight'])

        # reassinging the values to each node
        for n, values in nodes_data:
            n2 = a[n]
            for k, v in values.items():
                g2.node[n2][k] = v
        return g2
    def translate_graph(nodes_data, edges_data, threshold=threshold):
        N = len(nodes_data)  # number of nodes
        a = {n[0]: i for i, n in enumerate(nodes_data)}
        a_inv = {i: n[0] for i, n in enumerate(nodes_data)}

        g2 = rag.RAG()

        centroids = [np.array(n[1]['centroid']) for n in nodes_data]
        A = np.array(centroids)
        B = np.repeat(A, N, axis=1)
        X, Y = B[:, 0:N], B[:, N:2 * N]
        D = np.sqrt((X - X.T)**2 + (Y - Y.T)**2)
        # checks that the nodes will be connected with at least one other node to preserve the final number of nodes
        # in the resulting graph
        index_unconnected_nodes = np.invert((D < threshold).sum(axis=1) > 1)
        # sum the number problematic nodes indices has to be zero
        if not index_unconnected_nodes.sum() == 0:
            D2 = skip_diag_strided(D)
            D_0 = D2.min(axis=1)[index_unconnected_nodes]
            threshold = 2 * D_0.max()
            print(
                'warning: Max distance is less than theshold, reassining theshold to 0.7min_dist=',
                threshold)
        D = np.triu(D, k=1)
        D_1 = D > 0
        D_th = D < threshold
        D_th = D_th * D_1
        a1, a2 = np.nonzero(D_th)
        # generating the edges with the new numeration
        for i, j in zip(a1, a2):
            distance = D[i, j]
            g2.add_edge(i, j, weight=distance)

        # reassinging the values to each node
        for n, values in nodes_data:
            n2 = a[n]
            for k, v in values.items():
                g2.node[n2][k] = v
        return g2
Beispiel #3
0
    w1 = g[n].get(src, {'weight': -np.inf})['weight']
    w2 = g[n].get(dst, {'weight': -np.inf})['weight']
    return {'weight': max(w1, w2)}


def display(g, title):
    """Displays a graph with the given title."""
    pos = nx.circular_layout(g)
    plt.figure()
    plt.title(title)
    nx.draw(g, pos)
    nx.draw_networkx_edge_labels(g, pos, font_size=20)


g = rag.RAG()
g.add_edge(1, 2, weight=10)
g.add_edge(2, 3, weight=20)
g.add_edge(3, 4, weight=30)
g.add_edge(4, 1, weight=40)
g.add_edge(1, 3, weight=50)

# Assigning dummy labels.
for n in g.nodes():
    g.node[n]['labels'] = [n]

gc = g.copy()

display(g, "Original Graph")

g.merge_nodes(1, 3)