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

refactor: modifications and additional tests for weighted distance #5305

Merged
merged 48 commits into from
Jul 28, 2022

Conversation

lucasmccabe
Copy link
Contributor

@lucasmccabe lucasmccabe commented Feb 5, 2022

Fixes #5257

Additional tests have been added for weighted graph distance computations.

@dschult
Copy link
Member

dschult commented Feb 5, 2022

Rather than a boolean input using a fixed attribute to hold the weights, we want this to allow input of the attribute name that should be used as a weight, and even allow a weight function. See the treatment done for weight arguments in the algorithms/shortest_paths/weighted.py module.

@rossbar
Copy link
Contributor

rossbar commented Jun 27, 2022

Gentle ping @lucasmccabe - this looks to be on the right track, the main thing that needs to be handled is updating the API to handle a user-specified weight attribute rather than baking-in the attribute name.

@lucasmccabe
Copy link
Contributor Author

Thanks for the ping @rossbar. In the most recent commit, I modeled the weight arguments in distance_measures after those in shortest_paths/weighted. Is this closer to what you and @dschult had envisioned?

@lucasmccabe

This comment was marked as outdated.

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.

This looks good! Thanks. I've got a couple of concerns on the handling of extrema_bounding (see below) and the same comments arise for eccentricity so I didn't repeat them. Basically, we don't want to worry about rounding the results. And we probably want to use the generic shortest_path_length so it can handle whether to compute with weights or not.

Removing the rounded values in the code might impact the tests, but probably not for these simple examples.

networkx/algorithms/distance_measures.py Outdated Show resolved Hide resolved
networkx/algorithms/distance_measures.py Outdated Show resolved Hide resolved
networkx/algorithms/distance_measures.py Outdated Show resolved Hide resolved
@dschult
Copy link
Member

dschult commented Jul 15, 2022

I resolved some conflicts with the main branch (which removed deprecated code). So you will need to pull the changes from your fork to your local machine before you edit your files more. Hope that's not trouble and if you need help sorting that out let us know.

- remove extraneous G_succ
- invoke shortest_path_length in _extrema_bounding and eccentricity
- add distance_method arg to _extrema_bounding, diameter, periphery, radius, and center
- weighted tests use integer weights
- remove precision-handling for float weights
- revert to float-weighted tests
- remove distance_method argument
- docs update re: weights
@dschult
Copy link
Member

dschult commented Jul 18, 2022

This looks good. I checked out how the documentation would look after this PR. You can do that by clicking on "Show all checks" on the right side of the box that says "All checks have passed". Then scroll down to the 3rd to last check which is run using "CircleCI" and says "document artifact". Click on the "Details" button and you are brought to a version of the webpage for documentation as it will be after this PR. It's a little bit of clicking to find your function: Reference (at the top) then algorithms and "distance measures". And then you can look at each function and how it renders wit its doc_string.

All your doc_strings look good.

But I do see that "resistance_distance" has some unformatted notes at the bottom. Would you be willing to change the formatting near lines 672. After the "Overview discussion:" we need a blank line and then indent the two bullet lines below it. Similarly, after the heading "Additional Details:", only those also need a star in front to make a bullet in the webpage. If you have had enough, just say so and we'll stop here and merge this. The other change can be done in another PR.

Thanks very much! Job well done! :}

impacted: resistance_distance and __extrema_bounding
@lucasmccabe
Copy link
Contributor Author

lucasmccabe commented Jul 18, 2022

This looks good.

Great! Thanks for the review, @dschult.

But I do see that "resistance_distance" has some unformatted notes at the bottom.

I see what you mean. In the most recent commit, I address this and make a similar adjustment in _extrema_bounding.

Some checks were not successful

I'm not sure what to make of the three cancelled checks. They seem to all be for Mac OS w/ 3.8. The only differences between 072b960 and d90a41f are the docstring updates. Perhaps it has something to do with this?

jarrodmillman and others added 5 commits July 18, 2022 11:36
* Add test for nice exception.

* Add input validation to geometric_edges.

* Fix None identity checking.

Co-authored-by: Dan Schult <dschult@colgate.edu>

Co-authored-by: Dan Schult <dschult@colgate.edu>
Fixes the formulae used to calculate gain and removal cost in calculation
of one level of the Louvain partition tree. Errors here were causing infinite loops
for some cases in the past, see networkxgh-5175 and networkxgh-5704.

