-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDiffusion.py
More file actions
64 lines (54 loc) · 1.54 KB
/
Diffusion.py
File metadata and controls
64 lines (54 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'''
Created on Sep 1, 2016
@author: urishaham
'''
import numpy as np
from sklearn.neighbors import NearestNeighbors
#from scipy.sparse.linalg import svds
def Diffusion(K, nEigenVals = 12): # Laplace - Beltrami
d = np.sum(K, axis = 0)
d = np.sqrt(d)
D = np.diag(d)
D_inv = np.linalg.inv(D)
A = np.dot(np.dot(D_inv,K), D_inv)
Vals, Vecs = np.linalg.eig(A)
# sort eigenvalues
I = Vals.argsort()[::-1]
Vals = Vals[I]
Vecs = Vecs[:,I]
Vecs = np.dot(D_inv, Vecs)
Vecs = Vecs / Vecs[0,0]
Vecs = Vecs[:, 1:nEigenVals]
Vals = Vals[1:nEigenVals]
return (Vecs,Vals)
def makeRowStoch(K):
d = np.sum(K, axis = 0)
D = np.diag(d)
D_inv = np.linalg.inv(D)
return np.dot(D_inv,K)
def ComputeLBAffinity(X, k=16,sig=0):
Idx, Dx = Knnsearch(X, X, k)
K,W = ComputeKernel(Idx, Dx, sig=sig)
d = np.sum(K, axis = 0)
D = np.diag(d)
D_inv = np.linalg.inv(D)
K = np.dot(np.dot(D_inv,K), D_inv)
return K
def Knnsearch(X,Y,k):
nbrs = NearestNeighbors(n_neighbors=k, algorithm='auto').fit(X)
Dx, Idx = nbrs.kneighbors(Y)
return (Idx,Dx)
def ComputeKernel(Idx,Dx,epsilon=1,sig=0):
n = Dx.shape[0]
if sig == 0:
ep = np.median(Dx,axis=1)
ep = epsilon*np.tile(ep[:,None],(1,Idx.shape[1]))
else:
ep = sig
temp = np.exp(-np.power(np.divide(Dx,ep),2))
#temp[np.where(temp<1.0e-3)] = 0
W = np.zeros(shape=(n,n))
for i in range(n):
W[i,Idx[i,:]] = temp[i,:]
A = (np.transpose(W) + W)/2
return (A,W)