-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.py
184 lines (166 loc) · 7.07 KB
/
split.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
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
import numpy as np
import random
import os
def mkdir(path):
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
print("--- new folder... ---")
print("--- OK ---")
else:
print("--- There is this folder! ---")
def x_write(list_name, list_to_file_name):
"""
write a list of test value to file
:param list_name:
:param list_to_file_name:
:return: no
"""
with open(list_to_file_name, 'w') as f:
for line_value in list_name:
f.write(str(line_value) + "\n")
def y_write(list_name, list_to_file_name):
"""
write the test result matrix to file
:param list_name:
:param list_to_file_name:
:return: no
"""
with open(list_to_file_name, 'w') as f:
for line_value in list_name:
s = ""
for i in range(len(line_value)):
if i != len(line_value) - 1:
s += str(int(line_value[i]))
s += " "
else:
s += str(int(line_value[i]))
s += "\n"
f.write(s)
def y_write_float(list_name, list_to_file_name):
"""
wirte the float test result matrix to file
:param list_name:
:param list_to_file_name:
:return: no
"""
with open(list_to_file_name, 'w') as f:
for line_value in list_name:
s = ""
for i in range(len(line_value)):
if i != len(line_value) - 1:
s += str(line_value[i])
s += " "
else:
s += str(line_value[i])
s += "\n"
f.write(s)
def load_data(edge_list):
"""
load text
:param edge_list: a list of the edges
:return:
edge_predict a list of matrix according to the edge_list
"""
# convert the input list to string list
edge_str = ["l" + str(i[0]) + "_" + "l" + str(i[1]) for i in edge_list]
edge_predict = []
edge_predict_round = []
edge_truth = []
# for edge
for edge in edge_str:
edge_path = "data/store/" + edge + "/" + edge
edge_predict.append(np.loadtxt(edge_path + "_predict.txt"))
edge_predict_round.append(np.loadtxt(edge_path + "_predict_round.txt", dtype=int))
# for edge truth
edge_truth_path = "data/split_test/" + edge + "_truth.txt"
edge_truth.append(np.loadtxt(edge_truth_path, dtype=int))
return edge_predict, edge_predict_round, edge_truth
def write_edge_truth(edge_list, y_test):
"""
write the true edge info to split
:param edge_list: a list of edge
:param y_test: the result of the test
:return: no
"""
for edge in edge_list:
edge_name = "l" + str(edge[0]) + "_" + "l" + str(edge[1])
true_edge = [y_test[i, edge[0]] * y_test[i, edge[1]] for i in range(y_test.shape[0])]
true_edge_path = "data/split_test/" + edge_name + "_truth.txt"
with open(true_edge_path, "a+") as f:
for value in true_edge:
f.write(str(value) + "\n")
def split_data(edge_list, edge_predict, edge_predict_round, edge_truth,
y_test, original_predict, original_predict_round, total_num, adjust_num):
"""
split data to adjust and left randomly
:param edge_list:
:param edge_predict:
:param edge_predict_round:
:param edge_truth:
:param y_test:
:param original_predict:
:param original_predict_round:
:param total_num:
:param adjust_num:
:return: the split result
"""
random.seed(0)
sample_index = random.sample(range(total_num), adjust_num)
left_num = total_num - adjust_num
#
edge_str = ["l" + str(i[0]) + "_" + "l" + str(i[1]) for i in edge_list]
# for edge
for edge_index in range(len(edge_list)):
edge_predict_adjust = []
edge_predict_adjust_round = []
edge_predict_left = []
edge_predict_left_round = []
edge_truth_adjust = []
edge_truth_left = []
for index in range(y_test.shape[0]):
if index in sample_index:
edge_predict_adjust.append(edge_predict[edge_index][index])
edge_predict_adjust_round.append(edge_predict_round[edge_index][index])
edge_truth_adjust.append(edge_truth[edge_index][index])
else:
edge_predict_left.append(edge_predict[edge_index][index])
edge_predict_left_round.append(edge_predict_round[edge_index][index])
edge_truth_left.append(edge_truth[edge_index][index])
x_write(edge_predict_adjust, "data/split_test/" + edge_str[edge_index] + "_predict_" + str(adjust_num) + ".txt")
x_write(edge_predict_adjust_round, "data/split_test/" + edge_str[edge_index] + "_predict_" + str(adjust_num) + "_round.txt")
x_write(edge_predict_left, "data/split_test/" + edge_str[edge_index] + "_predict_" + str(left_num) + ".txt")
x_write(edge_predict_left_round, "data/split_test/" + edge_str[edge_index] + "_predict_" + str(left_num) + "_round.txt")
x_write(edge_truth_adjust, "data/split_test/" + edge_str[edge_index] + "_truth_" + str(adjust_num) + ".txt")
x_write(edge_truth_left, "data/split_test/" + edge_str[edge_index] + "_truth_" + str(left_num) + ".txt")
# split y_test, original_predict, original_predict_round
y_test_adjust = []
original_predict_adjust = []
original_predict_adjust_round = []
y_test_left = []
original_predict_left = []
original_predict_left_round = []
for index in range(y_test.shape[0]):
if index in sample_index:
y_test_adjust.append(y_test[index])
original_predict_adjust.append(original_predict[index])
original_predict_adjust_round.append(original_predict_round[index])
else:
y_test_left.append(y_test[index])
original_predict_left.append(original_predict[index])
original_predict_left_round.append(original_predict_round[index])
y_write(y_test_adjust, "data/split_test/y_test_" + str(adjust_num) + ".txt")
y_write(y_test_left, "data/split_test/y_test_" + str(left_num) + ".txt")
y_write_float(original_predict_adjust, "data/split_test/original_predict_" + str(adjust_num) + ".txt")
y_write_float(original_predict_left, "data/split_test/original_predict_" + str(left_num) + ".txt")
y_write(original_predict_adjust_round, "data/split_test/original_predict_" + str(adjust_num) + "_round.txt")
y_write(original_predict_left_round, "data/split_test/original_predict_" + str(left_num) + "_round.txt")
def prepare_test_data(edge_list, total_num, adjust_num):
mkdir("data/split_test")
y_test = np.loadtxt("data/processed/y_test.txt", dtype=int)
original_predict = np.loadtxt("data/store/original/original_predict.txt")
original_predict_round = np.loadtxt("data/store/original/original_predict_round.txt")
write_edge_truth(edge_list, y_test)
edge_predict, edge_predict_round, edge_truth = load_data(edge_list)
split_data(edge_list, edge_predict, edge_predict_round, edge_truth,
y_test, original_predict, original_predict_round, total_num, adjust_num)