-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_curation.py
282 lines (217 loc) · 9.09 KB
/
data_curation.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
# coding: utf-8
# In[80]:
import os
import transformers
from pathlib import Path
import torch
import numpy as np
from math import ceil
from random import shuffle
from itertools import chain
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras.layers import Input, Dense, LSTM, TimeDistributed, Conv1D, MaxPooling1D, UpSampling1D
from tensorflow.keras.models import Model
import tensorflow.keras.optimizers as opt
import pandas as pd
from transformers import RobertaTokenizer
# In[81]:
#basePath = '/home/himesh/TagCoder/pythonNotebook'
basePath = r'C:\Users\Himesh\Documents\thesis\pythonNotebook'
positivePathSuffix = '/Positive'
negativePathSuffix = '/Negative'
tokenizerInPath = basePath + '\\tokenizerIn'
tokenizerOutPath = basePath + '\\tokenizerOut'
train_ratio = 0.7
# In[83]:
#tokenizer = transformers.AutoTokenizer.from_pretrained("bert-base-uncased")
tokenizer = RobertaTokenizer.from_pretrained('Salesforce/codet5-base')
# In[85]:
smellList = ['ComplexMethod']
final_text = ""
print(tokenizerInPath)
for smell in smellList:
smellPath = os.path.join(tokenizerInPath, smell,'Positive',"")
#print(smellPath)
for file in os.listdir(smellPath):
#print(os.path.basename(file))
with open(os.path.join(smellPath, file),"r") as read_file:
try:
text = read_file.read()
tokenized_text = tokenizer.tokenize(text)#,padding = "max_length")
input_ids = tokenizer.convert_tokens_to_ids(tokenized_text)
modint = (len(input_ids)) % 512
#print(modint)
length = len(input_ids) - modint
input_ids = input_ids[0:length]
final_text += ' '.join(map(str, input_ids))+' '
except Exception as e:
print(e)
pass
#Path(os.path.join(tokenizerOutPath,smell,positivePathSuffix, 'tokenizer.tok')).touch(exist_ok=True)
with open(os.path.abspath(os.path.join(tokenizerOutPath,smell,'Positive', 'tokenizer.tok')),'w',errors='ignore') as out_file:
#out_file.touch(exist_ok=True)
#print(final_text)
out_file.write(final_text)
smellPath = os.path.join(tokenizerInPath, smell,'Negative',"")
print(smellPath)
for file in os.listdir(smellPath):
#print(os.path.basename(file))
with open(os.path.join(smellPath, file),"r") as read_file:
try:
text = read_file.read()
tokenized_text = tokenizer.tokenize(text)#,padding = "max_length")
input_ids = tokenizer.convert_tokens_to_ids(tokenized_text)
modint = (len(input_ids)) % 512
#print(modint)
length = len(input_ids) - modint
input_ids = input_ids[0:length]
final_text += ' '.join(map(str, input_ids))+' '
except Exception as e:
print(e)
pass
#Path(os.path.join(tokenizerOutPath,smell,positivePathSuffix, 'tokenizer.tok')).touch(exist_ok=True)
with open(os.path.abspath(os.path.join(tokenizerOutPath,smell,'Negative', 'tokenizer.tok')),'w',errors='ignore') as out_file:
#out_file.touch(exist_ok=True)
#print(final_text)
out_file.write(final_text)
# In[72]:
posInput = []
num_lines_pos = sum(1 for line in open(os.path.join(tokenizerOutPath,smell,'Positive', 'tokenizer.tok'),"r"))
with open(os.path.join(tokenizerOutPath,smell,'Positive', 'tokenizer.tok'),"r") as read_file:
text = read_file.read()
text = text.replace('\n', ' ')
text = text.replace('\r', ' ')
#print(text)
posInput = np.fromstring(text, sep=" ").tolist()
print(len(posInput))
# for line in read_file:
# if line == '\n':
# continue
# arr = np.fromstring(line, dtype=np.int32, sep=" ").tolist()
# posInput.append(arr)
negInput = []
num_lines_neg = sum(1 for line in open(os.path.join(tokenizerOutPath,smell,'Negative', 'tokenizer.tok'),"r"))
with open(os.path.join(tokenizerOutPath,smell,'Negative', 'tokenizer.tok'),"r") as read_file:
text = read_file.read()
text = text.replace('\n', ' ')
text = text.replace('\r', ' ')
#print(text)
negInput = np.fromstring(text, dtype=np.int32, sep=" ").tolist()
# for line in read_file:
# if line == '\n':
# continue
# arr = np.fromstring(line, dtype=np.int32, sep=" ").tolist()
# negInput.append(arr)
num_lines_all = num_lines_pos if num_lines_pos < num_lines_neg else num_lines_pos
# In[73]:
posInputLen = len(posInput)
negInputLen = len(negInput)
print(str(posInputLen)+" "+str(negInputLen))
train_data = []
test_data = []
posSize = ceil(posInputLen*train_ratio) - ceil(posInputLen*train_ratio) % 512
print(posSize)
negSize = ceil(negInputLen*train_ratio) - ceil(negInputLen*train_ratio) % 512
test_data.append(posInput[posSize+1:])
test_data[0] = test_data[0][0:len(test_data[0]) - (len(test_data[0])%512)]
test_data.append(negInput[negSize+1:])
test_data[1] = test_data[1][0:len(test_data[1]) - (len(test_data[1])%512)]
test_data_flattened = list(chain.from_iterable(test_data))
test_data_np = np.array(test_data_flattened)
test_label = np.empty(shape=[len(test_data_np)], dtype=np.float32)
print(len(test_label))
test_label[0:posSize] = 1.0
test_label[posSize+1:] = 0.0
total_train_data = 0
train_data.append(posInput[0:posSize])
train_data[0] = train_data[0][0:len(train_data[0]) - (len(train_data[0])%512)]
total_train_data += len(train_data[0])
train_data.append(negInput[0:negSize])
train_data[1] = train_data[1][0:len(train_data[1]) - (len(train_data[1])%512)]
total_train_data += len(train_data[1])
print('total_train_data'+str(total_train_data))
train_data_flattened = list(chain.from_iterable(train_data))
#shuffle(train_data_flattened)
#print(train_data[1])
train_data_np = np.array(train_data_flattened)
train_data_np = train_data_np.reshape(-1,512,1)
shuffle(train_data_np)
print('arr shape')
print(train_data_np.shape)
test_data_np = test_data_np.reshape(len(test_label),1)
# In[ ]:
# In[78]:
def autoencoder_lstm(train_data, test_data_np, smell, layers=1, encoding_dimension=8, no_of_epochs=10, with_bottleneck=True, is_final=False):
encoding_dim = encoding_dimension
input_layer = Input(shape=(512, 1))
# input_layer = BatchNormalization()(input_layer)
no_of_layers = layers
prev_layer = input_layer
for i in range(no_of_layers):
encoder = LSTM(int(encoding_dim / pow(2, i)),
#activation="relu",
return_sequences=True,
recurrent_dropout=0.1,
dropout=0.1)(prev_layer)
prev_layer = encoder
if with_bottleneck:
prev_layer = LSTM(int(encoding_dim / pow(2, no_of_layers + 1)),
#activation="relu",
return_sequences=True,
recurrent_dropout=0.1,
dropout=0.1)(prev_layer)
for j in range(no_of_layers - 1, -1, -1):
decoder = LSTM(int(encoding_dim / pow(2, j)),
#activation='relu',
return_sequences=True,
recurrent_dropout=0.1,
dropout=0.1)(prev_layer)
prev_layer = decoder
prev_layer = TimeDistributed(Dense(1))(prev_layer)
autoencoder = Model(inputs=input_layer, outputs=prev_layer)
autoencoder.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['accuracy'])
autoencoder.summary()
batch_sizes = [32, 64]
b_size = int(len(train_data) / 512)
if b_size > len(batch_sizes) - 1:
b_size = len(batch_sizes) - 1
history = autoencoder.fit(train_data,
train_data,
epochs=no_of_epochs,
batch_size=batch_sizes[b_size],
verbose=1,
validation_split=0.2,
shuffle=True).history
plt.plot(history['loss'])
plt.plot(history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper right')
plt.show()
predictions = autoencoder.predict(test_data_np)
predictions = predictions.reshape(predictions.shape[0], predictions.shape[1])
test_data_np = test_data_np.reshape(test_data_np.shape[0], test_data_np.shape[1])
mse = np.mean(np.power(test_data_np - predictions, 2), axis=1)
error_df = pd.DataFrame({'Reconstruction_error': mse,
'True_class': test_label})
print(error_df.describe())
# In[79]:
layers = [1,2]
encoding_dim = [8, 16, 32]
epochs = 100
cur_iter = 1
skip_iter = 2
for layer in layers:
for bottleneck in [True]:
for encoding in encoding_dim:
if cur_iter <= skip_iter:
cur_iter += 1
continue
cur_iter += 1
autoencoder_lstm(train_data_np,test_data_np, smell, layers=layer,encoding_dimension=encoding,no_of_epochs=epochs, with_bottleneck=bottleneck)
# In[ ]: