Search code examples
pythonarraysmatrixadjacency-matrix

Vectorize compressed sparse matrix from array in Python


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?


Solution

  • 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)