Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pull-266-laplacian' into master.
Reviewed on PR-266. Closes ticket 1681.
- Loading branch information
Showing
2 changed files
with
68 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,27 +1,66 @@ | |||
# Author: Gael Varoquaux <gael.varoquaux@normalesup.org> | # Author: Gael Varoquaux <gael.varoquaux@normalesup.org> | ||
# Jake Vanderplas <vanderplas@astro.washington.edu> | |||
# License: BSD | # License: BSD | ||
|
|
||
import numpy as np | import numpy as np | ||
from scipy import sparse | from scipy import sparse | ||
|
|
||
from scipy.sparse import csgraph | from scipy.sparse import csgraph | ||
|
|
||
def _explicit_laplacian(x, normed=False): | |||
if sparse.issparse(x): | |||
x = x.todense() | |||
x = np.asarray(x) | |||
y = -1.0 * x | |||
for j in range(y.shape[0]): | |||
y[j,j] = x[j,j+1:].sum() + x[j,:j].sum() | |||
if normed: | |||
d = np.diag(y).copy() | |||
d[d == 0] = 1.0 | |||
y /= d[:,None]**.5 | |||
y /= d[None,:]**.5 | |||
return y | |||
|
|
||
def test_graph_laplacian(): | def _check_graph_laplacian(mat, normed): | ||
for mat in (np.arange(10) * np.arange(10)[:, np.newaxis], | if not hasattr(mat, 'shape'): | ||
np.ones((7, 7)), | mat = eval(mat, dict(np=np, sparse=sparse)) | ||
np.eye(19), |
|
||
np.vander(np.arange(4)) + np.vander(np.arange(4)).T, | if sparse.issparse(mat): | ||
): | sp_mat = mat | ||
mat = sp_mat.todense() | |||
else: | |||
sp_mat = sparse.csr_matrix(mat) | sp_mat = sparse.csr_matrix(mat) | ||
|
|||
laplacian = csgraph.laplacian(mat, normed=normed) | |||
n_nodes = mat.shape[0] | |||
if not normed: | |||
np.testing.assert_array_almost_equal(laplacian.sum(axis=0), | |||
np.zeros(n_nodes)) | |||
np.testing.assert_array_almost_equal(laplacian.T, | |||
laplacian) | |||
np.testing.assert_array_almost_equal(\ | |||
laplacian, | |||
csgraph.laplacian(sp_mat, normed=normed).todense()) | |||
|
|||
np.testing.assert_array_almost_equal( | |||
laplacian, | |||
_explicit_laplacian(mat, normed=normed)) | |||
|
|||
def test_graph_laplacian(): | |||
mats = ('np.arange(10) * np.arange(10)[:, np.newaxis]', | |||
'np.ones((7, 7))', | |||
'np.eye(19)', | |||
'sparse.diags([1, 1], [-1, 1], shape=(4,4))', | |||
'sparse.diags([1, 1], [-1, 1], shape=(4,4)).todense()', | |||
'np.asarray(sparse.diags([1, 1], [-1, 1], shape=(4,4)).todense())', | |||
'np.vander(np.arange(4)) + np.vander(np.arange(4)).T', | |||
) | |||
|
|||
for mat_str in mats: | |||
for normed in (True, False): | for normed in (True, False): | ||
laplacian = csgraph.laplacian(mat, normed=normed) | yield _check_graph_laplacian, mat_str, normed | ||
n_nodes = mat.shape[0] |
|
||
if not normed: |
|
||
np.testing.assert_array_almost_equal(laplacian.sum(axis=0), | if __name__ == '__main__': | ||
np.zeros(n_nodes)) | import nose | ||
np.testing.assert_array_almost_equal(laplacian.T, | nose.runmodule() | ||
laplacian) | |||
np.testing.assert_array_almost_equal(\ | |||
laplacian, | |||
csgraph.laplacian(sp_mat, normed=normed).todense()) |