in data.py [0:0]
def compute_rfa(features, mode='features', k_neighbours=15, distfn='sym',
connected=False, sigma=1.0, distlocal='minkowski'):
"""
Computes the target RFA similarity matrix. The RFA matrix of
similarities relates to the commute time between pairs of nodes, and it is
built on top of the Laplacian of a single connected component k-nearest
neighbour graph of the data.
"""
start = timeit.default_timer()
if mode == 'features':
KNN = kneighbors_graph(features,
k_neighbours,
mode='distance',
metric=distlocal,
include_self=False).toarray()
if 'sym' in distfn.lower():
KNN = np.maximum(KNN, KNN.T)
else:
KNN = np.minimum(KNN, KNN.T)
n_components, labels = csgraph.connected_components(KNN)
if connected and (n_components > 1):
from sklearn.metrics import pairwise_distances
distances = pairwise_distances(features, metric=distlocal)
KNN = connect_knn(KNN, distances, n_components, labels)
else:
KNN = features
if distlocal == 'minkowski':
# sigma = np.mean(features)
S = np.exp(-KNN / (sigma*features.size(1)))
# sigma_std = (np.max(np.array(KNN[KNN > 0])))**2
# print(sigma_std)
# S = np.exp(-KNN / (2*sigma*sigma_std))
else:
S = np.exp(-KNN / sigma)
S[KNN == 0] = 0
print("Computing laplacian...")
L = csgraph.laplacian(S, normed=False)
print(f"Laplacian computed in {(timeit.default_timer() - start):.2f} sec")
print("Computing RFA...")
start = timeit.default_timer()
RFA = np.linalg.inv(L + np.eye(L.shape[0]))
RFA[RFA==np.nan] = 0.0
print(f"RFA computed in {(timeit.default_timer() - start):.2f} sec")
return torch.Tensor(RFA)