Skip to content

BiRank Algorithm Implementation - #7978

Merged
dschult merged 44 commits into
networkx:mainfrom
LorentzFactor:birank-implementation
May 15, 2025
Merged

dschult merged 44 commits into
networkx:mainfrom
LorentzFactor:birank-implementation

Conversation

@LorentzFactor

Copy link
Copy Markdown
Contributor

This pull request adds an implementation of the BiRank algorithm to networkx under networkx.algorithms.bipartite.link_analysis, as proposed in #7963. It attempts to match the api patterns found in similar algorithms, like those found under networkx.algorithms.bipartite.centrality, as closely as possible.

Additionally, two unit tests have been added to ensure code coverage.

While the code is document to the standards set out by the contribution guidelines, I am considering adding additional context to the introduction to better explain when/what it should be used for.

If this seems like something worth moving forward with, I would like to make sure the my choices of input validation (ensuring alpha and beta have valid values, and ensuring there exists at least one node in the top and bottom sets) and Exception type (nx.NetworkXAlgorithmError) are inline with networkx conventions. I see some algorithms raise a generic NetworkXError- and I'm not sure if this the preference or not.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice contribution! Thanks!

I only ask that you wrap the comments and docstrings to ~80 chars (code is enforced to <88. But we don't have a tool for wrapping docstrings yet). I think the docstring is the only part of review I have left to do.

My other comments are all incredibly picky so only do the ones you feel improve what's here.

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good to me. Just a few very minor tweaks/suggestions. If you prefer not for any of them that's fine. They aren't blockers for sure.

I approve this PR.

bipartite node set ('bottom') are automatically determined by taking the
complement of 'top' with respect to the graph `G`.

alpha : float, optional (default=0.80 if top_personalization not empty, else 1)

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.

Does the paper specify why these 0.8 default? Would be good to add that to the docs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The paper talks a little about choices for alpha and beta. Their summary is:

BiRank needs more iterations to converge with a larger α (and β). [... A] smaller value of α (and β) leads to a larger effect of the static query vectors, helpful in achieving quick convergence.

My summary is: Larger alpha and beta reduce the effect of the personalizations and increase the number of iterations before convergence. Your results may vary by application and sometimes even by dataset. The choice of default value 0.8 is reasonable start, but you should see what works in your setting.

pytest.importorskip("scipy")


class TestBipartiteLinkAnalysis:

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.

Question regarding testing: is there any family of graphs where you can induce the result without calling the algorithm? For example, the case where all top connect to all bottoms.

If that's the case, can you put up a test that generates a random set of those graphs and validates the result matches the expected value?

@LorentzFactor

Copy link
Copy Markdown
Contributor Author

Hmm... just pushed a few commits implementing some of the suggestions you guys had. Looks like it's failing on extra for ubuntu 3.13 on a completely unrelated test:
FAILED networkx/readwrite/multiline_adjlist.py::networkx.readwrite.multiline_adjlist.read_multiline_adjlist

I'm not really sure what could be happening here, could this be an unrelated issue with networkx testing?

@dschult

dschult commented Apr 23, 2025

Copy link
Copy Markdown
Member

The failed test is almost certainly an update to one of the packages our CI is testing (or just using) in the "extra" suite of tests. It works on my machine locally, so we'll chase down what updated package is causing troubles.

You don't need to worry about that for this PR. :)

@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.

This is a very nice addition, thanks @LorentzFactor !

Just a couple minor things related to the function signature but otherwise LGTM!

@LorentzFactor

Copy link
Copy Markdown
Contributor Author

Made a few changes, including to the test values that are being used as references to compare the output with. See the reply to @amcandio for the explanation. I'm going to add a few more tests tomorrow to at least be testing both the case $\alpha=\beta=1$ and otherwise.

Does this mean you need to run it for at least one iteration?

Yes! It technically does mean that. And attempting to fix this issue actually lead me to realize there was a bigger one!

My solution was to initialize u to beta * (S @ p) + (1 - beta) * u0 - so effectively one iteration will occur for p when we update it in the loop.

BUT - this caused the test test_davis_birank_with_personalization to fail. The outputs were all different - but their linear ratios remained the same. Turns out that, naturally, in the case where there are no personalization vectors, there's nothing to enforce a scale on the converging solution. So the magnitude (but not direction) of the output vector is dependent on the input vector.

At the end of the day, this wouldn't impact the actually rankings of the nodes. But it definitely is preferable to have reproducible scores, regardless of α and β . So I now normalize the output vectors to unit vectors w.r.t L 1 in this case.

Thanks for the catch!

@LorentzFactor

Copy link
Copy Markdown
Contributor Author

Okay - a few changes later, and this should be in a mergable position. I've added some more parameterized tests to get better coverage. For large graphs, testing becomes pretty expensive due to converting the graph to a biadjancency matrix, so for now at least I'm limiting the total range of values tested.

@amcandio amcandio 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.

Approving with minor comments. This is a great addition!

@dschult

dschult commented Apr 28, 2025

Copy link
Copy Markdown
Member

Just two (maybe small -- let me know if not) requests left:

  • based on my skimming of the paper, the choice of alpha and beta is not well understood and probably changes with problem type and dataset. Can we say something about that in the parameter descriptions?
  • should we require personalization values to be positive? (raise? or ignore negative? Now we ignore zero, but are negative useful?)

@LorentzFactor

Copy link
Copy Markdown
Contributor Author

Just two (maybe small -- let me know if not) requests left:
...

  • should we require personalization values to be positive? (raise? or ignore negative? Now we ignore zero, but are negative useful?)

Regarding this - I'm for raising when there negative personalization values, although I can see the case for allowing them From a purely algorithmic perspective, there's nothing wrong with providing negative personalizations - convergence will still occur. But I'm not really sure why a user would want to use negatives, or really any personalization system where the minimum score is not represented by 0.

What bothers me is that the choice of minimum personalization seems to impact the output birank in a non-trivial way. That is to say, shifting personalizations by a uniform vector $c$ to achieve entirely non-negative values will transform the output birank score by $(I-S^TS)^{-1} c$. This is definitely not a uniform shift in the general case, and therefore will likely impact the rankings. But I find it hard to interpret the impact of such a transformation in a meaningful way.

It also is pretty clear in the paper that the intent was for values to be non-negative.

So ultimately this seems like the way to go, and I've added a commit to implement this.

@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.

Thanks @LorentzFactor , this LGTM! I took the liberty of pushing up minor docstring formatting fixes (including a whitespace issue, so the diff looks worse than it is!). This is a really nice addition!

I'll give @dschult a chance for one last once-over, otherwise I'll merge in the near future (certainly before the 3.5 release!)

@dschult dschult left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a lot of variation in the whitespace/indentation for the docs here. The math indent is 3 spaces, bullets are 2, cases-list is 4, G parameter is 3, but other parameters are 2.

Could you take a look at the indentation levels and select them intentionally. I htink the normal approach is 4 spaces for each indentation -- but e.g. references sometimes follow a different rule -- and @rossbar set those as 3. The bullet points on lines 37-47 look pretty good the way they are. But I think other than bullet points and references, the rest should probably be 4 spaces per indentation. What you choose isn't as important as making it consistent (so long as the docs look good -- can check by clicking on "details" of the "documentation artifact" test and then navigating to the page for the function you are changing/adding).

Thanks for this -- we are getting to some nitty details. So that means we are close to merging!
:)

@LorentzFactor

Copy link
Copy Markdown
Contributor Author

Sounds good - I've standardized non-bullet/reference indentation to 4 spaces. Let me know if there's anything else, formatting-wise.

LorentzFactor and others added 26 commits May 8, 2025 00:09
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
…nly compute u's error once p's check has passed
@dschult
dschult force-pushed the birank-implementation branch from 8650ddd to c64e6f1 Compare May 8, 2025 04:09
@dschult
dschult merged commit 092f50e into networkx:main May 15, 2025
@jarrodmillman jarrodmillman added this to the 3.5 milestone May 29, 2025
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.

5 participants