-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gui.py
359 lines (307 loc) · 14.7 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
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
#!/usr/bin/python
import wx
import os
from shutil import copyfile
import time
from ObjectListView import ObjectListView, ColumnDefn
from hurry.filesize import size, alternative
import classification
ID_EXIT = 200
class FileManager(wx.App):
def OnInit(self):
myFrame = MainFrame(None, -1, 'Machine Learning File Manager')
myFrame.Show(True)
return True
class MainFrame(wx.Frame):
def __init__(self, parent, id, title):
"""Constructor method. This will set up the components of the main window, and their sizes and positions."""
wx.Frame.__init__(self, parent, -1, title)
self.Maximize(True)
# Create a panel to make sure it looks right on all systems
self.panel = wx.Panel(self, wx.ID_ANY)
# create the Menubar items
filemenu = wx.Menu()
filemenu.Append(ID_EXIT, "&Exit", " Terminate the program")
m_open = filemenu.Append(101, '&Open', 'Open a new directory of Documetns')
self.Bind(wx.EVT_MENU, self.on_open, m_open)
editmenu = wx.Menu()
helpmenu = wx.Menu()
# Add items to menubar
menuBar = wx.MenuBar()
menuBar.Append(filemenu, "&File")
menuBar.Append(editmenu, "&Edit")
menuBar.Append(helpmenu, "&Help")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
# create object list view to display files, and an input field
self.olv = ListView(self.panel)
# Create a drop-down list menu containing filter options
self.btn_import = wx.Button(self.panel, -1, "Import Files")
self.btn_import.Bind(wx.EVT_BUTTON, self.onselect_btn_import)
# Button to export files to structured directory based on classifications. Disable until labels are available.
self.btn_export = wx.Button(self.panel, -1, "Export Files")
self.btn_export.Bind(wx.EVT_BUTTON, self.onselect_btn_export)
self.btn_export.Disable()
# Button to create a custom classifier.
self.btn_classify = wx.Button(self.panel, -1, "Classify Files")
self.btn_classify.Bind(wx.EVT_BUTTON, self.onselect_btn_classify)
# Create top sizer and the two inner sizers to hold the list and header bar
topSizer = wx.BoxSizer(wx.VERTICAL)
inputSizer = wx.BoxSizer(wx.HORIZONTAL)
listSizer = wx.BoxSizer(wx.HORIZONTAL)
# Add components to inner sizers, and add those to the top sizer
inputSizer.Add(self.btn_import, 0, wx.ALL, 5)
inputSizer.Add(self.btn_classify, 0, wx.ALL, 5)
# inputSizer.Add((150, -1), 1, flag = wx.EXPAND | wx.ALIGN_RIGHT)
inputSizer.Add(self.btn_export, 0, wx.ALL, 5)
listSizer.Add(self.olv, 1, wx.EXPAND|wx.ALL)
topSizer.Add(inputSizer, 0, wx.EXPAND|wx.ALL, border=15)
topSizer.Add(listSizer, 1, wx.EXPAND|wx.ALL, border=15)
# put top sizer inside the panel and display
self.panel.SetSizer(topSizer)
topSizer.Fit(self)
self.Center()
self.Show(True)
def onselect_btn_export(self, event):
""""""
dlg = wx.DirDialog(self, "Choose target directory:", style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
chosen_dir = dlg.GetPath() +'/'
src_dir = self.olv.directory
files = self.olv.files
for cls in self.olv.classes:
# create directory to hold files.
target_dir = chosen_dir + cls + "/"
if not os.path.exists(target_dir):
os.makedirs(target_dir)
# copy files of each label into the appropriate directory.
for f in files:
target_file = target_dir + f.name
src_file = src_dir + f.name
if f.classification == cls:
copyfile(src_file, target_file)
def onselect_btn_import(self, event):
"""Import Files Button"""
dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE
# | wx.DD_DIR_MUST_EXIST
# | wx.DD_CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
self.olv.directory = dlg.GetPath() +'/'
self.olv.set_files()
dlg.Destroy()
def onselect_btn_classify(self, event):
"""Open window to create and use a classifier"""
frame = PopupWindow(self)
frame.Show()
def on_open(self, event):
# open working data [tk]
print "yaa"
def OnExit(self,e):
self.Close(True)
class ListView(ObjectListView):
def __init__(self, parent):
ObjectListView.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT | wx.SUNKEN_BORDER)
self.directory = '/home/james/PycharmProjects/final-year-project/working_data/20news_2topic/'
self.files = [] # Files being displayed in the ListView.
self.file_contents = []
self.classes = []
self.set_files()
def set_files(self, data=None):
"""
Description needed.[tk]
"""
# clear old values
self.files = []
self.file_contents = []
classification.remove_sklearn_incompatible(self.directory)
classification.remove_nltk_incompatible(self.directory)
file_names = os.listdir(self.directory)
for fil in file_names:
fsize = size(os.path.getsize(self.directory + fil), system=alternative)
modif = time.ctime(os.path.getmtime(self.directory + fil))
with file(self.directory + fil) as f:
contents = f.read()
f = File(fil, fsize, modif)
self.file_contents.append(contents)
self.files.append(f)
self.SetColumns([
ColumnDefn("Name", "left", 220, "name", isSpaceFilling=True),
ColumnDefn("Size", "left", 100, "size"),
ColumnDefn("Last Modified", "left", 150, "last_modified"),
ColumnDefn("Classification", "left", 300, "classification")
])
self.SetObjects(self.files)
def set_classes(self, classes):
"""Assign classifications to files in list and display them."""
for i, f in enumerate(self.files):
f.classification = classes[i]
# self.classes is a list of all unique classes in the files. Used in export function.
self.classes = list(set(classes))
# Apply the classifications to the files.
self.SetObjects(self.files)
class PopupWindow(wx.Frame):
""""""
# ----------------------------------------------------------------------
def __init__(self, parent_window):
"""Constructor"""
wx.Frame.__init__(self, None, title="Create Classifier", size= (550, 320),
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX)
# get reference to the MainFrame window calling the popup. Allows popup to update the list controll of parent.
self.parent_window = parent_window
# define sizers and grid layout of popup
panel = wx.Panel(self)
topSizer = wx.BoxSizer(wx.VERTICAL)
grid = wx.FlexGridSizer(6, 2, 9, 100)
# input values for user to enter: classifier name and location of training data
# make them as class variables so they can be accessed by methods
self.type_input = wx.ComboBox(panel, -1, '', size=(135, -1))
self.type_input.Append('Text Classifier (Supervised)')
self.type_input.Append('Topic Modelling (Unsupervised)')
self.type_input.Bind(wx.EVT_COMBOBOX, self.onselect_type)
self.numtopics_input = wx.TextCtrl(panel)
self.chk_box = wx.CheckBox(panel)
self.chk_box.Bind(wx.EVT_CHECKBOX, self.onselect_checkbox)
self.cls_input = wx.ComboBox(panel, -1, '', size=(135, -1))
self.display_classifiers()
self.cls_input.Bind(wx.EVT_COMBOBOX, self.onselect_cls_input)
self.name_input = wx.TextCtrl(panel)
self.dir_input = wx.TextCtrl(panel, size=(255, 30))
# Disable inputs to start. Need to choose classifier first
self.chk_box.Disable()
self.cls_input.Disable()
self.name_input.Disable()
self.numtopics_input.Disable()
self.dir_input.Disable()
# define text for the input boxes
type_txt = wx.StaticText(panel, label="Classifier Type:")
numtopics_txt = wx.StaticText(panel, label="Number of Topics:")
chk_txt = wx.StaticText(panel, label="Use Existing Classifier?")
cls_txt = wx.StaticText(panel, label="Select Existing Classifier:")
name_txt = wx.StaticText(panel, label="Classifier Name:")
dir_txt = wx.StaticText(panel, label="Training Data Directory:")
# define button for choosing training data directory
self.dir_btn = wx.Button(panel, label="...", size=(30, 30))
self.dir_btn.Bind(wx.EVT_BUTTON, self.on_dir)
self.dir_btn.Disable()
# add directory dialogue and text input to sizer
dir_sizer = wx.BoxSizer(wx.HORIZONTAL)
dir_sizer.Add(self.dir_input, 1, flag = wx.EXPAND | wx.ALIGN_RIGHT)
dir_sizer.Add(self.dir_btn)
# add all elements to the grid layout
grid.AddMany([type_txt, (self.type_input, 1, wx.EXPAND), numtopics_txt, self.numtopics_input, chk_txt, self.chk_box, cls_txt, (self.cls_input, 1, wx.EXPAND),
name_txt, (self.name_input, 1, wx.EXPAND), dir_txt, dir_sizer])
# Define "close" & "ok" buttons, and wrap in a sizer (bottom sizer).
bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.ok_btn = wx.Button(panel, label='Classify', size=(70, 30))
self.ok_btn.Disable()
self.ok_btn.Bind(wx.EVT_BUTTON, self.on_ok)
close_btn = wx.Button(panel, label='Close', size=(70, 30))
close_btn.Bind(wx.EVT_BUTTON, self.on_close)
bottom_sizer.Add(self.ok_btn, flag=wx.LEFT | wx.BOTTOM, border=5)
bottom_sizer.Add(close_btn, flag=wx.LEFT | wx.BOTTOM, border=5)
# put grid and the two buttons withing bottom_sizer into topsizer
topSizer.Add(grid, proportion=1, flag=wx.ALL | wx.EXPAND, border=15)
topSizer.Add(bottom_sizer, flag=wx.ALIGN_RIGHT | wx.RIGHT, border=10)
panel.SetSizer(topSizer)
def display_classifiers(self):
"""Get all the existing classifiers and display in the combobox as options"""
self.cls_input.Clear()
for fil in os.listdir('classifiers/'):
# adds name of classifier file to combobox
self.cls_input.SetStringSelection(os.path.splitext(fil)[0])
self.cls_input.Append(os.path.splitext(fil)[0])
def onselect_cls_input(self, event):
self.ok_btn.Enable()
def on_dir(self, event):
"""
Show the DirDialog and print the user's choice to input box
"""
dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE
# | wx.DD_DIR_MUST_EXIST
# | wx.DD_CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
self.dir_input.SetValue(dlg.GetPath())
self.ok_btn.Enable()
dlg.Destroy()
def onselect_type(self, event):
""""""
if self.type_input.GetValue() == 'Topic Modelling (Unsupervised)':
self.ok_btn.Enable()
# Clear and disable classifier name and trainging data input as they are not needed
self.numtopics_input.Enable()
self.dir_input.Clear()
self.dir_input.Disable()
self.dir_btn.Disable()
self.chk_box.Disable()
self.cls_input.Disable()
self.name_input.Disable()
# if supervised text classifier is selected, enable relevant inputs
if self.type_input.GetValue() == 'Text Classifier (Supervised)':
self.dir_input.Enable()
self.dir_btn.Enable()
self.chk_box.Enable()
self.name_input.Enable()
# Disable ok button until required information is input by user
self.ok_btn.Disable()
self.numtopics_input.Disable()
self.chk_box.SetValue(False)
def onselect_checkbox(self, event):
if self.chk_box.GetValue():
self.cls_input.Enable()
self.name_input.Disable()
self.dir_input.Disable()
self.dir_btn.Disable()
else:
self.cls_input.Disable()
self.name_input.Enable()
self.dir_input.Enable()
self.dir_btn.Enable()
def on_ok(self, event):
"""confirm selections. Create and save the new classifier"""
labels = []
# if name field is empty, use default name.
if self.name_input.GetValue() == "":
self.name_input.SetValue('new_classifier')
# Create the new classifier using the appropriate function, depending on what classifier type is selected
if self.type_input.GetValue() == 'Text Classifier (Supervised)':
# check if you need to create a new classifier, or use a saved one.
if self.chk_box.GetValue():
labels = classification.load_supervised_classifier(self.cls_input.GetValue(),
self.parent_window.olv.file_contents)
else:
labels = classification.create_supervised_classifier(self.name_input.GetValue(), self.dir_input.GetValue(),
self.parent_window.olv.file_contents)
if self.type_input.GetValue() == 'Topic Modelling (Unsupervised)':
num_topics = self.numtopics_input.GetValue()
if not num_topics:
# default value for num_topics
num_topics = 5
else:
# ensure that the value entered for num topics is an integer.
try:
num_topics = int(num_topics)
except ValueError:
wx.MessageDialog(self, "Number of Topics: must be an integer", "Warning!",
wx.OK | wx.ICON_WARNING).ShowModal()
return
# create lda model using working data and number of topics entered by the user.
labels = classification.classify_unsupervised_lda(self.parent_window.olv.directory, num_topics)
# update parent window's classifier list
self.parent_window.olv.set_classes(labels)
self.parent_window.btn_export.Enable()
self.Close(True)
def on_close(self, event):
"""Close Popup window"""
self.Close(True)
class File:
def __init__(self, name, size, mod):
self.name = name
self.classification = None
self.size = size
self.last_modified = mod
app = FileManager(0)
app.MainLoop()