-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisPlotAnalysis.py
372 lines (322 loc) · 15.2 KB
/
DisPlotAnalysis.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
360
361
362
363
364
365
366
367
368
369
370
371
372
import matplotlib.pyplot as plt
import pandas as pd
import math
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
from BaseAnalysis import BaseAnalysis
import seaborn as sns
sns.set(style="darkgrid")
import plotly.express as px
import io
from PIL import Image
class DisPlotAnalysis(BaseAnalysis):
def __init__(self, main_app):
super().__init__()
self.main_app = main_app
self.plot_title = tk.StringVar(value = "Dis Plot")
self.title_x_axis = tk.StringVar()
self.title_y_axis = tk.StringVar()
self.plot_with = tk.StringVar()
self.plot_hight = tk.StringVar()
self.last_window = None
self.last_canvas = None
#plot_args
self.kind = tk.StringVar()
self.hue = tk.StringVar()
self.rug = tk.StringVar()
self.row = tk.StringVar()
self.col = tk.StringVar()
def create_plot_args(self, data):
selected_columns = self.main_app.get_selected_columns()
if self.main_app.x_axis_combobox.get() != "":
x_axis = self.main_app.x_axis_combobox.get()
elif len(selected_columns) != 0:
x_axis = selected_columns[0]
y_axis = [col for col in selected_columns if col != x_axis]
if self.main_app.multiplot.get() == False and (len(selected_columns) > 2 or (len(selected_columns) == 2 and self.main_app.x_axis_combobox.get() != "" and self.main_app.x_axis_combobox.get() not in selected_columns)):
all_columns = list(data.columns)
if self.main_app.x_axis_combobox.get() == "":
id_vars = [col for col in all_columns if col not in selected_columns[1:]]
else:
id_vars = [col for col in all_columns if col not in selected_columns or col == x_axis]
melted_df = data.melt(id_vars=id_vars, value_vars=y_axis, var_name='Measurement', value_name='Value')
if self.main_app.use_plotly.get() == False:
plot_args = {"x": x_axis, "y": "Value", "hue": "Measurement", "data": melted_df}
else:
plot_args = {"x": x_axis, "y": "Value", "color": "Measurement", "data_frame": melted_df}
elif (len(selected_columns) == 1 and (self.main_app.x_axis_combobox.get() == "" or selected_columns[0] == self.main_app.x_axis_combobox.get())) or (len(selected_columns) == 0 and self.main_app.x_axis_combobox.get() != ""):
plot_args = {"x": x_axis, "data": data}
elif len(selected_columns) == 0 and self.main_app.x_axis_combobox.get() == "":
plot_args = {"data": data}
else:
plot_args = {"x": x_axis, "y": y_axis[0] if len(y_axis) == 1 else y_axis, "data": data}
if self.main_app.use_plotly.get() == False:
if self.hue.get():
plot_args["hue"] = self.hue.get()
if self.kind.get():
plot_args["kind"] = self.kind.get()
if self.rug.get() == "True":
plot_args["rug"] = self.rug.get()
if self.row.get():
plot_args["row"] = self.row.get()
if self.col.get():
plot_args["col"] = self.col.get()
else:
if "data" in plot_args:
del plot_args["data"]
if "data_frame" not in plot_args:
plot_args["data_frame"] = data
if self.hue.get():
plot_args["color"] = self.hue.get()
if self.rug.get() == "True":
if self.kind.get() == "hist" or self.kind.get() == "ecdf":
plot_args["marginal"] = "rug"
elif self.kind.get() == "kde":
plot_args["marginal_x"] = "rug"
plot_args["marginal_y"] = "rug"
if self.row.get():
plot_args["facet_row"] = self.row.get()
if self.col.get():
plot_args["facet_col"] = self.col.get()
return plot_args
# Create a Seaborn Dis plot
def create_seaborn_plot(self, refresh_plot, plot_args):
if plot_args is None:
selected_columns = self.main_app.get_selected_columns()
if len(selected_columns) > 1 and self.kind.get() == "ecdf":
messagebox.showinfo("Information", "ECDF is not supported for more than one column")
return
plot_args = self.create_plot_args(self.main_app.df)
# Create a Seaborn dis plot
g = sns.displot(**plot_args)
if self.title_x_axis.get() != "":
g.set_axis_labels(x_var=self.title_x_axis.get())
if self.title_y_axis.get() != "":
g.set_axis_labels(y_var=self.title_y_axis.get())
fig = g.fig
if self.plot_with.get() and self.plot_hight.get():
fig.set_size_inches(float(self.plot_with.get()), float(self.plot_hight.get()))
fig.suptitle(self.plot_title.get(), verticalalignment='top', fontsize=12)
fig.subplots_adjust(top=0.94)
# Save the axes and the corresponding facet title
axes_facet_map = {}
for ax in g.axes.flatten():
facet_title = ax.get_title()
axes_facet_map[ax] = facet_title
def on_click(event):
if self.main_app.multiplot.get():
self.main_app.open_windows.append(self.display_plot(event.canvas.figure))
return
ax_clicked = event.inaxes
if ax_clicked in axes_facet_map:
facet_title = axes_facet_map[ax_clicked]
splitted_facets = facet_title.split(" | ")
filtered_data = self.main_app.df.copy()
if splitted_facets != ['']:
for facet in splitted_facets:
column, value = facet.split(" = ")
value = int(value) if value.isdigit() else value
filtered_data = filtered_data[filtered_data[column] == value]
self.show_clicked_plot(False, filtered_data)
# add event-handeler for clicking on facets
if self.main_app.use_plotly.get() == False:
g.fig.canvas.mpl_connect('button_press_event', on_click)
if self.main_app.multiplot.get():
return fig
if refresh_plot:
self.display_refresh_plot(fig)
else:
self.main_app.open_windows.append(self.display_plot(fig))
# Create Plotly plot
def create_plotly_plot(self, refresh_plot):
selected_columns = self.main_app.get_selected_columns()
if len(selected_columns) > 1 and self.kind.get() == "ecdf":
messagebox.showinfo("Information", "ECDF is not supported for more than one column")
return
if len(selected_columns) == 0 and self.kind.get() == "kde":
messagebox.showinfo("Information", "KDE in plotly needs x and y axis")
return
# Create a Plotly dis plot
plot_args = self.create_plot_args(self.main_app.df)
if self.kind.get() == "hist" or self.kind.get() == "":
fig = px.histogram(**plot_args)
elif self.kind.get() == "kde":
fig = px.density_contour(**plot_args)
elif self.kind.get() == "ecdf":
fig = px.ecdf(**plot_args)
if self.plot_title.get():
fig.update_layout(title=self.plot_title.get())
if self.title_x_axis.get():
fig.update_layout(xaxis_title=self.title_x_axis.get())
if self.title_y_axis.get():
fig.update_layout(yaxis_title=self.title_y_axis.get())
fig.update_layout(autosize=True, width=None, height=None)
if self.plot_with.get() and self.plot_hight.get():
fig.update_layout(width=float(self.plot_with.get()), height=float(self.plot_hight.get()))
if refresh_plot:
self.display_refresh_plot(fig)
else:
self.main_app.open_windows.append(self.display_plotly_plot(fig))
# Create a multiplot
def create_multi_plot(self, refresh_plot):
selected_columns = self.main_app.get_selected_columns()
if len(selected_columns) == 0:
selected_columns = list(self.main_app.df.columns)
if self.main_app.x_axis_combobox.get() != "":
selected_columns.remove(self.main_app.x_axis_combobox.get())
if self.main_app.multi_plot_rows_var.get() == 0 and self.main_app.multi_plot_columns_var.get() == 0:
messagebox.showinfo("Information", "Number of rows or columns must be greater than 0")
return
if self.main_app.multi_plot_rows_var.get() != 0 and self.main_app.multi_plot_columns_var.get() != 0 and (self.main_app.multi_plot_rows_var.get() * self.main_app.multi_plot_columns_var.get() < len(selected_columns)):
messagebox.showinfo("Information", "Number of rows times number of columns must be greater than or equal to the number of selected columns")
return
if self.main_app.x_axis_combobox.get() != "" and self.kind.get() == "ecdf":
messagebox.showinfo("Information", "ECDF is not supported for more than one column")
return
# Calculate numer of rows and columns
number_of_plots = len(selected_columns)
if self.main_app.multi_plot_rows_var.get() == 0:
if self.main_app.multi_plot_columns_var.get() >= number_of_plots:
canvas_rows = 1
canvas_columns = number_of_plots
else:
canvas_rows = math.ceil(number_of_plots / self.main_app.multi_plot_columns_var.get())
canvas_columns = self.main_app.multi_plot_columns_var.get()
elif self.main_app.multi_plot_columns_var.get() == 0:
if self.main_app.multi_plot_rows_var.get() >= number_of_plots:
canvas_rows = number_of_plots
canvas_columns = 1
else:
canvas_rows = self.main_app.multi_plot_rows_var.get()
canvas_columns = math.ceil(number_of_plots // self.main_app.multi_plot_rows_var.get())
else:
canvas_rows = self.main_app.multi_plot_rows_var.get()
canvas_columns = self.main_app.multi_plot_columns_var.get()
# Create figs
figures = []
for i in range(len(selected_columns)):
plot_args = self.create_plot_args(self.main_app.df)
if self.main_app.x_axis_combobox.get() == "":
plot_args.pop("y", None)
plot_args["x"] = selected_columns[i]
else:
plot_args["y"] = selected_columns[i]
figures.append(self.create_seaborn_plot(refresh_plot, plot_args))
if refresh_plot:
self.display_refresh_multiple_plots(figures, canvas_rows, canvas_columns)
else:
self.main_app.open_windows.append(self.display_multiple_plots(figures, canvas_rows, canvas_columns))
def show_dis_plot(self, refresh_plot):
if self.main_app.use_plotly.get() == False and self.main_app.multiplot.get() == False:
self.create_seaborn_plot(refresh_plot, None)
elif self.main_app.multiplot.get():
self.create_multi_plot(refresh_plot)
else:
self.create_plotly_plot(refresh_plot)
def show_clicked_plot(self, refresh_plot, filtered_data):
selected_columns = self.main_app.get_selected_columns()
plot_args = self.create_plot_args(filtered_data)
g = sns.displot(**plot_args)
if self.title_x_axis.get() != "":
g.set_axis_labels(x_var=self.title_x_axis.get())
if self.title_y_axis.get() != "":
g.set_axis_labels(y_var=self.title_y_axis.get())
fig = g.fig
if self.plot_with.get() and self.plot_hight.get():
fig.set_size_inches(float(self.plot_with.get()), float(self.plot_hight.get()))
fig.suptitle(self.plot_title.get(), verticalalignment='top', fontsize=12)
fig.subplots_adjust(top=0.94)
if refresh_plot:
self.display_refresh_plot(fig)
else:
self.main_app.open_windows.append(self.display_plot(fig))
def init_ui(self):
self.plot_arguments_frame_visible = True
# Remove previously created widgets in the parent_frame
for widget in self.main_app.displot_tab_frame.winfo_children():
widget.destroy()
# Frame for buttons
button_frame = tk.Frame(self.main_app.displot_tab_frame)
button_frame.pack(padx=5, pady=5, anchor='c')
# Button to show Dis Plot in new window
show_plot_button = tk.Button(button_frame, text="Show Plot", command=lambda: self.show_dis_plot(False))
show_plot_button.grid(row=0, column=0, padx=5)
# Button to refresh Dis Plot in same window
show_plot_button = tk.Button(button_frame, text="Refresh Plot", command=lambda: self.show_dis_plot(True))
show_plot_button.grid(row=0, column=1, padx=5)
# Button to show and hide more plot aguments
self.toggle_arguments_button = tk.Button(button_frame, text="Hide Arguments", command=self.toggle_plot_arguments_frame)
self.toggle_arguments_button.grid(row=0, column=2, padx=5)
# Frame for main plot args
main_plot_arguments_frame = tk.Frame(self.main_app.displot_tab_frame)
main_plot_arguments_frame.pack(padx=5, pady=5, anchor='w')
# Entry for plot title
title_label = tk.Label(main_plot_arguments_frame, text="Plot title:")
title_label.grid(row=0, column=0, padx=5)
title_entry = tk.Entry(main_plot_arguments_frame, textvariable=self.plot_title)
title_entry.grid(row=0, column=1)
# Entry for x-axis label
x_axis_label = tk.Label(main_plot_arguments_frame, text="x-axis title:")
x_axis_label.grid(row=1, column=0, padx=5)
x_axis_entry = tk.Entry(main_plot_arguments_frame, textvariable=self.title_x_axis)
x_axis_entry.grid(row=1, column=1, padx=5)
# Entry for y-axis label
y_axis_label = tk.Label(main_plot_arguments_frame, text="y-axis title:")
y_axis_label.grid(row=2, column=0, padx=5)
y_axis_entry = tk.Entry(main_plot_arguments_frame, textvariable=self.title_y_axis)
y_axis_entry.grid(row=2, column=1, padx=5)
# Entry for plot with
plot_with_label = tk.Label(main_plot_arguments_frame, text="plot with:")
plot_with_label.grid(row=3, column=0, padx=5)
plot_with_entry = tk.Entry(main_plot_arguments_frame, textvariable=self.plot_with)
plot_with_entry.grid(row=3, column=1, padx=5)
# Entry for plot hight
plot_hight_label = tk.Label(main_plot_arguments_frame, text="plot hight:")
plot_hight_label.grid(row=4, column=0, padx=5)
plot_hight_entry = tk.Entry(main_plot_arguments_frame, textvariable=self.plot_hight)
plot_hight_entry.grid(row=4, column=1, padx=5)
# Frame for plot arguments
self.plot_arguments_frame = tk.Frame(self.main_app.displot_tab_frame)
self.plot_arguments_frame.pack(padx=5, pady=5, fill='x')
# Kind
self.kind_label = tk.Label(self.plot_arguments_frame, text="Kind:")
self.kind_label.grid(row=0, column=0, padx=5, pady=5, sticky='w')
self.kind_combobox = ttk.Combobox(self.plot_arguments_frame, textvariable=self.kind)
self.kind_combobox.grid(row=0, column=1, padx=5, pady=5, sticky='w')
# Hue
self.hue_label = tk.Label(self.plot_arguments_frame, text="Hue:")
self.hue_label.grid(row=1, column=0, padx=5, pady=5, sticky='w')
self.hue_combobox = ttk.Combobox(self.plot_arguments_frame, textvariable=self.hue)
self.hue_combobox.grid(row=1, column=1, padx=5, pady=5, sticky='w')
# Rug
self.rug_label = tk.Label(self.plot_arguments_frame, text="Rug:")
self.rug_label.grid(row=2, column=0, padx=5, pady=5, sticky='w')
self.rug_combobox = ttk.Combobox(self.plot_arguments_frame, textvariable=self.rug)
self.rug_combobox.grid(row=2, column=1, padx=5, pady=5, sticky='w')
# Row
self.row_label = tk.Label(self.plot_arguments_frame, text="Row:")
self.row_label.grid(row=3, column=0, padx=5, pady=5, sticky='w')
self.row_combobox = ttk.Combobox(self.plot_arguments_frame, textvariable=self.row)
self.row_combobox.grid(row=3, column=1, padx=5, pady=5, sticky='w')
# Col
self.col_label = tk.Label(self.plot_arguments_frame, text="Col:")
self.col_label.grid(row=4, column=0, padx=5, pady=5, sticky='w')
self.col_combobox = ttk.Combobox(self.plot_arguments_frame, textvariable=self.col)
self.col_combobox.grid(row=4, column=1, padx=5, pady=5, sticky='w')
self.load_argument_values()
def load_argument_values(self):
if self.main_app.df is not None:
self.main_app.displot_analysis.kind_combobox['values'] = ['hist', 'kde', 'ecdf']
self.main_app.displot_analysis.hue_combobox['values'] = list(self.main_app.df.columns)
self.main_app.displot_analysis.rug_combobox['values'] = ['True', 'False']
self.main_app.displot_analysis.row_combobox['values'] = list(self.main_app.df.columns)
self.main_app.displot_analysis.col_combobox['values'] = list(self.main_app.df.columns)
def toggle_plot_arguments_frame(self):
if self.plot_arguments_frame_visible:
self.plot_arguments_frame.pack_forget()
self.toggle_arguments_button.config(text="Show Plot Arguments")
else:
self.plot_arguments_frame.pack(padx=5, pady=5, fill='x')
self.toggle_arguments_button.config(text="Hide Plot Arguments")
self.plot_arguments_frame_visible = not self.plot_arguments_frame_visible