-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clustering coefficient definitions (#316)
* moved modules * add clustering code * added clustering functions * add tests * fixed tests * Update xgi/algorithms/clustering.py Co-authored-by: Maxime Lucas <[email protected]> * updated centrality names * added more info on clustering * updated all names * fixed name * update docstrings * added tests * Update clustering.py * changed NaN to nan https://stackoverflow.com/questions/53436339/difference-between-np-nan-and-np-nan * response to review * change to isnan * fixed more tests --------- Co-authored-by: Maxime Lucas <[email protected]> Co-authored-by: Leo Torres <[email protected]>
- Loading branch information
1 parent
ca4fb51
commit 7ee8aa2
Showing
19 changed files
with
671 additions
and
114 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
xgi.algorithms.clustering | ||
========================= | ||
|
||
.. currentmodule:: xgi.algorithms.clustering | ||
|
||
.. automodule:: xgi.algorithms.clustering | ||
|
||
.. rubric:: Functions | ||
|
||
.. autofunction:: clustering_coefficient | ||
.. autofunction:: local_clustering_coefficient | ||
.. autofunction:: two_node_clustering_coefficient |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,6 @@ linalg package | |
.. rubric:: Modules | ||
|
||
.. autosummary:: | ||
:toctree: classes | ||
:toctree: linalg | ||
|
||
~xgi.linalg.matrix |
File renamed without changes.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,6 @@ utils package | |
.. rubric:: Modules | ||
|
||
.. autosummary:: | ||
:toctree: classes | ||
:toctree: utils | ||
|
||
~xgi.utils.utilities |
File renamed without changes.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import xgi | ||
from xgi.exception import XGIError | ||
|
||
|
||
def test_local_clustering_coefficient(edgelist8): | ||
H = xgi.random_hypergraph(3, [1]) | ||
|
||
cc = xgi.local_clustering_coefficient(H) | ||
true_cc = {0: 1.0, 1: 1.0, 2: 1.0} | ||
assert cc == true_cc | ||
|
||
H = xgi.random_hypergraph(3, [1, 1]) | ||
cc = xgi.local_clustering_coefficient(H) | ||
true_cc = {0: 1.0, 1: 1.0, 2: 1.0} | ||
assert cc == true_cc | ||
|
||
H = xgi.random_hypergraph(3, [0, 1]) | ||
cc = xgi.local_clustering_coefficient(H) | ||
true_cc = {0: 0.0, 1: 0.0, 2: 0.0} | ||
assert cc == true_cc | ||
|
||
H = xgi.Hypergraph() | ||
cc = xgi.local_clustering_coefficient(H) | ||
assert cc == {} | ||
|
||
H = xgi.Hypergraph() | ||
H.add_nodes_from(range(3)) | ||
cc = xgi.local_clustering_coefficient(H) | ||
assert cc == {0: 0, 1: 0, 2: 0} | ||
|
||
H = xgi.Hypergraph(edgelist8) | ||
cc = xgi.local_clustering_coefficient(H) | ||
true_cc = { | ||
0: 0.6777777777777778, | ||
1: 0.575, | ||
2: 0.3333333333333333, | ||
3: 0.3333333333333333, | ||
4: 0.6666666666666666, | ||
5: 0.0, | ||
6: 0.0, | ||
} | ||
assert cc == true_cc | ||
|
||
|
||
def test_clustering_coefficient(edgelist1): | ||
H = xgi.random_hypergraph(3, [1]) | ||
|
||
cc = xgi.clustering_coefficient(H) | ||
true_cc = {0: 1.0, 1: 1.0, 2: 1.0} | ||
assert cc == true_cc | ||
|
||
H = xgi.random_hypergraph(3, [1, 1]) | ||
cc = xgi.clustering_coefficient(H) | ||
true_cc = {0: 1.0, 1: 1.0, 2: 1.0} | ||
assert cc == true_cc | ||
|
||
H = xgi.random_hypergraph(3, [0, 1]) | ||
cc = xgi.clustering_coefficient(H) | ||
true_cc = {0: 1.0, 1: 1.0, 2: 1.0} | ||
assert cc == true_cc | ||
|
||
H = xgi.Hypergraph() | ||
cc = xgi.clustering_coefficient(H) | ||
assert cc == {} | ||
|
||
H = xgi.Hypergraph() | ||
H.add_nodes_from(range(3)) | ||
cc = xgi.clustering_coefficient(H) | ||
assert {0: 0, 1: 0, 2: 0} | ||
|
||
H = xgi.Hypergraph(edgelist1) | ||
cc = xgi.clustering_coefficient(H) | ||
true_cc = {1: 1.0, 2: 1.0, 3: 1.0, 4: 0, 5: 0, 6: 1 / 3, 8: 1.0, 7: 1.0} | ||
assert cc == true_cc | ||
|
||
|
||
def test_two_node_clustering_coefficient(edgelist1, edgelist8): | ||
H = xgi.random_hypergraph(3, [1]) | ||
|
||
cc = xgi.two_node_clustering_coefficient(H) | ||
true_cc = {0: 1 / 3, 1: 1 / 3, 2: 1 / 3} | ||
assert cc == true_cc | ||
|
||
# check default keyword | ||
cc1 = xgi.two_node_clustering_coefficient(H, kind="union") | ||
assert cc == cc1 | ||
|
||
H = xgi.random_hypergraph(3, [1, 1]) | ||
cc = xgi.two_node_clustering_coefficient(H) | ||
true_cc = {0: 0.5, 1: 0.5, 2: 0.5} | ||
assert cc == true_cc | ||
|
||
H = xgi.Hypergraph(edgelist1) | ||
cc1 = xgi.two_node_clustering_coefficient(H, kind="union") | ||
cc2 = xgi.two_node_clustering_coefficient(H, kind="min") | ||
cc3 = xgi.two_node_clustering_coefficient(H, kind="max") | ||
|
||
true_cc1 = {1: 1.0, 2: 1.0, 3: 1.0, 4: 0, 5: 0.5, 6: 0.5, 8: 0.75, 7: 0.75} | ||
true_cc2 = {1: 1.0, 2: 1.0, 3: 1.0, 4: 0, 5: 1.0, 6: 1.0, 8: 1.0, 7: 1.0} | ||
true_cc3 = {1: 1.0, 2: 1.0, 3: 1.0, 4: 0, 5: 0.5, 6: 0.5, 8: 0.75, 7: 0.75} | ||
|
||
assert cc1 == true_cc1 | ||
assert cc2 == true_cc2 | ||
assert cc3 == true_cc3 | ||
|
||
with pytest.raises(XGIError): | ||
xgi.two_node_clustering_coefficient(H, kind="test") | ||
|
||
H = xgi.Hypergraph(edgelist8) | ||
H.add_node(10) | ||
cc = xgi.two_node_clustering_coefficient(H, kind="min") | ||
true_cc = { | ||
0: 0.6533333333333333, | ||
1: 0.4888888888888888, | ||
2: 0.5833333333333333, | ||
3: 0.5833333333333333, | ||
4: 0.5666666666666667, | ||
5: 0.5, | ||
6: 0.5, | ||
10: 0, | ||
} | ||
assert cc == true_cc |
This file contains 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
Oops, something went wrong.