-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
369 lines (290 loc) · 10.2 KB
/
main.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
import tkinter
from tkinter import *
from tkinter.messagebox import *
from tkinter import filedialog as fd
import csv
# graphing imports
# import matplotlib as mpl
# mpl.rcParams['toolbar'] = 'None' # remove navbar
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import numpy as np
# set default figure size to fit on window
plt.rcParams["figure.figsize"] = (10, 7)
frame_config = {
"relief": GROOVE,
}
file_entry_config = {
"width": 30,
"relief": RAISED,
"font": ('Verdana', 10)
}
# plot option buttons
plot_buttons = ["PLOT RAW", "PLOT FILTERED", "COMBINE", "CLR"]
plot_btns_config = {
"relief": RAISED,
"font": ("Verdana", 10, "bold")
}
# file operations variables
# functions
def open_raw_file():
"""
open raw data file, csv, split it plot
:return:
"""
file_types = (
('csv files', '*.csv'),
('text files', '*.txt')
)
raw_data_file = fd.askopenfilename(
title='Choose file',
filetypes=file_types
)
# update GUI
raw_file_entry.delete(0, END)
raw_file_entry.insert(0, raw_data_file)
# show file opening operations
raw_file_open_status = Label(info_frame, text="[ ] Raw file loaded. Ready to plot...")
raw_file_open_status.pack(anchor="w")
def open_filtered_file():
"""
open filtered data file, csv
:return:
"""
file_types = (
('csv files', '*.csv'),
('text files', '*.txt')
)
raw_data_file = fd.askopenfilename(
title='Choose file',
filetypes=file_types
)
# update GUI
filter_file_entry.delete(0, END)
filter_file_entry.insert(0, raw_data_file)
# show file opening operations
filtered_file_open_status = Label(info_frame, text="[ ] Filtered file loaded. Ready to plot...")
filtered_file_open_status.pack(anchor="w")
def show_message(title, message):
"""
Show app messages
:return:
"""
showerror(title=title, message=message)
def plot_raw():
"""
plot raw data
:return:
"""
draw_plot(filtered=False)
def plot_filtered_data():
"""
plot filtered data
:return:
"""
draw_plot(filtered=True, combine=False)
def plot_raw_and_filtered():
"""
combine raw data and filtered on the same plot
:return:
"""
draw_plot(filtered=False, combine=True)
def draw_plot(filtered=False, combine=False):
"""
draw a plot
:param filtered:
:param combine:
:return:
"""
# acceleration values
x_acc = []
y_acc = []
z_acc = []
# filtered acceleration values
x_acc_filtered = []
y_acc_filtered = []
z_acc_filtered = []
# plot raw data
if filtered is False and combine is False:
raw_data_file = raw_file_entry.get()
if len(raw_data_file) == 0:
show_message("Error", "Please select a file")
# get the raw_data csv file and split into x,y,z
with open(raw_data_file, 'r') as file:
reader = csv.reader(file, delimiter=",")
for row in reader:
# split into x, y, z lists
x_acc.append(float(row[0]))
y_acc.append(float(row[1]))
z_acc.append(float(row[2]))
# plot x, y, z
fig, ax = plt.subplots()
ax.grid()
# plot the data
ax.plot(x_acc, label="x_acceleration")
ax.plot(y_acc, label="y_acceleration")
ax.plot(z_acc, label="z_acceleration")
# set legends
ax.legend()
# set axes title
ax.set_title("Raw acceleration data")
# set x and y labels
ax.set_ylabel("Acceleration (m/s^2)")
ax.set_xlabel("Time (s)")
# create a tk drawing area
canvas = FigureCanvasTkAgg(fig, master=graph_frame)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=True)
# toolbar = NavigationToolbar2Tk(canvas, root)
# toolbar.update()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=True)
# plot filtered data
elif filtered is True and combine is False:
# check if filtered data file is selected
filtered_data_file = filter_file_entry.get()
# error on empty select box
if len(filtered_data_file) == 0:
show_message("Error", "Please select a file")
# open file
with open(filtered_data_file, 'r') as file:
reader = csv.reader(file, delimiter=",")
for row in reader:
# split into x, y, z lists
x_acc_filtered.append(float(row[0]))
y_acc_filtered.append(float(row[1]))
z_acc_filtered.append(float(row[2]))
# plot x, y, z
# create the plot canvas
fig, ax = plt.subplots()
ax.grid()
# plot the data
ax.plot(x_acc_filtered, label="filtered_x_acceleration")
ax.plot(y_acc_filtered, label="filtered_y_acceleration")
ax.plot(z_acc_filtered, label="filtered_z_acceleration")
# set legends
ax.legend()
# set axes title
ax.set_title("Filtered acceleration data")
# set x and y labels
ax.set_ylabel("Acceleration (m/s^2)")
ax.set_xlabel("Time (s)")
# create a tk drawing area
canvas = FigureCanvasTkAgg(fig, master=graph_frame)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=True)
# toolbar = NavigationToolbar2Tk(canvas, root)
# toolbar.update()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=True)
# plot combined data
elif filtered is False and combine is True:
# delete the previous canvas
filtered_data_file = filter_file_entry.get()
raw_data_file = raw_file_entry.get()
if len(filtered_data_file) == 0 or len(raw_data_file) == 0:
show_message("Error", "Please select a file")
# open raw data file
with open(raw_data_file, 'r') as file:
reader = csv.reader(file, delimiter=",")
for row in reader:
# split into x, y, z lists
x_acc.append(float(row[0]))
y_acc.append(float(row[1]))
z_acc.append(float(row[2]))
# open filtered data file
with open(filtered_data_file, 'r') as file:
reader = csv.reader(file, delimiter=",")
for row in reader:
# split into x, y, z lists
x_acc_filtered.append(float(row[0]))
y_acc_filtered.append(float(row[1]))
z_acc_filtered.append(float(row[2]))
# plot x, y, z
fig, ax = plt.subplots()
ax.grid()
# plot the raw data
ax.plot(x_acc, label="x_acceleration")
ax.plot(y_acc, label="y_acceleration")
ax.plot(z_acc, label="z_acceleration")
# plot the filtered data
ax.plot(x_acc_filtered, label="filtered_x_acceleration")
ax.plot(y_acc_filtered, label="filtered_y_acceleration")
ax.plot(z_acc_filtered, label="filtered_z_acceleration")
# set legends
ax.legend()
# set axes title
ax.set_title("Acceleration data")
# set x and y labels
ax.set_ylabel("Acceleration (m/s^2)")
ax.set_xlabel("Time (s)")
# create a tk drawing area
canvas = FigureCanvasTkAgg(fig, master=graph_frame)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=True)
# toolbar = NavigationToolbar2Tk(canvas, root)
# toolbar.update()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=True)
def clear_plots():
"""
clear the file entry boxes
:return:
"""
raw_file_entry.delete(0, END)
filter_file_entry.delete(0, END)
# delete the graph figure
fig.clf()
"""
Initialize widgets for the app
"""
root = Tk()
root.geometry('1400x700')
root.title("Plottar")
# parent frame
root_frame = LabelFrame(root)
root_frame.pack(side="left", fill=Y, ipadx=10, ipady=20, pady=10)
graph_frame = LabelFrame(root)
graph_frame.pack(side="left", expand=True)
# create file select frame
file_frame = LabelFrame(root_frame, text="Select files")
file_frame.pack(ipadx=10, side="top", pady=15)
raw_file_frame = Frame(file_frame)
raw_file_frame.pack(side="top")
raw_file_label = Label(raw_file_frame, text="Raw file")
raw_file_label.pack(side="left")
raw_file_entry = Entry(raw_file_frame, file_entry_config)
raw_file_entry.pack(side="left")
raw_file_select_btn = Button(raw_file_frame, text="...", command=open_raw_file)
raw_file_select_btn.pack(side="left")
filter_file_frame = Frame(file_frame)
filter_file_frame.pack(side="top")
filter_file_label = Label(filter_file_frame, text="Filter file")
filter_file_label.pack(side="left")
filter_file_entry = Entry(filter_file_frame, file_entry_config)
filter_file_entry.pack(side="left")
filter_file_select_btn = Button(filter_file_frame, text="...", command=open_filtered_file)
filter_file_select_btn.pack(side="left")
# create plotting buttons
plot_btn_frame = LabelFrame(root_frame, frame_config)
plot_btn_frame.pack(side="top", pady=15, padx=15, fill=X, ipady=5)
plot_raw_btn = Button(plot_btn_frame, plot_btns_config, text=plot_buttons[0], command=plot_raw)
plot_raw_btn.pack(side="left", expand=True)
plot_filtered_btn = Button(plot_btn_frame, plot_btns_config, text=plot_buttons[1], command=plot_filtered_data)
plot_filtered_btn.pack(side="left", expand=True)
combine_plots_btn = Button(plot_btn_frame, plot_btns_config, text=plot_buttons[2], command=plot_raw_and_filtered)
combine_plots_btn.pack(side="left", expand=True)
clear_plots_btn = Button(plot_btn_frame, plot_btns_config, text=plot_buttons[3], command=clear_plots)
clear_plots_btn.pack(side="left", expand=True)
# create a tk drawing area
# fig, ax = plt.subplots()
# plot dummy data
# ax.plot([1,2,3,4,1,2,3,8,1])
# pcanvas = FigureCanvasTkAgg(fig, master=graph_frame)
# pcanvas.draw()
# pcanvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=True)
# # toolbar = NavigationToolbar2Tk(canvas, root)
# # toolbar.update()
# pcanvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=True)
# info frame
info_frame = LabelFrame(root_frame, frame_config, text="Info")
info_frame.pack(side="left", anchor="w", padx=15, pady=15, expand=True, fill=BOTH)
mainloop()