2
2
import numpy as np
3
3
from datetime import timedelta
4
4
import pyqtgraph as pg
5
- from pyqtgraph .Qt import QtGui , QtWidgets
5
+ from pyqtgraph .Qt import QtGui
6
6
from PyQt5 .QtCore import Qt
7
7
8
8
from config .gui_settings import event_history_len , state_history_len , analog_history_dur
@@ -114,34 +114,28 @@ def run_start(self):
114
114
self .data = np .zeros ([self .data_len * 2 , 2 ], int )
115
115
for plot in self .plots .values ():
116
116
plot .clear ()
117
- self .cs = self .state_IDs [0 ]
118
- self .updated_states = []
119
117
120
118
def process_data (self , new_data ):
121
119
'''Store new data from board'''
122
120
new_states = [nd for nd in new_data if nd [0 ] == 'D' and nd [2 ] in self .state_IDs ]
123
- self .updated_states = [self .cs ]
124
121
if new_states :
125
122
n_new = len (new_states )
126
123
self .data = np .roll (self .data , - 2 * n_new , axis = 0 )
127
124
for i , ns in enumerate (new_states ): # Update data array.
128
125
timestamp , ID = ns [1 :]
129
- self .updated_states .append (ID )
130
126
j = 2 * (- n_new + i ) # Index of state entry in self.data
131
127
self .data [j - 1 :,0 ] = timestamp
132
128
self .data [j : ,1 ] = ID
133
- self .cs = ID
134
129
135
130
def update (self , run_time ):
136
131
'''Update plots.'''
137
- self .data [- 1 ,0 ] = 1000 * run_time # Update exit time of current state to current time.
138
- for us in self .updated_states : # Set data for updated state plots.
139
- state_data = self .data [self .data [:,1 ]== us ,:]
140
- timestamps , ID = (state_data [:,0 ]/ 1000 , state_data [:,1 ])
141
- self .plots [us ].setData (x = timestamps , y = ID , connect = 'pairs' )
142
- # Shift all state plots.
143
- for plot in self .plots .values ():
144
- plot .setPos (- run_time , 0 )
132
+ self .data [- 1 ,0 ] = run_time * 1000 # Update exit time of current state to current time.
133
+ for ID in self .state_IDs :
134
+ state_data = self .data [self .data [:,1 ]== ID ,:]
135
+ timestamps , IDs = (state_data [:,0 ]/ 1000 - run_time , state_data [:,1 ])
136
+ if timestamps .size > 0 :
137
+ self .plots [ID ].setData (x = timestamps , y = IDs , connect = 'pairs' )
138
+
145
139
146
140
# Events_plot--------------------------------------------------------
147
141
@@ -187,10 +181,8 @@ def process_data(self, new_data):
187
181
188
182
def update (self , run_time ):
189
183
'''Update plots'''
190
- # Should not need to setData but setPos does not cause redraw otherwise.
191
184
if not self .event_IDs : return
192
- self .plot .setData (self .data , symbolBrush = [pg .intColor (ID ) for ID in self .data [:,1 ]])
193
- self .plot .setPos (- run_time , 0 )
185
+ self .plot .setData (x = self .data [:,0 ]- run_time , y = self .data [:,1 ], symbolBrush = [pg .intColor (ID ) for ID in self .data [:,1 ]])
194
186
195
187
# ------------------------------------------------------------------------------------------
196
188
@@ -205,15 +197,12 @@ def __init__(self, parent=None, data_dur=10):
205
197
self .axis .setMouseEnabled (x = True ,y = False )
206
198
self .axis .showGrid (x = True ,alpha = 0.75 )
207
199
self .axis .setLimits (xMax = 0 )
208
- self .legend = None
209
200
210
201
def set_state_machine (self , sm_info ):
211
202
self .inputs = sm_info ['analog_inputs' ]
212
203
if not self .inputs : return # State machine may not have analog inputs.
213
- if self .legend :
214
- self .legend .close ()
215
- self .legend = self .axis .addLegend (offset = (10 , 10 ))
216
204
self .axis .clear ()
205
+ self .legend = self .axis .addLegend (offset = (10 , 10 ))
217
206
self .plots = {ai ['ID' ]: self .axis .plot (name = name ,
218
207
pen = pg .mkPen (pg .intColor (ai ['ID' ],len (self .inputs )))) for name , ai in sorted (self .inputs .items ())}
219
208
self .axis .getAxis ('bottom' ).setLabel ('Time (seconds)' )
@@ -232,7 +221,6 @@ def process_data(self, new_data):
232
221
'''Store new data from board.'''
233
222
if not self .inputs : return # State machine may not have analog inputs.
234
223
new_analog = [nd for nd in new_data if nd [0 ] == 'A' ]
235
- self .updated_inputs = [na [1 ] for na in new_analog ]
236
224
for na in new_analog :
237
225
ID , sampling_rate , timestamp , data_array = na [1 :]
238
226
new_len = len (data_array )
@@ -243,10 +231,9 @@ def process_data(self, new_data):
243
231
def update (self , run_time ):
244
232
'''Update plots.'''
245
233
if not self .inputs : return # State machine may not have analog inputs.
246
- for ID in self .updated_inputs :
247
- self .plots [ID ].setData (self .data [ID ])
248
- for plot in self .plots .values ():
249
- plot .setPos (- run_time , 0 )
234
+ for ai in self .inputs .values ():
235
+ ID = ai ['ID' ]
236
+ self .plots [ID ].setData (x = self .data [ID ][:,0 ]- run_time , y = self .data [ID ][:,1 ])
250
237
251
238
# -----------------------------------------------------
252
239
0 commit comments