Skip to content

Add panther++ - #4400

Merged
rossbar merged 90 commits into
networkx:mainfrom
mrecachinas:feature/panther++
Sep 8, 2025
Merged

rossbar merged 90 commits into
networkx:mainfrom
mrecachinas:feature/panther++

Conversation

@mrecachinas

@mrecachinas mrecachinas commented Nov 29, 2020 •

Copy link
Copy Markdown
Contributor

As discussed, follow on to #3886.

This PR implements the Panther++ algorithm from https://doi.org/10.1145/2783258.2783267. In particular, a couple decisions were made to improve the algorithm's performance:

  • I noticed during testing that the similarities were not normalized to [0, 1], so I'm now normalizing to the maximum similarity value.
  • Also noticed that small graphs and some edge cases were causing NaNs, so I added another epsilon value to avoid that, and converted any lingering NaNs to 1.0 (which should never happen I think).
    • Note: I didn't reuse the epsilon parameter because for a sufficiently small graph, that value could be 1.0, which I think would be entirely too large for a reasonable epsilon.

  • Need to figure out how to convert similarities (i.e., 1 / || theta(v_i) - theta(v_j) ||) into a true similarity metric. Perhaps just normalized to between 0 and 1?
  • Probably should pull out the common core of panther_similarity and panther_similarity_pp
  • Add tests
  • Rename panther_similarity to panther_path_similarity? Changing to panther_similarity_pp.
  • Find a better place for generate_random_paths? Leaving for now.

@rossbar

rossbar commented Feb 20, 2021

Copy link
Copy Markdown
Contributor

@mrecachinas thanks for this PR and #3886!

There will be a NetworkX release in the near future (O[weeks]) which will have panther_similarity in it. One thing we would like to try to confirm is that the API for Panther and Panther++ is consistent, and that future development in this PR won't necessitate changes in the panther_similarity API. For example, in one of the bullets above, you mention changing the name from panther_similarity to panther_path_similarity. Changing the function name/signature for panther_similarity is painless now, but will require a deprecation cycle after release. I just wanted to check to see how happy you were with the current function names and signatures. Ideally we'd be able to get the function names and signatures settled prior to the NX 2.6 release. LMK what you think!

@mrecachinas

Copy link
Copy Markdown
Contributor Author

@rossbar I'm good with either -- definitely up to you all. The paper refers to the actual Panther path similarity algorithm simply as "Panther" and the vector similarity algorithm as "Panther++" --

image

I wasn't sure about naming it panther_plus_plus (since can't do panther++), but I'm happy with whichever you prefer.

@storopoli

Copy link
Copy Markdown

Guys, since I was the one who brought the panther paper as a feature request, I will chip in and give my two cents.

Since the algorithm and code returns a dictionary of nodes, I prefer panther_similarity without any paths. We are not returning paths I guess only similar nodes.

Also panther_similarity_plus_plus is a little big. What about panther_similarity_pp?

@dschult

dschult commented Feb 20, 2021

Copy link
Copy Markdown
Member

+1 for the _pp suffix. That's what VF2++ will use: either vf2pp or vf2_pp

@mrecachinas

Copy link
Copy Markdown
Contributor Author

Sounds good!

@mrecachinas

Copy link
Copy Markdown
Contributor Author

Also, wanted to revisit if @dschult @rossbar @jarrodmillman had anymore thoughts on a more permanent home for generate_random_paths (i.e., a Markovian random walk)?

@dschult

dschult commented Feb 22, 2021

Copy link
Copy Markdown
Member

I think we should leave generate_random_paths where it is until NXEP3 is figured out. Then there will be many more "path constructors" to put this with. (The idea is that many of our graph generators are really just constructing edges and/or paths so maybe we should make those available as edge constructors rather than -- or in addition to -- graph constructors.)

@rossbar

rossbar commented Feb 23, 2021

Copy link
Copy Markdown
Contributor

Re: naming, I agree with @dschult and @storopoli : Panther -> panther_similarity and Panther++ -> panther_similarity_pp.

I think we should leave generate_random_paths where it is until NXEP3 is figured out.

I agree. If it is likely that generate_random_paths will be moved or wrapped up in some other object, it might be worth making it "private" for the 2.6 release (i.e. prepend _ to the name). That way, it will still be available but the _ will indicate to users that there may be a name change/API reshuffle on the horizon. OTOH this may be overly cautious, and the arguments for moving it somewhere else may never come to anything. I'm ambivalent, but if others think that there is a very high likelihood that we'd want to move generate_random_paths to a new location in the near future, it's worth thinking about.

