Fix: Performance by setting a maximum number of data points

This commit is contained in:
CPD 2025-03-10 11:08:03 +01:00
parent 40632d5de5
commit 040fb37ac1

View File

@ -1,11 +1,14 @@
import numpy as np
import pyqtgraph as pg
from PyQt6.QtWidgets import QWidget
class Plot(pg.GraphicsLayoutWidget):
"""
pyqtgraph plot widget for showing voltage and LED vs time
"""
def __init__(self):
def __init__(self, max_data_points=1000):
super().__init__()
self.setWindowTitle("CPD - LED")
@ -36,21 +39,25 @@ class Plot(pg.GraphicsLayoutWidget):
# self.setLabel("right", "LED [%]")
# self.getAxis("right").setRange(0, 110) # Adding some margin
# self.showGrid(x=True, y=True)
self.time = []
self.voltage = []
self.led = []
self.MAX_DATA_POINTS = max_data_points
self.data_t = np.empty(self.MAX_DATA_POINTS, dtype=float)
self.data_v = np.empty(self.MAX_DATA_POINTS, dtype=float)
self.data_l = np.empty(self.MAX_DATA_POINTS, dtype=float)
self.n_data_array = 0
self.n_data_total = 0
self.n_data_skip = 0
self.v_line = pg.PlotCurveItem(
self.time,
self.voltage,
[],
[],
pen=pg.mkPen("b", width=2),
symbol="o",
symbolSize=5,
symbolBrush="b",
)
self.l_line = pg.PlotCurveItem(
self.time,
self.led,
[],
[],
pen=pg.mkPen("r", width=2)
)
self.l_box.addItem(self.l_line)
@ -76,19 +83,36 @@ class Plot(pg.GraphicsLayoutWidget):
voltage
led
"""
self.time.append(time)
self.voltage.append(voltage)
self.led.append(led)
self.v_line.setData(self.time, self.voltage)
self.l_line.setData(self.time, self.led)
# if the array is full, keep only every second data point
# and skip every second data point.
if self.n_data_array == self.MAX_DATA_POINTS:
for i in range(0, self.MAX_DATA_POINTS//2):
self.data_t[i] = self.data_t[2 * i]
self.data_v[i] = self.data_v[2 * i]
self.data_l[i] = self.data_l[2 * i]
self.n_data_array = self.MAX_DATA_POINTS//2
self.n_data_skip += 1
# skip data points to keep a constant interval
if self.n_data_skip == 0 or self.n_data_total % self.n_data_skip == 0:
self.data_t[self.n_data_array] = time
self.data_v[self.n_data_array] = voltage
self.data_l[self.n_data_array] = led
self.n_data_array += 1
# update the plots
self.v_line.setData(self.data_t[:self.n_data_array], self.data_v[:self.n_data_array])
self.l_line.setData(self.data_t[:self.n_data_array], self.data_l[:self.n_data_array])
self.n_data_total += 1
def clear_data(self):
"""
Clear the lines and data
Returns
"""
self.time = []
self.voltage = []
self.led = []
self.v_line.setData(self.time, self.voltage)
self.l_line.setData(self.time, self.led)
self.data_t = np.empty(self.MAX_DATA_POINTS, dtype=float)
self.data_v = np.empty(self.MAX_DATA_POINTS, dtype=float)
self.data_l = np.empty(self.MAX_DATA_POINTS, dtype=float)
self.n_data_array = 0
self.n_data_total = 0
self.n_data_skip = 0
self.v_line.setData([], [])
self.l_line.setData([], [])