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

Eccentricity Lifting (Graph to Complex) #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,6 @@
transform_type: 'lifting'
transform_name: "SimplicialEccentricityLifting"
complex_dim: 3
preserve_edge_attr: False
signed: True
feature_lifting: ProjectionSum
4 changes: 4 additions & 0 deletions modules/transforms/data_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
from modules.transforms.liftings.graph2simplicial.clique_lifting import (
SimplicialCliqueLifting,
)
from modules.transforms.liftings.graph2simplicial.eccentricity_lifting import (
SimplicialEccentricityLifting,
)

TRANSFORMS = {
# Graph -> Hypergraph
"HypergraphKNNLifting": HypergraphKNNLifting,
# Graph -> Simplicial Complex
"SimplicialCliqueLifting": SimplicialCliqueLifting,
"SimplicialEccentricityLifting": SimplicialEccentricityLifting,
# Graph -> Cell Complex
"CellCycleLifting": CellCycleLifting,
# Feature Liftings
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from itertools import combinations

import networkx as nx
import torch_geometric
from toponetx.classes import SimplicialComplex

from modules.transforms.liftings.graph2simplicial.base import Graph2SimplicialLifting


class SimplicialEccentricityLifting(Graph2SimplicialLifting):
r"""Lifts graphs to simplicial complex domain using eccentricity.

Parameters
----------
**kwargs : optional
Additional arguments for the class.
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)

def lift_topology(self, data: torch_geometric.data.Data) -> dict:
r"""Lifts the topology of a graph to a simplicial complex by identifying connected subgraphs as simplices.

Parameters
----------
data : torch_geometric.data.Data
The input data to be lifted.

Returns
-------
dict
The lifted topology.
"""
graph = self._generate_graph_from_data(data)
simplicial_complex = SimplicialComplex()
eccentricities = nx.eccentricity(graph)
simplices = [set() for _ in range(2, self.complex_dim + 1)]

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))

return self._get_lifted_topology(simplicial_complex, graph)
131 changes: 131 additions & 0 deletions test/transforms/liftings/graph2simplicial/test_eccentricity_lifting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""Test the message passing module."""

import torch

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


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 = SimplicialEccentricityLifting(complex_dim=3, signed=True)
self.lifting_unsigned = SimplicialEccentricityLifting(
complex_dim=3, signed=False
)

def test_lift_topology(self):
"""Test the lift_topology method."""

# Test the lift_topology method
lifted_data_signed = self.lifting_signed.forward(self.data.clone())
lifted_data_unsigned = self.lifting_unsigned.forward(self.data.clone())

expected_incidence_1_singular_values_unsigned = torch.tensor(
[3.7417, 2.4495, 2.4495, 2.4495, 2.4495, 2.4495, 2.4495, 2.4495]
)

expected_incidence_1_singular_values_signed = torch.tensor(
[
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
6.8993e-08,
]
)

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

assert torch.allclose(
expected_incidence_1_singular_values_unsigned, S_unsigned, atol=1.0e-04
), "Something is wrong with unsigned incidence_1 (nodes to edges)."
assert torch.allclose(
expected_incidence_1_singular_values_signed, S_signed, atol=1.0e-04
), "Something is wrong with signed incidence_1 (nodes to edges)."

expected_incidence_2_singular_values_unsigned = torch.tensor(
[
4.2426,
3.1623,
3.1623,
3.1623,
3.1623,
3.1623,
3.1623,
3.1623,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
2.0000,
]
)

expected_incidence_2_singular_values_signed = torch.tensor(
[
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
2.8284e00,
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,
]
)

U, S_unsigned, V = torch.svd(lifted_data_unsigned.incidence_2.to_dense())
U, S_signed, V = torch.svd(lifted_data_signed.incidence_2.to_dense())
assert torch.allclose(
expected_incidence_2_singular_values_unsigned, S_unsigned, atol=1.0e-04
), "Something is wrong with unsigned incidence_2 (edges to triangles)."
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)."
75 changes: 40 additions & 35 deletions tutorials/graph2simplicial/clique_lifting.ipynb

Large diffs are not rendered by default.

356 changes: 356 additions & 0 deletions tutorials/graph2simplicial/eccentricity_lifting.ipynb

Large diffs are not rendered by default.

Loading