-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrtbsa.py
More file actions
executable file
·1297 lines (1014 loc) · 48.4 KB
/
rtbsa.py
File metadata and controls
executable file
·1297 lines (1014 loc) · 48.4 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/local/lcls/package/python/current/bin/python
# Written by Zimmer, edited by Ahmed, refactored by Lisa
from os import path
from sys import argv, exit
from time import time
from epics import PV
# TODO import these with the namespace
from numpy import (polyfit, poly1d, polyval, corrcoef, std, mean, concatenate,
empty, nan, zeros, isnan, linalg, abs,
fft, argsort, interp, arange, nanmin, nanmax)
from PyQt4.QtCore import QTimer, QObject, SIGNAL, Qt
from PyQt4.QtGui import (QMainWindow, QLabel, QGridLayout, QPalette,
QApplication, QAction, QFileDialog, QIcon, QMessageBox)
from pyqtgraph import PlotWidget, PlotCurveItem, ScatterPlotItem, TextItem
from scipy.stats import nanmean, nanstd
from subprocess import CalledProcessError, check_output
from rtbsa_UI import Ui_RTBSA
import rtbsaUtils
# noinspection PyArgumentList,PyCompatibility
class RTBSA(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.help_menu = self.menuBar().addMenu("&Help")
self.file_menu = self.menuBar().addMenu("&File")
self.status_text = QLabel()
self.plot = PlotWidget(alpha=0.75)
self.ui = Ui_RTBSA()
self.ui.setupUi(self)
self.setWindowTitle('Real Time BSA')
self.loadStyleSheet()
self.setUpGraph()
self.bsapvs = ['GDET:FEE1:241:ENRC', 'GDET:FEE1:242:ENRC',
'GDET:FEE1:361:ENRC', 'GDET:FEE1:362:ENRC']
self.populateBSAPVs()
self.connectGuiFunctions()
# Initial number of points
self.numPoints = 2800
# Initial number of standard deviations
self.stdDevstoKeep = 3.0
# 20ms polling time
self.updateTime = 50
# Set initial polynomial fit to 2
self.fitOrder = 2
self.disableInputs()
self.abort = True
# Used to update plot
self.timer = QTimer(self)
# self.ratePV = PV('IOC:IN20:EV01:RG01_ACTRATE')
self.ratePV = PV("EVNT:SYS0:1:LCLSBEAMRATE")
self.menuBar().setStyleSheet('QWidget{background-color:grey;color:purple}')
self.create_menu()
self.create_status_bar()
# The PV names
self.devices = {"A": "", "B": ""}
self.pvObjects = {"A": None, "B": None}
# The raw, unsynchronized, unfiltered buffers
self.rawBuffers = {"A": empty(2800), "B": empty(2800)}
# The times when each buffer finished its last data acquisition
self.timeStamps = {"A": None, "B": None}
self.synchronizedBuffers = {"A": empty(2800), "B": empty(2800)}
# Versions of data buffers A and B that are filtered by standard
# deviation. Didn't want to edit those buffers directly so that we could
# unfilter or refilter with a different number more efficiently
self.filteredBuffers = {"A": empty(2800), "B": empty(2800)}
# Text objects that appear on the plot
self.text = {"avg": None, "std": None, "slope": None, "corr": None}
# All things plot related!
self.plotAttributes = {"curve": None, "fit": None, "parab": None,
"frequencies": None}
# Used for the kill swtich
self.counter = {"A": 0, "B": 0}
# Used to implement scrolling for time plots
self.currIdx = {"A": 0, "B": 0}
def getRate(self):
# return rtbsaUtils.rateDict[self.ratePV.value]
return self.ratePV.value
def disableInputs(self):
self.ui.fitOrder.setDisabled(True)
self.ui.searchInputA.setDisabled(True)
self.ui.searchInputB.setDisabled(True)
self.ui.labelFitOrder.setDisabled(True)
self.ui.bsaListA.setDisabled(True)
self.ui.bsaListB.setDisabled(True)
self.statusBar().showMessage('Hi there! I missed you!')
self.ui.checkBoxPolyFit.setChecked(False)
def populateBSAPVs(self):
# Generate list of BSA PVS
try:
BSAPVs = check_output(['eget', '-ts', 'ds', '-a',
'tag=LCLS.BSA.rootnames']).splitlines()[1:-1]
self.bsapvs.extend(BSAPVs)
# Backup for eget error
except CalledProcessError:
print("Unable to pull most recent PV list")
# bsaPVs is pulled from the Constants file
self.bsapvs.extend(rtbsaUtils.bsaPVs)
# TODO un-hardcode machine and add common SPEAR PV's
except OSError:
print "Other machines coming soon to an RTBSA near you!"
pass
for pv in self.bsapvs:
self.ui.bsaListA.addItem(pv)
self.ui.bsaListB.addItem(pv)
def connectGuiFunctions(self):
# enter 1 is the text input box for device A, and 2 is for B
QObject.connect(self.ui.searchInputA, SIGNAL("textChanged(const QString&)"),
self.searchA)
QObject.connect(self.ui.searchInputB, SIGNAL("textChanged(const QString&)"),
self.searchB)
# Changes the text in the input box to match the selection from the list
self.ui.bsaListA.itemClicked.connect(self.setEnterA)
self.ui.bsaListB.itemClicked.connect(self.setEnterB)
# Dropdown menu for device A (add common BSA PV's)
self.ui.dropdownA.addItems(rtbsaUtils.commonlist)
# Make bunch length default selection
index = rtbsaUtils.commonlist.index("BLEN:LI24:886:BIMAX")
self.ui.dropdownA.setCurrentIndex(index)
self.ui.dropdownA.activated.connect(self.inputActivated)
# Dropdown menu for device B
self.ui.dropdownB.addItems(rtbsaUtils.commonlist)
self.ui.dropdownB.activated.connect(self.inputActivated)
# All the checkboxes in the Settings section
self.ui.checkBoxAvsT.clicked.connect(self.AvsTClick)
self.ui.checkBoxBvsA.clicked.connect(self.AvsBClick)
self.ui.checkBoxFFT.clicked.connect(self.AFFTClick)
self.ui.checkBoxShowAve.clicked.connect(self.avg_click)
self.ui.checkBoxShowStdDev.clicked.connect(self.std_click)
self.ui.checkBoxCorrCoeff.clicked.connect(self.corr_click)
self.ui.checkBoxPolyFit.clicked.connect(self.parab_click)
self.ui.checkBoxLinFit.clicked.connect(self.line_click)
self.ui.checkBoxShowGrid.clicked.connect(self.showGrid)
# All the buttons in the Controls section
self.ui.startButton.clicked.connect(self.initializePlot)
self.ui.stopButton.clicked.connect(self.stop)
self.ui.lclsLogButton.clicked.connect(self.logbook)
self.ui.mccLogButton.clicked.connect(self.MCCLog)
# fitedit is the text input box for "Order"
self.ui.fitOrder.returnPressed.connect(self.fitOrderActivated)
# The radio buttons that enable the dropdown menus
self.ui.dropdownButtonA.clicked.connect(self.common_1_click)
self.ui.dropdownButtonB.clicked.connect(self.common_2_click)
# The radio buttons that enable the search bars
self.ui.searchButtonA.clicked.connect(self.enter_1_click)
self.ui.searchButtonB.clicked.connect(self.enter_2_click)
# Pressing enter in the text input boxes for points and std dev triggers
# updating the plot
self.ui.numPoints.returnPressed.connect(self.points_entered)
self.ui.numStdDevs.returnPressed.connect(self.stdDevEntered)
# Triggers a redrawing upon pressing enter in the search bar.
# Proper usage should be using the search bar to search, and selecting
# from the results in the list. If it's not in the list, it's an invalid
# PV with no reason to attempt plotting
self.ui.searchInputA.returnPressed.connect(self.inputActivated)
self.ui.searchInputB.returnPressed.connect(self.inputActivated)
def showGrid(self):
self.plot.showGrid(self.ui.checkBoxShowGrid.isChecked(),
self.ui.checkBoxShowGrid.isChecked())
def setUpGraph(self):
layout = QGridLayout()
self.ui.widgetPlot.setLayout(layout)
layout.addWidget(self.plot, 0, 0)
self.plot.showGrid(1, 1)
def loadStyleSheet(self):
currDir = path.abspath(path.dirname(__file__))
cssFile = path.join(currDir, "style.css")
try:
with open(cssFile, "r") as f:
self.setStyleSheet(f.read())
except IOError:
print("Error loading style sheet")
pass
def create_status_bar(self):
palette = QPalette()
palette.setColor(palette.Foreground, Qt.magenta)
self.statusBar().addWidget(self.status_text, 1)
self.statusBar().setPalette(palette)
# Effectively an autocomplete
def search(self, enter, widget):
widget.clear()
query = str(enter.text())
for pv in self.bsapvs:
if query.lower() in pv.lower():
widget.addItem(pv)
def searchA(self):
self.search(self.ui.searchInputA, self.ui.bsaListA)
def searchB(self):
self.search(self.ui.searchInputB, self.ui.bsaListB)
def setEnter(self, widget, enter, search, enter_rb):
selection = widget.currentItem()
enter.textChanged.disconnect()
enter.setText(selection.text())
QObject.connect(enter, SIGNAL("textChanged(const QString&)"), search)
if not self.abort and enter_rb.isChecked():
self.stop()
self.timer.singleShot(250, self.initializePlot)
def setEnterA(self):
self.setEnter(self.ui.bsaListA, self.ui.searchInputA, self.searchA,
self.ui.searchButtonA)
def setEnterB(self):
self.setEnter(self.ui.bsaListB, self.ui.searchInputB, self.searchB,
self.ui.searchButtonB)
def correctInput(self, errorMessage, acceptableTxt, textBox):
self.statusBar().showMessage(errorMessage, 6000)
textBox.setText(acceptableTxt)
def correctNumpoints(self, errorMessage, acceptableValue):
self.correctInput(errorMessage, str(acceptableValue), self.ui.numPoints)
self.numPoints = acceptableValue
def correctStdDevs(self, errorMessage, acceptableValue):
self.correctInput(errorMessage, str(acceptableValue),
self.ui.numStdDevs)
self.stdDevstoKeep = acceptableValue
def stdDevEntered(self):
try:
self.stdDevstoKeep = float(self.ui.numStdDevs.text())
if self.stdDevstoKeep <= 0:
raise ValueError
except ValueError:
self.correctStdDevs('Enter a float > 0', 3.0)
return
def points_entered(self):
try:
self.numPoints = int(self.ui.numPoints.text())
except ValueError:
self.correctNumpoints('Enter an integer, 1 to 2800', 120)
return
if self.numPoints > 2800:
self.correctNumpoints('Max # points is 2800', 2800)
return
if self.numPoints < 1:
self.correctNumpoints('Min # points is 1', 1)
return
self.reinitialize_plot()
############################################################################
# Where the magic happens (well, where it starts to happen). This
# initializes the BSA plotting and then starts a timer to update the plot.
############################################################################
def initializePlot(self):
plotTypeIsValid = (self.ui.checkBoxAvsT.isChecked()
or self.ui.checkBoxBvsA.isChecked()
or self.ui.checkBoxFFT.isChecked())
if not plotTypeIsValid:
self.statusBar().showMessage('Pick a Plot Type (PV vs. time '
'or B vs A)', 10000)
return
self.ui.startButton.setDisabled(True)
self.abort = False
self.cleanPlot()
self.pvObjects["A"], self.pvObjects["B"] = None, None
# Plot history buffer for one PV
if self.ui.checkBoxAvsT.isChecked():
if self.populateDevices(self.ui.dropdownButtonA, self.ui.dropdownA,
self.ui.searchButtonA, self.ui.searchInputA,
"A"):
self.genPlotAndSetTimer(self.genTimePlotA,
self.updateTimePlotA)
# Plot for 2 PVs
elif self.ui.checkBoxBvsA.isChecked():
if self.updateValsFromInput():
self.genPlotAndSetTimer(self.genPlotAB,
self.updatePlotAB)
# Plot power spectrum
else:
if self.populateDevices(self.ui.dropdownButtonA, self.ui.dropdownA,
self.ui.searchButtonA, self.ui.searchInputA,
"A"):
self.genPlotAndSetTimer(self.InitializeFFTPlot,
self.updatePlotFFT)
def populateDevices(self, common_rb, common, enter_rb, enter, device):
if common_rb.isChecked():
self.devices[device] = str(common.currentText())
elif enter_rb.isChecked():
pv = str(enter.text()).strip()
# Checks that it's non empty and that it's a BSA pv
if pv and pv in self.bsapvs:
self.devices[device] = pv
else:
self.printStatus('Device ' + device + ' invalid. Aborting.')
self.ui.startButton.setEnabled(True)
return False
return True
def printStatus(self, message, printToXterm=True):
if printToXterm:
print message
self.statusBar().showMessage(message)
def updateValsFromInput(self):
if not self.populateDevices(self.ui.dropdownButtonA, self.ui.dropdownA,
self.ui.searchButtonA, self.ui.searchInputA,
"A"):
return False
if not self.populateDevices(self.ui.dropdownButtonB, self.ui.dropdownB,
self.ui.searchButtonB, self.ui.searchInputB,
"B"):
return False
self.printStatus("Initializing/Synchronizing " + self.devices["A"]
+ " vs. " + self.devices["B"] + " buffers...")
self.initializeBuffers()
return True
def initializeBuffers(self):
# Initial population of our buffers using the HSTBR PV's in our
# callback functions
self.clearAndUpdateCallbacks("HSTBR", resetTime=True)
while ((not self.timeStamps["A"] or not self.timeStamps["B"])
and not self.abort):
QApplication.processEvents()
self.adjustSynchronizedBuffers(True)
# Switch to BR PVs to avoid pulling an entire history buffer on every
# update.
self.clearAndUpdateCallbacks("BR", resetRawBuffer=True)
def clearAndUpdateCallbacks(self, suffix, resetTime=False,
resetRawBuffer=False):
self.clearAndUpdateCallback("A", suffix, self.callbackA,
self.devices["A"], resetTime,
resetRawBuffer)
self.clearAndUpdateCallback("B", suffix, self.callbackB,
self.devices["B"], resetTime,
resetRawBuffer)
# noinspection PyTypeChecker
def clearAndUpdateCallback(self, device, suffix, callback, pvName,
resetTime=False, resetRawBuffer=False):
self.clearPV(device)
# Without the time parameter, we wouldn't get the timestamp
self.pvObjects[device] = PV(pvName + suffix, form='time')
if resetTime:
self.timeStamps[device] = None
# Make sure that the initial raw buffer is synchronized and pad with
# nans if it's less than 2800 points long
if resetRawBuffer:
nanArray = empty(2800 - self.synchronizedBuffers[device].size)
nanArray[:] = nan
self.rawBuffers[device] = \
concatenate([self.synchronizedBuffers[device], nanArray])
self.pvObjects[device].add_callback(callback)
# Callback function for Device A
# noinspection PyUnusedLocal
def callbackA(self, pvname=None, value=None, timestamp=None, **kw):
self.updateTimeAndBuffer("A", pvname, timestamp, value)
# Callback function for Device B
# noinspection PyUnusedLocal
def callbackB(self, pvname=None, value=None, timestamp=None, **kw):
self.updateTimeAndBuffer("B", pvname, timestamp, value)
############################################################################
# This is where the data is actually acquired and saved to the buffers.
# Callbacks are effectively listeners that listen for change, so we
# basically put a callback on the PVs of interest (devices A and/or B) so
# that every time the value of that PV changes, we get that new value and
# append it to our raw data buffer for that device.
# Initialization of the buffer is slightly different in that the listener is
# put on the history buffer of that PV (denoted by the HSTBR suffix), so
# that we just immediately write the previous 2800 points to our raw buffer
############################################################################
def updateTimeAndBuffer(self, device, pvname, timestamp, value):
def padRawBufferWithNans(start, end):
rtbsaUtils.padWithNans(self.rawBuffers[device], start, end)
if "HSTBR" in pvname:
self.timeStamps[device] = timestamp
# value is the buffer because we're monitoring the HSTBR PV
self.rawBuffers[device] = value
# Reset the counter every time we reinitialize the plot
self.counter[device] = 0
else:
if not self.timeStamps[device]:
return
rate = self.getRate()
if rate < 1:
return
scalingFactor = 1.0 / rate
elapsedPulses = round((timestamp - self.timeStamps[device])
/ scalingFactor)
currIdx = int((timestamp / scalingFactor) % self.numPoints)
if elapsedPulses <= 0:
return
# Pad the buffer with nans for missed pulses
elif elapsedPulses > 1:
# noinspection PyTypeChecker
lastIdx = int((self.timeStamps[device] / scalingFactor)
% self.numPoints)
# Take care of wrap around
if currIdx < lastIdx:
padRawBufferWithNans(lastIdx + 1, self.numPoints)
padRawBufferWithNans(0, currIdx)
else:
padRawBufferWithNans(lastIdx + 1, currIdx)
# Directly index into the raw buffer using the timestamp
self.rawBuffers[device][currIdx] = value
self.counter[device] += elapsedPulses
self.timeStamps[device] = timestamp
self.currIdx[device] = currIdx
def clearPV(self, device):
pv = self.pvObjects[device]
if pv:
pv.clear_callbacks()
pv.disconnect()
def adjustSynchronizedBuffers(self, syncByTime=False):
numBadShots = self.populateSynchronizedBuffers(syncByTime)
blength = 2800 - numBadShots
# Make sure the buffer size doesn't exceed the desired number of points
if self.numPoints < blength:
self.synchronizedBuffers["A"] = \
self.synchronizedBuffers["A"][:self.numPoints]
self.synchronizedBuffers["B"] = \
self.synchronizedBuffers["B"][:self.numPoints]
# A spin loop that waits until the beam rate is at least 1Hz
def waitForRate(self):
start_time = time()
gotStuckAndNeedToUpdateMessage = False
# self.rate is a PV, such that .value is shorthand for .getval
while self.ratePV.value < 1:
# while self.ratePV.value < 2:
# # noinspection PyArgumentList
QApplication.processEvents()
#
if time() - start_time > 0.5:
gotStuckAndNeedToUpdateMessage = True
self.printStatus("Waiting for beam rate to be at least 1Hz...",
False)
if gotStuckAndNeedToUpdateMessage:
self.printStatus("Running", False)
# return rtbsaUtils.rateDict[self.ratePV.value]
return self.ratePV.value
############################################################################
# Time 1 is when Device A started acquiring data, and Time 2 is when Device
# B started acquiring data. Since they're not guaranteed to start
# acquisition at the same time, one data buffer might be ahead of the other,
# meaning that the intersection of the two buffers would not include the
# first n elements of one and the last n elements of the other. See the
# diagram below, where the dotted line represents the time axis (one buffer
# is contained by square brackets [], the other by curly braces {}, and the
# times where each starts and ends is indicated right underneath).
#
#
# [ { ] }
# <----------------------------------------------------------------------> t
# t1_start t2_start t1_end t2_end
#
#
# Note that both buffers are of the same size (2800) so that:
# (t1_end - t1_start) = (t2_end - t2_start)
#
# From the diagram, we see that only the time between t2_start and t1_end
# contains data from both buffers (t1_start to t2_start only contains data
# from buffer 1, and t1_end to t2_end only contains data from buffer 2).
# Using that, we can chop the beginning of buffer 1 and the end of buffer 2
# so that we're only left with the overlapping region.
#
# In order to figure out how many points we need to chop from each buffer
# (it's the same number for both since they're both the same size), we
# multiply the time delta by the beam rate (yay dimensional analysis!):
# seconds * (shots/second) = (number of shots)
#
# That whole rigmarole only applies to the initial population of the buffers
# (where we're pulling the entire history buffer at once using the HSTBR
# suffix). From then on, we're indexing into the raw buffers using the
# pulse ID modulo 2800, so they're inherently synchronized
############################################################################
def populateSynchronizedBuffers(self, syncByTime):
def padSyncBufferWithNans(device, startIdx, endIdx):
lag = endIdx - startIdx
if lag > 20:
print ("Reinitializing buffers due to " + str(lag)
+ " shot lag for device " + device)
self.initializeBuffers()
else:
rtbsaUtils.padWithNans(self.synchronizedBuffers[device],
startIdx + 1, endIdx + 1)
def checkIndices(device, startIdx, endIdx):
# Check for index wraparound
if endIdx < startIdx:
padSyncBufferWithNans(device, startIdx, self.numPoints - 1)
padSyncBufferWithNans(device, 0, endIdx)
else:
padSyncBufferWithNans(device, startIdx, endIdx)
if syncByTime:
numBadShots = int(round((self.timeStamps["B"]
- self.timeStamps["A"])
* self.getRate()))
startA, endA = rtbsaUtils.getIndices(numBadShots, 1)
startB, endB = rtbsaUtils.getIndices(numBadShots, -1)
self.synchronizedBuffers["A"] = self.rawBuffers["A"][startA:endA]
self.synchronizedBuffers["B"] = self.rawBuffers["B"][startB:endB]
return abs(numBadShots)
else:
self.synchronizedBuffers["A"] = self.rawBuffers["A"]
self.synchronizedBuffers["B"] = self.rawBuffers["B"]
# The timestamps and indices get updated by the callbacks, so we
# store the values at the time of buffer-copying
timeStampA = self.timeStamps["A"]
timeStampB = self.timeStamps["B"]
currIdxA = self.currIdx["A"]
currIdxB = self.currIdx["B"]
diff = timeStampA - timeStampB
if diff > 0:
checkIndices("A", currIdxB, currIdxA)
elif diff < 0:
checkIndices("B", currIdxA, currIdxB)
return 0
def genPlotAndSetTimer(self, genPlot, updateMethod):
if self.abort:
return
try:
genPlot()
except UnboundLocalError:
self.printStatus('No Data, Aborting Plotting Algorithm')
return
self.timer = QTimer(self)
# Run updateMethod every updatetime milliseconds
self.timer.singleShot(self.updateTime, updateMethod)
self.printStatus('Running')
# noinspection PyTypeChecker
def genTimePlotA(self):
newData = self.initializeData()
if not newData.size:
self.printStatus('Invalid PV? Unable to get data. Aborting.')
self.ui.startButton.setEnabled(True)
return
data = newData[:self.numPoints]
self.plotAttributes["curve"] = PlotCurveItem(data, pen=1)
self.plot.addItem(self.plotAttributes["curve"])
self.plotFit(arange(self.numPoints), data, self.devices["A"])
############################################################################
# This is the main plotting function for "Plot A vs Time" that gets called
# every self.updateTime seconds
# noinspection PyTypeChecker
############################################################################
def updateTimePlotA(self):
if not self.checkPlotStatus():
return
xData, yData = self.filterTimePlotBuffer()
if yData.size:
self.plotAttributes["curve"].setData(yData)
if self.ui.checkBoxAutoscale.isChecked():
mx = max(yData)
mn = min(yData)
if mx - mn > .00001:
self.plot.setYRange(mn, mx)
self.plot.setXRange(0, len(yData))
if self.ui.checkBoxShowAve.isChecked():
rtbsaUtils.setPosAndText(self.text["avg"], mean(yData), 0,
min(yData), 'AVG: ')
if self.ui.checkBoxShowStdDev.isChecked():
rtbsaUtils.setPosAndText(self.text["std"], std(yData),
self.numPoints / 4, min(yData),
'STD: ')
if self.ui.checkBoxCorrCoeff.isChecked():
self.text["corr"].setText('')
if self.ui.checkBoxLinFit.isChecked():
self.text["slope"].setPos(self.numPoints / 2, min(yData))
self.getLinearFit(xData, yData, True)
elif self.ui.checkBoxPolyFit.isChecked():
self.text["slope"].setPos(self.numPoints / 2, min(yData))
self.getPolynomialFit(xData, yData, True)
self.timer.singleShot(self.updateTime, self.updateTimePlotA)
def checkPlotStatus(self):
QApplication.processEvents()
if self.abort:
return False
self.waitForRate()
# kill switch to stop backgrounded, forgetten GUIs. Somewhere in the
# ballpark of 20 minutes assuming 120Hz
if self.counter["A"] > 150000:
self.stop()
self.printStatus("Stopping due to inactivity")
return True
def filterTimePlotBuffer(self):
currIdx = self.currIdx["A"]
choppedBuffer = self.rawBuffers["A"][:self.numPoints]
# All this nonsense to make it scroll :P Thanks to Ben for the
# inspiration!
if currIdx > 0:
choppedBuffer = concatenate([choppedBuffer[currIdx:],
choppedBuffer[:currIdx]])
xData, yData = rtbsaUtils.filterBuffers(choppedBuffer,
lambda x: ~isnan(x),
arange(self.numPoints),
choppedBuffer)
if self.devices["A"] == "BLEN:LI24:886:BIMAX":
xData, yData = rtbsaUtils.filterBuffers(yData,
lambda x:
x < rtbsaUtils.IPK_LIMIT,
xData, yData)
if self.ui.checkBoxStdDev.isChecked():
stdDevFilterFunc = self.StdDevFilterFunc(mean(yData), std(yData))
xData, yData = rtbsaUtils.filterBuffers(yData, stdDevFilterFunc,
xData, yData)
return xData, yData
def getLinearFit(self, xData, yData, updateExistingPlot):
# noinspection PyTupleAssignmentBalance
m, b = polyfit(xData, yData, 1)
fitData = polyval([m, b], xData)
self.text["slope"].setText('Slope: ' + str("{:.3e}".format(m)))
if updateExistingPlot:
self.plotAttributes["fit"].setData(xData, fitData)
else:
# noinspection PyTypeChecker
self.plotAttributes["fit"] = PlotCurveItem(xData, fitData, 'g-',
linewidth=1)
def getPolynomialFit(self, xData, yData, updateExistingPlot):
co = polyfit(xData, yData, self.fitOrder)
pol = poly1d(co)
xDataSorted = sorted(xData)
fit = pol(xDataSorted)
if updateExistingPlot:
self.plotAttributes["parab"].setData(xDataSorted, fit)
else:
# noinspection PyTypeChecker
self.plotAttributes["parab"] = PlotCurveItem(xDataSorted, fit,
pen=3, size=2)
if self.fitOrder == 2:
self.text["slope"].setText('Peak: ' + str(-co[1] / (2 * co[0])))
elif self.fitOrder == 3:
self.text["slope"].setText(str("{:.2e}".format(co[0])) + 'x^3'
+ str("+{:.2e}".format(co[1]))
+ 'x^2'
+ str("+{:.2e}".format(co[2])) + 'x'
+ str("+{:.2e}".format(co[3])))
def genPlotAB(self):
if self.ui.checkBoxStdDev.isChecked():
self.plotCurveAndFit(self.filteredBuffers["A"],
self.filteredBuffers["B"])
else:
self.plotCurveAndFit(self.synchronizedBuffers["A"],
self.synchronizedBuffers["B"])
def plotCurveAndFit(self, xData, yData):
# noinspection PyTypeChecker
self.plotAttributes["curve"] = ScatterPlotItem(xData, yData, pen=1,
symbol='x', size=5)
self.plot.addItem(self.plotAttributes["curve"])
self.plotFit(xData, yData,
self.devices["B"] + ' vs. ' + self.devices["A"])
def plotFit(self, xData, yData, title):
self.plot.addItem(self.plotAttributes["curve"])
self.plot.setTitle(title)
# Fit line
if self.ui.checkBoxLinFit.isChecked():
self.getLinearFit(xData, yData, False)
self.plot.addItem(self.plotAttributes["fit"])
# Fit polynomial
elif self.ui.checkBoxPolyFit.isChecked():
self.ui.fitOrder.setDisabled(False)
try:
self.getPolynomialFit(xData, yData, False)
self.plot.addItem(self.plotAttributes["parab"])
except linalg.linalg.LinAlgError:
print "Error getting polynomial fit"
############################################################################
# This is the main plotting function for "Plot B vs A" that gets called
# every self.updateTime milliseconds
############################################################################
def updatePlotAB(self):
if not self.checkPlotStatus():
return
QApplication.processEvents()
self.adjustSynchronizedBuffers()
self.filterNans()
self.filterPeakCurrent()
if self.ui.checkBoxStdDev.isChecked():
self.filterStdDev()
self.updateLabelsAndFit(self.filteredBuffers["A"],
self.filteredBuffers["B"])
else:
self.updateLabelsAndFit(self.synchronizedBuffers["A"],
self.synchronizedBuffers["B"])
self.timer.singleShot(self.updateTime, self.updatePlotAB)
def filterNans(self):
def filterFunc(x): return ~isnan(x)
self.filterData(self.synchronizedBuffers["A"], filterFunc, True)
self.filterData(self.synchronizedBuffers["B"], filterFunc, True)
# Need to filter out errant indices from both buffers to keep them
# synchronized
def filterData(self, dataBuffer, filterFunc, changeOriginal):
bufferA, bufferB = rtbsaUtils.filterBuffers(dataBuffer, filterFunc,
self.synchronizedBuffers[
"A"],
self.synchronizedBuffers[
"B"])
if changeOriginal:
self.synchronizedBuffers["A"] = bufferA
self.synchronizedBuffers["B"] = bufferB
else:
self.filteredBuffers["A"] = bufferA
self.filteredBuffers["B"] = bufferB
# This PV gets insane values, apparently
def filterPeakCurrent(self):
def filterFunc(x):
return x < rtbsaUtils.IPK_LIMIT
if self.devices["A"] == "BLEN:LI24:886:BIMAX":
self.filterData(self.synchronizedBuffers["A"], filterFunc, True)
if self.devices["B"] == "BLEN:LI24:886:BIMAX":
self.filterData(self.synchronizedBuffers["B"], filterFunc, True)
def filterStdDev(self):
bufferA = self.synchronizedBuffers["A"]
bufferB = self.synchronizedBuffers["B"]
self.filterData(bufferA, self.StdDevFilterFunc(mean(bufferA),
std(bufferA)), False)
self.filterData(bufferB, self.StdDevFilterFunc(mean(bufferB),
std(bufferB)), False)
def StdDevFilterFunc(self, average, stdDev):
return lambda x: abs(x - average) < self.stdDevstoKeep * stdDev
# noinspection PyTypeChecker
def updateLabelsAndFit(self, bufferA, bufferB):
self.plotAttributes["curve"].setData(bufferA, bufferB)
try:
if self.ui.checkBoxAutoscale.isChecked():
self.setPlotRanges(bufferA, bufferB)
minBufferA = nanmin(bufferA)
minBufferB = nanmin(bufferB)
maxBufferA = nanmax(bufferA)
maxBufferB = nanmax(bufferB)
if self.ui.checkBoxShowAve.isChecked():
rtbsaUtils.setPosAndText(self.text["avg"], nanmean(bufferB),
minBufferA,
minBufferB, 'AVG: ')
if self.ui.checkBoxShowStdDev.isChecked():
xPos = (minBufferA + (minBufferA + maxBufferA) / 2) / 2
rtbsaUtils.setPosAndText(self.text["std"], nanstd(bufferB),
xPos, minBufferB, 'STD: ')
if self.ui.checkBoxCorrCoeff.isChecked():
correlation = corrcoef(bufferA, bufferB)
rtbsaUtils.setPosAndText(self.text["corr"], correlation.item(1),
minBufferA, maxBufferB,
"Corr. Coefficient: ")
if self.ui.checkBoxLinFit.isChecked():
self.text["slope"].setPos((minBufferA + maxBufferA) / 2,
minBufferB)
self.getLinearFit(bufferA, bufferB, True)
elif self.ui.checkBoxPolyFit.isChecked():
self.text["slope"].setPos((minBufferA + maxBufferA) / 2,
minBufferB)
self.getPolynomialFit(bufferA, bufferB, True)
except ValueError:
print "Error updating plot range"
def setPlotRanges(self, bufferA, bufferB):
mx = nanmax(bufferB)
mn = nanmin(bufferB)
if mn != mx:
self.plot.setYRange(mn, mx)
mx = nanmax(bufferA)
mn = nanmin(bufferA)
if mn != mx:
self.plot.setXRange(mn, mx)
def InitializeFFTPlot(self):
self.genPlotFFT(self.initializeData(), False)
# TODO I have no idea what's happening here
def genPlotFFT(self, newdata, updateExistingPlot):
if not newdata.size:
return None
newdata = newdata[:self.numPoints]
nans, x = isnan(newdata), lambda z: z.nonzero()[0]
# interpolate nans
newdata[nans] = interp(x(nans), x(~nans), newdata[~nans])
# remove DC component
newdata = newdata - mean(newdata)
newdata = concatenate([newdata, zeros(self.numPoints * 2)])
ps = abs(fft.fft(newdata)) / newdata.size
self.waitForRate()
frequencies = fft.fftfreq(newdata.size, 1.0 / self.getRate())
keep = (frequencies >= 0)
ps = ps[keep]
frequencies = frequencies[keep]
idx = argsort(frequencies)
if updateExistingPlot:
self.plotAttributes["curve"].setData(x=frequencies[idx], y=ps[idx])
else:
# noinspection PyTypeChecker
self.plotAttributes["curve"] = PlotCurveItem(x=frequencies[idx],
y=ps[idx], pen=1)
self.plot.addItem(self.plotAttributes["curve"])
self.plot.setTitle(self.devices["A"])
self.plotAttributes["frequencies"] = frequencies
return ps
# noinspection PyTypeChecker
def cleanPlot(self):
self.plot.clear()
self.text["avg"] = TextItem('', color=(200, 200, 250), anchor=(0, 1))