-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_preprocess.py
More file actions
205 lines (179 loc) · 8.67 KB
/
data_preprocess.py
File metadata and controls
205 lines (179 loc) · 8.67 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import networkx as nx
from networkx.readwrite import json_graph
import json
import os
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
class DataPreprocess():
"""
This class provided these methods:
- edgelist2networkx: convert an edgelist file to graph in networkx format.
If features file is present, it must be in dict format, with keys are nodes' id and values are features in list format.
- networkx2edgelist: convert an graph of networkx format to edgelist file,
save features if present in networkx.
"""
@staticmethod
def edgelist2networkx(edgelist_file, output_dir, features_file=None):
"""
:param edgelist_file: path to edgelist file, each line contains source node's id and target node's id.
:param output_dir: Output directory containing G.json and id2idx.json.
:return:
"""
g = nx.read_edgelist(edgelist_file, nodetype=str)
# save networkx
if not os.path.exists(output_dir):
os.makedirs(output_dir)
id2idx = {}
for idx, node in enumerate(g.nodes()):
id2idx[node] = idx
if features_file is not None:
features_dict = json.load(open(features_file))
feature_dim = len(list(features_dict.values())[0])
n_nodes = len(features_dict.keys())
features_arr = np.zeros((n_nodes, feature_dim))
for id, feature in features_dict.items():
g.node[id]['feature'] = feature
features_arr[id2idx[id]] = feature
np.save(os.path.join(output_dir, 'feats.npy'), features_arr)
with open(os.path.join(output_dir, 'G.json'), 'w+') as file:
file.write(json.dumps(json_graph.node_link_data(g)))
with open(os.path.join(output_dir, 'id2idx.json'), 'w+') as file:
file.write(json.dumps(id2idx))
@staticmethod
def network2edgelist(g_file, output_dir):
g_data = json.load(open(g_file))
g = json_graph.node_link_graph(g_data)
features = None
if 'feature' in g.node[g.nodes()[0]].keys():
features = {}
for node in g.nodes():
features[node] = g.node[node]['feature']
# save data
if not os.path.exists(output_dir):
os.makedirs(output_dir)
nx.write_edgelist(g, os.path.join(output_dir, 'edgelist'), delimiter=' ', data=False)
if features is not None:
with open(os.path.join(output_dir, 'feats.dict'), 'w+') as file:
file.write(json.dumps(features))
@staticmethod
def visualize_degree_distribution(G, output_dir, name):
deg = np.zeros((len(G.nodes()),)).astype(int)
for i in range(len(deg)):
deg[i] = len(G.neighbors(G.nodes()[i]))
sorted_deg = np.sort(deg)
plt.style.use('seaborn-whitegrid')
fig = plt.figure()
axes = fig.gca()
axes.set_ylim([1,np.max(sorted_deg)])
axes.plot(range(len(sorted_deg)), sorted_deg , color='blue')
outfile = output_dir + name + "_deg_dist.png"
plt.savefig(outfile)
return deg
# unique, counts = np.unique(deg, return_counts=True)
# y_max = max(counts)
# std = np.std(deg)
# mean = np.mean(deg)
# print("Standard deviation: ", std)
# print("Mean degree: ", mean)
# print("Max degree: ", max(deg))
# # quality = round(max(deg) / std, 1)
# # print("Quality: ", quality)
# plt.hist(np.array(sorted_deg), int(max(deg) / 2))
# plt.vlines(mean - std, 0, y_max, linestyle='dashed', linewidth=0.8, label='std line')
# plt.vlines(mean + std, 0, y_max, linestyle='dashed', linewidth=0.8)
# plt.vlines(mean, 0, y_max, color='red', linestyle='dashed', linewidth=1, label='mean line')
# plt.text(mean +std*0.05, y_max*2/3, 'mean = ' + str(round(mean, 1)) + ', std = ' + str(round(std, 1)), color='b')
# # plt.text(max(deg) / 2, y_max/2, 'quality = ' + str(quality))
# plt.xlabel('degree')
# plt.ylabel('num nodes')
# plt.title('deg')
# plt.grid(True)
# outfile = output_dir + name + "_deg.png"
# plt.savefig(outfile)
# print("Degree distribution saved to %s" % outfile)
@staticmethod
def visualize_distribution(dist, output_dir, name):
plt.style.use('seaborn-whitegrid')
fig = plt.figure()
axes = fig.gca()
axes.hist(dist, normed=False, bins=30)
outfile = output_dir + name + "_diff_dist.png"
plt.savefig(outfile)
@staticmethod
def visualize_line_distribution(dist1, dist2, output_dir, name):
idxs = range(len(dist1))
plt.style.use('seaborn-whitegrid')
fig = plt.figure()
axes = fig.gca()
axes.set_ylim([1,max(np.max(dist1), np.max(dist2))])
axes.plot(idxs, dist1, color='red')
axes.plot(idxs, dist2, color='blue')
outfile = output_dir + name + "_deg_dist.png"
plt.savefig(outfile)
@staticmethod
def evaluateDataset(dataset1, dataset2, groundtruth, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
outfile = open(os.path.join(output_dir, 'statistics.txt'), 'w+')
print("Source dataset info:")
outfile.write("Source dataset info:\n")
print(nx.info(dataset1.G))
outfile.write(nx.info(dataset1.G))
print("Target dataset info:")
outfile.write("\nTarget dataset info:\n")
print(nx.info(dataset2.G))
outfile.write(nx.info(dataset2.G))
target_deg = DataPreprocess.visualize_degree_distribution(dataset2.G, output_dir, "dataset2")
source_deg = DataPreprocess.visualize_degree_distribution(dataset1.G, output_dir, "dataset1")
# gt_src_deg = []
# gt_trg_deg = []
# scale_ratio = np.mean(source_deg) / np.mean(target_deg)
# for source_key in groundtruth.keys():
# source_id = source_key
# target_id = groundtruth[source_id]
# source_idx = dataset1.id2idx[source_id]
# target_idx = dataset2.id2idx[target_id]
# gt_src_deg.append(source_deg[source_idx])
# gt_trg_deg.append(int(target_deg[target_idx] * scale_ratio))
# DataPreprocess.visualize_line_distribution(gt_src_deg, gt_trg_deg, output_dir, "source_target_gt")
gt_diff_deg = []
for source_key in groundtruth.keys():
source_id = source_key
target_id = groundtruth[source_id]
source_idx = dataset1.id2idx[source_id]
target_idx = dataset2.id2idx[target_id]
gt_diff_deg.append(source_deg[source_idx] - target_deg[target_idx])
DataPreprocess.visualize_distribution(gt_diff_deg, output_dir, "source_target_gt")
stats = 0
total_stats = 0
# Check if neibours of source dataset in groundtruth are neighbors in target dataset
for source_node in groundtruth.keys():
target_node = groundtruth[source_node]
source_neighbors = dataset1.G.neighbors(source_node)
target_neighbors = dataset2.G.neighbors(target_node)
for neighbor in source_neighbors:
total_stats += 1
if neighbor in groundtruth.keys():
neighbor_in_target = groundtruth[neighbor]
if neighbor_in_target in target_neighbors:
stats += 1
print("Ratio of neighbors in source are also neighbors in target: %.4f" % (stats/total_stats))
outfile.write("\nRatio of neighbors in source are also neighbors in target: %.4f" % (stats/total_stats))
# Check if source dataset in groundtruth has the same feature with target dataset
if (dataset1.features is not None) and (dataset2.features is not None):
stats = 0
total_stats = 0
for source_key in groundtruth.keys():
total_stats += 1
source_id = source_key
target_id = groundtruth[source_id]
source_idx = dataset1.id2idx[source_id]
target_idx = dataset2.id2idx[target_id]
if np.array_equal(dataset1.features[source_idx], dataset2.features[target_idx]):
stats += 1
print("Ratio of same feature groundtruth: %.4f" % (stats/total_stats))
outfile.write("\nRatio of same feature groundtruth: %.4f" % (stats/total_stats))
print("Stats has been stored in %s" % (output_dir + 'statistics.txt'))
outfile.close()