-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWIZ.py
305 lines (291 loc) · 12.8 KB
/
WIZ.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
import Tkinter as Tk
import ttk
from Graph import Graph
from MainWindow import MainWindow
from SettingsWindow import SettingsWindow
import tkFileDialog
import FileDialog
import numpy as np
import math
import json
import os
import re
from TemplateCreator import TemplateCreator
import pickle
import tkMessageBox
class InitialWindow(Tk.Tk):
"""Tk object which creates an initial window for loading projects and files, ultimately creating a MainWindow"""
__author__ = "Thomas Schweich"
defaultProgramSettings = {
"Load Chunk Size": 100000,
"Plot Chunk Size": 100000,
"Max Preview Points": 100000,
"DPI": 100,
"Style": ["ggplot"],
"User Font Size": 12,
"Icon Location": r'res\WIZ.ico'
}
def __init__(self, win=None, *args, **kwargs):
# noinspection PyCallByClass,PyTypeChecker
Tk.Tk.__init__(self, *args, **kwargs)
# Load settings. If they don't exist, create default settings file.
if os.path.isfile('programSettings.json'):
with open('programSettings.json', 'r') as settingsFile:
self.settings = json.load(settingsFile)
else:
with open('programSettings.json', 'w+') as settingsFile:
json.dump(InitialWindow.defaultProgramSettings, settingsFile)
self.settings = InitialWindow.defaultProgramSettings
self.iconbitmap(self.settings["Icon Location"])
self.wm_title("WIZ")
self.defaultWidth, self.defaultHeight = self.winfo_screenwidth() * .25, self.winfo_screenheight() * .5
# self.geometry("%dx%d+%d+%d" % (self.defaultWidth, self.defaultHeight, self.defaultWidth * 1.5,
# 0))
self.win = win
self.baseFrame = Tk.Frame(master=self, width=self.defaultWidth, height=self.defaultHeight)
self.baseFrame.pack(fill=Tk.X, side=Tk.TOP, expand=True)
self.newFrame = Tk.Frame(self.baseFrame, width=self.defaultWidth, height=self.defaultHeight)
self.error = Tk.Label(self.baseFrame, text="Invalid selection", fg="red")
text = Tk.Label(self.baseFrame,
text=" Welcome to WIZ ")
text.pack()
if not self.win:
loadButton = ttk.Button(self.baseFrame, text="Load Project", command=self.loadProject)
loadButton.pack(fill=Tk.X, side=Tk.TOP, expand=True)
blankButton = ttk.Button(self.baseFrame, text="New Blank Project", command=self.createBlankProject)
blankButton.pack(fill=Tk.X, side=Tk.TOP)
rawButton = ttk.Button(self.baseFrame, text="Load Raw Data" if not self.win else "Load More Raw Data",
command=self.loadRawData)
rawButton.pack(fill=Tk.X, side=Tk.TOP)
templateButton = ttk.Button(self.baseFrame, text="Create Template", command=lambda: TemplateCreator(self))
templateButton.pack(fill=Tk.X, side=Tk.TOP)
if not self.win:
projFromTemplate = ttk.Button(self.baseFrame, text="Create Project From Template",
command=self.createFromTemplate)
projFromTemplate.pack(fill=Tk.X, side=Tk.TOP)
ttk.Button(self.baseFrame, text="Settings", command=lambda: SettingsWindow(self.baseFrame).open()).pack(fill=Tk.X, side=Tk.TOP)
ttk.Button(self.baseFrame, text="About", command=self.about).pack(fill=Tk.X, side=Tk.TOP)
def about(self):
tkMessageBox.showinfo("WIZ", "Created By: Thomas Schweich")
self.lift()
def loadProject(self):
"""Loads an .npz file using MainWindow.loadProject"""
self.error.pack_forget()
path = tkFileDialog.askopenfilename(filetypes=[("WIZ Project", ".gee.npy")])
loading = Tk.Label(self.baseFrame, text="Loading...")
loading.pack()
self.update()
try:
MainWindow.loadProject(path, destroyTk=self)
# window.lift()
# window.mainloop()
except (IOError, TypeError):
loading.pack_forget()
self.error.pack()
return
def loadRawData(self, callFunc=None):
"""Loads data from a text file using MainWindow.loadData() and prompts the user for a slice"""
self.error.pack_forget()
self.newFrame.destroy()
self.lift()
self.newFrame = Tk.Frame(self.baseFrame)
self.newFrame.pack(side=Tk.BOTTOM, expand=True)
path = tkFileDialog.askopenfilename()
if not path:
self.error.pack()
return
extension = path[path.rfind("."):]
print extension
if extension != ".npy": # in self.settings["Non Binary Extensions"]:
instructions = Tk.Label(self.newFrame, text="The first 10 lines of your data are displayed below.")
instructions.pack()
lines = []
with open(path) as f:
for i, line in enumerate(f):
if i == 0:
firstline = line
if i == 1:
secondline = line
lines.append(line)
if i == 10:
break
regex = r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?'
try:
numbers = re.findall(regex, firstline)
print 'First Line: "%s"\nNumbers: %s' % (firstline, str(numbers))
except NameError:
self.error.pack()
return
try:
if len(numbers) == 0:
numbers = re.findall(regex, secondline)
print 'Second Line "%s"\nNumbers (second line): %s' % (secondline, str(numbers))
except NameError:
self.eror.pack()
return
tree = ttk.Treeview(self.newFrame, height=10)
tree.pack()
cols = ()
style = ttk.Style(self)
style.configure("Treeview", rowheight=int(.3 * self.settings["DPI"]))
for i in range(len(numbers)):
cols += tuple(str(i))
# tree.heading(str(i), text="Column")
tree["columns"] = cols
for i in range(len(numbers)):
tree.heading(str(i), text="Column %d" % i)
for i, l in enumerate(lines):
tree.insert("", "end", text="Line %d" % i, values=re.findall(regex, l))
xFrame = Tk.Frame(self.newFrame)
xFrame.pack(expand=True)
xLabel = Tk.Label(xFrame, text="X-Data Column: ")
xLabel.pack(side=Tk.LEFT)
xEntry = Tk.Entry(xFrame, width=2)
xEntry.insert(0, "0")
xEntry.pack(side=Tk.RIGHT)
yFrame = Tk.Frame(self.newFrame)
yFrame.pack(expand=True)
yLabel = Tk.Label(yFrame, text="Y-Data Column: ")
yLabel.pack(side=Tk.LEFT)
yEntry = Tk.Entry(yFrame, width=2)
yEntry.insert(0, "1")
yEntry.pack(side=Tk.RIGHT)
headerVal = Tk.IntVar()
headerVal.set(0)
Tk.Checkbutton(self.newFrame, text="Data Contains Column Headers", variable=headerVal).pack()
cleanVal = Tk.IntVar()
cleanVal.set(1)
Tk.Checkbutton(self.newFrame, text="Clean infs and NaNs (recommended)", variable=cleanVal).pack()
chunkVal = Tk.IntVar()
chunkVal.set(1)
Tk.Checkbutton(self.newFrame, text="Read data in chunks (recommended)", variable=chunkVal).pack()
Tk.Button(self.newFrame, text="Load", command=lambda: self.load(
path, xEntry.get(), yEntry.get(), headerVal.get(), cleanVal.get(), chunkVal.get(),
callFunc=callFunc)).pack()
else:
self.load(path, shouldChunk=False, callFunc=callFunc)
self.update()
self.lift()
def load(self, path, xCol=0, yCol=1, hasHeaders=False, shouldClean=True, shouldChunk=True, callFunc=None):
self.newFrame.destroy()
self.newFrame = Tk.Frame(self.baseFrame)
self.newFrame.pack(side=Tk.BOTTOM)
self.lift()
try:
xCol = int(xCol)
yCol = int(yCol)
except ValueError:
self.error.pack()
raise
loading = Tk.Label(self.newFrame, text="Loading data...")
loading.pack()
progress = None
if shouldChunk:
progress = ttk.Progressbar(self.newFrame, length=self.defaultWidth * .5, mode="indeterminate", maximum=10)
progress.pack()
self.update()
try:
data = MainWindow.loadData(path, chunkSize=self.settings['Load Chunk Size'], tkProgress=progress,
tkRoot=self, xCol=xCol, yCol=yCol, header=hasHeaders, clean=shouldClean,
chunkRead=shouldChunk)
except (ValueError, IOError):
loading.pack_forget()
if progress: progress.pack_forget()
self.error.pack()
return
loading.pack_forget()
if progress: progress.pack_forget()
Tk.Label(self.newFrame, text="How much data would you like to use?").pack()
tkVar = Tk.IntVar()
start = Tk.Entry(self.newFrame)
start.insert(0, "0")
end = Tk.Entry(self.newFrame)
end.insert(0, str(len(data[0])))
start.pack()
end.pack()
Tk.Radiobutton(self.newFrame, text="By Index (from 0 to %d)" % len(data[0]), variable=tkVar, value=0).pack()
Tk.Radiobutton(self.newFrame, text="By x-value (from %d to %d)" % (data[0][0], data[0][-1]), variable=tkVar,
value=1).pack()
Tk.Button(self.newFrame, text="Create Project" if not self.win else "Add Graph",
command=lambda: self.sliceData(data, tkVar, start.get(), end.get(), callFunc=callFunc)).pack()
def createBlankProject(self):
self.quit()
self.destroy()
win = MainWindow()
win.setGraphs([[Graph(window=win, title="New Project")]])
win.plotGraphs()
win.mainloop()
def sliceData(self, data, tkVar, begin, end, callFunc=None):
"""Creates a graph of the slice of data and creates a new MainWindow with .graphs assigned to the new graph"""
if not callFunc: callFunc = self.createMain
begin = float(begin)
end = float(end)
# By index
if tkVar.get() == 0:
newDat = data[0][int(begin):int(end)], data[1][int(begin):int(end)]
# By x value
else: # elif tkVar.get() == 1:
newBegin, newEnd = np.searchsorted(data[0], np.array([np.float64(begin), np.float64(end)]))
newDat = data[0][newBegin:newEnd], data[1][newBegin:newEnd]
callFunc(newDat) # Currently either createMain() or applyTemplate())
def createMain(self, newDat):
# self.quit()
self.destroy()
if self.win:
gr = Graph(window=self.win, title="Raw Data")
print "Additional Graph Created"
gr.setRawData(newDat)
print "Raw Data Set"
self.win.addGraph(gr)
print "Added to Window"
#self.win.plotGraphs()
else:
win = MainWindow()
gr = Graph(window=win, title="Raw Data")
gr.setRawData(newDat)
win.setGraphs([[gr]])
win.plotGraphs()
win.mainloop()
def createFromTemplate(self):
self.error.pack_forget()
self.newFrame.destroy()
self.lift()
self.newFrame = Tk.Frame(self.baseFrame)
self.newFrame.pack(side=Tk.BOTTOM, expand=True)
instructions = Tk.Label(self.newFrame, text="Select Your Template")
templatePath = tkFileDialog.askopenfilename(filetypes=[("WIZ Template", ".wizt")])
try:
with open(templatePath, 'r') as f:
chain = pickle.load(f)
except (ValueError, IOError):
self.error.pack()
return
instructions.pack_forget()
self.loadRawData(callFunc=lambda data: self.applyTemplate(chain, data))
def applyTemplate(self, expChain, data):
win = MainWindow()
from Graph import Graph
expChain.addVariable('ORIGINAL', Graph(window=win, rawXData=data[0], rawYData=data[1]))
import Graph
expChain.modules = (Graph, np, math)
progress = ttk.Progressbar(win, length=self.defaultWidth, mode="determinate", maximum=len(expChain))
progress.pack()
info = Tk.Label(win, text="Loading...")
info.pack()
for gr, name in expChain:
gr.setTitle(name)
try:
win.addGraph(gr)
except AttributeError:
pass # Non-Graph object
progress.step(1)
win.update()
progress.destroy()
info.destroy()
self.quit()
self.destroy()
win.lift()
win.mainloop()
if __name__ == "__main__":
initial = InitialWindow()
initial.mainloop()