Repository navigation
BUG: Handle KeyErrors in directed not strongly-connected group_betweenness_centrality - #8882
Merged
Merged
Conversation
Contributor
|
Nice! I'm loving this series of PRs! |
amcandio
approved these changes
Aug 30, 2026
2 of 5 tasks
13 of 18 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR tests and fixes KeyErrors in using the
Ddict-of-dicts for group_betweenness_centrality.The first commit adds the test -- I was pleased to find that a path graph with the group being an
interior 3-path segment tested all the KeyErrors needed.
The fix: (Note: I use e.g.
Dv = D[v], to pre-lookup and name the outer dicts)D[x][y] is needed for all 3 cases where adjustment of counts occurs.
So I made the for-loop over
yonly loop over keys inD[x](and in the group).The case
D[y][x]is not used. We are looking for wherevfits into a path fromxtoy.So we don't care about
ytox. Note that the loop will cover theytoxpatheventually with the names switched. So we do get that case in another iteration of the loop.
Of the remaining cases,
x_in_Dvandv_in_Dxare stored in the middle loop (where x and v are known).Then
y_in_Dvandv_in_Dyare stored in the inner loop. Neither of these save much time -- but I found itmuch easier to read
y_in_Dxthany in Dxnext to the rest of the multiple phrase if-condition.The underscores hold that operation together -- and there could be a very small help from pre-computing.
The resulting lines are like:
if x_in_Dv and Dv[y] == Dv[x] + Dx[y] and sig_vy:A nearby comment points out that
y_in_Dvmust hold ifx_in_Dv. The reason is that y is a descendant of x. So if x is a descendant of v, y is also a descendant of v. So we only need oneDmembership check in the first two cases and two checks in the third case. Thesig_vypart of the if-condition ensures that we skip when division by zero would occur.This treatment allows the tests to pass!
Also note that we recently included the example from #8666 by @amcandio as a test. It is passing here without KeyError (and with the correct value of
1instead of3). That bug was already fixed before this PR. But that test raised a KeyError in #8854 and doesn't with this approach.We still have on our list these two points:
endpoints=Trueand figure out how to make it meaningful, or remove that option.prominent_group_*,group_degree_*andgroup_closeness_*.