|
| 1 | +from itertools import combinations |
| 2 | + |
| 3 | +import gudhi |
| 4 | +import gudhi.simplex_tree |
| 5 | +import networkx as nx |
| 6 | +import numpy as np |
| 7 | +import torch |
| 8 | +from toponetx.classes import SimplicialComplex |
| 9 | +from torch_geometric.data import Data |
| 10 | + |
| 11 | +from modules.data.utils.utils import get_complex_connectivity |
| 12 | +from modules.transforms.liftings.pointcloud2simplicial.base import ( |
| 13 | + PointCloud2SimplicialLifting, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class RandomFlagComplexLifting(PointCloud2SimplicialLifting): |
| 18 | + """ Lifting of pointclouds to simplicial complexes using the Random Flag Complex construction. |
| 19 | + """ |
| 20 | + def __init__(self, steps, alpha: float | None = None, p: float | None = None, **kwargs): |
| 21 | + self.alpha = alpha |
| 22 | + self.steps = steps |
| 23 | + self.p = p |
| 24 | + super().__init__(**kwargs) |
| 25 | + |
| 26 | + def lift_topology(self, data: Data) -> dict: |
| 27 | + # Get the number of points and generate an empty graph |
| 28 | + n = data["x"].size(0) |
| 29 | + if self.p is None: |
| 30 | + self.p = np.power(n, -self.alpha) |
| 31 | + self.p = float(self.p) |
| 32 | + |
| 33 | + adj_mat = np.zeros((n, n)) |
| 34 | + indices = np.tril_indices(n) |
| 35 | + |
| 36 | + st = gudhi.SimplexTree() |
| 37 | + |
| 38 | + generator = np.random.default_rng() |
| 39 | + # For each step, sample from random binomial distribution |
| 40 | + # for each edge appearign |
| 41 | + for _ in range(self.steps): |
| 42 | + number_of_edges = n*(n+1)//2 |
| 43 | + prob = generator.binomial(1, self.p, size=number_of_edges) |
| 44 | + print(prob) |
| 45 | + tmp_mat = np.zeros((n, n)) |
| 46 | + tmp_mat[indices] = prob |
| 47 | + np.logical_or(adj_mat, tmp_mat, out=adj_mat) |
| 48 | + np.fill_diagonal(adj_mat, 0) |
| 49 | + |
| 50 | + # Insert all vertices |
| 51 | + for i in range(n): |
| 52 | + st.insert([i]) |
| 53 | + |
| 54 | + graph: nx.Graph = nx.from_numpy_matrix(adj_mat).to_undirected() |
| 55 | + |
| 56 | + # Insert all edges |
| 57 | + for v, u in graph.edges: |
| 58 | + st.insert([v, u]) |
| 59 | + |
| 60 | + simplicial_complex = SimplicialComplex(graph) |
| 61 | + |
| 62 | + # Add features to the vertices |
| 63 | + feats = { |
| 64 | + i: f |
| 65 | + for i, f in enumerate(data["x"]) |
| 66 | + } |
| 67 | + |
| 68 | + simplicial_complex.set_simplex_attributes(feats, name="features") |
| 69 | + |
| 70 | + # Find the cliques up to the maximum dimension specified |
| 71 | + cliques = nx.find_cliques(graph) |
| 72 | + simplices = [set() for _ in range(2, self.complex_dim + 1)] |
| 73 | + |
| 74 | + for clique in cliques: |
| 75 | + for i in range(2, self.complex_dim + 1): |
| 76 | + for c in combinations(clique, i + 1): |
| 77 | + simplices[i - 2].add(tuple(c)) |
| 78 | + |
| 79 | + |
| 80 | + # Add the k-tuples as simplices |
| 81 | + for set_k_simplices in simplices: |
| 82 | + for k_simplex in set_k_simplices: |
| 83 | + st.insert(k_simplex) |
| 84 | + simplicial_complex.add_simplices_from(list(set_k_simplices)) |
| 85 | + |
| 86 | + return self._get_lifted_topology(simplicial_complex, st) |
| 87 | + |
| 88 | + def _get_lifted_topology(self, simplicial_complex: SimplicialComplex, st: gudhi.SimplexTree) -> dict: |
| 89 | + |
| 90 | + # Connectivity of the complex |
| 91 | + lifted_topology = get_complex_connectivity( |
| 92 | + simplicial_complex, self.complex_dim, signed=False |
| 93 | + ) |
| 94 | + # Computing the persitence to obtain the Betti numbers |
| 95 | + st.compute_persistence(persistence_dim_max=True) |
| 96 | + |
| 97 | + # Save the Betti numbers in the Data object |
| 98 | + lifted_topology["betti"] = torch.tensor(st.betti_numbers()) |
| 99 | + |
| 100 | + |
| 101 | + lifted_topology["x_0"] = torch.stack( |
| 102 | + list(simplicial_complex.get_simplex_attributes("features", 0).values()) |
| 103 | + ) |
| 104 | + |
| 105 | + # Add the indices of the simplices |
| 106 | + for r in range(simplicial_complex.dim): |
| 107 | + lifted_topology[f"x_idx_{r}"] = torch.tensor(simplicial_complex.skeleton(r)) |
| 108 | + |
| 109 | + return lifted_topology |
0 commit comments