@jarrodmillman

Copy link
Copy Markdown
Member

I would go with _generate_random_paths for the 2.6 release, and revisit it with NXEP3. The rename could be a separate PR, since it should be merged quickly. It probably makes sense to add a note to NXEP3 mentioning _generate_random_paths in the same rename PR, so that we don't forget to revisit this.

@dschult

dschult commented Feb 24, 2021

Copy link
Copy Markdown
Member

The problem with making it a private function is that people won't see it or know they can use it. This is potentially a useful function for sampling paths. We shouldn't hide it. If we use an underscore and still put it in the __all__ variable and have it in the docs and in the main namespace, then I'm fine with making it private. But I'd prefer not to hide it from the documentation.

If it was up to me, I would leave it where it is without an underscore. If we get NXEP3 in place for v3.0 then I feel like we can still move the function to a new location within the package for v3.0. The API for the function will be the same. The function will still be the same -- we're only deprecating the location within the package. nx.generate_random_paths will work exactly as it does now. Only users who used the full path to the function will have to change, and the error they get will be clear and easy to fix.

I'm open to adding the _ if that's what should be done -- I'm just wanting verification that changing the location of a function within the package is a big enough change that the cost of keeping this function private is worth the benefit.

@jarrodmillman

Copy link
Copy Markdown
Member

I think we all understand the arguments for making it public or private. So I will leave it to @dschult to decide. It if is made public in 2.6 and deprecated in 2.7, we can still remove it in 3.0. If it is deprecated after the 3.0 release, it can be removed in 3.2.

@jarrodmillman

Copy link
Copy Markdown
Member

@dschult Here is another suggestion (but you and Ross should make the call on this):

We could mark the function private in networkx/algorithms/similarity.py; but then import it in the top-level namespace as follows:

from networkx.algorithms.similarity import _generate_random_paths as generate_random_paths

@dschult

dschult commented Feb 24, 2021

Copy link
Copy Markdown
Member

Interesting... Lots of Options...
Maybe @rossbar and I can explore the possibilities and come up with a rationale for when to do which. :}

@rossbar

rossbar commented Feb 25, 2021

Copy link
Copy Markdown
Contributor

TL;DR - I agree with @dschult , I'm +1 on having generate_random_paths remain fully public (no _-prepended) for 2.6 and beyond.

I think the points in @dschult 's points are the most compelling. One thing that I hadn't considered was that generate_random_paths is available in the top-level namespace (i.e. nx.generate_random_paths), so which pkg/module it is defined in is basically an implementation detail that isn't interesting for most users. It can be moved to pretty much any module, including a new one - as long as it's still imported into the top-level namespace, the usage-impact will be minimal. Of course, moving it would cause minor changes, e.g. where it can be found in the reference docs etc, but the search bar alleviates that concern.

@dschult dschult modified the milestones: networkx-2.6, networkx-2.7 Mar 3, 2021
Base automatically changed from master to main March 4, 2021 18:20
@jarrodmillman jarrodmillman modified the milestones: networkx-2.7, networkx-3.0 Apr 8, 2021
@rossbar rossbar modified the milestones: networkx-2.7, networkx-3.0 Feb 12, 2022
@rossbar rossbar removed this from the networkx-3.0 milestone Nov 29, 2022

@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 for the updates @mrecachinas - as I see it there are still 3 things that need to be addressed:

  • Let's remove the addition of the exception for isolated nodes inside generate_random_path. Off the top of my head there are two relatively simple alternatives that don't affect backward compatibility:
    1. Explicitly check for degree-0 nodes before calling generate_random_paths within the panther function(s)
    2. When an isolated node is selected, an exception is already raised so you could wrap the generate_random_paths call inside a try/except catching ValueError's that result from bad transition probabilities.
  • The doctest failure shows that the result is not deterministic. That may be okay, but let's double-check the source of the non-determinism and ensure it's not a result of failing to seed something.
  • The other test failure also seems real - I'd guess whatever fix was put in place for the k-off-by-one issue isn't working properly for k=1.

@mrecachinas

mrecachinas commented Aug 28, 2025 •

Copy link
Copy Markdown
Contributor Author

@rossbar Thanks for the feedback:

Let's remove the addition of the exception for isolated nodes inside generate_random_path. Off the top of my head there are two relatively simple alternatives that don't affect backward compatibility:

