Repository navigation
BUG/MAINT: fix edge betweenness centrality scaling when k<N and merge all b.c. rescale helper functions - #8256
Conversation
dschult
left a comment
There was a problem hiding this comment.
I'm not sure we want to join these two just yet. If I remember right, we revamped the scaling of betweenness for various sample sizes last winter/spring. We will likely need to work through what is needed for proper scaling of edge betweenness too. I can't recall the details of that discussion right now, but will try to go back to it.
I believe the reference is actually ok.
|
I'm assuming you're talking about #7908? In that PR, the consensus seems to have been that
|
dschult
left a comment
There was a problem hiding this comment.
Thanks for this --
I've got some very minor wrapping suggestions, but push back if any reason to keep them as are.
This Looks good to me.
…ies, add related methods to "See Also" section
Co-authored-by: Dan Schult <[email protected]>
|
The wrapping suggestions sound good too, done in b5c30f1! Thanks Dan :) |
rossbar
left a comment
There was a problem hiding this comment.
+1 for the consolidation, I didn't even realize there were multiple different versions of the _rescale_e helper floating around! Unfortunately there's now more logic to traverse in the general case, but not significantly more - so this seems good to me. Thanks @Peiffap !
|
Just to be sure, is this
supposed to be the case? I'm not familiar enough with these centrality measures to tell, but it does strike me as slightly weird... Note that this PR does not constitute a change in that behavior. :) Should that not be an issue, is there anything else left to do here? |
My hypothesis would be that it was copied over from a pre-existing |
|
I believe it would then make sense to take How about renaming the
|
|
Be careful -- the rescaling for The subset version also doesn't have a |
|
The keyword The important thing is to make sure all the various |
|
I'd rather stick with |
|
Thanks for going over this again Dan! I'll try to address all of your remarks here. I think the first thing to note is that this PR has unfortunately grown to cover two separate goals (although related):
To expand on that some more, I believe the PR as of b5c30f1 (which is the version with the two approving reviews) had exactly the same behavior as in the pre-refactor state. I'm happy to revert back to that state and handle (2.) in a separate issue/pull request! For clarity, this state had:
To address some of your remarks more directly:
There are two versions of
It doesn't have the same signature. The
Isn't it the other way around? Adding default values allows us to avoid these redundancies, whereas when no defaults are given, they need to be explicitly passed each time. The next paragraph has some details for why I believe the default value should actually be
+
The insight in your second quoted paragraph here actually helped a lot to wrap my head around this (thanks!). I was mentally treating To summarize: I'm happy to revert this PR back to the state where everything works exactly the same way as it did before, but with less boilerplate rescaling code. |
6167b89 to
0b1d986
Compare
Yes, indeed -- not passing
What I meant here is that when you do want to pass in the non-default value the call looks like: So, I would agree with you that we should "connect up" the |
|
|
|
Actually, this is still wrong. I'm trying to work through some examples manually and I don't like the answers I'm getting. The formula for the edge betweenness of an edge For example, consider an undirected 3-node path graph (i.e. 0-1-2), and disregard normalization. When However, running the following snippet on this PR doesn't agree with that >>> import networkx as nx
>>> G = nx.path_graph(3)
>>> # This choice of `k` and `seed` selects nodes 0 and 2.
>>> nx.edge_betweenness_centrality(G, k=2, seed=42, normalized=False)
{(0, 1): 2.25, (1, 2): 2.25}On main, the result is not much better: >>> import networkx as nx
>>> G = nx.path_graph(3)
>>> nx.edge_betweenness_centrality(G, k=2, seed=42, normalized=False)
{(0, 1): 1.5, (1, 2): 1.5}Am I going crazy here? I'm struggling to see how either output makes any sense. I'm growing increasingly convinced there are several things that should probably be changed/rethought about this module (and the subset betweenness module too). For instance, why are we always rescaling based on |
|
Setting k should not make V smaller. It should leave V the same but only sum over k starting nodes s from V. Use all_pairs shortest path, so sample the starting node and compute to all targets for the sampled starting nodes. At least that is how it works in node_betweenness. |
|
Ah, that makes sense. Thanks for clarifying! |
|
Added the test for There is one remaining question for me (sorry...): we always normalize by 2 when the graph is undirected, to avoid double-counting, but when |
|
We worked out the node betweenness centrality scaling in the Said another way, I think the "pairs in only one direction" is equally likely for all edges so we don't end up adjusting for it. We scale edge betweenness independent of which nodes were sampled. For the 3-cycle example with k=2, edge This is the kind of discussion we did in May with PR #7908 and we decided to wait until later to figure it out with edge betweenness. So, thanks for working through this with me. Am I missing something (its easy to do that). My conclusion is that we had it right in the |
|
I'm not sure I completely follow your comment, so I'll work through it bit by bit and you can tell me where I'm misunderstanding.
I see your point regarding dividing by 1 for directed and by 2 for undirected; I suppose there are many valid ways to choose what this correction factor should be; dividing by the total number of possible pairs to normalize (you wrote "edges" but I'm assuming that should be "pairs"?) seems reasonable. Note that when normalizing (i.e. the
Agreed, this sounds reasonable to me! Thanks for clarifying; as with some other points that came up during this discussion, it'll be good to add this to the docstring IMO! The choices make sense but they could perhaps be communicated more clearly.
The example I gave was a 3-path, but we can work through the cycle too!
There are 4 paths indeed (0->1, 0->2, 2->0, 2->1), we are missing the paths starting at 1, i.e. 1->0 and 1->2.
My final point of confusion in my last message was about whether the correction factor for undirected graphs should always be 2, and you've given me some confidence that it's sensical to do so!
Yes, if we disregard the fact Thanks for spending the time to dig into this with me Dan! I don't think you're missing anything, or at least nothing I'm not also missing—in any case, that'll be for someone else to dive into. 😄 One final note: can we get a bug fix label on here? Otherwise this is a breaking change now that |
dschult
left a comment
There was a problem hiding this comment.
Yes, you are correct that the normalized cases don't have any correction for the undirected case. The count and the max-possible-count in the normalized ratio both double-count undirected edges. So all those 2s I put in for normalized case aren't correct.
It looks like we're ready with this one! I'll put a BUG label on it.
k<N and merge all b.c. rescale helper functions
* sync edge bc scaling logic and rescale helpers with nx PR networkx/networkx#8256 * lint fix
* Requires that edge BC tests use NX 3.5 for comparisons, otherwise those tests are XFAIL'd. * Changes to NX 3.6 caused some comparisons in tests to fail when NX 3.6 is used as the reference. We're essentially skipping for now but will look into those changes (likely [here](networkx/networkx#8256)) * This also updates the `requires_nx` marker to take up to 2 args for min and max required version, and a flag for skip vs. xfail. Authors: - Rick Ratzel (https://github.com/rlratzel) - Brad Rees (https://github.com/BradReesWork) Approvers: - Brad Rees (https://github.com/BradReesWork) - Don Acosta (https://github.com/acostadon) URL: #5355
Fixes #5358. NetworkX 3.6 updated edge betweenness centrality rescaling for sampled sources. In cuGraph, normalized and undirected unnormalized cases already apply the sampled-source correction, but directed unnormalized edge betweenness with `k < n` currently skips the scaling step. This PR: * applies the missing `n / k` scaling for directed, unnormalized sampled-source edge betweenness; * updates the CPU reference rescaling to reflect the corrected behavior; * removes the legacy test-side NetworkX rescaling workaround and compares directly with NetworkX 3.6+; * adds deterministic explicit-source regression coverage at the C API and Python levels; * covers one source, a subset of sources, and all sources for normalized and unnormalized results. Full-source behavior is preserved, and the Brandes shortest-path accumulation is unchanged. Weighted edge betweenness, source-sampling redesign, and unrelated SG/MG/Dask changes remain outside the scope of this fix. ### Tests * Added independently specified expected-value coverage using a directed three-vertex path. * Added deterministic normalized and unnormalized tests with predefined source vertices. * Added coverage for `k=1`, `k<n`, and `k=n`. * Existing NetworkX comparison tests continue to cover directed and undirected graphs and `k=None`. ### API compatibility No public API or ABI changes. Related: #4974 and networkx/networkx#8256. Authors: - SAURAV SINGLA (https://github.com/sauravsingla) - Chuck Hastings (https://github.com/ChuckHastings) Approvers: - Chuck Hastings (https://github.com/ChuckHastings) - Seunghwa Kang (https://github.com/seunghwak) - Alex Barghi (https://github.com/alexbarghi-nv) URL: #5679
This PR refactors the rescaling code in
betweenness.pyandbetweenness_subset.pyto use the common_rescalefunction. Previously, these were separate functions for each situation (subset or not, node or edge).Only one of the four cases (
_rescaleinbetweenness.py, the version we keep) uses the more complicated logic withk,endpoints, andsampled_nodes. By reworking the helper function to no longer use akparameter (instead relying onsampled_nodesbeing eitherNone, or having lengthk), and giving default values to the other parameters, we end up being able to reuse the same code each time (modulo an off-by one change for edge betweenness centralities).It also fixes an incorrect reference for the edge betweenness centralities.
One thing that stood out to me while working on this is that the (removed in this PR) function
_rescale_einbetweenness.pyhad akparameter, but never set it to anything other thanNone(and it didn't have asampled_nodesparameter at all). None of the tests seem to catch this no matter what I set it to, but it'd be good to make sure this is supposed to be ignored!