Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tensor centralities #600

Merged
merged 27 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,24 @@ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


---------------------------------

License

Tensor methods for nonuniform hypergraphs

* Tensor methods functionality for the CompleX Group Interactions library

Copyright 2023, 2024 Battelle Memorial Institute

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5 changes: 5 additions & 0 deletions docs/source/using-xgi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Published work
2024
----

Sinan G. Aksoy, Ilya Amburg, and Stephen J. Young, "Scalable Tensor Methods for Nonuniform Hypergraphs", *SIAM Journal on Mathematics of Data Science*, Vol. 6, Iss. 2, 481-503 (2024).

:bdg-link-primary-line:`Paper <https://doi.org/10.1137/23M1584472>`
:bdg-link-primary-line:`Code <https://github.com/pnnl/GENTTSV>`

Gonzalo Contreras-Aso, Regino Criado, and Miguel Romance, "Beyond directed hypergraphs: heterogeneous hypergraphs and spectral centralities", *Journal of Complex Networks*, Volume 12, Issue 4, cnae037 (2024).

:bdg-link-primary-line:`Paper <https://doi.org/10.1093/comnet/cnae037>`
Expand Down
160 changes: 126 additions & 34 deletions tests/algorithms/test_centrality.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def test_clique_eigenvector_centrality():
H = xgi.sunflower(3, 1, 3)
c = H.nodes.clique_eigenvector_centrality.asnumpy()
assert norm(c[1:] - c[1]) < 1e-4
assert abs(c[0] / c[1] - ratio(3, 3, kind="CEC")) < 1e-4
assert abs(c[0] / c[1] - _ratio(3, 3, kind="CEC")) < 1e-4

H = xgi.sunflower(5, 1, 7)
c = H.nodes.clique_eigenvector_centrality.asnumpy()
assert norm(c[1:] - c[1]) < 1e-4
assert abs(c[0] / c[1] - ratio(5, 7, kind="CEC")) < 1e-4
assert abs(c[0] / c[1] - _ratio(5, 7, kind="CEC")) < 1e-4


@pytest.mark.slow
Expand All @@ -59,12 +59,12 @@ def test_h_eigenvector_centrality():
H = xgi.sunflower(3, 1, 5)
c = H.nodes.h_eigenvector_centrality(max_iter=1000).asnumpy()
assert norm(c[1:] - c[1]) < 1e-4
assert abs(c[0] / c[1] - ratio(3, 5, kind="HEC")) < 1e-4
assert abs(c[0] / c[1] - _ratio(3, 5, kind="HEC")) < 1e-4

H = xgi.sunflower(5, 1, 7)
c = H.nodes.h_eigenvector_centrality(max_iter=1000).asnumpy()
assert norm(c[1:] - c[1]) < 1e-4
assert abs(c[0] / c[1] - ratio(5, 7, kind="HEC")) < 1e-4
assert abs(c[0] / c[1] - _ratio(5, 7, kind="HEC")) < 1e-4

with pytest.raises(XGIError):
H = xgi.Hypergraph([[1, 2], [2, 3, 4]])
Expand Down Expand Up @@ -128,36 +128,6 @@ def test_line_vector_centrality():
xgi.line_vector_centrality(H)


def ratio(r, m, kind="CEC"):
"""Generate the ratio between largest and second largest centralities
for the sunflower hypergraph with one core node.

Parameters
----------
r : int
Number of petals
m : int
Size of edges
kind : str, default: "CEC"
"CEC" or "HEC"

Returns
-------
float
Ratio

References
----------
Three Hypergraph Eigenvector Centralities,
Austin R. Benson,
https://doi.org/10.1137/18M1203031
"""
if kind == "CEC":
return 2 * r * (m - 1) / (np.sqrt(m**2 + 4 * (m - 1) * (r - 1)) + m - 2)
elif kind == "HEC":
return r ** (1.0 / m)


def test_katz_centrality(edgelist1, edgelist8):
# test hypergraph with no edge
H = xgi.Hypergraph()
Expand Down Expand Up @@ -195,3 +165,125 @@ def test_katz_centrality(edgelist1, edgelist8):
}
for n in c:
assert np.allclose(c[n], expected_c[n])


