Skip to content

Commit

Permalink
removed randomness from modularity_maximization test
Browse files Browse the repository at this point in the history
  • Loading branch information
Coerulatus committed Mar 7, 2025
1 parent 0d27c30 commit ee8e160
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_lift_topology(self):
torch.manual_seed(42)

# Test the lift_topology method
lifted_data = self.lifting.lift_topology(self.data.clone())
lifted_data = self.lifting.lift_topology(self.data.clone(), permute_x=False)

expected_n_hyperedges = self.data.num_nodes

Expand All @@ -110,8 +110,8 @@ def test_lift_topology(self):
[0., 1., 1., 1., 0., 0., 0., 0.],
[0., 0., 1., 1., 1., 0., 0., 0.],
[0., 0., 0., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1., 1., 0., 1.]
[0., 0., 0., 0., 1., 1., 1., 0.],
[0., 0., 0., 0., 0., 1., 1., 1.]
]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def modularity_matrix(self, data):
m = data.edge_index.size(1) / 2
return a - torch.outer(k, k) / (2 * m)

def kmeans(self, x, n_clusters, n_iterations=100):
def kmeans(self, x, n_clusters, n_iterations=100, permute_x=True):
r"""Perform k-means clustering on the input data.
Note: This implementation uses random initialization, so results may vary
Expand All @@ -64,6 +64,8 @@ def kmeans(self, x, n_clusters, n_iterations=100):
The number of clusters to form.
n_iterations : int, optional
The maximum number of iterations. Default is 100.
permute_x : bool, optional
Whether to permute the input data. Default is True.
Returns
-------
Expand All @@ -76,7 +78,14 @@ def kmeans(self, x, n_clusters, n_iterations=100):
may differ each time the code is run, even with the same input.
"""
# Initialize cluster centers randomly
centroids = x[torch.randperm(x.shape[0])[:n_clusters]]
if permute_x:
centroids = x[
torch.randperm(
x.shape[0],
)[:n_clusters]
]
else:
centroids = x[:n_clusters]
cluster_assignments = torch.zeros(x.shape[0], dtype=torch.long)
for _ in range(n_iterations):
# Assign points to the nearest centroid
Expand All @@ -98,13 +107,15 @@ def kmeans(self, x, n_clusters, n_iterations=100):

return cluster_assignments

def detect_communities(self, b):
def detect_communities(self, b, permute_x=True):
r"""Detect communities using spectral clustering on the modularity matrix.
Parameters
----------
b : torch.Tensor
The modularity matrix.
permute_x : bool, optional
Whether to permute the node features when clustering. Default is True.
Returns
-------
Expand All @@ -117,15 +128,21 @@ def detect_communities(self, b):
]

# Use implemented k-means clustering on the leading eigenvectors
return self.kmeans(leading_eigvecs, self.num_communities)
return self.kmeans(
leading_eigvecs, self.num_communities, permute_x=permute_x
)

def lift_topology(self, data: torch_geometric.data.Data) -> dict:
def lift_topology(
self, data: torch_geometric.data.Data, permute_x=True
) -> dict:
r"""Lift the graph topology to a hypergraph based on community structure and k-nearest neighbors.
Parameters
----------
data : torch_geometric.data.Data
The input graph data.
permute_x : bool, optional
Whether to permute the node features. Default is True.
Returns
-------
Expand All @@ -134,7 +151,7 @@ def lift_topology(self, data: torch_geometric.data.Data) -> dict:
and the original node features.
"""
b = self.modularity_matrix(data)
community_assignments = self.detect_communities(b)
community_assignments = self.detect_communities(b, permute_x)

num_nodes = data.x.shape[0]
num_hyperedges = num_nodes
Expand Down

0 comments on commit ee8e160

Please sign in to comment.