You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add k-clique community algorithm (migrated from Trac #652) #640
You might put this in the same file as find_cliques, but I would say makes sense to create a file called percolate.py (perhaps in a community finding subfolder?). There are other types of percolation on cliques (or other objects), and those could be implemented in the same percolate.py file.
Note also that only cliques of size k or larger need to be found. Thus, if it's easy to change the find_cliques function to have a minimum size argument, this could speed up clique finding for the purposes of clique percolation.
In that gist code I've tried to include a doctest, but I don't really know what I'm doing. I just saw one mentioned the other day in a persentation, so I thought maybe NetworkX uses them as well---if not, then you can remove that example in the docstring.
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-11-14
In [10cf64e/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="10cf64e27d6df99760383dda89debfdafa368eb7"
Fix doctest to work with python2 and python3
Addresses #652
'''
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-11-14
In [c2e70f7/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="c2e70f7a0413e33e9e1f374437baa928383ff56d"
fix python 2/3 issue in test
Addresses #652
'''
There is a bug in this code that a friend of mine caught.
Basically, all cliques of size k should be part of some community in the end, but in the current code only the cliques that percolate are included in communities.
If you add one line you can fix this bug.
'''
...
perc_graph = nx.Graph()
perc_graph.add_nodes_from(cliques) # added line
for clique in cliques:
...
'''
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-11-24
In [4b39686/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="4b39686d7c047b0a70cca27f82ef6e5356112620"
Make sure all cliques of size k are in community.
Addresses #652
'''
Yes, the additional test looks correct and would catch the bug if it were re-introduced.
Maybe we should include a more extensive test. For example, I could run a smallish network like Zachary's Karate club through both CFinder and SCP (two implementations of clique percolation) and make sure that their results are the same. We could then use those results as the expected output of the networkx implementation.
How does that sound? If you like that idea, do you have any proposals for the test network? I guess it should be big enough that a buggy implementation is likely to do something wrong, but small enough to not bog down a long list of tests.
More tests are always helpful.
Your idea of the Zachary graph is good - we already have nx.karate_club_graph() so we just need the correct answer for various k.
Other ideas for tests are corner cases (empty graph, isolated cliques, etc.)
Can we be confident that the ground truth output is bug-free? I think so. Some researchers in my group are benchmarking a number of clique percolation techniques, and found that implementation to be bug-free, as in its output exactly matches to the output of two other clique percolation methods. (Except for the case where k=2, which corresponds to connected components--the sequential clique percolation incorrectly returns an empty set in that case).
We might want to raise an exception if the user attempts to use any value less than 2 for the value of k. We might want to raise a warning if the user selects 2 for the value of k (as this is quite a trivial--it asks which edges percolate into each other, which is equivalent to finding connected components).
Instead of an error should the k=1 case return an empty list? Or a list of one set for each node?
I think the k=1 case should work as follows: The cliques are single nodes (and all larger cliques). As usual, a pair of cliques percolate if they share k-1 (i.e., zero) nodes. Thus all cliques percolate into each other, and the algorithm should return on single community: all nodes in the graph (even if the graph is not connected).
However, this is not the behavior that will come out of the current implementation, because the find_cliques method does not return single nodes as cliques---the smallest cliques it returns are edges. In the implementation we could solve this problem by handling the k=1 case sepratately. Or we could raise an error. Is a 1-clique actually a clique? Not according to the find_cliques function.
Instead of an error should the k=1 case return an empty list? Or a list of one set for each node?
I think the k=1 case should work as follows: The cliques are single nodes (and all larger cliques). As usual, a pair of cliques percolate if they share k-1 (i.e., zero) nodes. Thus all cliques percolate into each other, and the algorithm should return on single community: all nodes in the graph (even if the graph is not connected).
However, this is not the behavior that will come out of the current implementation, because the find_cliques method does not return single nodes as cliques---the smallest cliques it returns are edges. In the implementation we could solve this problem by handling the k=1 case sepratately. Or we could raise an error. Is a 1-clique actually a clique? Not according to the find_cliques function.
I think you are right - a clique should have at least one edge...
I suggest we force k>=2 and raise an exception if not.
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-12-08
In [4331cbc/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="4331cbc4eacf11d62b2ccf38efdfbec6edd65895"
Raise error if k < 2.
Addresses #652
'''
Activity
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @conradlee, 2011-11-06
Oops, somehow this comment got duplicated--see comment below.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by conradlee, 2011-11-06
You might put this in the same file as find_cliques, but I would say makes sense to create a file called percolate.py (perhaps in a community finding subfolder?). There are other types of percolation on cliques (or other objects), and those could be implemented in the same percolate.py file.
Note also that only cliques of size k or larger need to be found. Thus, if it's easy to change the find_cliques function to have a minimum size argument, this could speed up clique finding for the purposes of clique percolation.
In that gist code I've tried to include a doctest, but I don't really know what I'm doing. I just saw one mentioned the other day in a persentation, so I thought maybe NetworkX uses them as well---if not, then you can remove that example in the docstring.
networkx-trac commentedon Jul 20, 2012
Attachment in Trac by @hagberg, 2011-11-06: kclique.patch
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-11-06
Here is a patch to include that code with some light changes - naming, header, doctest, documentation, nose test.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-11-14
Added in [1978/networkx]
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-11-14
In [7143955/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="7143955d6d2b6535b4a84d05c6902ac884065047"
Add init for community.
Addresses #652
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-11-14
In [10cf64e/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="10cf64e27d6df99760383dda89debfdafa368eb7"
Fix doctest to work with python2 and python3
Addresses #652
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-11-14
In [c2e70f7/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="c2e70f7a0413e33e9e1f374437baa928383ff56d"
fix python 2/3 issue in test
Addresses #652
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-11-20
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @conradlee, 2011-11-23
There is a bug in this code that a friend of mine caught.
Basically, all cliques of size k should be part of some community in the end, but in the current code only the cliques that percolate are included in communities.
If you add one line you can fix this bug.
'''
...
perc_graph = nx.Graph()
perc_graph.add_nodes_from(cliques) # added line
for clique in cliques:
...
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-11-24
In [4b39686/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="4b39686d7c047b0a70cca27f82ef6e5356112620"
Make sure all cliques of size k are in community.
Addresses #652
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-11-24
I added that fix and updated the tests.
Can you look to make sure the tests are correct?
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @conradlee, 2011-11-24
Yes, the additional test looks correct and would catch the bug if it were re-introduced.
Maybe we should include a more extensive test. For example, I could run a smallish network like Zachary's Karate club through both CFinder and SCP (two implementations of clique percolation) and make sure that their results are the same. We could then use those results as the expected output of the networkx implementation.
How does that sound? If you like that idea, do you have any proposals for the test network? I guess it should be big enough that a buggy implementation is likely to do something wrong, but small enough to not bog down a long list of tests.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-11-24
More tests are always helpful.
Your idea of the Zachary graph is good - we already have nx.karate_club_graph() so we just need the correct answer for various k.
Other ideas for tests are corner cases (empty graph, isolated cliques, etc.)
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @conradlee, 2011-12-07
I've added the Zachary's karate club test: https://gist.github.com/1443573
The expected output is based on the sequential clique percolation code located here: http://www.lce.hut.fi/~mtkivela/kclique.html
Can we be confident that the ground truth output is bug-free? I think so. Some researchers in my group are benchmarking a number of clique percolation techniques, and found that implementation to be bug-free, as in its output exactly matches to the output of two other clique percolation methods. (Except for the case where k=2, which corresponds to connected components--the sequential clique percolation incorrectly returns an empty set in that case).
We might want to raise an exception if the user attempts to use any value less than 2 for the value of k. We might want to raise a warning if the user selects 2 for the value of k (as this is quite a trivial--it asks which edges percolate into each other, which is equivalent to finding connected components).
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-12-07
In [c9188c7/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="c9188c78da82cbe49e8f47871194597c48b81328"
Add Zachary test.
Addresses #652
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-12-07
Thanks for the test; I added it.
Instead of an error should the k=1 case return an empty list? Or a list of one set for each node?
I think the k=2 case is OK as is (doesn't need a warning since it produces a correct but maybe not very interesting answer).
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @conradlee, 2011-12-07
I think the k=1 case should work as follows: The cliques are single nodes (and all larger cliques). As usual, a pair of cliques percolate if they share k-1 (i.e., zero) nodes. Thus all cliques percolate into each other, and the algorithm should return on single community: all nodes in the graph (even if the graph is not connected).
However, this is not the behavior that will come out of the current implementation, because the find_cliques method does not return single nodes as cliques---the smallest cliques it returns are edges. In the implementation we could solve this problem by handling the k=1 case sepratately. Or we could raise an error. Is a 1-clique actually a clique? Not according to the find_cliques function.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2011-12-07
Replying to [comment:17 conradlee]:
I think you are right - a clique should have at least one edge...
I suggest we force k>=2 and raise an exception if not.
networkx-trac commentedon Jul 20, 2012
Comment in Trac by Aric Hagberg <aric.hagberg, 2011-12-08
In [4331cbc/networkx]:
'''
#!CommitTicketReference repository="networkx" revision="4331cbc4eacf11d62b2ccf38efdfbec6edd65895"
Raise error if k < 2.
Addresses #652
'''
networkx-trac commentedon Jul 20, 2012
Comment in Trac by @hagberg, 2012-04-08