Skip to content

Add input validation to non_randomness() and clarify its behavior - #8057

Merged
MridulS merged 7 commits into
networkx:mainfrom
Peiffap:validate_nonrandomness_input
Jun 26, 2025
Merged

MridulS merged 7 commits into
networkx:mainfrom
Peiffap:validate_nonrandomness_input

Conversation

@Peiffap

@Peiffap Peiffap commented May 20, 2025

Copy link
Copy Markdown
Member

Fixes #4115.

This PR does three related things:

  1. It adds input validation for k, the number of communities, in non_randomness(). In particular, it raises a ValueError when k doesn't satisfy 1 <= k < n. Tests for this scenario are also added.
  2. It checks that the value of p computed 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.
  3. It improves (imo) the docstring, adding an example of this weird behavior for certain values of k and all-round making it more informative.

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally LGTM, just a few in-line comments!

Comment thread networkx/algorithms/non_randomness.py Outdated
# 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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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!

Comment thread networkx/algorithms/non_randomness.py Outdated

if k is None:
k = len(tuple(nx.community.label_propagation_communities(G)))
elif not 1 <= k < n:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've updated it to just be an if, that way the value set by label_propagation_communities gets checked as well.

@Peiffap
Peiffap force-pushed the validate_nonrandomness_input branch from ae00448 to 7d64c5d Compare May 30, 2025 22:10

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally LGTM - the documentation updates are clear improvements. I'd prefer to leave the existing exception messages unchanged, but am willing to not let perfect be the enemy of improvement in this case! Thanks @Peiffap

@Peiffap

Peiffap commented Jun 20, 2025

Copy link
Copy Markdown
Member Author

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.

@Peiffap

Peiffap commented Jun 20, 2025

Copy link
Copy Markdown
Member Author

Test failure strikes me as unrelated.

@MridulS
MridulS merged commit cadcffa into networkx:main Jun 26, 2025
@MridulS MridulS added this to the 3.6 milestone Jun 26, 2025
@Peiffap
Peiffap deleted the validate_nonrandomness_input branch June 26, 2025 11:09
amcandio pushed a commit to amcandio/networkx that referenced this pull request Jul 20, 2025
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

non_randomness fail with ValueError: math domain error on simple network

4 participants