Skip to content

Commit b5d8c06

Browse files
mdeffnperraud
authored andcommitted
correct number of edges (PR #21)
1 parent e166a3e commit b5d8c06

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

pygsp/graphs/nngraphs/nngraph.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -348,23 +348,23 @@ def __init__(self, features, center=True, rescale=True,
348348
# Discard distance to self.
349349
self.kernel_width = np.mean([np.mean(d[1:]) for d in D])
350350

351-
countV = list(map(len, NN))
352-
count = sum(countV)
353-
spi = np.zeros((count))
354-
spj = np.zeros((count))
355-
spv = np.zeros((count))
351+
n_edges = [len(x) - 1 for x in NN] # remove distance to self
352+
value = np.empty(sum(n_edges), dtype=np.float)
353+
row = np.empty_like(value, dtype=np.int)
354+
col = np.empty_like(value, dtype=np.int)
356355

357356
start = 0
358-
for i in range(N):
359-
length = countV[i] - 1
360-
distance = np.power(D[i][1:], 2)
361-
spi[start:start + length] = np.kron(np.ones((length)), i)
362-
spj[start:start + length] = NN[i][1:]
363-
spv[start:start + length] = np.exp(-distance / self.kernel_width)
364-
start = start + length
365-
366-
367-
W = sparse.csc_matrix((spv, (spi, spj)), shape=(N, N))
357+
for vertex in range(N):
358+
if kind == 'knn':
359+
assert n_edges[vertex] == k
360+
end = start + n_edges[vertex]
361+
distance = np.power(D[vertex][1:], 2)
362+
value[start:end] = np.exp(-distance / self.kernel_width)
363+
row[start:end] = np.full(n_edges[vertex], vertex)
364+
col[start:end] = NN[vertex][1:]
365+
start = end
366+
367+
W = sparse.csc_matrix((value, (row, col)), shape=(N, N))
368368

369369
# Enforce symmetry. May have been broken by k-NN. Checking symmetry
370370
# with np.abs(W - W.T).sum() is as costly as the symmetrization itself.

0 commit comments

Comments
 (0)