-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.py
324 lines (269 loc) · 12.8 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
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from TWNBA_sweepplotter import Data, lineplot, set_grid, save_plot, kdeplot
from tkinter import * # NOQA
from tkinter import ttk
from tkinter import filedialog
from itertools import cycle
root = Tk()
root.title('TWNBA Sweepplotter')
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w * 0.5, h * 0.5))
# Parameters
global canvas
global class_data
global num_plots, sec_cycle_value, sec_cycle, first_cycle
global list_of_lines
first_digit_4 = [0, 0, 1, 1]
second_digit_4 = [0, 1, 0, 1]
first_digit_9 = [0, 0, 0, 1, 1, 1, 2, 2, 2]
second_digit_9 = [0, 1, 2, 0, 1, 2, 0, 1, 2]
first_digit_16 = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]
second_digit_16 = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
# Button to load in a file
button_open = Button(root, text="open", command=lambda: open_files())
button_open.grid(row=0, column=0)
def open_files():
try:
canvas.get_tk_widget().destroy()
except NameError:
pass
for count in range(9):
try:
list_of_lines[count].combo_hue.grid_forget()
list_of_lines[count].combo_first_param.grid_forget()
list_of_lines[count].combo_sec_param.grid_forget()
list_of_lines[count].combo_first_value.grid_forget()
list_of_lines[count].combo_sec_value.grid_forget()
except (NameError, IndexError):
pass
file_name = filedialog.askopenfilename(title="Open File",
filetypes=(("Text Files", "*.txt"), ("All Files", "*.*"))
)
global class_data
class_data = Data(file_name)
text.delete('1.0', END)
text.insert("end", "File name: " + file_name.split('/')[-1]
+ "\n" "parameters: " + str(class_data.parameters)
+ "\n" "sweep: " + str(class_data.sweep) + "\n" "steps: "
+ str(class_data.steps) + "\n" "runs: "
+ str(class_data.runs) + "\n")
text.see("end")
combo_num_plots.config(state='readonly')
button_set_num_plots.config(state='normal')
# Placement of the log
text = Text(root, width=40, height=8)
text.grid(row=0, column=3, columnspan=10)
# Combo to pick the number of plots
num_plots_values = ['1', "4", "9", "16"]
combo_num_plots = ttk.Combobox(root, values=num_plots_values, state='disabled')
combo_num_plots.set("How many plots?")
combo_num_plots.grid(row=0, column=1)
button_set_num_plots = Button(root, text='Set', state='disabled',
command=lambda: set_num_plots(combo_num_plots.get(), class_data))
button_set_num_plots.grid(row=0, column=2)
def set_num_plots(num, data):
global num_plots, sec_cycle_value, sec_cycle, first_cycle
global list_of_lines
try:
canvas.get_tk_widget().destroy()
except NameError:
pass
for count in range(16):
try:
list_of_lines[count].combo_hue.grid_forget()
list_of_lines[count].combo_first_param.grid_forget()
list_of_lines[count].combo_sec_param.grid_forget()
list_of_lines[count].combo_first_value.grid_forget()
list_of_lines[count].combo_sec_value.grid_forget()
except (NameError, IndexError):
pass
num_plots = int(num)
list_of_lines = []
lines = 1
for count in range(num_plots):
list_of_lines.append(Command(lines, 1, data))
lines += 1
try:
first_cycle = cycle(range(len(data.dict_of_unique_param[list_of_lines[0].combo_first_param.get()])))
except KeyError:
pass
try:
sec_cycle = cycle(range(len(data.dict_of_unique_param[list_of_lines[0].combo_sec_param.get()])))
except KeyError:
pass
cycle_help = [1, 0, 0, 1, 0, 0, 1, 0, 0]
cycle_help_16 = [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]
cycle_count = 0
if num_plots == 4:
for line in list_of_lines:
first_cycle_value = next(first_cycle)
line.combo_first_value.current(first_cycle_value)
if num_plots == 9:
for line in list_of_lines:
first_cycle_value = next(first_cycle)
line.combo_first_value.current(first_cycle_value)
try:
if cycle_help[cycle_count] == 1:
sec_cycle_value = next(sec_cycle)
line.combo_sec_value.current(sec_cycle_value)
except (TclError, NameError):
pass
cycle_count += 1
if num_plots == 16:
for line in list_of_lines:
first_cycle_value = next(first_cycle)
line.combo_first_value.current(first_cycle_value)
try:
if cycle_help_16[cycle_count] == 1:
sec_cycle_value = next(sec_cycle)
line.combo_sec_value.current(sec_cycle_value)
except (TclError, NameError):
pass
cycle_count += 1
button_exec_lineplot = Button(root, text='Execute Lineplot', command=lambda: exec_plot('line'))
button_exec_lineplot.grid(row=34, column=3)
button_exec_kdeplot = Button(root, text='Execute Kdeplot', command=lambda: exec_plot('kde'))
button_exec_kdeplot.grid(row=34, column=4)
# Recipe input
class Command:
def __init__(self, row, column, data):
self.combo_first_value = None
self.combo_sec_value = None
param_with_none = data.parameters.copy()
param_with_none.append('None')
def first_param_update(_):
self.combo_first_value.config(values=data.dict_of_unique_param[self.combo_first_param.get()])
def sec_param_update(_):
if self.combo_sec_param.get() == 'None':
self.combo_sec_value.config(values=['None'])
self.combo_sec_value.current(0)
else:
self.combo_sec_value.config(values=data.dict_of_unique_param[self.combo_sec_param.get()])
self.combo_first_param = ttk.Combobox(root, values=data.parameters, state='readonly')
self.combo_first_param.set("First parameter")
self.combo_first_param.grid(row=row, column=column, padx=2, pady=2)
self.combo_first_param.bind("<<ComboboxSelected>>", first_param_update)
self.combo_first_value = ttk.Combobox(root, state='readonly', values=['None'])
self.combo_first_value.set("First value")
self.combo_first_value.grid(row=row, column=column + 1, padx=2, pady=2)
self.combo_sec_param = ttk.Combobox(root, values=param_with_none, state='readonly')
self.combo_sec_param.set("Second parameter")
self.combo_sec_param.grid(row=row, column=column + 2, padx=2, pady=2)
self.combo_sec_param.bind("<<ComboboxSelected>>", sec_param_update)
self.combo_sec_value = ttk.Combobox(root, state='readonly', values=['None'])
self.combo_sec_value.set("Second value")
self.combo_sec_value.grid(row=row, column=column + 3, padx=2, pady=2)
self.combo_hue = ttk.Combobox(root, values=data.parameters, state='readonly')
self.combo_hue.set("hue")
self.combo_hue.grid(row=row, column=column + 4, padx=2, pady=2)
# Set the inital selected parameters
if num_plots == 4:
self.combo_first_param.current(1)
self.combo_sec_param.current(2)
self.combo_hue.current(0)
first_param_update('_')
sec_param_update('_')
if num_plots == 9:
self.combo_first_param.current(1)
self.combo_sec_param.current(2)
self.combo_hue.current(0)
first_param_update('_')
sec_param_update('_')
if num_plots == 16:
self.combo_first_param.current(1)
self.combo_sec_param.current(2)
self.combo_hue.current(0)
first_param_update('_')
sec_param_update('_')
# Executing the plot
def exec_plot(kind):
global canvas
plot = None
try:
canvas.get_tk_widget().destroy()
except NameError:
pass
set_grid(num_plots)
if kind == 'line':
for count in range(num_plots):
try:
if num_plots == 4:
plot = lineplot(class_data, first_digit_4[count], second_digit_4[count],
list_of_lines[count].combo_hue.get(),
list_of_lines[count].combo_first_param.get(),
list_of_lines[count].combo_first_value.get(),
list_of_lines[count].combo_sec_param.get(),
list_of_lines[count].combo_sec_value.get())
elif num_plots == 9:
plot = lineplot(class_data, first_digit_9[count], second_digit_9[count],
list_of_lines[count].combo_hue.get(),
list_of_lines[count].combo_first_param.get(),
list_of_lines[count].combo_first_value.get(),
list_of_lines[count].combo_sec_param.get(),
list_of_lines[count].combo_sec_value.get())
elif num_plots == 16:
plot = lineplot(class_data, first_digit_16[count], second_digit_16[count],
list_of_lines[count].combo_hue.get(),
list_of_lines[count].combo_first_param.get(),
list_of_lines[count].combo_first_value.get(),
list_of_lines[count].combo_sec_param.get(),
list_of_lines[count].combo_sec_value.get())
else:
plot = 0
except (TypeError, KeyError):
pass
if num_plots == 1:
plot = lineplot(class_data, 'None', 'None',
list_of_lines[0].combo_hue.get(),
list_of_lines[0].combo_first_param.get(),
list_of_lines[0].combo_first_value.get(),
list_of_lines[0].combo_sec_param.get(),
list_of_lines[0].combo_sec_value.get())
if kind == 'kde':
for count in range(num_plots):
try:
if num_plots == 4:
plot = kdeplot(class_data, first_digit_4[count], second_digit_4[count],
list_of_lines[count].combo_hue.get(),
list_of_lines[count].combo_first_param.get(),
list_of_lines[count].combo_first_value.get(),
list_of_lines[count].combo_sec_param.get(),
list_of_lines[count].combo_sec_value.get())
elif num_plots == 9:
plot = kdeplot(class_data, first_digit_9[count], second_digit_9[count],
list_of_lines[count].combo_hue.get(),
list_of_lines[count].combo_first_param.get(),
list_of_lines[count].combo_first_value.get(),
list_of_lines[count].combo_sec_param.get(),
list_of_lines[count].combo_sec_value.get())
elif num_plots == 16:
plot = lineplot(class_data, first_digit_16[count], second_digit_16[count],
list_of_lines[count].combo_hue.get(),
list_of_lines[count].combo_first_param.get(),
list_of_lines[count].combo_first_value.get(),
list_of_lines[count].combo_sec_param.get(),
list_of_lines[count].combo_sec_value.get())
else:
plot = 0
except (TypeError, KeyError):
pass
if num_plots == 1:
plot = kdeplot(class_data, 'None', 'None',
list_of_lines[0].combo_hue.get(),
list_of_lines[0].combo_first_param.get(),
list_of_lines[0].combo_first_value.get(),
list_of_lines[0].combo_sec_param.get(),
list_of_lines[0].combo_sec_value.get())
root2 = Toplevel()
canvas = FigureCanvasTkAgg(plot, master=root2)
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1.0)
canvas.draw()
# Saving function
button_save = Button(root, text="save", command=lambda: save_files())
button_save.grid(row=34, column=5)
def save_files():
file_name = filedialog.asksaveasfilename(title="Save File",
filetypes=(("PNG Files", "*.png"), ("All Files", "*.*"))
)
save_plot(file_name, num_plots)
root.mainloop()