-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·232 lines (186 loc) · 8.49 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
"""test docstring please ignore"""
import csv
import tkinter
from tkinter import messagebox
from storedData import StoredData
from plotGUI import PlotGUI
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from tkinter import filedialog
from utils import Callbacks
from pathlib import Path
from plot_op import PlotOp
class GUIHandler:
root = None # instance of Tk class
cb = None # instance of Callback class
csv_was_called = None # bool
voltage_list = None # list
current_list = None # list
data_store = None # instance of StoredData class
plot = None # instance of PlotGUI class
open_prob_plot = None # another instance of PlotGUI class
open_prob_figure = None
open_prob_canvas = None
# testing these as global and sending them to plotgui
# leftEntry = None
# rightEntry = None
def __init__(self):
self.cb = Callbacks()
self.csv_was_called = False
self.voltage_list = []
self.current_list = []
self.root = tkinter.Tk()
self.root.wm_title("Open Probability of Ion Channels")
# create figure inside tkinter window and create axes that all plots can use
fig = Figure(figsize=(5, 4), dpi=100)
canvas = FigureCanvasTkAgg(fig, master=self.root) # A tk.DrawingArea.
# create our own toolbar, then remove all the buttons from it / gui objects from it
toolbar = NavigationToolbar2Tk(canvas, self.root, pack_toolbar=False)
for item in toolbar.children:
if type(toolbar.children[item]) in (tkinter.Button, tkinter.Frame, tkinter.Checkbutton):
toolbar.children[item].pack_forget()
toolbar.update()
self.open_prob_figure = Figure(figsize=(5, 4), dpi=100)
self.open_prob_canvas = FigureCanvasTkAgg(self.open_prob_figure, master=self.root)
# open_prob_toolbar = NavigationToolbar2Tk(self.open_prob_canvas, self.root, pack_toolbar=False)
# open_prob_toolbar.update()
# bottom frame
frame = tkinter.Frame(self.root)
self.leftEntry = tkinter.Entry(frame, width=10, borderwidth=2)
self.leftEntry.insert(0, '')
self.leftEntry.pack(side=tkinter.LEFT)
self.rightEntry = tkinter.Entry(frame, text='right', width=10, borderwidth=2)
self.rightEntry.insert(0, '')
self.rightEntry.pack(side=tkinter.LEFT)
range_button = tkinter.Button(frame, text="Update Range",
command=self.update_button_press)
range_button.pack(side=tkinter.RIGHT)
frame.pack(side=tkinter.BOTTOM)
# Packing order is important. Widgets are processed sequentially and if there
# is no space left, because the window is too small, they are not displayed.
# The canvas is rather flexible in its size, so we pack it last which makes
# sure the UI controls are displayed as long as possible.
toolbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
canvas.get_tk_widget().pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=1)
# toolbar2.pack(side=tkinter.BOTTOM, fill=tkinter.X)
self.open_prob_canvas.get_tk_widget().pack(side=tkinter.RIGHT, fill=tkinter.BOTH, expand=1)
self.root.after(100, self.update_graph, fig, canvas)
# generate empty plot, but keep bool to save on CPU usage
self.csv_was_called = True
# must call update_graph() before update_op_graph()
self.update_graph(fig, canvas)
self.plot.plot_data(fig, canvas, self.root)
self.update_op_graph(self.open_prob_figure, self.open_prob_canvas)
self.open_prob_plot.plot_data(self.open_prob_figure, self.open_prob_canvas, self.root)
menu_bar = tkinter.Menu(self.root)
file_menu = tkinter.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Import", command=self.browse_files)
file_menu.add_command(label="Export data", command=self.save)
file_menu.add_command(label="Exit", command=self.root.quit)
menu_bar.add_cascade(label="File", menu=file_menu)
self.root.config(menu=menu_bar)
self.root.mainloop()
# data_file.close()
# function to save currently plotted graph to a new csv file
def save(self):
# gives user a choice between saving as .csv file or .txt file
file_name = filedialog.asksaveasfile(defaultextension='.csv',
filetypes=[("CSV file (.csv)", ".csv"), ("Text file (.txt)", ".txt")])
if file_name is not None:
file = open(file_name.name, 'w', newline='')
regr_data = self.data_store.get_regression_data()
# file_data = [regr_data['x'], regr_data['y']]
x = regr_data['x']
y = regr_data['y']
op_data = self.data_store.get_op_for_saving()
file_writer = csv.writer(file, delimiter=',')
#for w in regr_data['x']:
# file_writer = csv.writer(file, delimiter=',', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
file_writer.writerows(zip(x, y, op_data))
file.close()
def read_csv(self):
"""
Reads a .csv file that the user selected in browse_files().
Checks if browse_files() was called (since this function is polled)
Then, checks if the file exists.
If the file exists, it reads in the data and stores it in memory in an instance of a StoredData class
TODO: make sure the the file is a valid csv file
"""
file_str = self.cb.get_file()
file_path = Path(file_str)
if file_path.is_file():
file = open(file_str, 'r', newline='', encoding='utf-8-sig')
raw_data = csv.reader(file)
self.__store_data_from_csv(raw_data)
file.close()
self.csv_was_called = True
# Function for opening the file explorer window
def browse_files(self):
self.cb.browse_files()
self.read_csv()
def update_graph(self, fig, canvas):
self.root.after(100, self.update_graph, fig, canvas)
if not self.csv_was_called:
return
self.data_store = StoredData(self.voltage_list, self.current_list)
self.plot = PlotGUI(self.data_store, self.leftEntry, self.rightEntry)
self.plot.plot_data(fig, canvas, self.root)
self.plot.update_textbox()
def update_op_graph(self, fig, canvas):
self.root.after(201, self.update_op_graph, fig, canvas)
if not self.csv_was_called:
return
self.open_prob_plot = PlotOp(self.data_store)
self.open_prob_plot.plot_data(fig, canvas, self.root)
self.csv_was_called = False
self.data_store.get_op_for_saving()
def update_button_press(self):
min_val = self.leftEntry.get()
max_val = self.rightEntry.get()
try:
min_val = float(min_val)
self.plot.update_from_textbox("min", min_val)
except ValueError:
print("min input not a float")
try:
max_val = float(max_val)
self.plot.update_from_textbox("max", max_val)
except ValueError:
print("max input not a float")
self.update_op_graph(self.open_prob_figure, self.open_prob_canvas)
self.open_prob_plot.plot_data(self.open_prob_figure, self.open_prob_canvas, self.root)
def __store_data_from_csv(self, data):
self.voltage_list = []
self.current_list = []
current_row = 1
valid_file = True
msg = None
label = None
for row in data:
if len(row) < 2:
valid_file = False
label = "Data missing in CSV"
msg = "Missing data on row {}".format(current_row)
break
try:
float(row[0])
float(row[1])
except ValueError:
valid_file = False
label = "Invalid data"
msg = "Data in row {} does not appear to be a floating point number".format(
current_row)
break
self.voltage_list.append(float(row[0]))
self.current_list.append(float(row[1]) / 1000.0)
current_row = current_row + 1
if not valid_file:
self.voltage_list = []
self.current_list = []
messagebox.showerror(label, msg)
self.root.update()
if __name__ == "__main__":
gui = GUIHandler()