Skip to content

Eccentricity Lifting (Graph to Complex) #10

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

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
transform_type: 'lifting'
transform_name: "SimplicialGraphInducedLifting"
transform_name: "SimplicialEccentricityLifting"
complex_dim: 3
preserve_edge_attr: False
signed: True
Expand Down
6 changes: 3 additions & 3 deletions modules/transforms/data_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from modules.transforms.liftings.graph2simplicial.clique_lifting import (
SimplicialCliqueLifting,
)
from modules.transforms.liftings.graph2simplicial.graph_induced_lifting import (
SimplicialGraphInducedLifting,
from modules.transforms.liftings.graph2simplicial.eccentricity_lifting import (
SimplicialEccentricityLifting,
)
from modules.transforms.liftings.graph2simplicial.line_lifting import (
SimplicialLineLifting,
Expand All @@ -45,9 +45,9 @@
"ExpanderGraphLifting": ExpanderGraphLifting,
# Graph -> Simplicial Complex
"SimplicialCliqueLifting": SimplicialCliqueLifting,
"SimplicialEccentricityLifting": SimplicialEccentricityLifting,
"SimplicialLineLifting": SimplicialLineLifting,
"SimplicialVietorisRipsLifting": SimplicialVietorisRipsLifting,
"SimplicialGraphInducedLifting": SimplicialGraphInducedLifting,
# Graph -> Cell Complex
"CellCycleLifting": CellCycleLifting,
# Point Cloud -> Simplicial Complex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
)


class SimplicialGraphInducedLifting(Graph2SimplicialLifting):
r"""Lifts graphs to simplicial complex domain by identifying connected subgraphs as simplices.
class SimplicialEccentricityLifting(Graph2SimplicialLifting):
r"""Lifts graphs to simplicial complex domain using eccentricity.

Parameters
----------
Expand All @@ -35,15 +35,20 @@ def lift_topology(self, data: torch_geometric.data.Data) -> dict:
The lifted topology.
"""
graph = self._generate_graph_from_data(data)
simplicial_complex = SimplicialComplex(graph)
all_nodes = list(graph.nodes)
simplicial_complex = SimplicialComplex()
eccentricities = nx.eccentricity(graph)
simplices = [set() for _ in range(2, self.complex_dim + 1)]

for k in range(2, self.complex_dim + 1):
for combination in combinations(all_nodes, k + 1):
subgraph = graph.subgraph(combination)
if nx.is_connected(subgraph):
simplices[k - 2].add(tuple(sorted(combination)))
for node in graph.nodes:
simplicial_complex.add_node(node, features=data.x[node])

for node, ecc in eccentricities.items():
neighborhood = list(
nx.single_source_shortest_path_length(graph, node, cutoff=ecc).keys()
)
for k in range(1, self.complex_dim):
for combination in combinations(neighborhood, k + 1):
simplices[k - 1].add(tuple(sorted(combination)))

for set_k_simplices in simplices:
simplicial_complex.add_simplices_from(list(set_k_simplices))
Expand Down
16 changes: 16 additions & 0 deletions pr_tutorial.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# <fork_name> - is a name of the person who created the PR
# <PR_name> - is a name of the PR
# Example: Jonas-Verhellen:eccentricity-lifting -->
# --> <fork_name> = jonas-verhellen <PR_name> = eccentricity-lifting

# First it is required to fetch the PR
git fetch <fork_name> <PR_name>

# Check that the PR is fetched
git branch -r

# Checkout to the PR
git checkout <fork_name>/<PR_name>

# Push to the PR
git push <fork_name> <PR_name>
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import torch

from modules.data.utils.utils import load_manual_graph
from modules.transforms.liftings.graph2simplicial.graph_induced_lifting import (
SimplicialGraphInducedLifting,
from modules.transforms.liftings.graph2simplicial.eccentricity_lifting import (
SimplicialEccentricityLifting,
)


class TestSimplicialCliqueLifting:
"""Test the SimplicialCliqueLifting class."""
class TestSimplicialEccentricityLifting:
"""Test the SimplicialEccentricityLifting class."""

def setup_method(self):
# Load the graph
self.data = load_manual_graph()

# Initialise the SimplicialCliqueLifting class
self.lifting_signed = SimplicialGraphInducedLifting(complex_dim=3, signed=True)
self.lifting_unsigned = SimplicialGraphInducedLifting(
self.lifting_signed = SimplicialEccentricityLifting(complex_dim=3, signed=True)
self.lifting_unsigned = SimplicialEccentricityLifting(
complex_dim=3, signed=False
)

Expand Down Expand Up @@ -57,14 +57,19 @@ def test_lift_topology(self):

expected_incidence_2_singular_values_unsigned = torch.tensor(
[
4.1190,
4.2426,
3.1623,
3.1623,
3.1623,
3.0961,
3.0000,
3.0000,
2.7564,
3.1623,
3.1623,
3.1623,
3.1623,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
Expand All @@ -80,11 +85,6 @@ def test_lift_topology(self):
2.0000,
2.0000,
2.0000,
1.7321,
1.6350,
1.4142,
1.4142,
1.0849,
]
)

Expand All @@ -106,18 +106,18 @@ def test_lift_topology(self):
2.8284e00,
2.8284e00,
2.8284e00,
2.6458e00,
2.6458e00,
2.2361e00,
1.7321e00,
1.7321e00,
9.3758e-07,
4.7145e-07,
4.3417e-07,
4.0241e-07,
3.1333e-07,
2.2512e-07,
1.9160e-07,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
7.0866e-07,
4.0955e-07,
3.2154e-07,
2.9976e-07,
2.8069e-07,
2.3097e-07,
9.4821e-08,
]
)

Expand All @@ -129,91 +129,3 @@ def test_lift_topology(self):
assert torch.allclose(
expected_incidence_2_singular_values_signed, S_signed, atol=1.0e-04
), "Something is wrong with signed incidence_2 (edges to triangles)."

expected_incidence_3_singular_values_unsigned = torch.tensor(
[
3.8466,
3.1379,
3.0614,
2.8749,
2.8392,
2.8125,
2.5726,
2.3709,
2.2858,
2.2369,
2.1823,
2.0724,
2.0000,
2.0000,
2.0000,
1.8937,
1.7814,
1.7321,
1.7256,
1.5469,
1.5340,
1.4834,
1.4519,
1.4359,
1.4142,
1.0525,
1.0000,
1.0000,
1.0000,
1.0000,
0.9837,
0.9462,
0.8853,
0.7850,
]
)

expected_incidence_3_singular_values_signed = torch.tensor(
[
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.6933e00,
2.6458e00,
2.6458e00,
2.6280e00,
2.4495e00,
2.3040e00,
1.9475e00,
1.7321e00,
1.7321e00,
1.7321e00,
1.4823e00,
1.0000e00,
1.0000e00,
1.0000e00,
1.0000e00,
1.0000e00,
1.0000e00,
1.0000e00,
1.0000e00,
1.0000e00,
7.3584e-01,
2.7959e-07,
2.1776e-07,
1.4498e-07,
5.5373e-08,
]
)

U, S_unsigned, V = torch.svd(lifted_data_unsigned.incidence_3.to_dense())
U, S_signed, V = torch.svd(lifted_data_signed.incidence_3.to_dense())

assert torch.allclose(
expected_incidence_3_singular_values_unsigned, S_unsigned, atol=1.0e-04
), "Something is wrong with unsigned incidence_3 (triangles to tetrahedrons)."
assert torch.allclose(
expected_incidence_3_singular_values_signed, S_signed, atol=1.0e-04
), "Something is wrong with signed incidence_3 (triangles to tetrahedrons)."
Loading