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

Added NodeNotFound exceptions to _apply_prediction and simrank, and ignored isolated nodes in panther_similarity #7110

Merged
merged 17 commits into from Dec 18, 2023

Conversation

Schefflera-Arboricola
Copy link
Contributor

@Schefflera-Arboricola Schefflera-Arboricola commented Nov 12, 2023

closes issue #7108

Please let me know if I should add any tests for these new NodeNotFound exceptions or mention them in the Raises sections. I didn't add any because it seemed excessive.

Thank you :)

@Schefflera-Arboricola Schefflera-Arboricola changed the title Added exceptions in link_prediction.py and similarity.py Added NodeNotFound exceptions to _apply_prediction and simrank, and ignored isolated nodes in panther_similarity Nov 16, 2023
@MridulS
Copy link
Member

MridulS commented Dec 5, 2023

Please let me know if I should add any tests for these new NodeNotFound exceptions or mention them in the Raises sections. I didn't add any because it seemed excessive.

Please do! It's generally a good idea to cover these exceptions in tests to make sure you can atleast hit the codeblocks. This may not seem that useful right now, but 10 years from now if someone is refactoring the code it should help them :)

@Schefflera-Arboricola Schefflera-Arboricola marked this pull request as ready for review December 13, 2023 07:05
@@ -1550,6 +1575,10 @@ def panther_similarity(
"""
import numpy as np

G = G.subgraph(
[node for node in G.nodes if node not in list(nx.isolates(G))]
).copy()
Copy link
Member

Choose a reason for hiding this comment

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

Is this supposed to silently ignore the isloates? Currently a KeyError pops up if there is a isolate and if there is only one node a ZeroDivisionError: float division by zero pops up.

Code examples:

>>> G = nx.Graph()
>>> G.add_node(1)
>>> nx.panther_similarity(G, 1)
...
...
ZeroDivisionError: float division by zero

>>> G.add_edge(1, 2)
>>> G.add_node(3)
>>> nx.panther_similarity(G, 3)
....
....
KeyError: 3

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review! I have raised NetworkXUnfeasible error if the source node is an isolated node and it will also take care of the null graph case. And otherwise it will silently ignore the isolated nodes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another way could be to only allow connected graphs(with more than 1 node) to be passed as G. Please let me know your thoughts on this!

@Schefflera-Arboricola
Copy link
Contributor Author

Also, here I think we should not remove the source node or initialize k=k+1 and then later remove the source node. Because currently the description of the argument k seems to suggest that a dictionary of k key-value pairs will be returned. Please let me know your thoughts on this.

Thank you :)

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.

Nice work!
I found one very minor nit below...

networkx/algorithms/similarity.py Outdated Show resolved Hide resolved
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.

Looks good to me!

@MridulS MridulS merged commit 59e6b7f into networkx:main Dec 18, 2023
39 checks passed
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
… and ignored isolated nodes in `panther_similarity` (networkx#7110)

* ignoring isolated nodes in panther_similarity

* added NodeNotFound in simrank_similarity

* added NodeNotFound in _apply_prediction

* bug fix

* added Raises sec to all funcs and updated error msg

* added Raises sec to simrank_similarity

* added tests to test_similarity.py and updated simrank_similarity docs

* updated link_prediction.py

* updated and added tests in test_link_prediction.py

* bug : updated NodeNotFound tests of simrank_similarity

* bug : updated test_simrank_target_not_found and style fixes

* added NetworkXUnfeasible to panther_similarity

* bug fix

* added NodeNotFound for panther_similarity

* style fix

* list() -> set()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants