-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathverifyTk.py
285 lines (214 loc) · 11.6 KB
/
verifyTk.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
#! /usr/bin/python
from os import system
import sys
import platform
import file_tools
from trace_tools import decimate_traces
from Plot import *
from noise import *
from datetime import datetime
if sys.version_info[0] < 3:
from Tkinter import *
from ttk import Separator, Radiobutton, Style
import tkFileDialog
#print (str(sys.version_info) +" Tkinter")
else:
from tkinter import *
from tkinter.ttk import Separator, Radiobutton, Style
from tkinter import filedialog as tkFileDialog
__author__="Andrew"
__date__ ="$07-Nov-2018$"
class verifyGUI:
introduction ="""
---- Verify fluctuations from ion channel noise in difference records ----\n
according to expectation of not exceeding 7 SD\
(from Heinemann and Conti 'Methods in Enzymology' 207).
Takes input from tab-delimited Excel file 'file.txt' with columns being current traces.
* Mean and variance are computed for the set to use in noise limit test.
* Baseline noise is determined for each difference trace from the first hundred points (2 ms at 50 kHz)
* Traces with extremely noisy baselines are removed.
* Traces that exceed the 7 SD limit are removed and the failing points are explicitly listed to the terminal.
Output is tab delimited text file with columns consisting of mean, variance and verified difference traces, default to 'verified.txt'
"""
def __init__(self, master):
plat = platform.system()
if plat == 'Darwin':
try:
system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
print ("Trying to force Python window to the front on macOS")
except:
system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python2.7" to true' ''')
print ("Trying to force Python 2.7 window to the front on macOS")
self.master = master
frame = Frame(master)
frame.config(background="#dcdcdc")
frame.config(borderwidth=5, relief=GROOVE)
frame.place(relx=0.5, rely=0.5, anchor=CENTER)
master.title('Verify v. 0.2') # Main frame title
master.config(background="#dcdcdc")
master.geometry('820x750')
menubar = Menu(master)
"""statmenu = Menu(menubar,tearoff=0)
statmenu.add_command(label="Fieller", command=self.on_fieller)
statmenu.rantest = Menu(statmenu)
statmenu.rantest.add_command(label="Continuously variable data", command=self.on_rantest_continuous)
statmenu.rantest.add_command(label="Binomial data (each result= yes or no)", command=self.on_rantest_binomial)
statmenu.add_cascade(label="Randomisation test", menu=statmenu.rantest)
statmenu.add_command(label="Help", command=self.on_help, state=DISABLED)
statmenu.add_command(label="Quit", command=master.quit)
menubar.add_cascade(label="Statistical Tests", menu=statmenu)
master.config(menu=menubar)
"""
b3 = Button(frame, text="Load traces", width=20, command=self.callback3, highlightbackground="#dcdcdc")
b3.grid(row=0, column=0, columnspan=2, padx=10, pady=8, sticky=W)
self.b4 = Button(frame, text="Verify traces variance", width=20, state=DISABLED, command=self.callback2, highlightbackground="#dcdcdc")
self.b4.grid(row=1, padx=10, pady=8, column=0, columnspan=2)
self.b5 = Button(frame, text="Plot Variance vs. current", width=20, state=DISABLED, command=self.callback5, highlightbackground="#dcdcdc")
self.b5.grid(row=2, padx=10, pady=8, column=0, columnspan=2)
#need to remove Pack to use separator
s1 = Separator(frame, orient=VERTICAL)
s1.grid(column=2, row=0, rowspan=40, pady=10, sticky=N+W+S)
self.input_filename_label = StringVar()
self.input_filename_label.set("No data loaded yet")
self.l1 = Label(frame, textvariable=self.input_filename_label, width=40, bg="#dcdcdc")
self.l1.grid(row=0, column=2, columnspan=4, pady=5)
Label(frame, text="Baseline range (pts)", bg="#dcdcdc").grid(row=1, column=2, columnspan=2, pady=5, sticky=E)
self.br = Entry(frame, justify=CENTER, width=5, highlightbackground="#dcdcdc")
self.br.grid(row=1, column=4, sticky=W, pady=5)
self.br.insert(END, '0, 50')
Label(frame, text="Decimation", bg="#dcdcdc").grid(row=2, column=2, columnspan=2, pady=5, sticky=E)
self.de = Entry(frame, justify=CENTER, width=5, highlightbackground="#dcdcdc")
self.de.grid(row=2, column=4, sticky=W, pady=5)
self.de.insert(END, '1')
Label(frame, text="Unitary current amplitude (pA)", bg="#dcdcdc").grid(row=3, column=2, columnspan=2, pady=5, sticky=E)
self.ua = Entry(frame, justify=CENTER, width=5, highlightbackground="#dcdcdc")
self.ua.grid(row=3, column=4, sticky=W, pady=5)
self.ua.insert(END, '1')
#default unitary current is 1 pA
Label(frame, text="Output filename", bg="#dcdcdc").grid(row=4, column=2, columnspan=2, pady=5)
style = Style()
style.theme_use('clam')
style.configure("w.TRadiobutton", padding=2, background="#dcdcdc", foreground="black", width=15)
style.configure("TRadiobutton", padding=2, background="#dcdcdc", foreground="black", width=8)
MODES = [
("verified.txt", 3),
("Save as...", 2),
("v_[infile]", 1),
("[date:time]_v_[infile]", 0)
]
self.v = IntVar()
self.v.set(0) # initialize
#note this is the ttk radiobutton, the tk one doesn't select at first
for text, mode in MODES:
b = Radiobutton(frame, text=text, command=self.callback_fname, variable=self.v, value=mode, state=NORMAL)
b.grid(row=5, padx=10, column=mode+2, sticky=E)
#the last button in the loop (0) is the wide one, so gets the wide style.
b.configure(style='w.TRadiobutton')
self.traceHost = Frame(frame)
self.traceHost.grid(row=15, column=0, columnspan=3)
self.p = Plot(self.traceHost)
self.Host2D = Frame(frame)
self.Host2D.grid(row=15, column=3, columnspan=3)
self.pcv = Plot(self.Host2D)
s2 = Separator(frame)
s2.grid(row=25, columnspan=6, sticky=S+E+W)
message = Message(frame, text=self.introduction, width=800, font=("Courier", 12), bg="#dcdcdc")
message.grid(row=26, rowspan=8, columnspan=6, sticky=EW)
s3 = Separator(frame)
s3.grid(row=35, columnspan=6, sticky=E+W)
version_text = "https://github.com/aplested/verify\nPython version:\t" + sys.version.replace("\n", "\t")
version = Message(frame, width=800, text=version_text, justify=LEFT, background="#dcdcdc", font=("Courier", 12))
version.grid(row=36, columnspan=5, sticky=EW)
self.b6 = Button(frame, text="Quit", command=master.quit, width=10, highlightbackground="#dcdcdc")
self.b6.grid(row=36, padx=10, pady=8, column=5, sticky=W)
def on_help():
pass
def callback_fname(self):
### there is no need to do anything until the analysis starts
#this is just for debugging
print self.v.get()
def callback5(self):
'Called by PLOT variance current button.'
#make a new routine here to plot preliminary analysis
pass
def callback3(self):
'Called by TAKE DATA FROM excel button'
self.input_traces, self.dfile = self.read_Data('excel')
#dfile contains source data path and filename
if self.dfile != None:
self.input_filename_label.set('Data loaded from ' + self.dfile)
self.b4.config(state=NORMAL) #turn on VERIFY button
self.p.prepTracePlot(self.input_traces[0])
self.p.addTitle("First trace in {}".format(self.dfile))
self.p.drawTrace()
else:
self.input_filename_label.set('No data loaded')
def callback2(self):
'Called by VERIFY button'
#send traces to be checked
print ("Verify")
#results are stored in self.verified_output
self.getResult()
#default
out_filename = "verified.txt"
#determine filename option
opt = self.v.get()
if opt == 2:
out_filename = self.getOutputFilename()
elif opt == 1:
out_filename = file_tools.addFilenamePrefix(self.dfile, prefix="v_")
elif opt == 0:
out_filename = file_tools.addFilenamePrefix(self.dfile, prefix=datetime.now().strftime("%y%m%d-%H%M%S") + "_v_")
#self.b5.config(state=NORMAL)
#plot preview of current variance automatically
self.pcv.prep2DPlot(self.meanI, self.ensVariance)
self.pcv.addTitle("Preview of Current Variance Plot")
self.pcv.draw2D()
write_output (self.verified_output, self.output_header, out_filename)
def getOutputFilename(self):
try:
userFilename = tkFileDialog.askopenfilename()
except:
userFilename = "verified.txt"
return userFilename
def read_Data(self, file_type):
""""Asks for a excel tab-delim to use for verification test.
file_type :string, can be txt or excel... no meaning here.
"""
data_file_name = tkFileDialog.askopenfilename()
if data_file_name == "":
return None, None
try:
data_in_lines = file_tools.file_read(data_file_name)#, file_type)
except:
print ("Error opening file")
return None, None
try:
input_traces = lines_into_traces (data_in_lines)
except:
print ("error converting loaded data, check the format")
return None, None
#input_traces = traces_scale(in_traces,5) # optional scaling if gain wrong
print ("Read {} traces from file".format(len(input_traces)))
# Imagine taking a header here, with data titles?
return input_traces, data_file_name
def getResult(self):
self.unitary = float(self.ua.get())
print ("Taking unitary current from GUI: {} pA".format(self.unitary))
self.decimation = int(self.de.get())
print ("Decimating by {} according to GUI".format(self.decimation))
#decimate but preserve the original files in case user wants to run again with different decimation etc
if self.decimation > 1:
self.dec_traces = decimate_traces(self.input_traces, self.decimation)
else:
self.dec_traces = self.input_traces
self.baseline_range = [int(x) for x in self.br.get().split(",")]
print ("Taking baseline range from GUI. Points from {} to {}".format(self.baseline_range[0], self.baseline_range[1]))
self.input_traces, message = clean_bad_baselines(self.dec_traces, self.baseline_range)
print ("MESSAGE FROM CLEAN BAD: "+message)
self.input_traces, self.difference_traces, messages, self.output_header = construct_diffs(self.dec_traces, self.unitary, self.baseline_range)
print ("MESSAGES FROM CONSTRUCT_DIFFS: "+messages)
self.verified_output = final_prep(self.dec_traces, self.difference_traces, self.baseline_range)
#the following are used for plotting
self.ensVariance = self.verified_output[1]
self.meanI = self.verified_output[0]