(img)
return ripser(sparseDM, distance_matrix=True, maxdim=0)["dgms"][0]
Construct a lower star filtration on an image

    Parameters
    ----------
    img: ndarray (M, N)
        An array of single channel image data

    Returns
    -------
    I: ndarray (K, 2)
        A 0-dimensional persistence diagram corresponding to the sublevelset filtration
Construct a lower star filtration on an image
Construct a lower star filtration on an image
def lower_star_img(img):
    """
    Construct a lower star filtration on an image

    Parameters
    ----------
    img: ndarray (M, N)
        An array of single channel image data

    Returns
    -------
    I: ndarray (K, 2)
        A 0-dimensional persistence diagram corresponding to the sublevelset filtration
    """
    m, n = img.shape

    idxs = np.arange(m * n).reshape((m, n))

    I = idxs.flatten()
    J = idxs.flatten()
    V = img.flatten()

    # Connect 8 spatial neighbors
    tidxs = np.ones((m + 2, n + 2), dtype=np.int64) * np.nan
    tidxs[1:-1, 1:-1] = idxs

    tD = np.ones_like(tidxs) * np.nan
    tD[1:-1, 1:-1] = img

    for di in [-1, 0, 1]:
        for dj in [-1, 0, 1]:

            if di == 0 and dj == 0:
                continue

            thisJ = np.roll(np.roll(tidxs, di, axis=0), dj, axis=1)
            thisD = np.roll(np.roll(tD, di, axis=0), dj, axis=1)
            thisD = np.maximum(thisD, tD)

            # Deal with boundaries
            boundary = ~np.isnan(thisD)
            thisI = tidxs[boundary]
            thisJ = thisJ[boundary]
            thisD = thisD[boundary]

            I = np.concatenate((I, thisI.flatten()))
            J = np.concatenate((J, thisJ.flatten()))
            V = np.concatenate((V, thisD.flatten()))

    sparseDM = sparse.coo_matrix((V, (I, J)), shape=(idxs.size, idxs.size))

    return ripser(sparseDM, distance_matrix=True, maxdim=0)["dgms"][0]
def lower_star_img ( img ) : m , n = img . shape idxs = np . arange ( m * n ) . reshape ( ( m , n ) ) I = idxs . flatten ( ) J = idxs . flatten ( ) V = img . flatten ( ) # Connect 8 spatial neighbors tidxs = np . ones ( ( m + 2 , n + 2 ) , dtype = np . int64 ) * np . nan tidxs [ 1 : - 1 , 1 : - 1 ] = idxs tD = np . ones_like ( tidxs ) * np . nan tD [ 1 : - 1 , 1 : - 1 ] = img for di in [ - 1 , 0 , 1 ] : for dj in [ - 1 , 0 , 1 ] : if di == 0 and dj == 0 : continue thisJ = np . roll ( np . roll ( tidxs , di , axis = 0 ) , dj , axis = 1 ) thisD = np . roll ( np . roll ( tD , di , axis = 0 ) , dj , axis = 1 ) thisD = np . maximum ( thisD , tD ) # Deal with boundaries boundary = ~ np . isnan ( thisD ) thisI = tidxs [ boundary ] thisJ = thisJ [ boundary ] thisD = thisD [ boundary ] I = np . concatenate ( ( I , thisI . flatten ( ) ) ) J = np . concatenate ( ( J , thisJ . flatten ( ) ) ) V = np . concatenate ( ( V , thisD . flatten ( ) ) ) sparseDM = sparse . coo_matrix ( ( V , ( I , J ) ) , shape = ( idxs . size , idxs . size ) ) return ripser ( sparseDM , distance_matrix = True , maxdim = 0 ) [ "dgms" ] [ 0 ]