This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
explorer.py
331 lines (265 loc) · 11.2 KB
/
explorer.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" ShapeOut - wx measurement explorer
"""
from __future__ import division, print_function
import numpy as np
import warnings
import wx
import wx.lib.agw.hypertreelist as HT
from wx.lib.scrolledpanel import ScrolledPanel
from .. import tlabwrap
class ExplorerPanel(ScrolledPanel):
""""""
def __init__(self, parent, frame, col_width=400):
"""Constructor
You can also use the function `self.BindAnalyze` to determine
where the tdmsfile list goes.
Use the method `self.SetProjectTree` to display projects.
"""
wx.Panel.__init__(self, parent)
self.frame = frame
# Parameters important to determine size of htree control
# (not important for one-column mode, just use something large)
self.s_mult = 10
self.s_off = 55
self.col_width = col_width
self.treelist = []
# Set up box
box = wx.StaticBox(self, label=_("Measurement browser"))
sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
# Load tree control
self.htreectrl = HT.HyperTreeList(self,
agwStyle=wx.TR_DEFAULT_STYLE|wx.TR_HIDE_ROOT|\
wx.TR_ROW_LINES|HT.TR_AUTO_CHECK_CHILD|\
HT.TR_AUTO_CHECK_PARENT|HT.TR_NO_HEADER)
sizer.Add(self.htreectrl, 1, wx.EXPAND, 3)
self.btn_selall = wx.Button(self, label=_("Select all"))
self.btn_selnon = wx.Button(self, label=_("Deselect all"))
# to add more butons, use
# wx.NewId and give it to the buttons as second argument
# -> buttons can be identified with this id from the event
self.btn_selflow1 = wx.Button(self, label=_("-"))
self.btn_selflow2 = wx.Button(self, label=_("-"))
self.btn_selflow3 = wx.Button(self, label=_("-"))
self.btn_add = wx.Button(self, label=_("Analyze"))
self.sizer_bag = wx.GridBagSizer()
# Set size of buttons
maxs = max(self.btn_selall.GetSize(), self.btn_selnon.GetSize())
self.btn_selall.SetMinSize((maxs[0]*1.5, maxs[1]))
self.btn_selnon.SetMinSize((maxs[0]*1.5, maxs[1]))
self.btn_selflow1.SetMinSize(maxs)
self.btn_selflow2.SetMinSize(maxs)
self.btn_selflow3.SetMinSize(maxs)
self.btn_add.SetMinSize((maxs[0]*2, maxs[1]))
self.sizer_bag.Add(self.btn_selall,(0,0), span=wx.GBSpan(1,3))
self.sizer_bag.Add(self.btn_selnon,(0,3), span=wx.GBSpan(1,3))
self.sizer_bag.Add(self.btn_selflow1,(1,0), span=wx.GBSpan(1,2))
self.sizer_bag.Add(self.btn_selflow2,(1,2), span=wx.GBSpan(1,2))
self.sizer_bag.Add(self.btn_selflow3,(1,4), span=wx.GBSpan(1,2))
self.sizer_bag.Add(self.btn_add,(2,1), span=wx.GBSpan(1,4))
minsize = self.sizer_bag.GetMinSize()
box.SetMinSize(minsize)
self.normal_width = box.GetMinSize()[0]+10
sizer.Add(self.sizer_bag)
self.SetSizer(sizer)
# sets self.flowrates
self.Update()
#self.SetProjectTree(tlabwrap.GetTDMSTreeGUI(".."))
self.Bind(wx.EVT_BUTTON, self.OnSelectAll, self.btn_selall)
self.Bind(wx.EVT_BUTTON, self.OnSelectNone, self.btn_selnon)
self.Bind(wx.EVT_BUTTON, self.OnSelectFlow1, self.btn_selflow1)
self.Bind(wx.EVT_BUTTON, self.OnSelectFlow2, self.btn_selflow2)
self.Bind(wx.EVT_BUTTON, self.OnSelectFlow3, self.btn_selflow3)
self.Bind(wx.EVT_BUTTON, self.OnAnalyze, self.btn_add)
self.BindAnalyze(lambda x: None)
self.Disable()
def BindAnalyze(self, func):
self.external_analyze = func
def CheckData(self, data=[]):
r = self.htreectrl.GetRootItem()
for c in r.GetChildren():
for k in c.GetChildren():
if k.GetData() in data:
k.Check(True)
def BoldifyData(self, data=[]):
r = self.htreectrl.GetRootItem()
for c in r.GetChildren():
for k in c.GetChildren():
if k.GetData() in data:
k.SetBold(True)
def external_analyze(self, *args, **kwargs):
""" to be overridden """
pass
def OnAnalyze(self, e=None):
"""
- calls self.external_analyze with the list of tdms files
- Updates bold font faces on tree view
"""
files = list()
r = self.htreectrl.GetRootItem()
for c in r.GetChildren():
for k in c.GetChildren():
if k.IsChecked():
k.SetBold(True)
files.append(k.GetData())
else:
k.SetBold(False)
self.htreectrl.SetColumnWidth(0, self.col_width)
self.external_analyze(files)
def OnSelectAll(self, e=None):
r = self.htreectrl.GetRootItem()
for c in r.GetChildren():
self.htreectrl.CheckItem(c)
def OnSelectFlow1(self, e=None):
self.OnSelectNone()
frate = self.flowrates[0]
r = self.htreectrl.GetRootItem()
for c in r.GetChildren():
for k in c.GetChildren():
f = k.GetData()
if ( not tlabwrap.GetRegion(f).lower() == "reservoir"
and frate == tlabwrap.GetFlowRate(f) ):
self.htreectrl.CheckItem(k)
def OnSelectFlow2(self, e=None):
self.OnSelectNone()
frate = self.flowrates[1]
r = self.htreectrl.GetRootItem()
for c in r.GetChildren():
for k in c.GetChildren():
f = k.GetData()
if ( not tlabwrap.GetRegion(f).lower() == "reservoir"
and frate == tlabwrap.GetFlowRate(f) ):
self.htreectrl.CheckItem(k)
def OnSelectFlow3(self, e=None):
self.OnSelectNone()
frate = self.flowrates[2]
r = self.htreectrl.GetRootItem()
for c in r.GetChildren():
for k in c.GetChildren():
f = k.GetData()
if ( not tlabwrap.GetRegion(f).lower() == "reservoir"
and frate == tlabwrap.GetFlowRate(f) ):
self.htreectrl.CheckItem(k)
def OnSelectNone(self, e=None):
r = self.htreectrl.GetRootItem()
for c in r.GetChildren():
self.htreectrl.CheckItem(c, False)
def SelectData(self, data=[]):
r = self.htreectrl.GetRootItem()
for c in r.GetChildren():
for k in c.GetChildren():
if k.GetData() in data:
self.htreectrl.CheckItem(k)
def SetProjectTree(self, data, add=False, marked=[]):
""" Update tree view with measurement data information
Parameters
----------
data : tuple (treelist, cols)
The return value of `tlabwrap.GetTDMSTreeGUI`.
add : bool
If True, the current treelist is updated with new
measurements. If False, the current treelist is replaced.
"""
treelist, cols = data
if add:
# update treelist
# check if already in tree
for item in self.treelist:
if not item in treelist:
treelist.append(item)
# Any checked or bold items ?
checked = list()
bold = marked
r = self.htreectrl.GetRootItem()
if r is not None:
for c in r.GetChildren():
for k in c.GetChildren():
f = k.GetData()
if k.IsBold():
bold.append(f)
if k.IsChecked():
checked.append(f)
self.treelist = treelist
# enable or disable window
if len(self.treelist) == 0:
self.Disable()
else:
self.Enable()
# Clear first
self.htreectrl.DeleteAllItems()
# define column lengths
col_width = 10
for _i in range(self.htreectrl.GetColumnCount()):
self.htreectrl.RemoveColumn(0)
self.htreectrl.AddColumn(cols[0])
rroot = self.htreectrl.AddRoot("", ct_type=0)
# Add projects to tree
for j in range(len(treelist)):
project = treelist[j]
root = self.htreectrl.AppendItem(rroot, project[0][0],
ct_type=1,
data=project[0][1])
for k in range(1,len(project)):
self.htreectrl.AppendItem(root, project[k][0],
ct_type=1,
data=project[k][1])
# displayed column width
col_width = max(len(project[k][0]), col_width)
#for i in range(1,len(collengths)):
# self.htreectrl.SetColumnWidth(i, collengths[i]*self.s_mult)
#self.normal_width = col_width*self.s_mult + self.s_off
# open the entire tree
# Add warning message
if len(treelist) == 0:
msg = _("No measurements found.")
root = self.htreectrl.AppendItem(rroot, msg, ct_type=0)
col_width = max(len(msg), col_width)
# Set size in htree control
self.htreectrl.SetColumnWidth(0, self.col_width)
# col_width*self.s_mult+self.s_off)
self.htreectrl.ExpandAll()
self.BoldifyData(bold)
self.CheckData(checked)
self.Update()
def SetProjectTreeAdd(self, data):
""" Convenience wrapper around SetProjectTree with True `add`"""
self.SetProjectTree(data, add=True)
def Update(self, e=None):
""" Updates this panel (e.g. dis-/enables buttons)
sets self.flowrates
"""
self.btn_selall.Disable()
self.btn_selnon.Disable()
self.btn_selflow1.Disable()
self.btn_selflow2.Disable()
self.btn_selflow3.Disable()
self.btn_add.Disable()
self.btn_selflow1.SetLabel(u"-")
self.btn_selflow2.SetLabel(u"-")
self.btn_selflow3.SetLabel(u"-")
self.flowrates=list()
if len(self.treelist) != 0:
self.btn_selall.Enable()
self.btn_selnon.Enable()
self.btn_add.Enable()
flr=list()
# get list of flow rates
for item in self.treelist:
# First tree item contains path to measurements
for meas in item[1:]:
flr.append(tlabwrap.GetFlowRate(meas[1]))
flr = np.unique(flr)
flr.sort()
if len(flr) >= 1:
self.btn_selflow1.SetLabel(u"{} µls⁻¹".format(flr[0]))
self.btn_selflow1.Enable()
if len(flr) >= 2:
self.btn_selflow2.SetLabel(u"{} µls⁻¹".format(flr[1]))
self.btn_selflow2.Enable()
if len(flr) >= 3:
self.btn_selflow3.SetLabel(u"{} µls⁻¹".format(flr[2]))
self.btn_selflow3.Enable()
if len(flr) > 3:
warnings.warn("Only using first three flowrates!")
self.flowrates = flr