Repository navigation
Conversation
| k = np.sqrt(1.0 / nnodes) | ||
|
|
||
| if method == "energy": | ||
| if method != "force": |
There was a problem hiding this comment.
I think this bug is just a symptom of code being hard to maintain. This line could break if we add extra methods in the future.
I think it will be easier if we replace method=auto by its resolved value right at the start of the call. Is that something we could do?
There was a problem hiding this comment.
How about we use: if method in ("energy", "auto"):
We could also put it at the start of the public function for this suite of calls. But that separates the processing from the use by quite a bit. Might be easier to maintain with it all handled within a few lines of each other.
Certainly testing whether method != "force" is dangerous -- if someone misspells the word for example.
That shows that we also need an elif clause on this if structure. Maybe like:
if method in ("energy", "auto"):
return ......
elif method != "force":
raise NetworkXError(fmethod parameter {method} should be "energy", "force" or "auto"')
...There was a problem hiding this comment.
Thank you for the comment. I really appreciate your help.
I think this bug is just a symptom of code being hard to maintain. This line could break if we add extra methods in the future.
Yes, you're absolutely right. I should have fixed it in a different way.
Regarding the misspelling, we have already checked it here.
networkx/networkx/drawing/layout.py
Lines 588 to 589 in ee45879
I think using if method in ("energy", "auto"): is one of the most reasonable choices, but I am wondering if there might be an even better solution.
The logic for method='auto' is also complicated here.
networkx/networkx/drawing/layout.py
Lines 624 to 644 in ee45879
Therefore, adding the following lines after if method not in ("auto", "force", "energy"): might be one option:
if method == "auto":
method = "force" if len(G) < 500 else "energy"(Maybe replacing the raise ValueError part with a simple if statement would simplify the code, but I'm not sure why this code uses try and except. It already existed before my first commit.)
I would appreciate hearing your thoughts on how best to handle method="auto".
There was a problem hiding this comment.
I think we should resolve method at the start (replacing auto) and clean up all the auto checks. This includes removing the try catch for method selection
There was a problem hiding this comment.
Thank you for your reply. I mostly agree with you, but my only concern is that removing the try block might cause some unintended behavior. As I mentioned earlier, I'm not sure why this code uses try and except. Do you happen to know any specific reason why it was written this way?
There was a problem hiding this comment.
I don't have the historical context but you should be able to find it with git blame. I'm okay with doing that clean up in a separate PR to not block the fix.
What about this interim structure:
if can we do sparse?
try:
return sparse_method(...)
catch ValueError:
pass # we fallback to non-sparse
# non sparse method|
Thanks for your reply. I thought doing the PR in a separate PR and inheriting the structure you suggested would be a good idea, but I realized that it would be better to do it in the same PR. So I’ve made the changes in this PR. First, I found that the Here is the corresponding file from that commit: I believe the
However, the first two issues are already addressed in the current code using Based on this, I fixed the bug and cleaned up the code as suggested in the current PR. I also tested the code with the following configurations and confirmed it works successfully: (Please refer to here for the results)
I would appreciate it if you could review the current PR. |
|
Great job, thanks for the deep dive! I checked the code and what you are saying makes sense to me. We have some 20 years old code here 😅 Can you update the PR description with all your analysis so it's easier to track in the future? Code-wise I don't have any other comment |
dschult
left a comment
There was a problem hiding this comment.
Thank you for this!
I approve these changes.
@amcandio, Did you mean that @HirokiHamaguchi should edit the initial post of the PR? Or that they should add a comment with more details? And what details/analysis are you referring to? The link to the jupyter notebook webpage seemed good -- did you want some of that in this PR?
@dschult I meant including part of this context on why it is safe to remove try except. Would be nice to see that directly in description |
|
I added some of the text from the recent comment to the top post (the PR description). |
amcandio
left a comment
There was a problem hiding this comment.
Yes, that's what I had in mind!
There was a problem hiding this comment.
Thanks for the quick fix @HirokiHamaguchi !
Since this slipped through review the first time, I thought it was worth adding an explicit unit test for the method selection logic1 based on the number-of-nodes heuristic. I took the liberty of pushing such a test up... essentially, it works by computing the seeded spring_layout twice: once with method="auto" and once with the method set explicitly based on the expected method given the number of nodes (see the first parametrization). It's not an inexpensive test, but I can't think of another way to probe the number-of-nodes heuristic other than generating inputs that are greater than the limit!
Anyways: +1 from me for the code improvements, so I approve. @dschult @amcandio could you take a look and make sure you're okay with the test? The second parametrization could be removed but was inspired by the types of tests @HirokiHamaguchi had set up in the linked notebook. I'm also happy to pytest.mark it slow. LMK!
Footnotes
-
FWIW the test fails on
mainbut passes here, so it checks the intended behavior! ↩
@dschult Thank you for the edition! It seems the post you edited well summarizes the content. Thank you very much. |
Good call actually. Test looks good to me! Footnotes
|
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("num_nodes", "expected_method"), [(100, "force"), (501, "energy")] |
There was a problem hiding this comment.
What's the reason for using 501 instead of 500?
There was a problem hiding this comment.
Probably the same reason as the typo you commented above. Would be cool to test both ends of the edge 499 and 500
There was a problem hiding this comment.
The 3 parametrizations of the 501 tests take about 1 sec each -- and they are 3 of the top 10 tests in terms of time. Bumping up the "force" case to 499 adds ~2.5 secs more, while the case with 100 adds about 0.03 secs.
Maybe we should split out the test of "auto" from the tests of "3d" and "fixed node positions". We should still run those with both "energy" and "force", but could use the smaller n100 size when testing the other kwargs.
I'm going to merge this to get it in before the release. We can tweak the tests in a separate PR.
There was a problem hiding this comment.
Thank you very much for checking the test performance🙇
Co-authored-by: Gilles Peiffer <[email protected]>
Fix bug in _sparse_fruchterman_reingold
Dear NetworkX maintainers,
I am Hiroki Hamaguchi, a contributor to NetworkX and the author of this previous pull request.
I sincerely apologize, but I have discovered a bug introduced in that PR. Specifically, when
method="auto"is specified, thenx.spring_layoutfunction does not use the new_energy_fruchterman_reingoldfunction as intended.The fix is trivial, and I would appreciate it if you could kindly review and merge the proposed correction.
Thank you very much for your time and consideration.
Added based on discussion (details in comments below)
I believe the try/except block [using ValueError as a signal to try a different branch of code] is unnecessary.
A ValueError can occur in the following situations:
pos is a NumPy array and if pos == None is evaluated
(Raises: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all())
fixed is a NumPy array and if fixed == None is evaluated
(Same error as above)
dim is set to -1, leading to calls like np.random.random((nnodes, dim)) or np.zeros(dim)
(Raises: ValueError: negative dimensions are not allowed)
However, the first two issues are already addressed in the current code using if pos is None and if fixed is None, so the try/except block is redundant.
For the third case, this ValueError is raised in the current version of NetworkX, so it is not related to the try/except block.
Based on this, I fixed the bug [about "method" keyword] and cleaned up the code as suggested in the current PR.
I also tested the code with the following configurations and confirmed it works successfully:
(Please refer to here for the results)
Different
methodvalues:"auto"(len(G)=499 and 500)"energy""force"Providing an initial layout via the
posargumentFixing certain nodes using the
fixedargumentChanging layout dimensionality with the
dimargument:dim=3dim=-1to confirm error handling