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