Repository navigation
Add input validation to non_randomness() and clarify its behavior - #8057
Conversation
rossbar
left a comment
There was a problem hiding this comment.
Generally LGTM, just a few in-line comments!
| # corner case: graph has no edges | ||
| if nx.is_empty(G): | ||
| raise nx.NetworkXError("non_randomness not applicable to empty graphs") | ||
| raise nx.NetworkXError("graph has no edges") |
There was a problem hiding this comment.
Changing exception messages is a recipe for bogging down review as it's (arguably) unnecessary code churn unrelated to the main changes.
I don't have strong feelings in this case, but generally speaking I'd recommend avoiding such changes to avoid distracting side-discussions during review. If you feel very strongly about updating an exception message, the best way to go about it (IMO) is to open a dedicated PR for it!
There was a problem hiding this comment.
You're right that it's an unnecessary change. I'm juggling two feelings here: the very strong feeling I have about the lack of standardization of error messages (and types for that matter) across the repo on one hand, and the desire to keep changes as small and "meaningful" as possible (with the added problem that changes, especially to exception types, can break things for downstream users) on the other. I'm not sure whether anyone else shares these concerns, or how to best go about alleviating them, so I appreciate your perspective!
|
|
||
| if k is None: | ||
| k = len(tuple(nx.community.label_propagation_communities(G))) | ||
| elif not 1 <= k < n: |
There was a problem hiding this comment.
Should this just be an if? If k >> n this approach doesn't really apply, correct? Might as well fail early on user-provided values too.
There was a problem hiding this comment.
I'm not sure I understand your reasoning. Making it an if just means the value of k generated by the community detection gets checked as well, but I made the assumption it won't return less than 1 or more than n-1 communities and so it wouldn't ever catch anything. I guess it could be that the returned number of communities is exactly n though? I'm not familiar enough to say for sure.
There was a problem hiding this comment.
I've updated it to just be an if, that way the value set by label_propagation_communities gets checked as well.
ae00448 to
7d64c5d
Compare
|
You're probably right that the error message changes are a net negative, Ross. I still feel an effort should be made to standardize the format but that's something for a separate discussion. I've reverted the changes in 70c971d. I've also slightly reworked the logic for the value checking—now we don't compute half of the answer if the probability would be invalid anyways. |
|
Test failure strikes me as unrelated. |
…etworkx#8057) * Add input validation to `non_randomness()` and clarify its behavior * Add test case for `p` not in `]0, 1[` * Punctuation and capitalization in docs * Apply change from code review + docstring fixes * Revert error message changes and clean up `ValueError` handling * Add test for not implemented types --------- Co-authored-by: Gilles Peiffer <[email protected]>
Fixes #4115.
This PR does three related things:
k, the number of communities, innon_randomness(). In particular, it raises aValueErrorwhenkdoesn't satisfy1 <= k < n. Tests for this scenario are also added.pcomputed can be interpreted as a probability, i.e.0 < p < 1. This avoids the mysterious failure in non_randomness fail with ValueError: math domain error on simple network #4115 and this SO question. The test from the issue was added to check this.kand all-round making it more informative.