Skip to content

DnD Lifting (Graph to Simplicial Complex) #11

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 6 commits into from
Feb 20, 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
6 changes: 6 additions & 0 deletions configs/transforms/liftings/graph2simplicial/dnd_lifting.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
transform_type: 'lifting'
transform_name: "SimplicialDnDLifting"
complex_dim: 6
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.dnd_lifting import (
SimplicialDnDLifting,
)

TRANSFORMS = {
# Graph -> Hypergraph
"HypergraphKNNLifting": HypergraphKNNLifting,
# Graph -> Simplicial Complex
"SimplicialCliqueLifting": SimplicialCliqueLifting,
"SimplicialDnDLifting": SimplicialDnDLifting,
# Graph -> Cell Complex
"CellCycleLifting": CellCycleLifting,
# Feature Liftings
Expand Down
102 changes: 102 additions & 0 deletions modules/transforms/liftings/graph2simplicial/dnd_lifting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import random
from itertools import combinations

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

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


class SimplicialDnDLifting(Graph2SimplicialLifting):
r"""Lifts graphs to simplicial complex domain using a Dungeons & Dragons inspired system.

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

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

def lift_topology(self, data: Data) -> dict:
r"""Lifts the topology of a graph to a simplicial complex using Dungeons & Dragons (D&D) inspired mechanics.

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

Returns
-------
dict
The lifted topology.
"""
graph = self._generate_graph_from_data(data)
simplicial_complex = SimplicialComplex()

characters = self._assign_attributes(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 in graph.nodes:
character = characters[node]
for k in range(1, self.complex_dim):
dice_roll = self._roll_dice(character, k)
neighborhood = list(
nx.single_source_shortest_path_length(
graph, node, cutoff=dice_roll
).keys()
)
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)

def _assign_attributes(self, graph):
"""Assign D&D-inspired attributes based on node properties."""
degrees = nx.degree_centrality(graph)
clustering = nx.clustering(graph)
closeness = nx.closeness_centrality(graph)
eigenvector = nx.eigenvector_centrality(graph)
betweenness = nx.betweenness_centrality(graph)
pagerank = nx.pagerank(graph)

attributes = {}
for node in graph.nodes:
attributes[node] = {
"Degree": degrees[node],
"Clustering": clustering[node],
"Closeness": closeness[node],
"Eigenvector": eigenvector[node],
"Betweenness": betweenness[node],
"Pagerank": pagerank[node],
}
return attributes

def _roll_dice(self, attributes, k):
"""Simulate a D20 dice roll influenced by node attributes where a different attribute is used based on the simplex level."""

attribute = None
if k == 1:
attribute = attributes["Degree"]
elif k == 2:
attribute = attributes["Clustering"]
elif k == 3:
attribute = attributes["Closeness"]
elif k == 4:
attribute = attributes["Eigenvector"]
elif k == 5:
attribute = attributes["Betweenness"]
else:
attribute = attributes["Pagerank"]

base_roll = random.randint(1, 20)
modifier = int(attribute * 20)
return base_roll + modifier
Loading
Loading