-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
579 lines (492 loc) · 30.7 KB
/
main.py
File metadata and controls
579 lines (492 loc) · 30.7 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# Machine Learning Project
# By James Quaife: j.quiafe1@nuigalway.ie, SID: 14100104
# and Andrew East: a.east1@nuigalway.ie, SID: 16280042
# National University of Ireland, Galway
# Computer Science CT475: Machine Learning
# November 2018
# Supervisor: Dr. Michael Madden
# Teamwork Attribution: This file was written by Andrew East
import tkinter as tk
from tkinter import filedialog, messagebox, IntVar, ttk
from datetime import datetime
import os
import logging
from about import get_about_message
from classes.Case import Case, ParseCsvError
from graph_tree import graph_model
from parse_csv import parse_csv, read_one
from split import clone_spliter
from test import test, score
from train import train
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.winfo_toplevel().title("Decision Tree Machine Learning")
self.pack(fill=tk.BOTH, expand=True)
self.filename = ""
self.is_file_prepared = False
self.trainer = None
self.master_data_set = None
self.training_set = []
self.testing_set = []
self.model = []
self.test_score = []
self.train_score = []
self.average_test_score = -1.0
self.graph_photoimage_img_data = []
self.graph_png_img_data = []
self.current_results_subframe_shown = -1
self.can_make_graphs = True
# ################## Frame Top: Control Buttons ################## #
self.frame_controls = tk.Frame(self)
self.frame_controls.pack(side=tk.TOP)
pack_options_button = {"side": tk.LEFT, "padx": 6, "pady": 6, "ipadx": 4, "ipady": 4}
self.button_load_file = tk.Button(self.frame_controls)
self.button_load_file["text"] = "Load Data File"
self.button_load_file["command"] = self.load_file
self.image_load_file = tk.PhotoImage(file="images/open.png")
self.button_load_file["compound"] = tk.LEFT
self.button_load_file["image"] = self.image_load_file
self.button_load_file.pack(pack_options_button)
self.button_train = tk.Button(self.frame_controls)
self.button_train["text"] = "Train on Data Set"
self.button_train["state"] = tk.DISABLED
self.button_train["command"] = self.train_on_data
self.image_train = tk.PhotoImage(file="images/process.png")
self.button_train["compound"] = tk.LEFT
self.button_train["image"] = self.image_train
self.button_train.pack(pack_options_button)
self.button_previous = tk.Button(self.frame_controls)
self.button_previous["text"] = "Previous"
self.button_previous["state"] = tk.DISABLED
self.button_previous["command"] = self.show_previous_subframe_results
self.image_previous = tk.PhotoImage(file="images/previous.png")
self.button_previous["compound"] = tk.LEFT
self.button_previous["image"] = self.image_previous
self.button_previous.pack(pack_options_button)
self.button_next = tk.Button(self.frame_controls)
self.button_next["text"] = "Next"
self.button_next["state"] = tk.DISABLED
self.button_next["command"] = self.show_next_subframe_results
self.image_next = tk.PhotoImage(file="images/next.png")
self.button_next["compound"] = tk.RIGHT
self.button_next["image"] = self.image_next
self.button_next.pack(pack_options_button)
self.button_save = tk.Button(self.frame_controls)
self.button_save["text"] = "Save Results"
self.button_save["state"] = tk.DISABLED
self.button_save["command"] = self.save_results
self.image_save = tk.PhotoImage(file="images/save.png")
self.button_save["compound"] = tk.LEFT
self.button_save["image"] = self.image_save
self.button_save.pack(pack_options_button)
self.button_about = tk.Button(self.frame_controls)
self.button_about["command"] = lambda: messagebox.showinfo("About this Program", get_about_message())
self.image_about = tk.PhotoImage(file="images/about.png")
self.button_about["image"] = self.image_about
self.button_about.pack({"side": tk.LEFT, "padx": 6, "pady": 6, "ipadx": 5, "ipady": 5})
# ################## Frame Bottom: Container ################## #
# Frame switching code using tkraise() to bring z-order of frame up: Bryan Oakley (26 September 2011) https://stackoverflow.com/a/7557028/5271224
self.frame_bottom = tk.Frame(self)
self.frame_bottom.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.frame_bottom.grid_rowconfigure(0, weight=1)
self.frame_bottom.grid_columnconfigure(0, weight=1)
# ################## Frame Bottom: Results ################## #
# Subframe to instruct how to get started
self.frame_home = tk.Frame(self.frame_bottom)
self.frame_home.grid(row=0, column=0, sticky="nsew")
self.label_instructions_header = tk.Label(self.frame_home, font=("TkDefaultFont", 22),
text="Decision Tree Machine Learning")
self.label_instructions_header.pack(padx=20, pady=20)
self.label_instructions_body = tk.Label(self.frame_home, font=("TkDefaultFont", 12),
text="Please click Load Data File above to get started.")
self.label_instructions_body.pack(padx=20)
# ################## Frame Bottom: Results ################## #
# Subframe to show results of the model; Make ten of them into arrays of GUI elements
self.subframe_results = []
self.tree_canvas = []
self.canvas_img_data = []
self.label_prediction_score = []
self.label_training_accuracy = []
self.label_aggregate_prediction_score = []
self.scrollframe_table_predictions = []
self.table_predictions = []
self.scrollbar_table_prediction = []
for idx in range(NUM_MODELS):
self.subframe_results.append(self.make_single_results_frame())
# ################## Frame Bottom: Set Input File Options ################## #
# Subframe with controls to set the column options
self.subframe_columns = tk.Frame(self.frame_bottom)
self.subframe_columns.grid(row=0, column=0, sticky="nsew")
self.subframe_column_name_inputs_area = tk.LabelFrame(self.subframe_columns, text="Input Column Names and Select Label/Class Column")
self.subframe_column_name_inputs_area.pack(padx=10, fill=tk.X)
# Build the subframe which will contain the text input boxes to input column names
self.subframe_col_options = tk.Frame(self.subframe_column_name_inputs_area)
self.subframe_col_options.pack()
self.subframe_col_options_inner = None
self.cols_text_boxes = None
self.cols_radio_buttons = None
self.cols_radio_var = None
self.button_process_csv = tk.Button(self.subframe_column_name_inputs_area)
self.button_process_csv["text"] = "Update Metadata"
self.button_process_csv["command"] = self.save_file_attributes
self.image_process_csv = tk.PhotoImage(file="images/data.png")
self.button_process_csv["compound"] = tk.LEFT
self.button_process_csv["image"] = self.image_process_csv
pack_options_button["side"] = tk.TOP
self.button_process_csv.pack(pack_options_button)
self.subframe_inputted_file_area = tk.LabelFrame(self.subframe_columns, text="Data File", padx=5, pady=5)
self.subframe_inputted_file_area.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
self.input_table_frame = tk.Frame(self.subframe_inputted_file_area, bd=2, relief=tk.SUNKEN)
self.table_loaded_input = ttk.Treeview(self.input_table_frame, show="headings", columns="message_column")
self.table_loaded_input.heading("message_column", text="Datafile not loaded yet")
ttk.Style().layout("Treeview", []) # Setting the style of all Treeview widgets successfully removes the border to better fit w/ the scrollbar
self.table_loaded_input.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.table_scrollbar = tk.Scrollbar(self.input_table_frame)
self.table_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.input_table_frame.pack(padx=20, pady=10, fill=tk.BOTH, expand=True)
self.table_loaded_input.config(yscrollcommand=self.table_scrollbar.set)
self.table_scrollbar.config(command=self.table_loaded_input.yview)
self.add_col_options() # Add default options, no data
# ################## Finalise Window Building ################## #
self.show_subframe_home()
# ################## Methods to support switching pages ################### #
def show_subframe_columns(self):
if self.filename is "":
self.__change_subframe_column_options_input_state(tk.DISABLED)
else:
self.__change_subframe_column_options_input_state(tk.NORMAL)
self.cols_text_boxes[0].focus_set() # Set focus on first text box so user can immediately start typing
self.subframe_columns.tkraise()
self.current_results_subframe_shown = -1
def disable_subframe_columns(self):
self.__change_subframe_column_options_input_state(tk.DISABLED)
def __change_subframe_column_options_input_state(self, state):
for text in self.cols_text_boxes:
text["state"] = state
for radio in self.cols_radio_buttons:
radio["state"] = state
self.button_process_csv["state"] = state
def show_subframe_results(self, idx):
if self.current_results_subframe_shown < 0: # Only do the work of disabling input boxes if that frame was already on top
self.disable_subframe_columns()
self.subframe_results[idx].tkraise()
self.current_results_subframe_shown = idx
if self.current_results_subframe_shown == 0:
self.button_previous["state"] = tk.DISABLED
else:
self.button_previous["state"] = tk.NORMAL
if self.current_results_subframe_shown == NUM_MODELS - 1: # Note this is not an elif because NUM_MODELS could be just 1
self.button_next["state"] = tk.DISABLED
else:
self.button_next["state"] = tk.NORMAL
def show_previous_subframe_results(self):
if self.current_results_subframe_shown <= 0:
self.show_subframe_results(NUM_MODELS - 1)
else:
self.show_subframe_results(self.current_results_subframe_shown - 1)
def show_next_subframe_results(self):
if self.current_results_subframe_shown >= NUM_MODELS - 1 or self.current_results_subframe_shown < 0:
self.show_subframe_results(0)
else:
self.show_subframe_results(self.current_results_subframe_shown + 1)
def show_subframe_home(self):
if self.current_results_subframe_shown < 0: # Only do the work of disabling input boxes if that frame was already on top
self.disable_subframe_columns()
self.current_results_subframe_shown = -1
self.frame_home.tkraise()
# Add variable number of text/check boxes to input column labels
def add_col_options(self):
"""
Generate the text boxes, labels, etc., one for each dataset attribute column
So that user can input the names for each column
Destroys existing input boxes within the frame and recreates them
"""
if self.subframe_col_options_inner is not None:
self.subframe_col_options_inner.destroy()
self.subframe_col_options_inner = tk.Frame(self.subframe_col_options)
self.subframe_col_options_inner.pack(expand=True, fill="both")
if self.filename is not "":
row = read_one(self.filename)
num_cols = len(row)
else:
num_cols = 4 # generate with four columns by default
row = [""] * num_cols
# Create empty "padding columns" at the beginning and end
self.subframe_col_options_inner.grid_columnconfigure(0, minsize=5)
self.subframe_col_options_inner.grid_columnconfigure(num_cols + 1, minsize=5)
# Allow all columns (that have controls in them) to resize
for col_num in range(num_cols):
self.subframe_col_options_inner.grid_columnconfigure(col_num + 1, weight=1)
self.cols_text_boxes = []
self.cols_radio_buttons = []
self.cols_radio_var = IntVar()
for idx in range(num_cols):
tk.Label(self.subframe_col_options_inner, text="#" + str(idx + 1) + ": " + row[idx]).grid(row=0, column=idx + 1)
for idx in range(num_cols):
text_box = tk.Entry(self.subframe_col_options_inner)
text_box.grid(row=1, column=idx + 1)
self.cols_text_boxes.append(text_box)
for idx in range(num_cols):
radio = tk.Radiobutton(self.subframe_col_options_inner,
text="Label", variable=self.cols_radio_var, value=idx)
radio.grid(row=2, column=idx + 1)
self.cols_radio_buttons.append(radio)
self.cols_radio_buttons[num_cols - 1].select() # Select last in list, since many data sets have the final column as the label
# TODO: Checkbox for each column to mark it as categorical vs. continuous? Will need to rework algorithm s.t. it can handle non-continuous values
# Skip over re-typing the column names every time for owls.csv
if DEBUG and "owls.csv" in self.filename:
for idx, name in enumerate(["body-length", "wing-length", "body-width", "wing-width", "type"]):
self.cols_text_boxes[idx].insert(0, name)
def make_single_results_frame(self):
"""
Generate one of the ten GUI "pages" to hold a graph/table of results
Appends elements that will be edited later to each of many arrays of GUI elements. (This may be the _least_ pure function I've ever written...)
"""
subframe_results = tk.Frame(self.frame_bottom)
subframe_results.grid(row=0, column=0, sticky="nsew")
canvas_area = tk.LabelFrame(subframe_results, text="Model %d of %d" % (len(self.subframe_results) + 1, NUM_MODELS), padx=5, pady=5)
canvas_area.pack(padx=10, fill=tk.BOTH, expand=True)
subframe_tree_canvas = tk.Frame(canvas_area, bd=2, relief=tk.SUNKEN)
subframe_tree_canvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
subframe_tree_canvas.grid_rowconfigure(0, weight=1)
subframe_tree_canvas.grid_columnconfigure(0, weight=1)
tree_canvas = tk.Canvas(subframe_tree_canvas, bd=0, scrollregion=(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT), background="#FCFEFC")
self.tree_canvas.append(tree_canvas)
self.canvas_img_data.append(None)
scroll_v = tk.Scrollbar(subframe_tree_canvas, orient=tk.VERTICAL)
scroll_v.grid(row=0, column=1, sticky="ns")
scroll_v.config(command=tree_canvas.yview)
scroll_h = tk.Scrollbar(subframe_tree_canvas, orient=tk.HORIZONTAL)
scroll_h.grid(row=1, column=0, sticky="ew")
scroll_h.config(command=tree_canvas.xview)
tree_canvas.config(xscrollcommand=scroll_h.set, yscrollcommand=scroll_v.set)
tree_canvas.grid(row=0, column=0, sticky="nsew")
subframe_results_predictions = tk.LabelFrame(subframe_results, text="Predictions %d of %d" % (len(self.subframe_results) + 1, NUM_MODELS), padx=5, pady=5)
subframe_results_predictions.pack(padx=10, pady=10, side=tk.TOP, fill=tk.X)
subframe_ca_container = tk.Frame(subframe_results_predictions)
subframe_ca_container.pack(side=tk.LEFT)
subframe_classification_accuracy = tk.LabelFrame(subframe_ca_container, text="Classification Accuracy", padx=5, pady=5)
subframe_classification_accuracy.pack(padx=5, pady=5, side=tk.TOP, fill=tk.X)
label_prediction_score = tk.Label(subframe_classification_accuracy, text="xx.x%", font=("TkDefaultFont", 18), justify=tk.LEFT)
label_prediction_score.pack()
self.label_prediction_score.append(label_prediction_score)
label_training_accuracy = tk.Label(subframe_classification_accuracy, text="xx.x%", font=("TkDefaultFont", 10), justify=tk.LEFT)
label_training_accuracy.pack()
self.label_training_accuracy.append(label_training_accuracy)
subframe_aggregate_classification_accuracy = tk.LabelFrame(subframe_ca_container, text="Average CA", padx=5, pady=5)
subframe_aggregate_classification_accuracy.pack(padx=5, pady=5, side=tk.BOTTOM, fill=tk.X)
label_aggregate_prediction_score = tk.Label(subframe_aggregate_classification_accuracy, text="xx.x%", font=("TkDefaultFont", 14), justify=tk.LEFT)
label_aggregate_prediction_score.pack()
self.label_aggregate_prediction_score.append(label_aggregate_prediction_score)
scrollframe_table_predictions = tk.Frame(subframe_results_predictions, bd=2, relief=tk.SUNKEN)
table_predictions = ttk.Treeview(scrollframe_table_predictions, height=5, show="headings", columns="message_column") # Height is number of rows
table_predictions.heading("message_column", text="Predictions not loaded yet")
table_predictions.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar_table_prediction = tk.Scrollbar(scrollframe_table_predictions)
scrollbar_table_prediction.pack(side=tk.RIGHT, fill=tk.Y)
scrollframe_table_predictions.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
table_predictions.config(yscrollcommand=scrollbar_table_prediction.set)
scrollbar_table_prediction.config(command=table_predictions.yview)
self.scrollframe_table_predictions.append(scrollframe_table_predictions)
self.table_predictions.append(table_predictions)
self.scrollbar_table_prediction.append(scrollbar_table_prediction)
return subframe_results
# ################## Methods called by buttons to do main functionality ################## #
def load_file(self):
chosen_file = filedialog.askopenfilename(initialdir=os.getcwd(),
title="Choose a data file",
filetypes=(("CSV files", "*.csv"), ("All files", "*.*")))
if chosen_file is not "":
self.filename = chosen_file # Temp variable used so cancelling the dialog when a file had already been loaded will not prevent proceeding
self.button_train["state"] = tk.DISABLED
self.button_previous["state"] = tk.DISABLED
self.button_next["state"] = tk.DISABLED
self.button_save["state"] = tk.DISABLED
# Recreate column attribute picker GUI elements for user to provide column names
self.add_col_options()
self.show_subframe_columns()
# Call save_file_attrib, even though user has not entered any column names
# This will make it so the file is loaded, but the columns will have default names
self.is_file_prepared = False
self.save_file_attributes()
def save_file_attributes(self):
"""
Gather properties of CSV file from user-inputted GUI elements
"""
logging.debug("Loading data file: " + self.filename)
if self.filename is not "":
Case.attributes_names = []
Case.label_column = self.cols_radio_var.get()
for idx, text_box in enumerate(self.cols_text_boxes):
value = text_box.get()
if len(value) == 0:
value = "column" + str(idx + 1)
if idx == Case.label_column:
Case.label_name = value
else:
Case.attributes_names.append(value)
# Show datatable of loaded data
self.table_loaded_input.destroy()
col_indices = list(range(len(Case.attributes_names) + 1)) # Make tuple of columns
self.table_loaded_input = ttk.Treeview(self.input_table_frame, show="headings", columns=col_indices)
self.table_loaded_input.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Create column headers
for idx, item in enumerate(Case.attributes_names + [Case.label_name]): # Label columns
self.table_loaded_input.column(str(idx), minwidth=5, width=20)
self.table_loaded_input.heading(str(idx), text=item, anchor="w")
# Reconnect scrollbar events to new Treeview object
self.table_loaded_input.config(yscrollcommand=self.table_scrollbar.set)
self.table_scrollbar.config(command=self.table_loaded_input.yview)
# Parse file into list of python objects
try:
self.master_data_set = parse_csv(self.filename)
except ParseCsvError as error:
if self.is_file_prepared: # This indicates that this file has already been read once, and then user changed the label column. So, make it an error
tk.messagebox.showerror("Parse CSV error", "Error while reading file %s\n\nBad line: %s\n\nItem could note be parsed to a float: %s"
% (self.filename, error.bad_line, error.bad_item))
else:
tk.messagebox.showinfo("Parse CSV error", "While reading file, a data point could not be read as a number: '%s'\n\nYou may need to select a different column as the label column."
% error.bad_item)
# Reset the GUI with no dataset loaded!
# This is done so the user does not see data there still, and ignore the error to continue training expecting that the file actually loaded
self.master_data_set = None
self.button_train["state"] = tk.DISABLED
self.is_file_prepared = False
return # Do not continue loading file
# Fill table with data from file
self.table_loaded_input.tag_configure("even", background="#eeeeee")
for idx, case in enumerate(self.master_data_set):
self.table_loaded_input.insert("", "end", values=[item for item in case.attributes + [case.label]], tags="even" if idx % 2 == 0 else "")
# Enable next step in UI flow
self.button_train["state"] = tk.NORMAL
self.is_file_prepared = True
else:
messagebox.showwarning("No file loaded", "Cannot select file attributes: no data file has been loaded")
def train_on_data(self):
if self.master_data_set is not None:
# Clear previous models before re-training
self.training_set = []
self.testing_set = []
self.model = []
self.test_score = []
self.train_score = []
self.graph_photoimage_img_data = []
self.graph_png_img_data = []
# Train, score, and display each model
col_indices = list(range(len(Case.attributes_names) + 2)) # Make tuple of column names, as defined by user. Same for each loop
for i in range(NUM_MODELS):
logging.info("Training model #" + str(i + 1))
# Get a randomised split of the data set, cloned so the master set remains ready for re-use
training_set, testing_set = clone_spliter(self.master_data_set)
self.training_set.append(training_set)
self.testing_set.append(testing_set)
# Build that model!
model = train(training_set)
self.model.append(model)
# Test & score on the holdout set, e.g. make predictions
test(model, testing_set)
test_score = score(testing_set)
self.test_score.append(test_score)
# Also score on the training set, which shows the confidence in the model itself (but usually can't be 100%, because of majority-class leaves)
test(model, training_set)
train_score = score(training_set)
self.train_score.append(train_score)
if self.can_make_graphs:
try:
# Make Graph using pydot python objects and return as a tk PhotoImage & PNG
graph_photoimage_img_data, graph_png_img_data = graph_model(model)
self.graph_photoimage_img_data.append(graph_photoimage_img_data)
self.graph_png_img_data.append(graph_png_img_data)
# Paint image
if self.canvas_img_data[i] is not None:
self.tree_canvas[i].delete(self.canvas_img_data[i]) # Remove existing image objects
self.canvas_img_data[i] = self.tree_canvas[i].create_image(0, 0, image=graph_photoimage_img_data, anchor=tk.NW)
# Reconfigure scrolling area of canvas to the area of the current graph
self.tree_canvas[i].config(scrollregion=(0, 0, graph_photoimage_img_data.width(), graph_photoimage_img_data.height()))
except OSError as e:
if "dot" in e.strerror:
logging.error("Graphviz 'dot' executable not found on PATH. No more graphs will be attempted this session")
messagebox.showwarning("Graphviz not found", "Graphviz's dot executable was not found.\nPlease install Graphviz, and if on Windows, add the" +
" Graphviz bin directory to the PATH\n\nNo more graphs will be attempted to be generated during this session, but the" +
" models can still be tested as normal.")
self.can_make_graphs = False
else:
raise # This was not the error for "dot not found", so throw it onward
# Write score
self.label_prediction_score[i]["text"] = "%.1f%%" % (test_score * 100)
self.label_training_accuracy[i]["text"] = "Training Set: %.1f%%" % (train_score * 100)
# Put results into datatable
self.table_predictions[i].destroy()
self.table_predictions[i] = ttk.Treeview(self.scrollframe_table_predictions[i], height=5, show="headings", columns=col_indices)
self.table_predictions[i].pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# Create column headers
for j, item in enumerate(Case.attributes_names):
self.table_predictions[i].column(str(j), minwidth=5, width=20)
self.table_predictions[i].heading(str(j), text=item, anchor="w")
for j, item in enumerate([Case.label_name, "Predicted"]):
self.table_predictions[i].column(str(j + len(Case.attributes_names)), minwidth=10, width=40)
self.table_predictions[i].heading(str(j + len(Case.attributes_names)), text=item, anchor="w")
# Create table contents
self.table_predictions[i].tag_configure("even", background="#eeeeee")
self.table_predictions[i].tag_configure("mismatchEven", background="#eeeeee", foreground="#cc0000")
self.table_predictions[i].tag_configure("mismatchOdd", foreground="#cc0000")
for j, case in enumerate(testing_set):
self.table_predictions[i].insert("", "end", values=[item for item in case.attributes + [case.label, case.predicted]],
tags="mismatch" + ("Even" if j % 2 == 0 else "Odd") if case.predicted != case.label else ("even" if j % 2 == 0 else "odd")
)
# Reconnect scrollbar events to new Treeview object
self.table_predictions[i].config(yscrollcommand=self.scrollbar_table_prediction[i].set)
self.scrollbar_table_prediction[i].config(command=self.table_predictions[i].yview)
# Calculate aggregate classification accuracy
total = 0.0
for test_score in self.test_score:
total += test_score
self.average_test_score = total / NUM_MODELS
for aggregate_result_label in self.label_aggregate_prediction_score:
aggregate_result_label["text"] = "%.1f%%" % (self.average_test_score * 100)
# Enable browsing through results and saving all results
self.button_previous["state"] = tk.NORMAL
self.button_next["state"] = tk.NORMAL
self.button_save["state"] = tk.NORMAL
# Show the first set of results
self.show_subframe_results(0)
else:
messagebox.showwarning("No file loaded", "Cannot train model: no data file has been loaded")
def save_results(self):
if len(self.model) != 0:
# Get now as a datetime string to tag files with
date_tag = datetime.now().strftime("%y%m%d-%H%M")
# Make a templating string to build each individual filename later
filename_template = "results - %s - %%d of %d.%%s" % (date_tag, NUM_MODELS)
chosen_folder = filedialog.askdirectory(initialdir=os.getcwd(),
title="Choose directory to save results into, files will be named \"" + filename_template % (0, "csv/png") + "\"")
if chosen_folder is not "":
csv_header_row = ",".join(Case.attributes_names + [Case.label_name, "predicted"]) + "\n"
for idx in range(NUM_MODELS):
# Write PNG file out
if self.can_make_graphs: # Don't attempt to write PNGs if Graphviz wasn't installed
png_filename = os.path.join(chosen_folder, filename_template % (idx + 1, "png"))
with open(png_filename, "wb") as png_file:
png_file.write(self.graph_png_img_data[idx])
# Write CSV file out
csv_filename = os.path.join(chosen_folder, filename_template % (idx + 1, "csv"))
with open(csv_filename, "w") as csv_file:
csv_file.write(csv_header_row)
for case in self.testing_set[idx]:
columns = [str(item) for item in case.attributes + [str(case.label), str(case.predicted)]]
csv_file.write(",".join(columns) + "\n")
logging.debug("Files with results and images of models have been saved")
messagebox.showinfo("Save successful", "Data files & tree graphs saved")
else:
messagebox.showwarning("No model trained", "Cannot save model results: no model has been trained")
DEBUG = False
if DEBUG:
logging.basicConfig(level=logging.DEBUG)
CANVAS_WIDTH = 600
CANVAS_HEIGHT = 600
NUM_MODELS = 10
root = tk.Tk()
root.geometry("650x700")
master_app = Application(master=root)
master_app.mainloop()