-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.py
139 lines (129 loc) · 5.63 KB
/
GUI.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
import os.path
import os
from Tkinter import *
import tkFileDialog
import tkMessageBox
import pandas as pd
import NaiveBayesClassifier
class GUI:
_nbc = None
def __init__(self, myroot):
self.test_df=None;
self.train_df = None;
self.bins = 0
self.BuildPassed = False;
self.root = myroot
root.title("Naive Bayes Classifier")
# define labels, entry`s and c #
self.Path_Label = Label(root, text='Directory Path') # path
self.Path_Entry = Entry(root, width=60)
self.Path_Browse_button = Button(root, text="Browse", command=self.browseClick, width=14)
self.Bins_Labels = Label(root, text='Discretization Bins') # Bins
self.Bins_Entry = Entry(root, width=20)
self.Bins_Entry.configure(state='disabled')
self.Build_button = Button(root, text="Build", command=self.buildClick, width=25) # Build and Classify
self.Classify_button = Button(root, text="Classify", command=self.classifyClick, width=25)
# locate labels ,entry's and buttons on grid #
self.Path_Label.grid(row=3, pady=35, padx=30) # path
self.Path_Entry.grid(row=3, column=1, padx=10, stick=W)
self.Path_Browse_button.grid(row=3, column=2, padx=10)
self.Bins_Labels.grid(row=4, column=0) # Bins
self.Bins_Entry.grid(row=4, column=1, padx=10, stick=W + N)
self.Build_button.grid(row=5, column=1, pady=20) # Build and Classify
self.Classify_button.grid(row=6, column=1, pady=10)
# set the size of the window
self.root.geometry('{}x{}'.format(700, 350))
self.root.resizable(0, 0) # disable the option to resize the window
# handles the browse button
def browseClick(self):
self.train_df = None
path = tkFileDialog.askdirectory(parent=root, title='Choose the directory path')
self.Path_Entry.delete(0, END)
self.Path_Entry.insert(0, path)
if self.checkValidPath() and self.check_not_empty_files():
self.Bins_Entry.configure(state='normal')
self.train_df = pd.read_csv(str(self.Path_Entry.get()) + "/train.csv")
else:
self.train_df = None
self.Bins_Entry.configure(state='disabled')
# handled the Build button
def buildClick(self):
if self.checkValidPath() and self.checkValidBins() and self.check_not_empty_files():
self._nbc = NaiveBayesClassifier.NaiveBayesClassifier(str(self.Path_Entry.get()), self.bins)
self._nbc.load_train_data_frame()
self.BuildPassed = True
tkMessageBox.showinfo("Naive Bayes Classifier","Building classifier using train-set is done!")
else:
self.BuildPassed = False
# handled the Classify button
def classifyClick(self):
if not self.BuildPassed:
tkMessageBox.showinfo("Naive Bayes Classifier", "Error!\nYou need to build the model first!")
else:
self._nbc.load_test_set()
tkMessageBox.showinfo("Naive Bayes Classifier", "The Classify is done!")
os._exit(0)
# Checks if all the files are exists in the given directory path
def checkValidPath(self):
error = False
missing_files = ""
if not os.path.isfile(self.Path_Entry.get()+"/Structure.txt"):
error = True
missing_files += "<Structure.txt> "
if not os.path.isfile(self.Path_Entry.get() + "/train.csv"):
error = True
missing_files += "<train.csv> "
if not os.path.isfile(self.Path_Entry.get() + "/test.csv"):
error = True
missing_files += "<test.csv> "
if error:
tkMessageBox.showinfo("Naive Bayes Classifier", "Error!\nThe following files are missing in the directory: \n" + missing_files)
return False
return True
# checks if the content of the files is empty
def check_not_empty_files(self):
error = False
empty_files = ""
if os.path.getsize(self.Path_Entry.get()+"/Structure.txt")==0:
error = True
empty_files += "<Structure.txt> "
# check train file
if self.check_is_csv_empty("train.csv"):
error = True
empty_files += "<train.csv> "
# check test file
if self.check_is_csv_empty("test.csv"):
error = True
empty_files += "<test.csv> "
if error:
tkMessageBox.showinfo("Naive Bayes Classifier", "Error!\nThe following files are empty: \n" + empty_files)
return False
return True
# check is the csv file empty
def check_is_csv_empty(self, file_name):
file = open(self.Path_Entry.get() + "/"+file_name, "r")
file_content = file.read()
file.close()
if file_content == "":
return True
else:
rows = file_content.split("\n", 1)
if rows[1] == "":
return True
return False
# Checks if bins value is valid
def checkValidBins(self):
self.bins = self.Bins_Entry.get()
num_of_records = self.train_df.shape[0]
# check if the bins value is digit AND bigger then 1
if not self.bins.isdigit():
tkMessageBox.showinfo("Naive Bayes Classifier", "Error!\nThe bins value is not valid. Bins value must be digit!")
return False
if int(self.bins) < 2 or int(self.bins) > num_of_records:
tkMessageBox.showinfo("Naive Bayes Classifier", "Error!\nThe bins value is out of range!")
return False
self.bins = int(self.bins)
return True;
root = Tk()
NaveBayesClassifier_Gui = GUI(root)
root.mainloop()