1212def test_compute_laplacian ():
1313 def glap (mtx ):
1414 deg = mtx .sum (axis = 1 )
15- adj = dc (mtx )
16- adj [np .diag_indices (mtx .shape [0 ])] = 0
1715
18- L = np .diag (deg ) - adj
16+ L = np .diag (deg ) - mtx
1917 return L , deg
2018
2119 mtx = np .random .rand (4 , 4 )
2220 mtx = (mtx + mtx .T ) / 2
2321
24- L , deg = glap (mtx )
22+ adj = dc (mtx )
23+ adj [np .diag_indices (mtx .shape [0 ])] = 0
24+ L , deg = glap (adj )
2525 lapl , degree = laplacian .compute_laplacian (mtx )
2626
27- assert (lapl == L ). all ( )
28- assert (degree == deg ). all ( )
27+ assert np . allclose (lapl , L )
28+ assert np . allclose (degree , deg )
2929
3030 lapl , degree = laplacian .compute_laplacian (mtx , selfloops = True )
31- Lsl = np . diag (mtx . sum ( axis = 1 )) - mtx
32- assert (lapl == Lsl ). all ( )
33- assert (degree == deg ). all ( )
31+ L , deg = glap (mtx )
32+ assert np . allclose (lapl , L )
33+ assert np . allclose (degree , deg )
3434
3535 rn_deg = np .random .rand (4 )
3636 lapl , degree = laplacian .compute_laplacian (mtx , selfloops = rn_deg )
37- updeg = deg + rn_deg
3837 adj = dc (mtx )
3938 adj [np .diag_indices (mtx .shape [0 ])] = rn_deg
40- Lsl = np . diag ( updeg ) - adj
41- assert (lapl == Lsl ). all ( )
42- assert (degree == updeg ). all ( )
39+ L , deg = glap ( adj )
40+ assert np . allclose (lapl , L )
41+ assert np . allclose (degree , deg )
4342
4443 lapl , degree = laplacian .compute_laplacian (mtx , selfloops = "degree" )
45- updeg = deg + deg
44+ adj [np .diag_indices (mtx .shape [0 ])] = 0
45+ _ , deg = glap (adj )
4646 adj [np .diag_indices (mtx .shape [0 ])] = deg
47- Lsl = np . diag ( updeg ) - adj
48- assert (lapl == Lsl ). all ( )
49- assert (degree == updeg ). all ( )
47+ L , deg = glap ( adj )
48+ assert np . allclose (lapl , L )
49+ assert np . allclose (degree , deg )
5050
5151 mtx = mtx - mtx .mean ()
5252
@@ -56,22 +56,21 @@ def glap(mtx):
5656 mtx_res = (mtx - mtx .min ()) / mtx .max ()
5757
5858 L , deg = glap (mtx_abs )
59- lapl , degree = laplacian .compute_laplacian (mtx , negval = "absolute" )
60- assert (lapl == L ). all ( )
61- assert (degree == deg ). all ( )
59+ lapl , degree = laplacian .compute_laplacian (mtx , negval = "absolute" , selfloops = True )
60+ assert np . allclose (lapl , L )
61+ assert np . allclose (degree , deg )
6262
6363 L , deg = glap (mtx_rem )
64- lapl , degree = laplacian .compute_laplacian (mtx , negval = "remove" )
65- assert (lapl == L ). all ( )
66- assert (degree == deg ). all ( )
64+ lapl , degree = laplacian .compute_laplacian (mtx , negval = "remove" , selfloops = True )
65+ assert np . allclose (lapl , L )
66+ assert np . allclose (degree , deg )
6767
6868 L , deg = glap (mtx_res )
69- lapl , degree = laplacian .compute_laplacian (mtx , negval = "rescale" )
70- assert (lapl == L ). all ( )
71- assert (degree == deg ). all ( )
69+ lapl , degree = laplacian .compute_laplacian (mtx , negval = "rescale" , selfloops = True )
70+ assert np . allclose (lapl , L )
71+ assert np . allclose (degree , deg )
7272
7373
74- @mark .xfail
7574def test_normalisation ():
7675 L = np .random .rand (4 , 4 )
7776 L = (L + L .T ) / 2
@@ -80,10 +79,10 @@ def test_normalisation():
8079
8180 lapl_symm = laplacian .normalisation (L , d , norm = "symmetric" )
8281
83- d = np .diag (d )
84- lapl_rwi = laplacian .normalisation (L , d , norm = "random walk" )
82+ deg = np .diag (d )
83+ lapl_rwi = laplacian .normalisation (L , deg , norm = "random walk" )
8584
86- lapl_rwo = laplacian .normalisation (L , d , norm = "rwo" )
85+ lapl_rwo = laplacian .normalisation (L , deg , norm = "rwo" )
8786
8887 d [2 ] = 1
8988 d_symm = np .diag (d ** (- 1 / 2 ))
0 commit comments