Skip to content

Forceatlas2 networkx implementation - #5392

Closed
cvanelteren wants to merge 26 commits into
networkx:mainfrom
cvanelteren:forceatlas2-networkx
Closed

cvanelteren wants to merge 26 commits into
networkx:mainfrom
cvanelteren:forceatlas2-networkx

Conversation

@cvanelteren

Copy link
Copy Markdown
Contributor

Implementation of forceatlas2 layout based on https://journals.plos.org/plosone/article/file?id=10.1371/journal.pone.0098679&type=printable

Example:

import networkx as nx, matplotlib.pyplot as plt
fig, ax = plt.subplots()
g = nx.florentine_families_graph()
pos =nx.forceatlas2_layout(g)
nx.draw(g, pos = pos)
# or
#nx.draw_forceatlas2(g, ax = ax)
plt.show(block = 1)

image

@cvanelteren

Copy link
Copy Markdown
Contributor Author

Small note. This implementation is not as fast as #5311. Running code without the cython dependency is not usable for larger graphs (in my opinion). Additionally, it does not contain the Barnes-Hut optimization yet. I may end up writing this in the future, or leave it up to the community. This is an initial implementation to get the layout out there.

@dschult

dschult commented Mar 15, 2022

Copy link
Copy Markdown
Member

Before you put a lot more effort into this, NetworkX does not provide any cpp or pxd code. It should be pure python.

@cvanelteren

Copy link
Copy Markdown
Contributor Author

@dschult code is in pure python with optional cython bindings.

@rossbar

rossbar commented Mar 15, 2022

Copy link
Copy Markdown
Contributor

Rather than relying on code that needs to be compiled (which would be a blocker, even optionally), I wonder if this implementation could instead be recast in terms of sparse/vectorized operations. FWIW the current force-directed layouts provided by NetworkX (kamada-kawai, fruchterman-reingold) are implemented this way.

@cvanelteren

Copy link
Copy Markdown
Contributor Author

@rossbar just pushed a vectorized implementation. This removes all the cython code.

@cvanelteren

Copy link
Copy Markdown
Contributor Author

In case there is a need for additional code, e.g. unittests, let me know. @rossbar @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.

I haven't had time to take a detailed look yet but yes it'd be great to add some tests. Ideally we could find some examples (perhaps from the paper) of the expected node positions (give or take scale factors) and verify that the results of the layout function against those. I skimmed the paper and didn't notice any obvious candidates. I'm also not a Gephi user so I have no idea if there's a good way to generate such test data. Any ideas?

@rossbar

rossbar commented Mar 23, 2022

Copy link
Copy Markdown
Contributor

I took the liberty of pushing up a minor change just to retrigger the CI.

- Added gravity
- Fixed swing update computation
@cvanelteren
cvanelteren force-pushed the forceatlas2-networkx branch from 2388aeb to 397ac4c Compare March 24, 2022 05:51
@cvanelteren

cvanelteren commented Mar 24, 2022 •

Copy link
Copy Markdown
Contributor Author

I wrote some code that calls gephi to run forceatlas2 starting from the same layout. I am not a gephi user unfortunately, and the gui does not run on my OS. It seems like the initial layout I give gephi through the cli is ignored. By eye, the layouts generated with the code above look similar, but not the same due to the different starting conditions. All other parameters are matched (code here).

I also removed the sparse matrix conversion, as distance are less likely to be sparse anyway. This prohibits the vectorized approach for larger graphs. I'm open to an alternative that is not using for loops for this (or use the earlier cythonized commit).
image

EDIT: I was going to use the gist in some form to generate ground truth positions and wrap those in a unittest (without the call to gephi ofcourse ;-))

@cvanelteren

Copy link
Copy Markdown
Contributor Author

Any comments on this PR or wanted changes?

Comment thread setup.py Resolved
@cvanelteren
cvanelteren force-pushed the forceatlas2-networkx branch from ff3b39d to f5133cc Compare May 12, 2022 05:46
@cvanelteren

Copy link
Copy Markdown
Contributor Author

@dschult I believe the PR is ready for merging. The last check seems to be related to the build environment, not to the PR. Best, C

@dschult

dschult commented May 13, 2022 •

Copy link
Copy Markdown
Member

I agree that this is ready for reviews/approvals...

The tests seem to indicate a problem on the circle-ci build action with being unable to update apt-get.

That has nothing to do with this PR. I will hopefully be able to get to this PR next week.

@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!! Thanks!!
It needs tests. Your pictures look great and agree with Gephi. But as you know we need to make sure it keeps looking great. So tests are needed. Use pytest. Take a look at the other layout tests. If you have questions, ask.

I'm a little confused as to why the setup.py changes appear in this PR. I thought those changes were already merged into networkx. Why are they showing up in this PR?

Comment thread networkx/drawing/layout.py Resolved
@cvanelteren

Copy link
Copy Markdown
Contributor Author

I believe this is ready to be merged @dschult.

@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! Thanks!

I have a few very small comments below.