Yeah good call here. Should be addressed in cb5cf49.

The doctest failure shows that the result is not deterministic. That may be okay, but let's double-check the source of the non-determinism and ensure it's not a result of failing to seed something.

Struggling with this one -- I can't reproduce the error we're seeing in CI. 🤔

The other test failure also seems real - I'd guess whatever fix was put in place for the k-off-by-one issue isn't working properly for k=1.

The issue was in querying the KD-tree. There was an implicit assumption that the source node would always be returned in the k requested nodes, hence why I updated it to query for k+1. Unfortunately, e.g., for the spokes of a star graph, there was no guarantee the source would be returned. Should be fixed in cccb49f. Basically, I added a check if we had k results, and if not, took the top k that were returned (sorted by similarity).

@mrecachinas

mrecachinas commented Aug 28, 2025 •

Copy link
Copy Markdown
Contributor Author

@rossbar I suspect the nondeterminism may be coming from "ties" when querying the KD-tree. I'm sorting after the ties, but I don't know how the KD-tree internally would handle ties/stability.

At least for now, I've modified the doctest to illustrate the point (in d35320e).

@rossbar

rossbar commented Aug 28, 2025

Copy link
Copy Markdown
Contributor

I suspect the nondeterminism may be coming from "ties" when querying the KD-tree. I'm sorting after the ties, but I don't know how the KD-tree internally would handle ties/stability.

I see - in that case it's probably worth a note in the docstring.

On a related note - while reading through the kdtree docstring I noticed this:

For large dimensions (20 is already large) do not expect this to run significantly faster than brute force. High-dimensional nearest-neighbor queries are a substantial open problem in computer science.

This seems directly relevant to our default choice of D, which is 50. IIUC, we're essentially constructing the tree out of an NxD array, which would be very inefficient. I see that the default D=50 appears to have come from the paper, but perhaps we should consider a more reasonable (i.e. smaller) default given computational limitations. Note that KDTree switches over to brute force via the leafsize parameter, with a default value of 10. Again, IIUC, this means we're essentially getting no benefit from similarity vectors in this way.

@mrecachinas

Copy link
Copy Markdown
Contributor Author

@rossbar Think I covered your last two points. Lmk.

@mrecachinas

Copy link
Copy Markdown
Contributor Author

@dschult @rossbar Let me know if I've covered everything or if there's any remaining items.

@dschult

dschult commented Sep 6, 2025

Copy link
Copy Markdown
Member

I think the remaining item for this is the default value of D (the number of dimensions to choose).
The paper uses 50, but the SciPy code switches to the more inefficient handling for D>= 10. I see that the default here was originally at 50 and now has been set to 15. But that is still in the size where we don't get any benefit from similarity vectors (or am I understanding something incorrectly).

@mrecachinas

Copy link
Copy Markdown
Contributor Author

@dschult I've changed it to 10.

Thinking about this a bit more, the leafsize kwarg is "the number of points at which the algorithm switches over to brute-force", and it defaults to 10. Right now we just use the default. They also say going above 20 won't be significantly faster than brute force. I'm wondering what the metrics are in practice and whether we should set leafsize based on D if it's still generally, although not significantly, faster than brute force.

In any case, I did change it from 15 -> 10.

@dschult

dschult commented Sep 7, 2025

Copy link
Copy Markdown
Member

The sklearn version of KDtree has a default dimension of 40.
And there is a pure python version of KDtree that claims to be about 60 lines of code.
But it has a CC0 license and I'm not sure if that plays nicely with us, but it means it probably wouldn't be too hard to code it up.

I'm thinking we should probably merge this and figure out how to handle the KDtree issues in follow-up PRs. Thoughts?

@mrecachinas

Copy link
Copy Markdown
Contributor Author

SGTM

@rossbar

rossbar commented Sep 8, 2025

Copy link
Copy Markdown
Contributor

I'm thinking we should probably merge this and figure out how to handle the KDtree issues in follow-up PRs. Thoughts?

Works for me, thanks for taking a look @dschult !

@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 LGTM, thanks @mrecachinas for sticking with it and your helpful responses to all the questions/back-and-forth!

@rossbar
rossbar merged commit ae93776 into networkx:main Sep 8, 2025
@dschult dschult added this to the 3.6 milestone Sep 8, 2025
@mrecachinas
mrecachinas deleted the feature/panther++ branch September 8, 2025 22:01
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.

7 participants