Example #1
0
def fromList(shape, coords, weights=None):
    if weights is not None:
        assert len(coords) == len(weights)

    if len(shape) == 1:
        NX = NY = shape[0]
    else:
        NX = shape[0]
        NY = shape[1]

    if weights is None:
        X = [a for (a, b) in coords]
        Y = [b for (a, b) in coords]
        G = sps.lil_matrix((NX * NY, 1), dtype=np.float64)
        lin_I = common.sub2ind((NX, NY), X, Y)
        G[lin_I, 0] = 1
        G = G.reshape((NX, NY))
    else:
        G = sps.lil_matrix((NX, NY), dtype=np.float64)

        for (i, w) in enumerate(weights):
            c = coords[i]
            x = c[0]
            y = c[1]
            G[x, y] = w
    return G
Example #2
0
def create_knn_graph(N, idx, k=None):
    if k is None:
        k = idx.shape[1]
    G = sps.lil_matrix((N * N, 1), dtype=np.float64)
    for j in xrange(k):  # assign entries using linear indexing
        Ij = idx[:, j]
        lin_I = common.sub2ind((N, N), xrange(N), Ij.T)
        G[lin_I, 0] = 1
    G = G.reshape((N, N))
    return G