epfl-lts2 / pygsp

Graph Signal Processing in Python
https://pygsp.rtfd.io
BSD 3-Clause "New" or "Revised" License
483 stars 93 forks source link

compute_fourier_basis Assertion Error #20

Closed lucy3 closed 5 years ago

lucy3 commented 6 years ago

I'm having trouble getting past assert self._e[-1] <= 2 with a graph when I try to compute its fourier basis, so I'm wondering in why this assertion would fail? Why does the largest eigenvalue have to be <= 2?

nperraud commented 6 years ago

This assertion is only done if the Laplacian is normalized.

if self.lap_type == 'normalized':
     # Spectrum bounded by [0, 2].
     assert self._e[-1] <= 2

In this case, the spectrum has to be bounded by 2 from the theory. So if your weight matrix is non-negative and symmetric, it should always pass this test.

lucy3 commented 6 years ago

Thank you so much for the reply; I really appreciate it. Hmmm... this is strange, then. The minimum weight in my weight matrix is 1 and G.is_directed() returns False. Any other possible reasons this would fail?

Edit: I tried this:

Gg=pygsp.graphs.Graph(nx.adjacency_matrix(G),lap_type='normalized')
e, U = np.linalg.eigh(Gg.L.toarray())
print e[-1]
Gg.compute_fourier_basis(recompute=True)

This printed 2.0 followed by the assertion error assert self._e[-1] <= 2

Also, I have the following:

print "largest eigenvalue:", Gg._e[-1]
print "Gg._e[-1] == 2.0", Gg._e[-1] == 2.0
print "2.0 <= 2", 2.0 <= 2
print "Gg._e[-1] <= 2", Gg._e[-1] <= 2
print "float(Gg._e[-1]) == 2.0", float(Gg._e[-1]) == 2.0
print "float(Gg._e[-1])", float(Gg._e[-1])

The print output of the above is: largest eigenvalue: 2.0 Gg._e[-1] == 2.0 False 2.0 <= 2 True Gg._e[-1] <= 2 False float(Gg._e[-1]) == 2.0 False float(Gg._e[-1]) 2.0

I'm pretty confused now. It seems like when I print Gg._e[-1], its value is 2.0, yet the comparisons aren't working.

nperraud commented 6 years ago

It really looks like a numerical error. We should relax slightly the assertion. I will do it this afternoon. Thanks for reporting this bug.

nperraud commented 6 years ago

Could you print for us self._e[-1]-2?

lucy3 commented 6 years ago

It prints out 4.84057238737e-14.

mdeff commented 6 years ago

It's indeed a numerical issue then (your largest eigenvalue is marginally above 2). We'll relax the assertion. In the mean time you can just comment it. Thanks for reporting!

mdeff commented 5 years ago

That was fixed in 58421d553e8929e145a14b194ed0d335c6942296.