Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH : Provide non-normalized and normalized directed laplacian matrix calculation #7199

Merged
merged 16 commits into from Jan 9, 2024

Conversation

smokestacklightnin
Copy link
Contributor

@smokestacklightnin smokestacklightnin commented Dec 31, 2023

This pull request addresses Issue #7148

Here, I provide both normalized and non-normalized graph Laplacian as well as improve the documentation for them.

I welcome any comments or questions

@smokestacklightnin smokestacklightnin changed the title Provide non-normalized directed laplacian matrix calculation DOC ENH : Provide non-normalized directed laplacian matrix calculation Dec 31, 2023
Copy link
Member

@dschult dschult left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done and with good tests.
And thanks for that reference.

For the docs, can you add the reference as part of that?
Maybe use the same example as is done for the undirected case so people can compare. If you have other better ideas please use them instead of my suggestions. :)

I don't think the current example should have the print statement. But the printing of numpy arrays in examples has been problematic in the past, so if removing that print function call makes it fail, leave it in obviously.

Thanks very much!!

@smokestacklightnin smokestacklightnin marked this pull request as ready for review January 4, 2024 03:22
@smokestacklightnin
Copy link
Contributor Author

smokestacklightnin commented Jan 4, 2024

@dschult I believe I've done everything you suggested in the Issue and your comments above.

I want to confirm that I understand your suggestion below:

In the doc_strings for those functions (or maybe in the module level doc_string, or maybe both: We should describe how all these relate. (The non-normalized version is produced by the main function, and normalized versions for undirected and the two normalizations for the directed case are provided by the other functions.)

Is something like this, this, and this what you had in mind?

Copy link
Member

@dschult dschult left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the tests, the addition of normalized_laplacian_matrix handling of DiGraph and the content of the doc_strings. The only things left as far as I can see involve formatting of doc_strings and making the examples differ from the test graphs. If this is too much trouble let me know as it's more formatting than coding and I know the NX formatting.

  • It looks like removing the "print" from the doc_string test did cause one test failure. Looks like the repr can vary from system to system, while str is consistent. So could we try putting the print back in? print(nx.laplacian_matrix(G).toarray())
  • The doc_strings should be wrapped to ~85 chars like the code. This can be a pain so if it doesn't fit, go over the limit, but if it is easy make a line break. Thsi doesn't change the html docs, but it does make it more uniform when reading the docs in the code.
  • Could we make the directed example one node smaller than the test graph (which comes fro the article)? For example, remove node 6. Or if you prefer 2 and 6? My goal is to make it easier to read the example, even if the resulting graph is not as good of a test of the algorithm.
  • Could we put the set_printoptions and comment on a separate line from the import of numpy?

I like the moving of the 1/2 in the math expressions. I like the tests. I like the expansion to the normalized_laplacian_matrix case. It's kind of amazing that we already had two normalized directed functions and we still didn't cover the basic extension of the undirected normalized definition. It just shows that there are many more possibilities in directed graphs than undirected graphs.

Thanks for this!

Also -- the dev tests might not pass due to new releases of pytest. We're working on that. After it is fixed on main we can merge this PR with main and they should pass.

networkx/linalg/laplacianmatrix.py Show resolved Hide resolved
@smokestacklightnin
Copy link
Contributor Author

smokestacklightnin commented Jan 6, 2024

  • It looks like removing the "print" from the doc_string test did cause one test failure. Looks like the repr can vary from system to system, while str is consistent. So could we try putting the print back in? print(nx.laplacian_matrix(G).toarray())
    

Done!

  • The doc_strings should be wrapped to ~85 chars like the code. This can be a pain so if it doesn't fit, go over the limit, but if it is easy make a line break. Thsi doesn't change the html docs, but it does make it more uniform when reading the docs in the code.
    

Done!

  • Could we make the directed example one node smaller than the test graph (which comes fro the article)? For example, remove node 6. Or if you prefer 2 and 6? My goal is to make it easier to read the example, even if the resulting graph is not as good of a test of the algorithm.
    

Done!

  • Could we put the set_printoptions and comment on a separate line from the import of numpy?
    

Done!

All the tests pass now. Ready for review an merge!

@smokestacklightnin smokestacklightnin changed the title DOC ENH : Provide non-normalized directed laplacian matrix calculation DOC ENH : Provide non-normalized and normalized directed laplacian matrix calculation Jan 6, 2024
@smokestacklightnin smokestacklightnin changed the title DOC ENH : Provide non-normalized and normalized directed laplacian matrix calculation ENH : Provide non-normalized and normalized directed laplacian matrix calculation Jan 6, 2024
Copy link
Contributor

@rossbar rossbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, thanks @smokestacklightnin . There's an issue with line length that should automatically be corrected by the linter, but which is currently not being handled correctly for whatever reason.

That issue should be handled separately though - for this PR, I think we can just go in and manually fix it prior to merging.


All calculations here are done using the out-degree. For Laplacians using in-degree, us `G.reverse(copy=False)` instead of `G`.

The `laplacian_matrix` function provides an unnormalized matrix, while `normalized_laplacian_matrix`, `directed_laplacian_matrix`, and `directed_combinatorial_laplacian_matrix` are all normalized.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did our linting rules change? These lines are clearly greater than 100 chars so I would've expected CI to complain about it. @MridulS any ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I go ahead and just fix the line lengths manually now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think our lint checks line lengths for comments. But I could be wrong.

Yes, @smokestacklightnin it would help to wrap the comment lines to about 80 chars (less than 88 for sure). There are others below...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@dschult dschult left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for this.
It looks good to me... :)

@rossbar
Copy link
Contributor

rossbar commented Jan 9, 2024

Excellent, thanks again @smokestacklightnin !

@rossbar rossbar merged commit e36da1d into networkx:main Jan 9, 2024
39 checks passed
@jarrodmillman jarrodmillman added this to the 3.3 milestone Jan 9, 2024
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
… calculation (networkx#7199)

Adds functionality for directed laplacian matrices

* Remove the `not_implemented_for("directed")` decorator on `laplacian_matrix`

* Add tests for 'laplacian_matrix` as applied to directed graphs

* Add pointers to `directed_laplacian_matrix` and `directed_combinatorial_laplacian_matrix` in docstring of `laplacian_matrix`

* Allow DiGraphs in `normalized_laplacian_matrix`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

4 participants