Repository navigation
ENH: adding magnetic_laplacian_matrix - #8562
Conversation
3ca9006 to
97473eb
Compare
This comment was marked as resolved.
This comment was marked as resolved.
5298a54 to
cc1630f
Compare
cc1630f to
9403b63
Compare
mcbosch
left a comment
There was a problem hiding this comment.
Main edit in file laplacianmatrix.py:
Added a simplified version of the magnetic Laplacian magnetic_laplacian_matrix and a normalized version normalized_magnetic_laplacian. Given a directed graph
Returns an scipy.sparse.csr_matrix.
Good upgrade should be adding a potential parameter which corresponds to the function
File spectrum.py:
Added two functions magnteic_spectrum and normalized_magnetic_spectrum which computes the spectrum of the magnetic Laplacian and the normalized version respectively with scipy.linalg.eigh(nx.linalg.magnetic_laplacian_matrix(G,q).todense(), eigvals_only=True)
|
In terms of the scipy parts of this PR, you should use a |
|
I did it that way because is it how it's computed in the other functions ( Regard the other There are other ways that you use "sparse eigenvalue finders"? |
There was a problem hiding this comment.
From the last review, the name of the functions have been changed removing the _matrix part, and it has been checked the format of the matrix; having a csr_array.
Returns an scipy.sparse.csr_array.
N.B: I searched for other options to compute the spectrum using the sparse format to have better performance. Other functions (scipy.sparse.linalg.eigh; scipy.sparse.linalg.eigs), with better computing performance, searches the k highest eigenvalues. I have aimed to scipy.linalg.eigh because the first eigenvalues are interesting and to maintain the structure of the other spectral methods.
dschult
left a comment
There was a problem hiding this comment.
Do you mean that you want to find the lowest eigenvalues, while these routines find the highest? Or do you mean that you want to find all the eigenvalues?
I dint think we will need the entire spectrum for most real world problems. Though maybe its helpful for small problems.
We should be able to turn the "lowest eigenvalues" into the "highest eigenvalues and vice versa fairly easily.
But I guess if it is done with dense matrices and complete spectra for the other laplacians these are questions beyond this PR. Sorry for the distraction.
Other distractions include: the spectrum functions all seems to be about one-liners with no clarity that it is the best way to get the eigenvalues one might want. Perhaps we should replace the spectrum.py module with examples sections in the doc_strings of the matrix creation routines. But again -- bigger than this PR.
As for code review, I've gone through the code part and have these comments. Havent looked at tests or docs yet.
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
dschult
left a comment
There was a problem hiding this comment.
This looks good!
I'm sure the tests could be parametrized. But that can be done in a follow-up.
This looks good to me!
I've got a couple of tweaks for the docs. I'm approving this in any case.
|
Thank you for all the review comments. I went through them and addressed them. In the last commit (only changed 6 lines in the doc-string) I got errors in the workflows, but check them, and they are Regarding the documentation I have one question before I do more commits: do I just need to add the new function to the |
rossbar
left a comment
There was a problem hiding this comment.
Thanks @mcbosch for taking another crack at it. This generally LGTM to me now - @dschult you had approved this in the past but enough has changed that it's worth another look!
IMV, there are two outstanding API questions:
- Should
normalized_magnetic_laplacianbe a separate function, or should it be anormalize=keyword argument tomagnetic_laplacian? My preference would be the kwarg approach just to keep the API surface small - Adding the corresponding
_spectrumfunctions. As mentioned, I'm not a fan of this pattern; however, it's so well-established that I think we go with precedent here and keep these function (or function, if we remove thenormalized_version.
That would be my preference anyway, but if folks feels strongly about having 2 separate functions for the unnormalized/normalized versions I would acquiesce!
dschult
left a comment
There was a problem hiding this comment.
Let's go forward with this as a single magnetic_laplacian function
with normalize keyword. And let's keep the separate function for
the _spectrum -- leaving deprecation/removal of the _spectrum
API surface to a separate PR covering all these _spectrum variants.
I've got two minor space-saving suggestions below.
And melding the normalized function into a keyword and related docs shift
is still needed of course. Let us know if you have questions.
And -- if the timing is bad for you let us know -- we could make the last changes and maybe get it in for the upcoming v3.7 release. :}
|
Thanks for all the reviews and help :). I've already changed the normalized function by the keyword option. Now I'll update the docstring. I thought in order to start the
But maybe it's better to discuss how we want this spectrum examples for a future separated Issue/PR working in the |
|
I think we should wait on the deprecation of the spectrum.py module. Otherwise this will take forever to get in :) But we should replace the normalized spectrum function with a keyword arg in the regular spectrum function -- similar to the laplacian functions themselves. Steps toward deprecation of spectrum involve multiple PRs:
Let's get this new function merged before we mix it up with the spectrum changes. So, leave out any examples involving spectrum from the doc-strings. At least thats my thoughts right now. Open for suggestions. |
so I can approve when final form is realized
dschult
left a comment
There was a problem hiding this comment.
I took the liberty of switching the normalized spectrum function to a keyword (to match magnetic_laplacian).
I also:
- changed the name to
magnetic_laplacian_matrixfrommagnetic_laplacianto match the rest of the functions here (and to show it is not the continuous laplacian in some way I guess. - changed the new tests to use
assert_allcloseinstead ofassert_almost_equalas recommended by numpy. - docstring cleanup
- test cleanup (keep expected answer near its test, etc)
I think this is ready for a final review
This no longer accurately describes the test class.
Check that the shape of the returned array is consistent with nodelist.
Matches the params that are kwarg-only in magnetic_laplacian.
Align wording with magnetic_laplacian_matrix docstring.
Preferring builtin exceptions where possible.
rossbar
left a comment
There was a problem hiding this comment.
Thanks @mcbosch for sticking with this through all the back-and-forth w.r.t. the API! This LGTM too - I took the liberty of pushing up a few final tweaks, mostly to ensure better alignment between the docstrings of magnetic_laplacian_matrix and magnetic_spectrum. I also took the liberty of changine one exception type from NetworkXError -> ValueError (for the check on q) as IMO builtin exceptions are generally preferred when they make sense!
I'll go ahead and merge once CI's green - thanks again @mcbosch and @dschult for the careful review!
I added a
magnetic_laplacian_matrixfunction onnetworkx/linalg/laplacianmatrix.py. I also added a test to verify it's workable. The Laplacian matrix works forDiGraphandGraph, nevertheless for an undirected graph returns a normal Laplacian Matrix.The Laplacian Matrix has been built as reference [1]. Here allows having weights on edges and does a mean in a symetrization process. The magnetic Laplacian is$L^{(q)}$ , a Hermitian matrix associated to a directed graph $G=(V,E)$ with weights $w_{jk}$ for edges $(j,k)\in E$ . Given a parameter $q\in [0, 1/2]$ , it is defined as:
where D is the degree of the underlying undirected graph, and$A^{(q)}$ is the Hermitian Magnetic Adjacency Matrix defined as
where$w_{jk}'$ is $0$ if neither $(j,k)$ or $(j,k)$ are in $E$ , and $(w_{jk}+w_{kj}))/2$ in other case, and $\theta_{jk}$ is either 1, -1 or 0 if the edge $(i,j)$ is in the specified direction, oposite or in both in $E$ respectively.
If this contribution seems correct maybe it's a good idea to add spectral functions and normalized version.
References
.. [1] Fanuel, M., Alaíz, C. M., Fernández, Á., & Suykens, J. A. (2018).
Magnetic eigenmaps for the visualization of directed graphs.
Applied and Computational Harmonic Analysis, 44(1), 189–199.