Skip to content

Commit

Permalink
add symmetric diffusion affinity for landmarking, fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgigante committed Jul 18, 2018
1 parent d7843b1 commit 05e809a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
25 changes: 25 additions & 0 deletions graphtools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,31 @@ def P(self):
self._diff_op = normalize(self.kernel, 'l1', axis=1)
return self._diff_op

@property
def diff_aff(self):
"""Symmetric diffusion affinity matrix
Return or calculate the symmetric diffusion affinity matrix
.. math:: A(x,y) = K(x,y) (d(x) d(y))^{-1/2}
where :math:`d` is the degrees (row sums of the kernel.)
Returns
-------
diff_aff : array-like, shape=[n_samples, n_samples]
symmetric diffusion affinity matrix defined as a
doubly-stochastic form of the kernel matrix
"""
row_degrees = np.array(self.kernel.sum(axis=1)).reshape(-1, 1)
col_degrees = np.array(self.kernel.sum(axis=0)).reshape(1, -1)
if sparse.issparse(self.kernel):
return self.kernel.multiply(1 / np.sqrt(row_degrees)).multiply(
1 / np.sqrt(col_degrees))
else:
return (self.kernel / np.sqrt(row_degrees)) / np.sqrt(col_degrees)

@property
def diff_op(self):
"""Synonym for P
Expand Down
46 changes: 41 additions & 5 deletions graphtools/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
log_complete,
log_warning,
log_debug)
from .base import DataGraph
from .base import DataGraph, PyGSPGraph


class kNNGraph(DataGraph):
Expand Down Expand Up @@ -457,7 +457,7 @@ def build_landmark_op(self):
is_sparse = sparse.issparse(self.kernel)
# spectral clustering
log_start("SVD")
_, _, VT = randomized_svd(self.diff_op,
_, _, VT = randomized_svd(self.diff_aff,
n_components=self.n_svd,
random_state=self.random_state)
log_complete("SVD")
Expand Down Expand Up @@ -485,12 +485,12 @@ def build_landmark_op(self):
pnm = pmn.transpose()
pmn = normalize(pmn, norm='l1', axis=1)
pnm = normalize(pnm, norm='l1', axis=1)
diff_op = pmn.dot(pnm) # sparsity agnostic matrix multiplication
landmark_op = pmn.dot(pnm) # sparsity agnostic matrix multiplication
if is_sparse:
# no need to have a sparse landmark operator
diff_op = diff_op.toarray()
landmark_op = landmark_op.toarray()
# store output
self._landmark_op = diff_op
self._landmark_op = landmark_op
self._transitions = pnm
log_complete("landmark operator")

Expand Down Expand Up @@ -1152,3 +1152,39 @@ def build_kernel_to_data(self, Y, gamma=None):
K = self.gamma * kernel_xy.minimum(kernel_yx.T) + \
(1 - self.gamma) * kernel_xy.maximum(kernel_yx.T)
return K


class kNNLandmarkGraph(kNNGraph, LandmarkGraph):
pass


class MNNLandmarkGraph(MNNGraph, LandmarkGraph):
pass


class TraditionalLandmarkGraph(TraditionalGraph, LandmarkGraph):
pass


class kNNPyGSPGraph(kNNGraph, PyGSPGraph):
pass


class MNNPyGSPGraph(MNNGraph, PyGSPGraph):
pass


class TraditionalPyGSPGraph(TraditionalGraph, PyGSPGraph):
pass


class kNNLandmarkPyGSPGraph(kNNGraph, LandmarkGraph, PyGSPGraph):
pass


class MNNLandmarkPyGSPGraph(MNNGraph, LandmarkGraph, PyGSPGraph):
pass


class TraditionalLandmarkPyGSPGraph(TraditionalGraph, LandmarkGraph, PyGSPGraph):
pass

0 comments on commit 05e809a

Please sign in to comment.