-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting.py
1404 lines (1160 loc) · 51.5 KB
/
plotting.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
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
'''Real-time plotting using pyqtgraph and PyQt5 packages.
This file is part of the EARS project <https://github.com/nalamat/ears>
Copyright (C) 2017-2021 Nima Alamatsaz <nima.alamatsaz@gmail.com>
'''
import time
import logging
import threading
import scipy.signal
import numpy as np
import scipy as sp
import tables as tb
import datetime as dt
import pyqtgraph as pg
from PyQt5 import QtCore, QtWidgets, QtGui
import hdf5
import misc
import pypeline
log = logging.getLogger(__name__)
# complib: zlib, lzo, bzip2, blosc, blosc:blosclz, blosc:lz4,
# blosc:lz4hc, blosc:snappy, blosc:zlib, blosc:zstd
# complevel: 0 (no compression) to 9 (maximum compression)
hdf5Filters = tb.Filters(complib='zlib', complevel=1)
class ScrollingPlotWidget(pg.PlotWidget):
'''A widget for plotting analog traces and epochs in a scrolling window.'''
@property
def fps(self):
'''Target rate of plotting in frames per second.'''
return self._fps
@property
def measuredFPS(self):
'''Actual rate of plotting in frames per second.'''
return self._measuredFPS
@property
def xRange(self):
'''The x-axis (time) range of the plot window in seconds.'''
return self._xRange
@property
def timeBase(self):
'''An instance of AnalogChannel to use as time base for determining
when to advance to the next plot window.'''
return self._timeBase
@timeBase.setter
def timeBase(self, value):
if not isinstance(value, (AnalogChannel, AnalogPlot)):
raise TypeError('`timeBase` can only be an instance '
'of `AnalogChannel`')
self._timeBase = value
def __init__(self, xRange=10, xGrid=1, yLimits=(-1,1), yGrid=1,
yLabel=None, fps=60, *args, **kwargs):
'''
Args:
xRange (float): The x-axis (time) range of the plot window in
seconds. Defaults to 10.
xGrid (float): Spacing between vertical grid lines. Defaults to 1.
yLimits (tuple of 2 floats): Minimum and maximum values shown on the
y-axis. Defaults to (1,-1).
yGrid (float or list-like of float): When given a single value,
determines spacing between the horizontal grid lines. If a list
of values is given, horizontal grid lines are drawn at y
intercepts specified by the list. Defaults to 1.
yLabel (str): Label to show on the y-axis. Defaults to None.
fps (float): Target plotting rate in frames per second.
Defaults to 60.
'''
super().__init__(*args, **kwargs)
# should not change after __init__
self._xRange = xRange
self._xGrid = xGrid
self._yLimits = yLimits
self._yGrid = yGrid
self._fps = fps
self._measuredFPS = 0
self._timeBase = None
self._updateLast = None
self._updateTimes = [] # keep last update times for measuring FPS
self._channels = []
self._tsMinLast = None # timestamp of last updated plot window
self._xTicksFormat = lambda x: '%02d:%02d' % (x//60,x%60)
self._timer = QtCore.QTimer()
self._timer.timeout.connect(self.update)
self._timer.setInterval(1000/self.fps)
self.setBackground('w')
self.setMouseEnabled(False, False)
self.setMenuEnabled(False)
self.setClipToView(False)
self.disableAutoRange()
self.setRange(xRange=(0, xRange), yRange=yLimits, padding=0)
self.setLimits(xMin=0, xMax=xRange, yMin=yLimits[0], yMax=yLimits[1])
self.hideButtons()
# draw all axis as black (draw box)
self.getAxis('left' ).setPen(pg.mkPen('k'))
self.getAxis('bottom').setPen(pg.mkPen('k'))
self.getAxis('right' ).setPen(pg.mkPen('k'))
self.getAxis('top' ).setPen(pg.mkPen('k'))
self.getAxis('bottom').setTicks([])
self.getAxis('right' ).setTicks([])
self.getAxis('right' ).show()
self.getAxis('top' ).show()
# set axis labels
self.getAxis('top' ).setLabel('<br />Time (min:sec)')
if yLabel is not None:
self.getAxis('left').setLabel('<br />' + yLabel)
gridMajorPen = pg.mkPen((150,150,150), style=QtCore.Qt.DashLine,
cosmetic=True)
gridMinorPen = pg.mkPen((150,150,150), style=QtCore.Qt.DotLine,
cosmetic=True)
# init x grid (vertical)
if xGrid:
self._xGridMajor = [None] * int(np.ceil(xRange/xGrid)-1)
self._xGridMinor = [None] * int(np.ceil(xRange/xGrid))
for i in range(len(self._xGridMajor)):
line = pg.InfiniteLine(None, 90, pen=gridMajorPen)
self.addItem(line)
self._xGridMajor[i] = line
for i in range(len(self._xGridMinor)):
line = pg.InfiniteLine(None, 90, pen=gridMinorPen)
self.addItem(line)
self._xGridMinor[i] = line
# draw x grid and ticks
self.getAxis('top').setStyle(tickLength=0)
self._updateXAxis()
# draw y grid (horizontal)
if yGrid is None: yGridAt = []
elif misc.listLike(yGrid): yGridAt = yGrid
else: yGridAt = np.arange(yLimits[0]+yGrid, yLimits[1], yGrid)
for y in yGridAt:
line = pg.InfiniteLine(y, 0, pen=gridMajorPen)
self.addItem(line)
# draw y ticks
yMajorTicks = [(y, '') for y in yGridAt]
yMinorTicks = []
self.getAxis('left').setTicks([yMajorTicks, yMinorTicks])
self.getAxis('left').setStyle(tickLength=0)
def _updateXAxis(self, xMin=0):
'''Update X ticks and grid'''
if not self._xGrid: return
# update vertical grid
for i in range(len(self._xGridMajor)):
self._xGridMajor[i].setValue(xMin+(i+1)*self._xGrid)
for i in range(len(self._xGridMinor)):
self._xGridMinor[i].setValue(xMin+(i+.5)*self._xGrid)
xMajorTicks = [(t, self._xTicksFormat(t))
for t in np.arange(xMin,xMin+self.xRange+self._xGrid/2,self._xGrid)]
xMinorTicks = [(t, self._xTicksFormat(t))
for t in np.arange(xMin+self._xGrid/2,xMin+self.xRange,self._xGrid)]
self.getAxis('top').setTicks([xMajorTicks, xMinorTicks])
def add(self, channel, label=None, labelOffset=0):
if not isinstance(channel, (AnalogChannel, AnalogPlot, BaseEpochChannel)):
raise TypeError('Plot should be an instance of AnalogChannel, '
'BaseEpochChannel or one of their subclasses')
if channel in self._channels:
raise RuntimeError('Cannot add the same channel twice')
self._channels.append(channel)
if label:
if not misc.listLike(label, False): label = [label ]
if not misc.listLike(labelOffset ): labelOffset = [labelOffset]
if len(label) != len(labelOffset):
raise ValueError('Number of `label`s (%d) should match number '
'of `labelOffset`s (%d)' % (len(label), len(labelOffset)))
ticks = self.getAxis('left')._tickLevels
for i in range(len(label)):
ticks = [ticks[0] + [(labelOffset[i],' '+label[i])], ticks[1]]
self.getAxis('left').setTicks(ticks)
# if not self._timer.isActive():
# self._updateLast = dt.datetime.now()
# self._timer.start()
def start(self):
if not self._timer.isActive():
self._updateLast = dt.datetime.now()
self._timer.start()
def stop(self):
if self._timer.isActive():
self._timer.stop()
self.update()
self._updateLast = None
def update(self):
try:
# log.debug('Updating plot')
# measure the actual fps
if self._updateLast is not None:
updateTime = (dt.datetime.now() -
self._updateLast).total_seconds()
self._updateTimes.append(updateTime)
if len(self._updateTimes)>60:
self._updateTimes.pop(0)
updateMean = np.mean(self._updateTimes)
fps = 1/updateMean if updateMean else 0
self._measuredFPS = fps
self._updateLast = dt.datetime.now()
# timing info to send to children
ts = None
tsMin = None
nextWindow = False
if self._timeBase is not None:
ts = self.timeBase.ts
tsMin = int(ts//self.xRange*self.xRange)
# determine when plot advances to next time window
if tsMin != self._tsMinLast:
self.setLimits(xMin=tsMin, xMax=tsMin+self.xRange)
self._updateXAxis(tsMin)
log.debug('Advancing to next plot window at %d', tsMin)
self._tsMinLast = tsMin
nextWindow = True
# update all channels
for channel in self._channels:
channel.update(ts, tsMin, nextWindow)
except:
log.exception('Failed to update plot, stopping plot timer')
self._timer.stop()
class FFTPlotWidget(pg.PlotWidget):
# '''A widget for plotting analog traces and epochs in a scrolling window.'''
@property
def fs(self):
'''Sampling frequency'''
return self._fs
@property
def lineCount(self):
return self._lineCount
@property
def fps(self):
'''Target rate of plotting in frames per second.'''
return self._fps
@property
def measuredFPS(self):
'''Actual rate of plotting in frames per second.'''
return self._measuredFPS
def __init__(self, fs, lineCount=1, xLimits=(0,5e-3), yLimits=(-5,5),
yScale=1, yOffset=0, yGap=1, chunk=.2, color=list('rgbcmyk'),
fps=60, *args, **kwargs):
'''
Args:
xLimits (tuple of 2 floats):
yLimits (tuple of 2 floats): Minimum and maximum values shown on the
y-axis. Defaults to (1,-1).
fps (float): Target plotting rate in frames per second.
Defaults to 60.
'''
super().__init__(*args, **kwargs)
# should not change after __init__
self._fs = fs
self._lineCount = lineCount
self._xLimits = xLimits
self._yLimits = yLimits
self._yScale = yScale
self._yOffset = yOffset
self._yGap = yGap
self._chunk = chunk
self._color = color if isinstance(color, list) else [color]
self._fps = fps
buffer1Size = int(self.fs*2)
self._buffer1 = misc.CircularBuffer((lineCount, buffer1Size))
self._measuredFPS = 0
self._updateLast = None
self._updateTimes = [] # keep last update times for measuring FPS
self._timer = QtCore.QTimer()
self._timer.timeout.connect(self.update)
self._timer.setInterval(1000/self.fps)
self.setBackground('w')
self.setMouseEnabled(False, False)
self.setMenuEnabled(False)
self.setClipToView(False)
self.disableAutoRange()
self.setRange(xRange=xLimits, yRange=yLimits, padding=0)
self.setLimits(xMin=xLimits[0], xMax=xLimits[1],
yMin=yLimits[0], yMax=yLimits[1])
self.hideButtons()
self.showGrid(x=True, y=True, alpha=0.5)
# draw all axis as black (draw box)
self.getAxis('left' ).setPen(pg.mkPen('k'))
self.getAxis('bottom').setPen(pg.mkPen('k'))
self.getAxis('right' ).setPen(pg.mkPen('k'))
self.getAxis('top' ).setPen(pg.mkPen('k'))
self.getAxis('bottom').setTicks([])
self.getAxis('right' ).setTicks([])
self.getAxis('right' ).show()
self.getAxis('top' ).show()
# set axis labels
self.getAxis('top' ).setLabel('<br />Frequency (kHz)')
# prepare plots
self._curves = [None]*self.lineCount
for i in range(self.lineCount):
self._curves[i] = self.plot([], [],
pen=self._color[i % len(self._color)])
def start(self):
if not self._timer.isActive():
self._updateLast = dt.datetime.now()
self._timer.start()
def stop(self):
if self._timer.isActive():
self._timer.stop()
self.update()
self._updateLast = None
def append(self, data):
'''
Args:
data (numpy.array): 1D for single line or 2D for multiple lines
with the following format: lines x samples
'''
# some format checking
if data.ndim == 1: data = np.array([data])
if data.ndim != 2: raise ValueError('Need 1 or 2-dimensional data')
if data.shape[0] != self.lineCount: raise ValueError('Size of first '
'dimension of data should match line count')
# keep a local copy of data in a circular buffer
# for online processing and fast plotting
with self._buffer1:
self._buffer1.write(data)
def update(self):
try:
# log.debug('Updating plot')
# measure the actual fps
if self._updateLast is not None:
updateTime = (dt.datetime.now() -
self._updateLast).total_seconds()
self._updateTimes.append(updateTime)
if len(self._updateTimes)>60:
self._updateTimes.pop(0)
updateMean = np.mean(self._updateTimes)
fps = 1/updateMean if updateMean else 0
self._measuredFPS = fps
self._updateLast = dt.datetime.now()
if not self._buffer1.updated: return
# if self._buffer1.nsAvailable<self._chunk*self.fs: return
with self._buffer1: # self._refreshLock
to = self._buffer1.nsWritten
frm = to-int(self._chunk*self.fs)
if frm < 0: return
data = self._buffer1.read(frm, to)
freq = np.fft.fftfreq(data.shape[-1], 1/self.fs)
mask = (self._xLimits[0]<=freq) & (freq<=self._xLimits[1])
freq = freq[mask]
for line in range(self.lineCount):
ps = np.abs(np.fft.fft(data[line,:]))**2 / data.shape[-1]
ps = ps[mask]
self._curves[line].setData(freq, ps*self._yScale +
self._yOffset + line*self._yGap)
except:
log.exception('Failed to update FFT plot, stopping plot timer')
self._timer.stop()
class BaseChannel():
'''Base class for all channels.
Contains not much functional code, mostly for class hierarchy purposes.
'''
@property
def source(self):
return self._source
@source.setter
def source(self, value):
if value and not isinstance(value, BaseChannel):
raise TypeError('`source` should be of type `BaseChannel`')
self._source = value
if value:
value._sink = self
@property
def sink(self):
return self._sink
@sink.setter
def sink(self, value):
if value and not isinstance(value, BaseChannel):
raise TypeError('`sink` should be of type `BaseChannel`')
self._sink = value
if value:
value._source = self
def __init__(self, source=None):
self.source = source
self._sink = None
class AnalogChannel(BaseChannel):
'''All-in-one class for storage and plotting of multiline analog signals.
Utilizes the `hdf5` module for storage in HDF5 file format.
Needs to be paired with a ScrollingPlotWidget for plotting.
'''
@property
def plotWidget(self):
return self._plotWidget
@property
def fs(self):
'''Sampling frequency'''
return self._fs
@property
def ns(self):
'''Total number of samples (per line) added to channel'''
# return self._ns
return self._buffer1.nsWritten
@property
def ts(self):
'''Last timestamp in seconds'''
return self.ns/self._fs
@property
def lineCount(self):
return self._lineCount
@property
def yScale(self):
return self._yScale
@yScale.setter
def yScale(self, value):
# verify
if hasattr(self, '_yScale') and self._yScale == value:
return
with self._refreshLock:
# save
self._yScale = value
# apply
if not self._refreshBlock:
self._refresh()
@property
def filter(self):
return self._filter
@filter.setter
def filter(self, value):
# verify
if not isinstance(value, tuple) or len(value) != 2:
raise ValueError('`filter` should be a tuple of 2')
if hasattr(self, '_filter') and self._filter == value:
return
with self._refreshLock:
# save
self._filter = value
# prep filter
fs = self.fs
nyq = fs/2
fl, fh = value
if (fl and 0<fl and fh and fh<fs):
filterBA = sp.signal.butter(6, [fl/nyq,fh/nyq], 'bandpass')
elif fl and 0<fl:
filterBA = sp.signal.butter(6, fl/nyq, 'highpass')
elif fh and fh<fs:
filterBA = sp.signal.butter(6, fh/nyq, 'lowpass')
else:
filterBA = None
self._filterBA = filterBA
if self._filterBA:
zi = sp.signal.lfilter_zi(*self._filterBA)
self._filterZI = np.zeros((self.lineCount, len(zi)))
# apply
if not self._refreshBlock:
self._refresh()
@property
def fsPlot(self):
return self._fsPlot
@property
def linesVisible(self):
return self._linesVisible
@linesVisible.setter
def linesVisible(self, value):
# verify
if not isinstance(value, tuple):
raise TypeError('Value should be a tuple')
if len(value) != self.lineCount:
raise ValueError('Value should have exactly `lineCount` elements')
for v in value:
if not isinstance(v, bool):
raise ValueError('All elements must be `bool` instances')
if hasattr(self, '_linesVisible') and self._linesVisible == value:
return
with self._refreshLock:
# save
self._linesVisible = value
# apply
if hasattr(self, '_curves'):
for i in range(self.lineCount):
for curve in self._curves[i]:
curve.setVisible(value[i])
@property
def linesGrandMean(self):
return self._linesGrandMean
@linesGrandMean.setter
def linesGrandMean(self, value):
# verify
if not isinstance(value, tuple):
raise TypeError('Value should be a tuple')
if len(value) != self.lineCount:
raise ValueError('Value should have exactly `lineCount` elements')
for v in value:
if not isinstance(v, bool):
raise ValueError('All elements must be `bool` instances')
if hasattr(self, '_linesGrandMean') and self._linesGrandMean == value:
return
with self._refreshLock:
# save
self._linesGrandMean = value
# apply
if not self._refreshBlock:
self._refresh()
@property
def threshold(self):
return self._threshold
@threshold.setter
def threshold(self, value):
# verify
if hasattr(self, '_threshold') and self._threshold == value:
return
with self._refreshLock:
# save
self._threshold = value
# apply
if not self._refreshBlock:
self._refresh()
@property
def refreshBlock(self):
return self._refreshBlock
@refreshBlock.setter
def refreshBlock(self, value):
self._refreshBlock = value
def __init__(self, fs, plotWidget=None, hdf5Node=None, source=None,
label=None, labelOffset=0, lineCount=1, yScale=1, yOffset=0,
yGap=1, color=list('rgbcmyk'), chunkSize=.5, filter=(None,None),
fsPlot=5e3, grandMean=False, threshold=0):
'''
Args:
fs (float): Actual sampling frequency of the channel in Hz.
plotWidget (ScrollingPlotWidget): Defaults to None.
hdf5Node (str): Path of the node to store data in HDF5 file.
Defaults to None.
source (BaseChannel): ...
label (str or list of str): ...
labelOffset (float): ...
lineCount (int): Number of lines. Defaults to 1.
yScale (float): In-place scaling factor for indivudal lines.
Defaults to 1.
yOffset (float): Defaults to 0.
yGap (float): The gap between multiple lines. Defaults to 10.
color (list of str or tuple): Defaults to list('rgbcmyk').
chunkSize (float): In seconds. Defaults to 0.5.
filter (tuple of 2 floats): Lower and higher cutoff frequencies.
fsPlot (float): Plotting sampling frequency in Hz. Defaults to 5e3.
'''
super().__init__(source)
self._refreshBlock = True
self._refreshLock = threading.Lock()
self._fs = fs
self._plotWidget = plotWidget
self._hdf5Node = hdf5Node
self._lineCount = lineCount
self.yScale = yScale
self._yOffset = yOffset
self._yGap = yGap
self._color = color if isinstance(color, list) else [color]
self._chunkSize = chunkSize
self.filter = filter
self.linesVisible = (True,) * lineCount
self.linesGrandMean = (grandMean,) * lineCount
self.threshold = threshold
# calculate an integer downsampling factor base on `fsPlot`
if fsPlot and fsPlot != self.fs:
# downsampling factor, i.e. number of consecutive samples to
# average, must be integer
self._dsFactor = round(self.fs/fsPlot)
# recalculate the plotting sampling frequency
self._fsPlot = self.fs / self._dsFactor
else:
self._dsFactor = None
self._fsPlot = self.fs
buffer1Size = int(self.plotWidget.xRange*self.fs *1.2)
buffer2Size = int(self.plotWidget.xRange*self.fsPlot*1.2)
# self._bufferX = misc.CircularBuffer((lineCount, buffer1Size))
self._buffer1 = misc.CircularBuffer((lineCount, buffer1Size))
self._buffer2 = misc.CircularBuffer((lineCount, buffer2Size))
if hdf5Node is not None:
if hdf5.contains(hdf5Node):
raise NameError('HDF5 node %s already exists' % hdf5Node)
hdf5.createEArray(hdf5Node, tb.Float32Atom(), (0,lineCount),
'', hdf5Filters, expectedrows=fs*60*30) # 30 minutes
hdf5.setNodeAttr(hdf5Node, 'fs', fs)
if plotWidget:
labelOffset += np.arange(lineCount)*yGap+yOffset
plotWidget.add(self, label, labelOffset)
# threshold guide lines
pen = pg.mkPen((255,0,0,150), style=QtCore.Qt.DashLine, cosmetic=True)
self._thresholdGuides = [[None]*2 for i in range(self.lineCount)]
for i in range(self.lineCount):
for j in range(2):
y = (self._yOffset + i*self._yGap +
self.yScale*self.threshold*(j*2-1))
line = pg.InfiniteLine(y, 0, pen=pen)
line.setVisible(bool(self.threshold))
self.plotWidget.addItem(line)
self._thresholdGuides[i][j] = line
# prepare plotting chunks
self._chunkCount = int(np.ceil(self.plotWidget.xRange/self._chunkSize))
self._curves = [[None]*self._chunkCount for i in range(self.lineCount)]
for i in range(self.lineCount):
for j in range(self._chunkCount):
self._curves[i][j] = self.plotWidget.plot([], [],
pen=self._color[i % len(self._color)])
self._curves[i][j].setVisible(self.linesVisible[i])
self._refreshBlock = False
self._refresh()
self._processThread = threading.Thread(target=self._processLoop)
self._processThread.daemon = True
self._processThread.start()
def _processLoop(self):
try:
while True:
self._buffer1.wait()
with self._refreshLock:
# read input buffer
with self._buffer1:
ns = self._buffer1.nsWritten-self._buffer1.nsRead
if self._dsFactor:
ns = ns//self._dsFactor*self._dsFactor
if ns <= 0: continue
ns += self._buffer1.nsRead
data = self._buffer1.read(to=ns).copy()
dataCopy = data.copy()
for line in range(data.shape[0]):
linesMask = np.array(self._linesGrandMean)
if not linesMask[line]: continue
linesMask[line] = False
if not linesMask.any(): continue
data[line,:] -= dataCopy[linesMask,:].mean(axis=0)
# IIR filter
if self._filterBA:
data, self._filterZI = sp.signal.lfilter(
*self._filterBA, data, zi=self._filterZI)
# downsample
if self._dsFactor:
data = data.reshape(
(self.lineCount, -1, self._dsFactor))
data = data.mean(axis=2)
# write to output buffer
with self._buffer2:
self._buffer2.write(data)
except:
log.exception('Failed to process data, stopping process thread')
def _refresh(self):
if hasattr(self, '_thresholdGuides'):
for i in range(self.lineCount):
for j in range(2):
y = (self._yOffset + i*self._yGap +
self.yScale*self.threshold*(j*2-1))
line = self._thresholdGuides[i][j]
line.setValue(y)
line.setVisible(bool(self._threshold))
# reset buffers to the beginning of the current plot window
nsRange = int(self.plotWidget.xRange*self.fs)
self._buffer1.nsRead = self._buffer1.nsRead // nsRange * nsRange
nsRange = int(self.plotWidget.xRange*self.fsPlot)
self._buffer2.nsWritten = self._buffer2.nsWritten // nsRange * nsRange
# self._chunkLast = 0
def append(self, data):
'''
Args:
data (numpy.array): 1D for single line or 2D for multiple lines
with the following format: lines x samples
'''
# some format checking
if data.ndim == 1: data = np.array([data])
if data.ndim != 2: raise ValueError('Need 1 or 2-dimensional data')
if data.shape[0] != self.lineCount: raise ValueError('Size of first '
'dimension of data should match line count')
# dump new data to HDF5 file
if self._hdf5Node is not None:
hdf5.appendArray(self._hdf5Node, data.transpose())
# if self._bufferX.nsAvailable+data.shape[-1]>self._bufferX.shape[-1]:
# oldData = self._bufferX.read()
# hdf5.appendArray(self._hdf5Node, oldData.transpose())
# self._bufferX.write(data)
# keep a local copy of data in a circular buffer
# for online processing and fast plotting
with self._buffer1:
self._buffer1.write(data)
def update(self, ts=None, tsMin=None, nextWindow=False):
if not self._buffer2.updated: return
with self._refreshLock, self._buffer2:
nsRange = int(self.plotWidget.xRange*self._fsPlot)
chunkSamples = int(self._chunkSize*self._fsPlot)
nsFrom = self._buffer2.nsRead
nsFromMin = nsFrom // nsRange * nsRange
chunkFrom = (nsFrom - nsFromMin) // chunkSamples
nsTo = self._buffer2.nsWritten
nsToMin = nsTo // nsRange * nsRange
chunkTo = (nsTo - nsToMin) // chunkSamples
chunkTo = min(chunkTo, self._chunkCount-1)
# when plot advances to next time window
# if nextWindow: chunkFrom = chunkTo = 0
if chunkTo < chunkFrom: chunkFrom = 0
try:
for chunk in range(chunkFrom, chunkTo+1):
nsChunkFrom = nsToMin + chunk*chunkSamples
nsChunkTo = min(nsTo, nsChunkFrom+chunkSamples)
if nsChunkFrom == nsChunkTo: continue
time = np.arange(nsChunkFrom,
nsChunkTo) / self._fsPlot
data = self._buffer2.read(nsChunkFrom, nsChunkTo)
for line in range(self.lineCount):
self._curves[line][chunk].setData(time,
data[line,:]*self._yScale
+ self._yOffset + line*self._yGap)
except:
log.info('nsRange %d, chunkSamples %d, nsFrom %d, '
'nsFromMin %d, chunkFrom %d, nsTo %d, nsToMin %d, '
'chunkTo %d, chunk %d, line %d', nsRange, chunkSamples,
nsFrom, nsFromMin, chunkFrom, nsTo, nsToMin, chunkTo,
chunk, line)
raise
# update threshold guide lines
# if self.threshold:
# buffer2Size = self._buffer2.shape[self._buffer2.axis]
# frm = max(0, self._buffer2.nsWritten-buffer2Size)
# data = self._buffer2.read(frm=frm, advance=False)
# if data.shape[-1]:
# for i in range(self.lineCount):
# sigma = np.median(np.abs(data[i,:]))
# for j in range(2):
# y = (self._yOffset + i*self._yGap +
# self.yScale*self.threshold*sigma*(j*2-1))
# self._thresholdGuides[i][j].setValue(y)
def refresh(self):
with self._refreshLock:
self._refresh()
class AnalogPlot(pypeline.Sampled):
'''All-in-one class for storage and plotting of multiline analog signals.
Utilizes the `hdf5` module for storage in HDF5 file format.
Needs to be paired with a ScrollingPlotWidget for plotting.
'''
@property
def plotWidget(self):
return self._plotWidget
@property
def lineCount(self):
return self._lineCount
@property
def yScale(self):
return self._yScale
@yScale.setter
def yScale(self, value):
self._yScale = value
def __init__(self, plotWidget, label=None, labelOffset=0, yScale=1,
yOffset=0, yGap=1, color=list('rgbcmyk'), chunkSize=.5, **kwargs):
self._plotWidget = plotWidget
self._label = label
self._labelOffset = labelOffset
self._yScale = yScale
self._yOffset = yOffset
self._yGap = yGap
self._color = color if isinstance(color, list) else [color]
self._chunkSize = chunkSize
super().__init__(**kwargs)
def _configured(self, params, sinkParams):
super()._configured(params, sinkParams)
self._lineCount = self._channels
buffer1Size = int(self.plotWidget.xRange*self._fs*1.2)
self._buffer1 = misc.CircularBuffer((self._lineCount, buffer1Size))
self._labelOffset += (np.arange(self._lineCount) * self._yGap
+ self._yOffset)
self._plotWidget.add(self, self._label, self._labelOffset)
# prepare plotting chunks
self._chunkCount = int(np.ceil(self.plotWidget.xRange
/ self._chunkSize))
self._curves = [[None]*self._chunkCount
for i in range(self._lineCount)]
for i in range(self._lineCount):
for j in range(self._chunkCount):
self._curves[i][j] = self.plotWidget.plot([], [],
pen=self._color[i % len(self._color)])
def _written(self, data, source):
'''
Args:
data (numpy.array): 1D for single line or 2D for multiple lines
with the following format: lines x samples
'''
# keep a local copy of data in a circular buffer
# for online processing and fast plotting
with self._buffer1:
self._buffer1.write(data)
super()._written(data, source)
def update(self, ts=None, tsMin=None, nextWindow=False):
if not self._buffer1.updated: return
with self._buffer1:
nsRange = int(self.plotWidget.xRange*self._fs)
chunkSamples = int(self._chunkSize*self._fs)
nsFrom = self._buffer1.nsRead
nsFromMin = nsFrom // nsRange * nsRange
chunkFrom = (nsFrom - nsFromMin) // chunkSamples
nsTo = self._buffer1.nsWritten
nsToMin = nsTo // nsRange * nsRange
chunkTo = (nsTo - nsToMin) // chunkSamples
chunkTo = min(chunkTo, self._chunkCount-1)
# when plot advances to next time window
# if nextWindow: chunkFrom = chunkTo = 0
if chunkTo < chunkFrom: chunkFrom = 0
try:
for chunk in range(chunkFrom, chunkTo+1):
nsChunkFrom = nsToMin + chunk*chunkSamples
nsChunkTo = min(nsTo, nsChunkFrom+chunkSamples)
if nsChunkFrom == nsChunkTo: continue
time = np.arange(nsChunkFrom,
nsChunkTo) / self._fs
data = self._buffer1.read(nsChunkFrom, nsChunkTo)
for line in range(self.lineCount):
self._curves[line][chunk].setData(time,
data[line,:]*self._yScale
+ self._yOffset + line*self._yGap)
except:
log.info('nsRange %d, chunkSamples %d, nsFrom %d, '
'nsFromMin %d, chunkFrom %d, nsTo %d, nsToMin %d, '
'chunkTo %d, chunk %d, line %d', nsRange, chunkSamples,
nsFrom, nsFromMin, chunkFrom, nsTo, nsToMin, chunkTo,
chunk, line)
raise
class MultiLine(pg.QtGui.QGraphicsPathItem):
def __init__(self, pen):
super().__init__()
self.setPen(pg.mkPen(pen))
def shape(self):
return pg.QtGui.QGraphicsItem.shape(self)
def boundingRect(self):
return self.path().boundingRect()
def setData(self, x, y):
connect = np.ones(x.shape, dtype=bool)
connect[:,-1] = 0 # don't draw the segment between each trace
path = pg.arrayToQPath(x.flatten(), y.flatten(), connect.flatten())
self.setPath(path)
class AnalogChannelFast(AnalogChannel):
@property
def linesVisible(self):
return super().linesVisible
@linesVisible.setter
def linesVisible(self, value):
# verify
if not isinstance(value, tuple):
raise TypeError('Value should be a tuple')
if len(value) != self.lineCount:
raise ValueError('Value should have exactly `lineCount` elements')
for v in value:
if not isinstance(v, bool):