This PR also adds test cases to ensure infinite loops are not entered for these cases.

Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
* Add more comprehensive tests for pydot

* remove the second element from frozenset for cleaner tests

Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
@dschult
Copy link
Member

dschult commented Jul 19, 2022

I think the tests just timed out on the hardware they were use. (There are time limits for some donated hardware.) I have restarted the first and it did not cancel this time. Will restart the other two and hope for the best.

@lucasmccabe
Copy link
Contributor Author

I think the tests just timed out on the hardware they were use. (There are time limits for some donated hardware.) I have restarted the first and it did not cancel this time. Will restart the other two and hope for the best.

Ah, I see. Thanks for your help!

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.

This is really good -- Thanks very much.

I only have one comment/question. In a few of the functions, you call with the first input value G=G instead of just G. The function signature doesn't provide a keyword argument for the first input. It's just G. It reminds me that recent versions of Python allow function arguments to be keyword-only or position-only. Is there a reason you used this syntax? I don't know the implications.

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.

It looks like we're on the right track!

My main concern is about the default value of the new weight kwarg - see my comment below for details.

I agree with @dschult - we should get rid of the G=G pattern in the calls.

Finally, there should be some test cases of the new weight argument - the added tests all assume the default value.

networkx/algorithms/tests/test_distance_measures.py Outdated Show resolved Hide resolved
networkx/algorithms/tests/test_distance_measures.py Outdated Show resolved Hide resolved
networkx/algorithms/distance_measures.py Outdated Show resolved Hide resolved
@lucasmccabe
Copy link
Contributor Author

@dschult @rossbar Thank you for your review and guidance. The adjustments recommended above are included in the most recent commit, and tests are implemented for all three weight options (None, "weight", function).

@dschult
Copy link
Member

dschult commented Jul 25, 2022

Ack -- looks like you merged with the v2.8 branch instead of the main branch.
I think we can fix that. I'll try later today.

The changing of the default weight looks good.
Can we have a test to make sure it works even if the name of the weight attribute is "cost' (or anything else that is not "weight")? Go ahead and change and push when you've got it. I'll fix the v2.8 branch stuff after that. (or before it if I get to it before you get to it)

@lucasmccabe
Copy link
Contributor Author

lucasmccabe commented Jul 25, 2022

make sure it works even if the name of the weight attribute is "cost"

Good point - I've added two more edge attributes in the tests: "cost" (which has the same value as "weight") and "high_cost" (which is 10x "cost"). I test that using weight="cost" produces the same results as weight="weight," then test that I get the appropriate results when using weight="high_cost" (i.e. distances like radius should be different, but vertex groups like center should be the same).

looks like you merged with the v2.8 branch

My mistake. I reverted the commit that merged with v2.8 and made a separate commit merging with main.

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.

This looks good to me!
Thanks @lucasmccabe for sticking with all our requests for changes.

@lucasmccabe lucasmccabe requested a review from rossbar July 28, 2022 13:44
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.

Thanks @lucasmccabe ! I took the liberty of making a few final cosmetic changes, but everything else LGTM

@rossbar rossbar merged commit 28f78cf into networkx:main Jul 28, 2022
@lucasmccabe
Copy link
Contributor Author

Great! Thanks for your help @dschult @rossbar

@jarrodmillman jarrodmillman added this to the networkx-3.0 milestone Jan 6, 2023
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
Adds the weight keyword argument to allow users to compute weighted distance metrics
e.g. diameter, eccentricity, periphery, etc. The kwarg works in the same fashion as the
weight param for shortest paths - i.e. if a string, look up with edge attr by key, if callable,
compute the weight via the function. Default is None, meaning return unweighted result
which is the current behavior.

Co-authored-by: Dan Schult <dschult@colgate.edu>
Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
Adds the weight keyword argument to allow users to compute weighted distance metrics
e.g. diameter, eccentricity, periphery, etc. The kwarg works in the same fashion as the
weight param for shortest paths - i.e. if a string, look up with edge attr by key, if callable,
compute the weight via the function. Default is None, meaning return unweighted result
which is the current behavior.

Co-authored-by: Dan Schult <dschult@colgate.edu>
Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Distance_measures.py functions should work for weighted graphs too.