@pytest.mark.slow
def test_h_eigenvector_tensor_centrality():
# test empty hypergraph
H = xgi.Hypergraph()
c = xgi.h_eigenvector_tensor_centrality(H)
assert c == dict()

# Test no edges
H.add_nodes_from([0, 1, 2])
hec = xgi.h_eigenvector_tensor_centrality(H)
for i in hec:
assert np.isnan(hec[i])

# test disconnected
H.add_edge([0, 1])
hec = xgi.h_eigenvector_tensor_centrality(H)
assert set(hec) == {0, 1, 2}
for i in hec:
assert np.isnan(hec[i])

H = xgi.sunflower(3, 1, 5)
c = xgi.h_eigenvector_tensor_centrality(H, max_iter=1000)
assert (
max([abs(c[0] / c[i + 1] - _ratio(3, 5, kind="HEC")) for i in range(12)]) < 1e-4
)

H = xgi.sunflower(5, 1, 7)
print(H.num_nodes)
c = xgi.h_eigenvector_tensor_centrality(H, max_iter=1000)
assert (
max([abs(c[0] / c[i + 1] - _ratio(5, 7, kind="HEC")) for i in range(29)]) < 1e-4
)

H = xgi.Hypergraph([[1, 2], [2, 3, 4]])
c = xgi.h_eigenvector_tensor_centrality(H)
true_c = {
1: 0.24458437592396465,
2: 0.3014043407819482,
3: 0.22700561916516002,
4: 0.22700566412892714,
}
for i in c:
assert np.allclose(c[i], true_c[i])


@pytest.mark.slow
def test_z_eigenvector_tensor_centrality():
# test empty hypergraph
H = xgi.Hypergraph()
c = xgi.z_eigenvector_tensor_centrality(H)
assert c == dict()

# Test no edges
H.add_nodes_from([0, 1, 2])
hec = xgi.z_eigenvector_tensor_centrality(H)
for i in hec:
assert np.isnan(hec[i])

# test disconnected
H.add_edge([0, 1])
hec = xgi.z_eigenvector_tensor_centrality(H)
assert set(hec) == {0, 1, 2}
for i in hec:
assert np.isnan(hec[i])

H = xgi.sunflower(3, 1, 5)
c = xgi.z_eigenvector_tensor_centrality(H, max_iter=1000)
assert (
max([abs(c[0] / c[i + 1] - _ratio(3, 5, kind="ZEC")) for i in range(12)]) < 1e-4
)

H = xgi.sunflower(5, 1, 7)
print(H.num_nodes)
c = xgi.z_eigenvector_tensor_centrality(H, max_iter=1000)
assert (
max([abs(c[0] / c[i + 1] - _ratio(5, 7, kind="ZEC")) for i in range(29)]) < 1e-4
)

H = xgi.Hypergraph([[1, 2], [2, 3, 4]])
c = xgi.z_eigenvector_tensor_centrality(H, max_iter=10000)
true_c = {
1: 0.45497398635982933,
2: 0.45900452108663403,
3: 0.04301074627676834,
4: 0.04301074627676829,
}
for i in c:
assert np.allclose(c[i], true_c[i])


def _ratio(r, m, kind="CEC"):
nwlandry marked this conversation as resolved.
Show resolved Hide resolved
"""Generate the _ratio between largest and second largest centralities
for the sunflower hypergraph with one core node.

Parameters
----------
r : int
Number of petals
m : int
Size of edges
kind : str, default: "CEC"
"CEC" or "HEC"

Returns
-------
float
Ratio

References
----------
Three Hypergraph Eigenvector Centralities,
Austin R. Benson,
https://doi.org/10.1137/18M1203031
"""
if kind == "CEC":
return 2 * r * (m - 1) / (np.sqrt(m**2 + 4 * (m - 1) * (r - 1)) + m - 2)
elif kind == "HEC":
return r ** (1.0 / m)
elif kind == "ZEC":
return r**0.5
Loading
Loading