-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtAllan2.py
362 lines (300 loc) · 14.2 KB
/
qtAllan2.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
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
from pyqtgraph.dockarea import *
import pyqtgraph.parametertree.parameterTypes as pTypes
from pyqtgraph.parametertree import Parameter, ParameterTree, ParameterItem, registerParameterType
import numpy as np
import time, allantools, sys, os
data_folder_path = "G:\eric.kroemer\Desktop\Vincent\Pilotage Data"
log_separator = ","
header_lines = 1
time_col_header = 'Time'
default_hidden_columns = ["DAQ time (s)","Error","Correction (Hz)","Heater current (A)","Carrier signal (V)","Laser current modulation signal (V)"]
def readHeader(filename):
logfile = open(filename,"r")
line = logfile.readline()
header = line[:-1].split(log_separator)
return header
def readLines(filename,read_every=1):
lines = []
logfile = open(filename,"r")
line_number = 0
# loglines = follow(logfile)
while True:
line = logfile.readline()
if line:
line_number += 1
if line_number % read_every == 0:
lines.append(line[:-1].split(log_separator))
if line_number % 1000 == 0:
sys.stdout.write("\rReading file: {} lines read".format(line_number))
sys.stdout.flush()
else:
break
sys.stdout.write("\r{} lines read, {} lines kept\n".format(line_number,len(lines)))
return lines[0], lines[header_lines:-1]
def open_filename():
filenames = QtGui.QFileDialog.getOpenFileNames(None, 'Select datalog file', data_folder_path, selectedFilter='*.txt')
return filenames
class ColumnData(object):
def __init__(self,parent,header,index):
self.parent = parent
self.header = header
self.index = index
self.time_data = False
self.plot = None
self.curve = None
if self.header == time_col_header:
self.time_data = True
class FileData(object):
def __init__(self,parent=None,filename=None):
self.parent = parent
self.fullfilename = filename
self.basename = os.path.basename(str(self.fullfilename))
self.headers = readHeader(self.fullfilename)
# self.load_data()
self.basep = Parameter.create(name=self.basename, type='group')
self.read_subsamp_param = Parameter.create(name='Read subsampling', type='int', value=1)
self.subsampling_param = Parameter.create(name='Plot subsampling', type='int', value=10)
self.basep.addChildren([self.read_subsamp_param,self.subsampling_param])
self.active_read_subsamp = None
self.active_subsampling = None
self.last_file_size = None
self.children = []
for idx, header in enumerate(self.headers):
columnData = ColumnData(self,header,idx)
self.children.append(columnData)
def file_changed(self):
file_size = os.stat(self.fullfilename).st_size
if self.last_file_size and self.last_file_size == file_size:
return False
self.last_file_size = file_size
return True
def load_data(self):
if self.file_changed() or (self.active_read_subsamp and self.active_read_subsamp != self.read_subsamp_param.value()):
self.active_read_subsamp = self.read_subsamp_param.value()
_ , self.array = readLines(self.fullfilename,read_every=self.active_read_subsamp)
print "Preparing array"
self.np_array = np.array(self.array,dtype=float)
self.make_sub_array()
else:
if self.active_subsampling != self.subsampling_param.value():
self.make_sub_array()
def make_sub_array(self):
print "Preparing subsampled array"
self.active_subsampling = self.subsampling_param.value()
self.sub_np_array = self.np_array[0::self.active_subsampling,:]
@property
def subsampling(self):
return self.subsampling_param.value
class TimeAxisItem(pg.AxisItem):
def __init__(self, *args, **kwargs):
super(TimeAxisItem,self).__init__(*args, **kwargs)
def tickStrings(self, values, scale, spacing):
return [time.strftime("%H:%M:%S\n%d-%m", time.localtime(value)) for value in values]
class LongValueAxisItem(pg.AxisItem):
def __init__(self, *args, **kwargs):
super(LongValueAxisItem,self).__init__(*args, **kwargs)
def tickStrings(self, values, scale, spacing):
return ["{:.12g}".format(value) for value in values]
class MyWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.filedatas = []
area = DockArea()
self.setCentralWidget(area)
self.setWindowTitle('Allan')
self.resize(1800,900)
d1 = Dock("Controls", size=(300, 1))
d2 = Dock("Graphs", size=(900, 1))
d3 = Dock("Allan dev", size=(600, 1))
area.addDock(d1, 'left')
area.addDock(d2, 'right')
area.addDock(d3, 'right')
pg.setConfigOptions(antialias=True)
add_file_action = Parameter.create(name='Add files', type='action')
remove_files_action = Parameter.create(name='Remove files', type='action')
self.file_group = Parameter.create(name='Files', type='group')
self.columns_group = Parameter.create(name='Columns', type='group')
plot_action = Parameter.create(name='Plot', type='action')
reload_action = Parameter.create(name='Reload and plot', type='action')
self.link_x_views_param = Parameter.create(name='Link x axis', type='bool', value=True)
allan_group = Parameter.create(name='Allan Dev', type='group')
self.allan_var_param = Parameter.create(name='Variable',type='list', values= {"":None})
self.fractional_var_param = Parameter.create(name='Fractionalize',type='bool',value=True)
update_allan_action = Parameter.create(name='Update Allan Dev', type='action')
allan_group.addChildren([self.allan_var_param,self.fractional_var_param,update_allan_action])
params = [add_file_action,remove_files_action,self.file_group,self.columns_group,plot_action,reload_action,self.link_x_views_param,allan_group]
self.base_param = Parameter.create(name='params', type='group', children=params)
self.param_tree = ParameterTree()
self.param_tree.setParameters(self.base_param, showTop=False)
d1.addWidget(self.param_tree)
self.base_param.sigTreeStateChanged.connect(self.change)
add_file_action.sigActivated.connect(self.add_file)
remove_files_action.sigActivated.connect(self.remove_files)
reload_action.sigActivated.connect(self.reload_data)
plot_action.sigActivated.connect(self.plot)
update_allan_action.sigActivated.connect(self.updateAllan)
# Time graphs
self.plots_layout = pg.GraphicsLayoutWidget() # border=(100,100,100)
self.plots = []
d2.addWidget(self.plots_layout)
# Allan dev graph
l2 = pg.GraphicsLayoutWidget() # border=(100,100,100)
d3.addWidget(l2)
self.pa = l2.addPlot(title="Stability")
self.pa.setLabel('left', "Allan deviation")
self.pa.setLabel('bottom', "Integration time", units='s')
self.curvea = self.pa.plot(pen="g")
self.err_bara = pg.ErrorBarItem(x=0,y=0,beam=0.05,pen=pg.mkPen(0,255,0,100))
self.pa.setLogMode(x=True, y=True)
self.pa.addItem(self.err_bara)
self.pa.showGrid(x=True, y=True, alpha=1)
self.show()
def change(self,param,changes):
for param, change, data in changes:
if param is self.link_x_views_param:
if self.link_x_views_param.value(): self.link_plots_x_axis(True)
else: self.link_plots_x_axis(False)
def file_already_opened(self, filename):
for fd in self.filedatas:
if fd.fullfilename == filename:
return True
return False
def add_file(self):
filenames = open_filename()
for filename in filenames:
if not self.file_already_opened(filename):
fd = FileData(self,filename)
self.filedatas.append(fd)
self.file_group.addChild(fd.basep)
self.update_column_list()
def find_column_param(self,header):
for header_param in self.columns_group.children():
if header_param.name() == header:
return header_param
return None
def update_column_list(self):
all_headers = []
for fd in self.filedatas:
for cd in fd.children:
if not cd.time_data:
all_headers.append(cd.header)
all_headers = list(set(all_headers))
self.columns_group.clearChildren()
for header in all_headers:
value = header not in default_hidden_columns
param = Parameter.create(name=header, type='bool', value=value)
self.columns_group.addChild(param)
def remove_files(self):
self.file_group.clearChildren()
self.filedatas = []
def reload_data(self):
for fd in self.filedatas:
fd.load_data()
self.plot()
def find_plot(self, columnName):
for item in self.plots:
if item['columnName'] == columnName:
return item['plot']
return None
def clear_plots(self):
for item in self.plots:
plot = item['plot']
plot.clear()
def remove_empty_plots(self):
for item in self.plots:
plot = item['plot']
if len(plot.listDataItems())==0:
self.plots_layout.removeItem(plot)
self.plots.remove(item)
def get_plot_list(self):
return [item['plot'] for item in self.plots]
def link_plots_x_axis(self,link=True):
plot_list = self.get_plot_list()
if len(plot_list)>1:
plot_list[0].setXLink(None)
for plot in plot_list[1:]:
if link: plot.setXLink(plot_list[0])
else: plot.setXLink(None)
def plot(self):
columnsList = []
self.clear_plots()
for fd in self.filedatas:
fd.make_sub_array()
for columnData in fd.children:
param = self.find_column_param(columnData.header)
if columnData.time_data or (param and param.value() == True):
columnData.data = fd.np_array[:,columnData.index]
columnData.sub_data = fd.sub_np_array[:,columnData.index]
if columnData.time_data:
fd.timeColumnData = columnData
else:
columnsList.append(columnData)
existing_plot = self.find_plot(columnData.header)
if existing_plot:
plot = existing_plot
else:
plot = self.plots_layout.addPlot(title=columnData.header,axisItems={'bottom': TimeAxisItem(orientation='bottom'), 'left': LongValueAxisItem(orientation='left')})
self.plots_layout.nextRow()
titleStyle = {'size': '9pt','align':'left'}
plot.setTitle(columnData.header,**titleStyle)
plot.showGrid(x=True, y=True, alpha=0.5)
plot.setDownsampling(mode='peak')
plot.setClipToView(True)
self.plots.append({"columnName":columnData.header,'plot':plot})
columnData.plot = plot
curve = plot.plot(pen="w")
curve.setData(x=fd.timeColumnData.sub_data,y=columnData.sub_data)
self.remove_empty_plots()
self.link_plots_x_axis(True)
plot_list = self.get_plot_list()
if len(plot_list)>4:
plot_list[-1].getAxis('bottom').setStyle(showValues=True)
for plot in plot_list[0:-1]:
plot.getAxis('bottom').setStyle(showValues=False)
columnsDic = {}
for column in columnsList:
columnsDic.update({column.header:column})
self.allan_var_param.setLimits(columnsDic)
def updateAllan(self):
# Get the selected column
column = self.allan_var_param.value()
data = column.data
times = column.parent.timeColumnData.data
# Check if DAQ must be used
daq_times = None
for sisterCol in column.parent.children:
if "DAQ time" in sisterCol.header:
sisterCol.data = column.parent.np_array[:,sisterCol.index]
daq_times = sisterCol.data
minTime, maxTime = column.plot.getViewBox().viewRange()[0]
indexes = np.argwhere(np.logical_and(times >= minTime,times <= maxTime))
if daq_times is not None:
rate = 1./(daq_times[1] - daq_times[0])
else: rate = len(times)/(max(times)-min(times))
if self.fractional_var_param.value():
data_freq = data[indexes]/data[indexes[0]]
else:
data_freq = data[indexes]
taus = np.logspace(0.0,4.0,num=50)
sys.stdout.write("Computing Allan deviation ")
(tau_used, ad , ade, adn) = allantools.adev(data_freq, rate, taus)
sys.stdout.write("Done\n")
x = tau_used
y = ad
top_error = np.log10(ad + ade/2) - np.log10(ad)
bot_error = np.log10(ad) - np.log10(ad - ade/2)
self.err_bara.setData(x=np.log10(x), y=np.log10(y), top=top_error, bottom=bot_error)
self.curvea.setData(x=x,y=y)
if self.fractional_var_param.value():
self.pa.setTitle("Fractional - "+column.header)
else:
self.pa.setTitle(column.header)
app = QtGui.QApplication([])
win = MyWindow()
if __name__=="__main__":
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
ret = QtGui.QApplication.instance().exec_()
sys.exit(ret)