ValueError crash in "nx.degree_pearson_correlation_coefficient" when input graph with no edges
Hello! I am so sorry to bother you to investigate again, but I have no idea about this probable issue.
When testing NetworkX with a graph without edges, I found a strange crash in nx.degree_pearson_correlation_coefficient, with the following message:
Exception has occurred: ValueError
File "/home/qiuyang/.local/lib/python3.10/site-packages/networkx/algorithms/assortativity/correlation.py", line 156, in degree_pearson_correlation_coefficient
x, y = zip(*xy)
ValueError: not enough values to unpack (expected 2, got 0)
The statement that triggers the crash is here.
Since I am not sure whether degree_pearson_correlation_coefficient has a good definition in such graphs, it would be highly appreciated if you could help me further confirm and investigate this issue.
If there is no good definition, maybe we could throw a NetworkX Exception for users instead of the system exception.
Best regard, Joye
Step to Reproduce
Running the following Python code:
G = nx.DiGraph()
G.add_node(0)
G.add_node(1)
print(nx.degree_pearson_correlation_coefficient(G))
Result:
Exception has occurred: ValueError
Environment
NetworkX 3.1 Python 3.10
After the investigation, I also found a similar issue in networkx.k_components.
Step to Reproduce
import networkx as nx
G = nx.Graph()
G.add_node(0)
G.add_node(1)
print(nx.k_components(G))
Result:
ValueError: max() arg is an empty sequence
Best regards, Joye
Hello! I am so sorry to bother you
No need to apologize @joyemang33 , we very much appreciate the work you're doing by finding & reporting these cases!
Out of curiosity - are you using some sort of "fuzzing" tool or otherwise procedural workflow to find corner-cases?
Hi @rossbar! Thanks for your response. We are trying to develop a fully automated tool for graph libraries, including a fuzzing component to detect such corner cases, as well as a method based on metamorphic testing to detect potential logic issues (i.e., strange results). I will try my best to reduce and check the test cases before reporting. But it may still cause some burdens of false alarm to you since I can not be an expert on all algorithms here. Thanks again for your kind understanding!
Best regards, Joye
This one occurs because there are no edges in the graph... It shouldn't matter how many nodes. So adding a check with nx.is_empty(G) and raising a NetworkXPointlessConcept sounds like a good approach.
Thanks!
Hi @dschult @rossbar @MridulS ! Thanks a lot for your kind review of these corner cases #6913 #6914 #6915 #6916. I will create PRs to fix them one by one in the future, as well as report the remaining cases in one new report.
For #6912, I have no idea about the root cause of the crash. It would be appreciated if you could help me take a look at that :).
Best regards, Joye