-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern_finder.py
401 lines (323 loc) · 12.9 KB
/
pattern_finder.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
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
import itertools
from pathlib import Path
import tkinter as tk
from tkinter import ttk
from tkinter import *
from PIL import ImageTk, Image
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn.manifold import TSNE
import numpy as np
import pandas
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import main
class ChooseVisual(tk.Frame):
def __init__(self, parent, container, filename):
super().__init__(container)
self.data = prepare_data(filename)
self.name = Path(filename).stem
# Back button
self.back_button = ttk.Button(
self,
text="Back To Home",
command=lambda: parent.show_frame(main.HomePage, "", "normal")
)
# Title label
self.title_frame = tk.Frame(self)
title = ttk.Label(
self.title_frame,
text='Choose Graph Type',
font=("Helvetica", 25),
)
title.pack(expand=True)
# Menu left
self.buttons = tk.Frame(self, width=200)
selected = {'fg': '#F0F0F0', 'bg': 'RoyalBlue3', 'activebackground':
'gray71', 'activeforeground': 'gray71'}
unselected = {'fg': 'black', 'bg': '#F0F0F0', 'activebackground':
'#F0F0F0', 'activeforeground': '#F0F0F0'}
bnt_list = []
# Correlation button
corr = Button(
self.buttons,
text='Correlation Heat Map',
command=lambda: [update(corr)]
)
corr.pack(expand=True)
bnt_list.append(corr)
# PCA 2D button
pca_2d = Button(
self.buttons,
text='2D PCA',
command=lambda: [update(pca_2d)]
)
pca_2d.pack(expand=True)
bnt_list.append(pca_2d)
# PCA 3D button
pca_3d = Button(
self.buttons,
text='3D PCA',
command=lambda: [update(pca_3d)]
)
pca_3d.pack(expand=True)
bnt_list.append(pca_3d)
# KMeans button
kmeans = Button(
self.buttons,
text='KMeans',
command=lambda: [update(kmeans)]
)
kmeans.pack(expand=True)
bnt_list.append(kmeans)
# t-SNE button
tsne = Button(
self.buttons,
text='t-SNE',
command=lambda: [update(tsne)]
)
tsne.pack(expand=True)
bnt_list.append(tsne)
# image
image = Image.open("Graphs/1-boxplot-dark.png")
photo = ImageTk.PhotoImage(image)
self.canvas_area = tk.Frame(self)
self.canvas_area.grid(row=1)
'''
image = Image.open("Graphs/1-boxplot-dark.png")
photo = ImageTk.PhotoImage(image)
canvas_area = Label(self, image=photo)
canvas_area.image = photo
canvas_area.grid(row=1)
'''
# save
self.save_button = Button(self, text="Save Image")
self.back_button.grid(row=0, column=0, sticky="nsew")
self.title_frame.grid(row=0, column=1, columnspan=2, pady=40, sticky="nsew")
self.buttons.grid(row=1, column=0, padx=30, pady=30, rowspan=2, sticky="nsew")
self.canvas_area.grid(row=1, column=1, sticky="nsew")
self.save_button.grid(row=2, column=1, sticky="nsew")
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
# Change color of button to indicate which graph is being shown and clear canvas
def update(clicked):
"""Update the canvas"""
for graph in self.canvas_area.winfo_children():
graph.destroy()
for button in bnt_list:
if button == clicked:
button.configure(**selected)
if bnt_list.index(clicked) == 0:
graph = FigureCanvasTkAgg(correlation(self.data, self.name), self.canvas_area)
elif bnt_list.index(clicked) == 1:
graph = FigureCanvasTkAgg(pca_2d_graph(self.data, self.name), self.canvas_area)
elif bnt_list.index(clicked) == 2:
graph = FigureCanvasTkAgg(pca_3d_graph(self.data, self.name), self.canvas_area)
elif bnt_list.index(clicked) == 3:
graph = FigureCanvasTkAgg(kmeans_graph(self.data, self.name), self.canvas_area)
elif bnt_list.index(clicked) == 4:
graph = FigureCanvasTkAgg(tsne_graph(self.data, self.name), self.canvas_area)
graph.get_tk_widget().pack(side="top", fill="both", expand=True)
else:
button.configure(**unselected)
class ChangeVisualiser(tk.Frame):
def __init__(self, parent, container):
super().__init__(container)
# Back button
self.back_button = ttk.Button(
self,
text="Back To Home",
command=lambda: parent.show_frame(main.HomePage, "", "normal")
)
# Title label
self.title_frame = tk.Frame(self)
title = ttk.Label(
self.title_frame,
text='Visualise Changes',
font=("Helvetica", 25),
)
title.pack(expand=True)
# Menu left
self.buttons = tk.Frame(self, width=200)
selected = {'fg': '#F0F0F0', 'bg': 'RoyalBlue3', 'activebackground':
'gray71', 'activeforeground': 'gray71'}
unselected = {'fg': 'black', 'bg': '#F0F0F0', 'activebackground':
'#F0F0F0', 'activeforeground': '#F0F0F0'}
bnt_list = []
# PCA 2D button
pca_2d = Button(
self.buttons,
text='2D PCA',
command=lambda: [update(pca_2d)]
)
pca_2d.pack(expand=True)
bnt_list.append(pca_2d)
# PCA 3D button
pca_3d = Button(
self.buttons,
text='3D PCA',
command=lambda: [update(pca_3d)]
)
pca_3d.pack(expand=True)
bnt_list.append(pca_3d)
# KMeans button
kmeans = Button(
self.buttons,
text='KMeans',
command=lambda: [update(kmeans)]
)
kmeans.pack(expand=True)
bnt_list.append(kmeans)
# t-SNE button
tsne = Button(
self.buttons,
text='t-SNE',
command=lambda: [update(tsne)]
)
tsne.pack(expand=True)
bnt_list.append(tsne)
# middle area
image = Image.open("Graphs/1-boxplot-dark.png")
photo = ImageTk.PhotoImage(image)
self.canvas_area = Label(self, image=photo)
self.canvas_area.image = photo
self.canvas_area.grid(row=1)
# right area
self.dropdowns = tk.Frame(self, width=200)
self.categories = ('Demographics', 'Dietary', 'Examination', 'Laboratory', 'Questionnaire')
self.clicked_category = tk.StringVar(self)
self.category = ttk.OptionMenu(self.dropdowns, self.clicked_category, self.categories[0], *self.categories)
self.category.pack(expand=True)
self.types = ("DEMO", 'Demographics', 'Dietary', 'Examination', 'Laboratory', 'Questionnaire')
self.clicked_type = tk.StringVar(self)
self.type = ttk.OptionMenu(self.dropdowns, self.clicked_type, self.types[0], *self.types)
self.type.pack(expand=True)
# year slider
self.slider = Scale(self, from_=1999, to=2015, tickinterval=2, orient=HORIZONTAL)
# save
self.save_button = Button(self, text="Save Image")
self.back_button.grid(row=0, column=0, sticky="nsew")
self.title_frame.grid(row=0, column=1, columnspan=2, pady=40, sticky="nsew")
self.buttons.grid(row=1, column=0, padx=30, pady=30, rowspan=2, sticky="nsew")
self.canvas_area.grid(row=1, column=1, rowspan=2, sticky="nsew")
self.dropdowns.grid(row=1, column=2, padx=30, pady=30, rowspan=2, sticky="nsew")
self.slider.grid(row=2, column=1, padx=20, pady=40, sticky="ew")
self.save_button.grid(row=3, column=1, sticky="nsew")
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
# Change color of button to indicate which graph is being shown and clear canvas
def update(clicked):
"""Update the canvas"""
for graph in self.canvas_area.winfo_children():
graph.destroy()
for button in bnt_list:
if button == clicked:
button.configure(**selected)
if bnt_list.index(clicked) == 0:
graph = FigureCanvasTkAgg(pca_2d_graph(self.data), self.canvas_area)
elif bnt_list.index(clicked) == 1:
graph = FigureCanvasTkAgg(pca_3d_graph(self.data), self.canvas_area)
elif bnt_list.index(clicked) == 2:
graph = FigureCanvasTkAgg(kmeans_graph(self.data), self.canvas_area)
elif bnt_list.index(clicked) == 3:
graph = FigureCanvasTkAgg(tsne_graph(self.data), self.canvas_area)
graph.get_tk_widget().pack(side="top", fill="both", expand=True)
else:
button.configure(**unselected)
def prepare_data(filename):
"""Return the dataframe"""
data_frame = pandas.read_sas(filename)
data_frame.replace([np.inf, -np.inf], np.nan, inplace=True)
data_frame.fillna(data_frame.mean(), inplace=True)
return data_frame
# Method that returns a heatmap showing correlations
def correlation(df, filename):
"""Return the correlation heat map"""
# Plotting the graph
heat_map = plt.figure(figsize=(12, 10))
cor = df.corr()
sns.heatmap(cor, annot=False, cmap=plt.cm.Reds)
# Labelling graph
plt.title('Correlation Heat Map Of ' + filename, fontsize=20)
plt.xlabel("Variables", fontsize=14)
plt.ylabel("Variables", fontsize=14)
return heat_map
# Method that returns a PCA 2D graph
def pca_2d_graph(df, filename):
"""Return the PCA 2D graph"""
# Create color dataframe and inspect first 5 rows with head()
color_features = []
for i in df.columns:
if 'color' in i:
color_features.append(i)
data_color = df[color_features]
data_color.head()
# Perform PCA with 2 principal components
pca = PCA(n_components=2)
X = pca.fit_transform(df)
# Plotting the graph
pca_2d_graph = plt.figure(figsize=(7, 7))
plt.scatter(X[:, 0], X[:, 1], alpha=0.1)
# Label graph
plt.title('PCA 2D Projection Of ' + filename, fontsize=20)
plt.xlabel("First Principal Component", fontsize=14)
plt.ylabel("Second Principal Component", fontsize=14)
return pca_2d_graph
# Method that returns a PCA 3D graph
def pca_3d_graph(df, filename):
"""Return the PCA 3D graph"""
sns.set_style("white")
# Perform PCA with 3 principle components
pca = PCA(n_components=3)
pca.fit(df)
# Store results of PCA in a data frame
result = pandas.DataFrame(pca.transform(df), columns=['PCA%i' % i for i in range(3)], index=df.index)
# Plotting the graph
pca_3d_graph = plt.figure()
ax = pca_3d_graph.add_subplot(111, projection='3d')
ax.scatter(result['PCA0'], result['PCA1'], result['PCA2'], cmap="Set2_r", s=60)
# Label graph
plt.title('PCA 3D Projection Of ' + filename, fontsize=20)
ax.set_xlabel("First Principal Component")
ax.set_ylabel("Second Principal Component")
ax.set_zlabel("Third Principal Component")
return pca_3d_graph
# Method that returns a K-means graph
def kmeans_graph(df, filename):
"""Return the K-means graph"""
# Perform PCA with 2 principle components
pca = PCA(n_components=2)
data = pca.fit_transform(df)
# Set the number of clusters & predict them
kmeans = KMeans(n_clusters=10)
label = kmeans.fit_predict(data)
# Get unique labels
u_labels = np.unique(label)
# Plotting the graph
kmeans_graph = plt.figure(figsize=(12, 10))
for i in u_labels:
plt.scatter(data[label == i, 0], data[label == i, 1], label="Cluster " + str(i))
# Label graph
plt.legend()
plt.title('KMeans Projection Of ' + filename, fontsize=20)
plt.xlabel("First Principal Component", fontsize=14)
plt.ylabel("Second Principal Component", fontsize=14)
return kmeans_graph
# Method that returns a t-SNE graph
def tsne_graph(df, filename):
"""Return the t-SNE graph"""
# Perform t-SNE
tsne = TSNE(random_state=17)
X_tsne = tsne.fit_transform(df)
# Plotting the graph
tsne_graph = plt.figure(figsize=(12, 10))
plt.scatter(X_tsne[:, 0], X_tsne[:, 1],
edgecolor='none', alpha=0.7, s=40,
cmap=plt.cm.get_cmap('nipy_spectral', 10))
plt.colorbar()
# Label graph
plt.title('t-SNE Projection Of ' + filename, fontsize=20)
plt.xlabel("t-SNE-x", fontsize=14)
plt.ylabel("t-SNE-y", fontsize=14)
return tsne_graph