-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
368 lines (304 loc) · 12 KB
/
run.py
File metadata and controls
368 lines (304 loc) · 12 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import sys
import pandas as pd
from scripts.wekaloader import start_weka, stop_weka, load_Arff_file
from scripts.clustering import run_clusterer
from scripts.bayes_networks import run_bayesNet
from scripts.naiveBayes import run_naiveBayes
from scripts.multilayer_percepton import run_multilayerPercepton
from weka.classifiers import Classifier, Evaluation, PredictionOutput
from weka.core.classes import Random
from weka.attribute_selection import ASSearch, ASEvaluation, AttributeSelection
from config import data_dirs, data_files, data_dirs3, data_dirs3_appended, data_files3
from helper import randomise_CSV, splitTestingSets, to_ARFF, output_eval, output_pred, get_ARFFs_in_dir, get_CSVs_in_dir, output_select_attribute
import time
Field2 = []
Field5 = []
Field10 = []
Field50 = []
a = 0
def init():
try:
# Start Javabridge
start_weka()
while True:
# Match with if's below
print("\nSelect Options to proceed:")
print("1 : Randomise CSV's")
print("2 : Run IBk algorithm")
print("3 : Select Attribute Creation")
print("4 : Run Bayes Net")
print("5 : Run Clusterer")
print("6 : Run Naive Bayes")
print("7 : Split file into testing set")
print("8 : Run Multilayer Percepton")
print("0 : End Program")
x = int(input())
if x == 1:
make_dirs3()
make_random_CSVs()
elif x == 2:
for dirs in data_dirs:
if dirs == "root":
continue
for file in get_ARFFs_in_dir(data_dirs[dirs]):
start = time.time()
print("IBk run time for file: %s", str(file))
run_ibk(file)
end = time.time()
timetaken = round(end - start, 2)
print("Time taken to run: %s seconds" % (timetaken))
elif x == 3:
for dirs in data_dirs3_appended:
if dirs == "root":
continue
for file in get_ARFFs_in_dir(data_dirs3_appended[dirs]):
select_attribute(file)
make_CSV_SA(
data_dirs3_appended[dirs] / "fer2018_main.csv")
elif x == 4:
for dirs in data_dirs: # Loops through all data dirs
if dirs == "root":
continue
for file in get_ARFFs_in_dir(data_dirs[dirs]):
# Only gets main files
if file.name.endswith("fer2018_main.arff") or file.name.endswith("Field.arff"):
# Only gets main files
start = time.time()
print("bayesNet run time for file: %s", str(file))
run_bayesNet(file)
end = time.time()
timetaken = round(end - start, 2)
print("Time taken to run: %s seconds" %
(timetaken))
elif x == 5:
for dirs in data_dirs: # Loops through all data dirs
if dirs == "root":
continue
for file in get_ARFFs_in_dir(data_dirs[dirs]):
# Only gets main files
if file.name.endswith("_main.arff"):
start = time.time()
print("Cluster run time for file: %s", str(file))
run_clusterer(file)
end = time.time()
timetaken = round(end - start, 2)
print("Time taken to run: %s seconds" %
(timetaken))
elif x == 6:
for dirs in data_dirs: # Loops through all data dirs
if dirs == "root":
continue
for file in get_ARFFs_in_dir(data_dirs[dirs]):
# Only gets main files
if file.name.endswith("fer2018_main.arff") or file.name.endswith("Field.arff"):
run_naiveBayes(file)
elif x == 7:
make_dirs3()
make_training_set()
elif x == 8:
for dirs in data_dirs3_appended: # Loops through all data dirs
if dirs == "root":
continue
print(data_dirs3_appended[dirs])
ARFFs = get_ARFFs_in_dir(data_dirs3_appended[dirs])
print(ARFFs)
if ARFFs[0].name.startswith("fer2017-training"):
train = ARFFs[0]
test = ARFFs[1]
else:
train = ARFFs[1]
test = ARFFs[0]
print("Training on " + train.name +
" and testing on " + test.name)
run_multilayerPercepton(train, test)
elif x == 0:
print("Exiting")
stop_weka()
sys.exit(0)
else:
print("Input %d not valid." % x)
except Exception as e:
print(e)
finally:
stop_weka()
def make_CSV_SA(file):
# Output directory
data_dir = file.parents[0]
name = file.name[:-5]
f = pd.read_csv(file)
keep_col_2Field = ['emotion']
keep_col_5Field = ['emotion']
keep_col_10Field = ['emotion']
keep_col_50Field = ['emotion']
print(str(len(Field2)))
for i in range(len(Field2)):
keep_col_2Field.append('pixel' + str(Field2[i]))
new_f = f[keep_col_2Field]
new_f.to_csv(data_dir / (name + "_2Field.csv"), index=False)
print(str(len(Field5)))
for i in range(len(Field5)):
keep_col_5Field.append('pixel' + str(Field5[i]))
new_f = f[keep_col_5Field]
new_f.to_csv(data_dir / (name + "_5Field.csv"), index=False)
print(str(len(Field10)))
for i in range(len(Field10)):
keep_col_10Field.append('pixel' + str(Field10[i]))
new_f = f[keep_col_10Field]
new_f.to_csv(data_dir / (name + "_10Field.csv"), index=False)
print(str(len(Field50)))
for i in range(len(Field50)):
keep_col_50Field.append('pixel' + str(Field50[i]))
new_f = f[keep_col_50Field]
new_f.to_csv(data_dir / (name + "_50Field.csv"), index=False)
to_ARFF(data_dir / (name + "_2Field.csv"))
to_ARFF(data_dir / (name + "_5Field.csv"))
to_ARFF(data_dir / (name + "_10Field.csv"))
to_ARFF(data_dir / (name + "_50Field.csv"))
def select_attribute(file):
global Field50
global Field10
global Field5
global Field2
global a
filename = file.parts[-1] # Get filename from Pathlib object
dir = file.parents[0] # Data directory currently in
print("Selecting attributes from %s" % filename)
if not filename.endswith(".arff"):
print("%s not ARFF file." % filename)
return
filename_base = filename[:-5] # Removes '.arff' from filename
data = load_Arff_file(file) # Load data from arff
data.class_is_first() # Set first attr as class
# Define Attribute selection
search = ASSearch(classname="weka.attributeSelection.Ranker",
options=["-T", "0.01", "-N", "-1"])
# Define Attribute Evaluator
evaluator = ASEvaluation(
classname="weka.attributeSelection.CorrelationAttributeEval", options=[])
# Run attribution selection
attsel = AttributeSelection()
attsel.search(search)
attsel.evaluator(evaluator)
attsel.select_attributes(data)
# Define filepath and output results
attsel_output = filename_base + "_attsel_results.txt"
output_select_attribute(attsel, dir / attsel_output)
# Debug Analysis
print(attsel.selected_attributes)
for i in range(2):
Field2.append(attsel.selected_attributes[i])
for i in range(5):
Field5.append(attsel.selected_attributes[i])
for i in range(10):
Field10.append(attsel.selected_attributes[i])
for i in range(50):
Field50.append(attsel.selected_attributes[i])
print(Field2)
print(Field5)
print(Field10)
print(Field50)
if len(set(Field10)) == len(Field10):
print("no duplicates found")
else:
print("duplicate found")
Field50 = list(set(Field50))
Field10 = list(set(Field10))
Field5 = list(set(Field5))
Field2 = list(set(Field2))
###
def run_ibk(file):
# Get filename from Pathlib object
filename = file.parts[-1]
dir = file.parents[0]
print("Running IBk on %s" % filename)
if not filename.endswith(".arff"):
print("%s not ARFF file." % filename)
return
# Removes '.arff' from filename
filename_base = filename[:-5]
# Load data with class as first attr
data = load_Arff_file(file)
data.class_is_first()
# Use IBk and set options
cls = Classifier(classname="weka.classifiers.lazy.IBk",
options=["-K", "3"])
# print(cls.options)
# Predictions stored in pout
pout = PredictionOutput(
classname="weka.classifiers.evaluation.output.prediction.PlainText")
# Evaluate data
evaluation = Evaluation(data)
evaluation.crossvalidate_model(cls, data, 10, Random(1), output=pout)
# Save summary, class details and confusion matrix to file
result_output = filename_base + "_eval_results.txt"
output_eval(evaluation, dir / result_output)
# Save the predicited results to file
prediction_output = filename_base + "_pred_results.txt"
output_pred(pout, dir / prediction_output)
print("IBk complete")
def make_dirs():
for dir in data_dirs:
data_dirs[dir].mkdir(parents=True, exist_ok=True)
def make_dirs3():
for dir in data_dirs3:
data_dirs3[dir].mkdir(parents=True, exist_ok=True)
def make_random_CSVs():
global Field50
global Field10
global Field5
global Field2
# Store made arff paths
arffs = []
files_created = []
for file in data_files3:
if(file.endswith(".csv")):
if file.startswith("fer2017-training"):
print("Reset Globals")
files_created = []
arffs = []
Field50 = []
Field10 = []
Field5 = []
Field2 = []
files_created += randomise_CSV(file)
# Convert random csv's to arffs
for fc in files_created:
arff_file = to_ARFF(fc)
if arff_file.name.startswith("fer2017-training"):
arffs.append(arff_file)
# Get best attrs of all 3 files
if file.startswith("fer2017-testing"):
# Get best attributes
for af in arffs:
select_attribute(af)
for fc in files_created:
make_CSV_SA(fc)
def make_training_set():
global Field50
global Field10
global Field5
global Field2
# Store made arff paths
arffs = []
for file in data_files3:
if(file.endswith(".csv")):
if file.startswith("fer2017-training"):
print("Reset Globals")
arffs = []
Field50 = []
Field10 = []
Field5 = []
Field2 = []
files_created = splitTestingSets(file)
# Convert random csv's to arffs
for fc in files_created:
arffs.append(to_ARFF(fc))
# Get best attrs of all 3 files
if file.startswith("fer2017-testing"):
# Get best attributes
for af in arffs:
select_attribute(af)
for fc in files_created:
make_CSV_SA(fc)
if __name__ == "__main__":
init()