Example #1
0
def neighborhood_graph(D, k=1, eps=0., verbose=True, callback=None):
    r'''Neighborhood graph.'''
    ds = squareform(D, force='tomatrix')

    np.fill_diagonal(ds, np.inf)
    min_eps = ds.min(axis=0).max()
    if verbose:
        print('Minimal epsilon: {0}.'.format(min_eps))
    N = len(ds)

    G = _csr_graph(N)
    UF = union_find(N)

    i = 0
    for r in range(N):
        for c in range(r+1,N):
            if D[i]<=eps:
                G.reserve_edge(r)
                G.reserve_edge(c)
                UF.Union(r,c)
            i += 1
    row1, col1 = np.nonzero(ds<=eps)

    if k>1:
        d, j = nearest_neighbors_from_dm(D, k, callback=callback)
        del d

        # Add two edges (both directions) for each pair of kNN
        row_col  = np.empty((2*N*k,2), dtype=j.dtype)
        row_col[:N*k,0] = np.repeat(np.arange(N), k)
        row_col[:N*k,1] = j.flat # np.ravel(j) ?
        row_col[N*k:,0] = row_col[:N*k,1]
        row_col[N*k:,1] = row_col[:N*k,0]

        # Remove points as their own nearest neighbors.
        row_col = row_col[row_col[:,0]!=row_col[:,1]]

        # Keep only edges with distance >eps
        s = (ds[row_col[:,0],row_col[:,1]] > eps)
        row_col = row_col[s]

        # Remove all duplicate edges.
        row, col = _unique_rows(row_col).T # TBD timeit

        for r in row:
            G.reserve_edge(r)

        for r,c in zip(row,col):
            UF.Union(r,c)

    G.finalize()

    i = 0
    for r in range(N):
        for c in range(r+1,N):
            if D[i]<=eps:
                G.add_edge(r, c, D[i])
                G.add_edge(c, r, D[i])
            i += 1

    if k>1:
        for r,c in zip(row,col):
            G.add_edge(r,c,ds[r,c])

    G.validate()

    if UF.ncomp>1:
        print('The neighborhood graph has {0} components.'.format(UF.ncomp))

    G.ncomp = UF.ncomp
    return (G.rowstart, G.targets, G.weights)
Example #2
0
def neighborhood_graph(D, k=1, eps=0., verbose=True, callback=None):
    r'''Neighborhood graph.'''
    ds = squareform(D, force='tomatrix')

    np.fill_diagonal(ds, np.inf)
    min_eps = ds.min(axis=0).max()
    if verbose:
        print('Minimal epsilon: {0}.'.format(min_eps))
    N = len(ds)

    G = _csr_graph(N)
    UF = union_find(N)

    i = 0
    for r in range(N):
        for c in range(r + 1, N):
            if D[i] <= eps:
                G.reserve_edge(r)
                G.reserve_edge(c)
                UF.Union(r, c)
            i += 1
    row1, col1 = np.nonzero(ds <= eps)

    if k > 1:
        d, j = nearest_neighbors_from_dm(D, k, callback=callback)
        del d

        # Add two edges (both directions) for each pair of kNN
        row_col = np.empty((2 * N * k, 2), dtype=j.dtype)
        row_col[:N * k, 0] = np.repeat(np.arange(N), k)
        row_col[:N * k, 1] = j.flat  # np.ravel(j) ?
        row_col[N * k:, 0] = row_col[:N * k, 1]
        row_col[N * k:, 1] = row_col[:N * k, 0]

        # Remove points as their own nearest neighbors.
        row_col = row_col[row_col[:, 0] != row_col[:, 1]]

        # Keep only edges with distance >eps
        s = (ds[row_col[:, 0], row_col[:, 1]] > eps)
        row_col = row_col[s]

        # Remove all duplicate edges.
        row, col = _unique_rows(row_col).T  # TBD timeit

        for r in row:
            G.reserve_edge(r)

        for r, c in zip(row, col):
            UF.Union(r, c)

    G.finalize()

    i = 0
    for r in range(N):
        for c in range(r + 1, N):
            if D[i] <= eps:
                G.add_edge(r, c, D[i])
                G.add_edge(c, r, D[i])
            i += 1

    if k > 1:
        for r, c in zip(row, col):
            G.add_edge(r, c, ds[r, c])

    G.validate()

    if UF.ncomp > 1:
        print('The neighborhood graph has {0} components.'.format(UF.ncomp))

    G.ncomp = UF.ncomp
    return (G.rowstart, G.targets, G.weights)
Example #3
0
 def nn(k):
     return nearest_neighbors_from_dm(D, k, callback)
Example #4
0
 def nn(k):
     return nearest_neighbors_from_dm(D, k, callback)