0

I am trying to apply graph theory methods to an image processing problem. I want to generate an adjacency matrix from an array containing the points I want to graph. I want to generate a complete graph of the points in the array. If I have N points in the array that I need to graph, I will need an NxN matrix. The weights should be the distances between the points, so this is the code that I have:

''' vertexarray is an array where the points that are to be 
    included in the complete graph are True and all others False.'''

import numpy as np
def array_to_complete_graph(vertexarray):

    vertcoords = np.transpose(np.where(vertexarray == True))

    cg_array = np.eye(len(vertcoords))

    for idx, vals in enumerate(vertcoords):
        x_val_1, y_val_1 = vals
        for jdx, wals in enumerate(vertcoords):
            x_diff = wals[0] - vals[0]
            y_diff = wals[1] - vals[1]
            cg_array[idx,jdx] = np.sqrt(x_diff**2 + y_diff**2)
    return cg_array

This works, of course, but my question is: can this same array be generated without the nested for loops?

randomusername
  • 7,927
  • 23
  • 50
carsonc
  • 85
  • 7

1 Answers1

0

Use the function scipy.spatial.distance.cdist():

import numpy as np

def array_to_complete_graph(vertexarray):

    vertcoords = np.transpose(np.where(vertexarray == True))

    cg_array = np.eye(len(vertcoords))

    for idx, vals in enumerate(vertcoords):
        x_val_1, y_val_1 = vals
        for jdx, wals in enumerate(vertcoords):
            x_diff = wals[0] - vals[0]
            y_diff = wals[1] - vals[1]
            cg_array[idx,jdx] = np.sqrt(x_diff**2 + y_diff**2)
    return cg_array

arr = np.random.rand(10, 20) > 0.75

from scipy.spatial.distance import cdist
y, x = np.where(arr)
p = np.c_[x, y]
dist = cdist(p, p)
np.allclose(array_to_complete_graph(arr), dist)
HYRY
  • 94,853
  • 25
  • 187
  • 187
  • That was incredibly fast. Thank you so much. I only regret that I have insufficient reputation to vote up. Thank you again. – carsonc Dec 22 '14 at 04:46