-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontraction.py
155 lines (129 loc) · 5.68 KB
/
contraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 16 19:56:46 2023
@author: chris
"""
import numpy as np
import networkx as nx
import random
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import OneHotEncoder
def contract_graph(G, num_features, num_classes, centrality = 'EC',
constraint = 'FL', N_cluster = 100, gamma = 0.5, steps = [],
multilabel = True):
print("Contracting Graph...")
print("=============== Graph Contraction ===============")
print(f"Centrality Measure: {centrality}")
print(f"Constraint: {constraint}")
print(f"K-Means Cluster: {N_cluster}")
print(f"Gamma: {gamma}")
print(f"Contraction steps: {steps}")
print(f"Pre-contraction graph nodes: {G.number_of_nodes()}")
print(f"Pre-contraction graph edges: {G.number_of_edges()}")
random.seed(42) #42
# =============================================================================
# # METIS Clustering (Community preserving)
# import metis
# if constraint == 'C':
#
# if N_cluster == 1:
# parts = [0 for i in range(G.number_of_nodes())]
# else:
# (_, parts) = metis.part_graph(G, N_cluster)
# =============================================================================
# K-Means Clustering (Feature Label Distribution Preserving)
if constraint == 'FL':
X = np.zeros((G.number_of_nodes(), num_features))
if multilabel:
Y = np.zeros((G.number_of_nodes(), num_classes))
else:
Y = np.zeros((G.number_of_nodes(),1))
for i,node in enumerate(G.nodes(data=True)):
X[i] = node[1]['x']
Y[i] = node[1]['y']
if not multilabel:
# one hot encoding to logits
enc = OneHotEncoder(handle_unknown='ignore')
enc.fit(Y)
Y = enc.transform(Y).toarray()
# scaling k-means attribute to [0,1]
scaler = MinMaxScaler()
scaler.fit(X)
X = scaler.transform(X)
scaler.fit(Y)
Y = scaler.transform(Y)
X = X * np.sqrt(gamma) * np.sqrt(max(num_features, num_classes)/num_features)
Y = Y * np.sqrt(1-gamma) * np.sqrt(max(num_features, num_classes)/num_classes)
XY = np.concatenate((X,Y), axis=1)
kmeans = KMeans(n_clusters=N_cluster, random_state=42, n_init="auto").fit(XY)
parts = kmeans.labels_
# Hierarchical Contraction
for budget in steps:
if centrality == 'BC':
BC = nx.betweenness_centrality(G, k=100, normalized=True, weight=None, endpoints=False, seed=42)
elif centrality == 'DC':
BC = nx.degree_centrality(G)
elif centrality == 'PR':
BC = nx.pagerank(G, alpha=0.85, personalization=None, max_iter=100, tol=1e-06, nstart=None, dangling=None)
elif centrality == 'EC':
BC = nx.eigenvector_centrality(G, max_iter=10000, tol=1e-06, nstart=None, weight=None)
elif centrality == 'CC':
BC = nx.closeness_centrality(G, u=None, distance=None, wf_improved=True)
elif centrality == 'RAND':
BC = {}
for i in range(G.number_of_nodes()):
BC[i] = random.random()
else:
print("Centrality Type Undefined, No Contraction Done!")
continue
# Reduce Training Graph (by nodes)
N_budget = budget # Budget for number of training nodes
num_training = 0
for node in G.nodes(data=True):
if node[1]['train']:
num_training += 1
remove_ratio = 1 - N_budget/num_training
partitioned_num_nodes = np.zeros((N_cluster,))
for node in G.nodes(data=True):
if node[1]['train']:
partitioned_num_nodes[parts[node[0]]] += 1
N_remove_partition = np.zeros((N_cluster,))
for i in range(N_cluster):
N_remove_partition[i] = remove_ratio*partitioned_num_nodes[i]
listBC = list(BC.items())
random.shuffle(listBC)
sortedBC = sorted(listBC, key=lambda x:x[1])
for i in range(G.number_of_nodes()):
# check the partition for node i
curr_node = sortedBC[i][0]
curr_part = parts[curr_node]
# check if training node
if not G.nodes[curr_node]['train']:
continue
# check the partition quota
if (N_remove_partition[curr_part] <= 0.0):
continue
elif (random.random() > N_remove_partition[curr_part]):
N_remove_partition[curr_part] -= 1
continue
N_remove_partition[curr_part] -= 1
node_to_remove = curr_node
neighbors = G.neighbors(node_to_remove)
max_nb_BC = 0
best_nb = None
for nb in neighbors:
if max_nb_BC < BC[nb]:
best_nb = nb
max_nb_BC = BC[nb]
if best_nb != None: # remove node by merging with best neighbour
nx.contracted_nodes(G, best_nb, node_to_remove, self_loops=False, copy=False)
else: # if no neighbor, just remove self
G.remove_node(node_to_remove)
print(f"Post-contraction graph nodes: {G.number_of_nodes()}")
print(f"Post-contraction graph edges: {G.number_of_edges()}")
print()
# Cleaning indexing after removal
G = nx.convert_node_labels_to_integers(G, first_label=0, ordering='default')
return G