As a topic for another PR (unless you want t take it on), many of the layout functions set the initial conditions by default to a random layout. But they don't provide a way to control the random seed for that output. We should either let them provide a seed, or (my preference) add a phrase in the documentation for the pos argument which says that the default is a random layout. If they want to control the seed of that random layout they should generate it themselves and not use the default pos. I think this could be done with the kamada_kawai_layout doc_string too. I didn't see others, but there might be others.

Comment thread networkx/drawing/tests/test_layout.py Resolved
Comment thread networkx/drawing/layout.py Resolved
@dschult

dschult commented Apr 23, 2024

Copy link
Copy Markdown
Member

Looks like a merge from main and a bunch of lint tweaks cleaned up the tests. You will need to pull it down to your local machine of course before making changes.

@cvanelteren

Copy link
Copy Markdown
Contributor Author

Should be good now from my end @dschult @rossbar

Comment thread networkx/drawing/layout.py Resolved
@cvanelteren
cvanelteren force-pushed the forceatlas2-networkx branch from d7654db to 21a7946 Compare May 25, 2024 06:39
@cvanelteren

Copy link
Copy Markdown
Contributor Author

Looks like a merge from main and a bunch of lint tweaks cleaned up the tests. You will need to pull it down to your local machine of course before making changes.

I think I messed up a pull somewhere, think I just restored it.

@cvanelteren

Copy link
Copy Markdown
Contributor Author

@dschult @rossbar, can we go ahead with the merge -- I am a bit confused on the status of the merge after the approval.

@jarrodmillman

Copy link
Copy Markdown
Member

@mbastian Hi Mathieu! We met last year in Leiden. I believe you have forceatlas2 in Gephi. If you have any time or interest, it would be great if you or someone on your team who is familiar with this layout could take a look.

@cvanelteren

Copy link
Copy Markdown
Contributor Author

@jarrodmillman I used the official gephi code as a base for this code base in case that was any concerns.

@rossbar

rossbar commented Jun 20, 2024

Copy link
Copy Markdown
Contributor

can we go ahead with the merge -- I am a bit confused on the status of the merge after the approval.

Sorry for the confusion - NetworkX requires approvals from two maintainers before merging. For cases like this one, where there has been a lot of back-and-forth, it is useful to verify that the suggestions of the reviewers are making it all the way into the code.

Indeed it seems there are still some changes that have been "undone" by the latest pushes, so this is still not ready IMO. I will take a stab at manually resolving things to see if I can't get it over the finish line.

@rossbar

rossbar commented Jun 20, 2024

Copy link
Copy Markdown
Contributor

Actually I take that back - looking at it more closely, it looks like most of the suggestions have been reverted... I think perhaps the last attempt to "fix" a missed pull may have actually made things worse. Did you perhaps force-push from a different local branch?

@cvanelteren

Copy link
Copy Markdown
Contributor Author

Actually I take that back - looking at it more closely, it looks like most of the suggestions have been reverted... I think perhaps the last attempt to "fix" a missed pull may have actually made things worse. Did you perhaps force-push from a different local branch?

hmm could be that I rebased wrong, could you push your latest changes, then I can merge them here locally.

@rossbar

rossbar commented Jun 20, 2024

Copy link
Copy Markdown
Contributor

hmm could be that I rebased wrong, could you push your latest changes, then I can merge them here locally.

I'll hold off on making any changes - just ping me when you think you've got the PR in the state you want it!

@cvanelteren

Copy link
Copy Markdown
Contributor Author

🔔 @rossbar 🔔 I see that the doc strings were old. I fixed it. I'm fine with the layout now, and have been using it for a while locally ;-).

@rossbar

rossbar commented Jun 26, 2024 •

Copy link
Copy Markdown
Contributor

It's more than just the docstrings, many of @dschult's original comments still seem to not be addressed (though they were marked as resolved - I un-resolved them).

@cvanelteren

Copy link
Copy Markdown
Contributor Author

I think it would be easier if someone can force push the local branch that contains the latest changes that are passable, it is currently for unclear what is not correct in this PR -- I don't have access to this older commit anymore, and this thread is spanning several years now. If there is anything needed on my part that requires changing, please ping me but from my side this PR is done.

@rossbar

rossbar commented Jun 26, 2024

Copy link
Copy Markdown
Contributor

I don't have access to this older commit anymore, and this thread is spanning several years now. If there is anything needed on my part that requires changing, please ping me but from my side this PR is done.

It sounds like some work may have been lost due to force pushing - in that case, I propose to resubmit the current commits in a new PR (to prevent unintentional force-pushes) so that we can finish polishing off the remaining issues. Re: attribution, the current commit history on this branch will be preserved. Does this sound okay to you @cvanelteren ?

@cvanelteren

Copy link
Copy Markdown
Contributor Author

@rossabar, I made a new PR #7521, should we use this PR to merge the old commits?

@rossbar rossbar mentioned this pull request Jul 3, 2024
@rossbar rossbar closed this Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: Enhancements Visualization Related to graph visualization or layout

Development

Successfully merging this pull request may close these issues.

6 participants