-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rick-9000.py
executable file
·435 lines (371 loc) · 16.7 KB
/
Rick-9000.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python3
'''
Copyright (C) 2022 Ryan Chiechi <ryan.chiechi@ncsu.edu>
Description:
This is a GUI front-end for parsing Seebeck data from the Rick-9000.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import sys
import platform
import logging
from types import SimpleNamespace
import tkinter.ttk as tk
from tkinter import Tk
# from tkinter import Toplevel
from tkinter import filedialog
import tkinter.scrolledtext as ScrolledText
from tkinter import Text, IntVar, StringVar, Listbox, Label, Toplevel, TclError
from tkinter import N, S, E, W, X, Y # pylint: disable=unused-import
from tkinter import TOP, BOTTOM, LEFT, RIGHT # pylint: disable=unused-import
from tkinter import END, BOTH, VERTICAL, HORIZONTAL # pylint: disable=unused-import
from tkinter import EXTENDED, RAISED, SOLID, DISABLED, NORMAL # pylint: disable=unused-import
from tkinter import PhotoImage
from tkinter.font import Font
BLACK = "#000000"
YELLOW = "#f4e012"
WHITE = "#ffffff"
RED = "#ff0000"
TEAL = "#78CBFD"
GREEN = "#09f218"
BLUE = "#090df2"
GREY = '#e8e8e8'
absdir = os.path.dirname(os.path.realpath(__file__))
class TextHandler(logging.Handler):
# This class allows you to log to a Tkinter Text or ScrolledText widget
# Adapted from Moshe Kaplan: https://gist.github.com/moshekaplan/c425f861de7bbf28ef06
def __init__(self, text):
# run the regular Handler __init__
logging.Handler.__init__(self)
# Store a reference to the Text it will log to
self.text = text
def emit(self, record):
msg = self.format(record)
def append():
self.text.configure(state='normal')
self.text.insert(END, msg + '\n')
self.text.configure(state='disabled')
# Autoscroll to the bottom
self.text.yview(END)
# This is necessary because we can't modify the Text from other threads
self.text.after(0, append)
class ToolTip(object):
def __init__(self, widget):
self.widget = widget
self.tipwindow = None
self.id = None
self.x = self.y = 0
self.text = str()
def showtip(self, text):
"Display text in tooltip window"
self.text = text
if self.tipwindow or not self.text:
return
x, y, cx, cy = self.widget.bbox("insert")
x = x + self.widget.winfo_rootx() + 27
y = y + cy + self.widget.winfo_rooty() + 27
self.tipwindow = tw = Toplevel(self.widget)
tw.wm_overrideredirect(1)
tw.wm_geometry("+%d+%d" % (x, y))
try:
# For Mac OS
tw.tk.call("::tk::unsupported::MacWindowStyle",
"style", tw._w,
"help", "noActivates")
except TclError:
pass
label = Label(tw, text=self.text, justify=LEFT,
background="#ffffe0", relief=SOLID, borderwidth=1,
font=("tahoma", "8", "normal"))
label.pack(ipadx=1)
def hidetip(self):
tw = self.tipwindow
self.tipwindow = None
if tw:
tw.destroy()
def createToolTip(widget, text):
toolTip = ToolTip(widget)
def enter(event):
toolTip.showtip(text)
def leave(event):
toolTip.hidetip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)
class ChooseFiles(tk.Frame):
'''The main frame for adding/removing files, accessing setttings
and parsing.'''
from seebeck.parse import GUIParse
opts = SimpleNamespace(in_files=[],
out_dir=os.path.join(os.getcwd(), 'parsed'),
out_file='',
plot=True,
write=True,
truetemp=False,
col_to_parse=1,
dTn=[],
cutoff_to_toss=100) # TODO: Make this a user option
raw_data = {}
gothreads = []
plots = []
outdir = ''
boolmap = {1: True, 0: False}
colmap = {1: 'Raw Voltage (uV)',
2: 'Corrected Voltage (uV)',
3: 'Top T',
4: 'Bottom T',
5: 'Delta-T (°C)',
6: 'Seebeck (uV/K)'}
FileListBoxFrameLabelVar = None
FileListBoxFrameLabel = None
filelist = None
FileListBox = None
checks = []
OutputFileName = None
DeltaTn = None
OptionsColString = None
OptionCol = None
def __init__(self, master=None):
if master is None:
master = Tk()
super().__init__(master)
bgimg = PhotoImage(file=os.path.join(absdir, 'gui', 'RCCLabFluidic.png'))
limg = Label(self.master, i=bgimg, background=GREY)
limg.pack(side=TOP)
try:
self.last_input_path = os.getcwd()
except KeyError:
self.last_input_path = os.path.expanduser('~')
master.tk_setPalette(background=GREY, activeBackground=GREY)
master.title("RCCLab Rick-9000 Parser")
master.geometry('800x850+250+250')
self.pack(fill=BOTH)
if len(sys.argv) > 1:
self.opts.in_files = sys.argv[1:]
self.__createWidgets()
self.ToFront()
def __createWidgets(self):
self.ButtonFrame = tk.Frame(self)
self.LoggingFrame = tk.Frame(self)
self.RightOptionsFrame = tk.Frame(self)
self.FileListBoxFrame = tk.Frame(self)
# ScrolledText widget to display logging output
stlogger = ScrolledText.ScrolledText(self.LoggingFrame, state='disabled')
stlogger.configure(font='TkFixedFont')
stlogger.pack(side=LEFT, fill=BOTH, expand=True)
# Create textLogger
text_handler = TextHandler(stlogger)
# Logging configuration
logging.basicConfig(filename='Rick-9000.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
# Add the handler to logger
logger = logging.getLogger(__package__)
logger.addHandler(text_handler)
self.__createButtons()
self.__createFileListBox()
self.__createOptions()
self.ButtonFrame.pack(side=BOTTOM, fill=None)
self.FileListBoxFrame.pack(side=TOP, fill=BOTH, expand=True)
self.RightOptionsFrame.pack(side=RIGHT, fill=Y)
self.LoggingFrame.pack(side=BOTTOM, fill=BOTH)
# self.logger.setLevel(getattr(logging, self.opts.loglevel.upper()))
self.updateFileListBox()
self.checkOptions()
logging.info('Select some files to get started.')
def __createButtons(self):
buttons = [{'name': 'Quit', 'text': 'QUIT', 'command': 'Quit', 'side': BOTTOM},
{'name': 'SpawnInputDialog', 'text': 'Add Input Files', 'side': LEFT},
{'name': 'RemoveFile', 'text': 'Remove Files', 'side': LEFT},
{'name': 'SpawnOutputDialog', 'text': 'Choose Output Directory', 'side': LEFT},
{'name': 'Parse', 'text': 'Parse!', 'side': LEFT}
]
for b in buttons:
button = tk.Button(self.ButtonFrame)
button.config(text=b['text'], command=getattr(self, b['name']+'Click'))
button.pack(side=b['side'])
setattr(self, 'Button'+b['name'], button)
def __createFileListBox(self):
self.FileListBoxFrameLabelVar = StringVar()
self.FileListBoxFrameLabel = tk.Label(self.FileListBoxFrame,
textvariable=self.FileListBoxFrameLabelVar,
font=Font(size=10, weight="bold"))
self.FileListBoxFrameLabel.pack(side=TOP, fill=X)
yScroll = tk.Scrollbar(self.FileListBoxFrame, orient=VERTICAL)
yScroll.pack(side=RIGHT, fill=Y)
xScroll = tk.Scrollbar(self.FileListBoxFrame, orient=HORIZONTAL)
xScroll.pack(side=BOTTOM, fill=X)
self.filelist = StringVar()
self.FileListBox = Listbox(self.FileListBoxFrame,
listvariable=self.filelist, selectmode=EXTENDED,
height=20, width=0, relief=RAISED, bd=1,
bg=WHITE,
# font=Font(size=10),
xscrollcommand=xScroll.set,
yscrollcommand=yScroll.set)
xScroll['command'] = self.FileListBox.xview
yScroll['command'] = self.FileListBox.yview
self.FileListBox.pack(side=LEFT, fill=BOTH, expand=True)
self.UpdateFileListBoxFrameLabel()
def __createOptions(self):
'''Create the widgets for options and use setattr to assign them to self.'''
self.checks = [{'name': 'plot', 'text': 'Plot', 'row': 1,
'tooltip': "Show summary plots after parsing."},
{'name': 'write', 'text': 'Write', 'row': 2,
'tooltip': "Write results to text files after parsing."},
{'name': 'truetemp', 'text': 'Use Real T', 'row': 3,
'tooltip': "Use the measured ΔT instead the ΔT field values."}
]
for _c in self.checks:
setattr(self, _c['name'], IntVar())
check = tk.Checkbutton(self.RightOptionsFrame, text=_c['text'],
variable=getattr(self, _c['name']),
command=self.checkOptions)
check.grid(column=0, row=_c['row'], sticky=W)
createToolTip(check, _c['tooltip'])
setattr(self, 'Check_'+_c['name'], check)
if getattr(self.opts, _c['name']):
getattr(self, _c['name']).set(1)
rowidx = len(self.checks)+1
tk.Label(self.RightOptionsFrame, text="Output file base name:").grid(
column=0, row=rowidx)
self.OutputFileName = tk.Entry(self.RightOptionsFrame, width=20,
font=Font(size=10, slant='italic'))
for n in ('<Return>', '<Leave>', '<Enter>'):
self.OutputFileName.bind(n, self.checkOutputFileName)
self.OutputFileName.grid(column=0, row=rowidx+1)
if self.opts.out_file:
self.OutputFileName.insert(0, self.opts.out_file)
tk.Label(self.RightOptionsFrame, text="ΔT values:").grid(
column=0, row=rowidx+2, sticky=W)
self.DeltaTn = tk.Entry(self.RightOptionsFrame, width=20,
font=Font(size=10))
self.DeltaTn.insert(0, '4,8,12')
for n in ('<Return>', '<Leave>', '<Enter>'):
self.DeltaTn.bind(None, self.checkOptions)
self.DeltaTn.grid(column=0, row=rowidx+3, sticky=W)
tk.Label(self.RightOptionsFrame, text="Cutoff Limit: ").grid(
column=0, row=rowidx+4, sticky=W)
self.cutoffEntry = tk.Entry(self.RightOptionsFrame, width=8,
font=Font(size=10))
self.cutoffEntry.insert(0, str(self.opts.cutoff_to_toss))
for n in ('<Return>', '<Leave>', '<Enter>'):
self.cutoffEntry.bind(None, self.checkOptions)
self.cutoffEntry.grid(column=0, row=rowidx+4, sticky=E)
tk.Label(self.RightOptionsFrame, text="Column to plot:").grid(
column=0, row=rowidx+6, sticky=W)
self.OptionsColString = StringVar()
self.OptionsColString.set(self.opts.col_to_parse)
self.OptionCol = tk.OptionMenu(self.RightOptionsFrame,
self.OptionsColString,
self.colmap[self.opts.col_to_parse],
command=self.checkOptions,
*list(self.colmap.values()))
# __menu = self.nametowidget(self.OptionCol)
# __menu.config(font=Font(size=10)) # Set the dropdown menu's font
self.OptionCol.grid(column=0, row=rowidx+5, sticky=W)
# tk.Label(self.RightOptionsFrame, text="ΔT values:").grid(
# column=0, row=rowidx+2, sticky=W)
# self.OptionsdTnString = StringVar()
# self.OptionsdTnString.set('.'.join(self.opts.dTn))
# self.OptionPlots = tk.OptionMenu(self.RightOptionsFrame,
# self.OptionsdTnString, self.opts.dTn, 'J', 'R',
# command=self.checkOptions)
# self.OptionPlots.grid(column=0, row=rowidx+3, sticky=W)
def RemoveFileClick(self):
self.checkOptions()
selected = [self.FileListBox.get(x) for x in self.FileListBox.curselection()]
todel = []
filelist = []
for i in range(0, len(self.opts.in_files)):
for _s in selected:
if self.opts.in_files[i].replace(" ", "_") == _s:
todel.append(i)
for i in range(0, len(self.opts.in_files)):
if i not in todel:
filelist.append(self.opts.in_files[i])
self.opts.in_files = filelist
self.updateFileListBox()
self.FileListBox.selection_clear(0, END)
def SpawnInputDialogClick(self):
# self.checkOptions()
self.opts.in_files += filedialog.askopenfilename(
title="Files to parse",
multiple=True,
initialdir=self.last_input_path,
filetypes=[('LabView Files', '*.lvm'), ('Data files', '*_data.txt'),
('Text files', '*.txt'), ('All files', '*')])
if self.opts.in_files:
self.last_input_path = os.path.split(self.opts.in_files[0])[0]
if not self.outdir:
self.opts.out_dir = os.path.join(self.last_input_path, 'parsed')
self.updateFileListBox()
if not self.opts.out_file:
self.OutputFileName.delete(0, END)
self.OutputFileName.insert(0, os.path.basename(
self.opts.in_files[-1]).lower().replace('.lvm', ''))
self.checkOutputFileName()
self.updateFileListBox()
def ParseClick(self):
self.checkOptions()
self.GUIParse()
def checkOptions(self, m=None):
for c in self.checks:
setattr(self.opts, c['name'], self.boolmap[getattr(self, c['name']).get()])
if self.opts.truetemp:
self.DeltaTn['state'] = DISABLED
else:
self.DeltaTn['state'] = NORMAL
self.opts.dTn = self.DeltaTn.get().split(',')
for __key in self.colmap:
if self.colmap[__key] == self.OptionsColString.get():
self.opts.col_to_parse = __key
_toss = self.cutoffEntry.get()
try:
self.opts.cutoff_to_toss = int(_toss)
except ValueError:
self.cutoffEntry.insert(0, str(self.opts.cutoff_to_toss))
def updateFileListBox(self):
self.filelist.set(" ".join([x.replace(" ", "_") for x in self.opts.in_files]))
def SpawnOutputDialogClick(self):
outdir = filedialog.askdirectory(title="Select Output File(s)",
initialdir=self.opts.out_dir)
if not outdir:
return
if os.path.exists(outdir):
self.opts.out_dir = outdir
self.outdir = self.opts.out_dir # So we know the user set the output dir
self.UpdateFileListBoxFrameLabel()
def UpdateFileListBoxFrameLabel(self):
self.FileListBoxFrameLabelVar.set(
f"Output to: {self.opts.out_dir}/{self.opts.out_file}_*.txt")
def checkOutputFileName(self, event=None):
self.opts.out_file = self.OutputFileName.get()
self.UpdateFileListBoxFrameLabel()
def QuitClick(self):
self.Quit()
def Quit(self):
self.master.quit()
self.master.destroy()
def ToFront(self):
'''Try to bring the main window to the front on different platforms'''
if platform.system() == "Darwin":
os.system(
'''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
else:
self.master.attributes('-topmost', 1)
self.master.attributes('-topmost', 0)
self.master.lift()
if __name__ == '__main__':
print("Starting GUI")
root = Tk()
gui = ChooseFiles(root)
gui.mainloop()