perf(waveform): hide curve symbols above 1000 points

This commit is contained in:
wyzula_j authored and Jan Wyzula committed 2026-11-17 18:41:56 +01:00
1 parent 92372122e2
commit 9e0cfc8518
5 files changed
+393 -12

No files matched your search

+4
View File
@@ -950,6 +950,7 @@ class Curve(RPCBase):
- symbol: str
- symbol_color: str
- symbol_size: int
- symbol_point_limit: int | None
- pen_width: int
- pen_style: Literal["solid", "dash", "dot", "dashdot"]
"""
@@ -991,6 +992,9 @@ class Curve(RPCBase):
"""
Change the symbol of the curve.
Above ``symbol_point_limit`` points the symbol stays hidden until the data
shrinks or the limit is raised with ``set(symbol_point_limit=...)``.
Args:
symbol(str): Symbol of the curve.
"""
+106 -6
View File
@@ -40,6 +40,13 @@ class CurveConfig(ConnectionConfig):
None, description="The color of the symbol of the curve."
)
symbol_size: int | None = Field(7, description="The size of the symbol of the curve.")
symbol_point_limit: int | None = Field(
1000,
description=(
"Hide the symbol once the curve holds more than this many points. "
"None keeps the symbol at every data size."
),
)
pen_width: int | None = Field(4, description="The width of the pen of the curve.")
pen_style: Literal["solid", "dash", "dot", "dashdot"] | None = Field(
"solid", description="The style of the pen of the curve."
@@ -63,6 +70,7 @@ class CurveConfig(ConnectionConfig):
_validate_symbol_color = field_validator("symbol_color")(Colors.validate_color)
# pylint: disable-next=too-many-public-methods
class Curve(BECConnector, pg.PlotDataItem):
USER_ACCESS = [
"remove",
@@ -93,6 +101,9 @@ class Curve(BECConnector, pg.PlotDataItem):
parent_item: Waveform | None = None,
**kwargs,
):
# Set before PlotDataItem.__init__, whose first setData already runs updateItems().
# The symbol pyqtgraph would draw without the symbol limit; see updateItems().
self._requested_symbol = None
if config is None:
config = CurveConfig(label=name, widget_class=self.__class__.__name__)
self.config = config
@@ -146,8 +157,86 @@ class Curve(BECConnector, pg.PlotDataItem):
self.setSymbolBrush(brush)
self.setSymbolSize(self.config.symbol_size)
# updateItems() keeps it hidden while the curve is above the symbol limit.
self.setSymbol(self.config.symbol)
def setData(self, *args, **kwargs):
"""
Wrap ``PlotDataItem.setData`` to track how often the data changes and which
symbol is requested.
"""
# getattr guards against setData being reached before __init__ sets the field.
self._data_version = getattr(self, "_data_version", 0) + 1
if "symbol" in kwargs:
self._requested_symbol = kwargs["symbol"]
elif self._requested_symbol is None and any(
key in kwargs for key in ("symbolPen", "symbolBrush", "symbolSize")
):
# pyqtgraph falls back to "o" for symbol styles given without a symbol.
self._requested_symbol = "o"
super().setData(*args, **kwargs)
def setSymbol(self, symbol):
"""
Wrap ``PlotDataItem.setSymbol`` to remember the requested symbol, also while
the curve is above the symbol limit and the symbol is hidden.
Args:
symbol: The symbol, as accepted by ``PlotDataItem.setSymbol``.
"""
self._requested_symbol = symbol
super().setSymbol(symbol)
def updateItems(self, styleUpdate: bool = True):
"""
Redraw the curve and its symbols, hiding the symbol above the symbol limit.
pyqtgraph draws symbols through ``ScatterPlotItem``, which builds a style
tuple per point in Python (``SymbolAtlas._keys``). That is linear in the
number of points and dominates everything else: at 50k points a ``setData``
costs ~90 ms with a symbol and ~0.5 ms without. Scans routinely exceed the
limit, so the symbol is dropped there and restored when the data shrinks.
pyqtgraph calls this after every data or style change, once the new dataset
is in place, so the decision counts the points of the stored dataset, whatever
form ``setData`` received them in. It also overrides the default ``"o"`` that
pyqtgraph adds for ``symbolSize``/``symbolPen``/``symbolBrush`` without a symbol.
Below the limit it draws the symbol last requested through ``setData`` or
``setSymbol``, including one requested while the symbol was hidden.
Args:
styleUpdate(bool): Passed on to ``PlotDataItem.updateItems``.
"""
if getattr(self, "config", None) is not None:
if self._exceeds_symbol_limit(self._point_count()):
self.opts["symbol"] = None
else:
self.opts["symbol"] = self._requested_symbol
super().updateItems(styleUpdate=styleUpdate)
def _exceeds_symbol_limit(self, data_length: int) -> bool:
"""
Whether a dataset of this length is above the configured symbol limit.
Args:
data_length(int): Number of points.
Returns:
bool: True if the symbol should be hidden; a limit of None never hides it.
"""
limit = self.config.symbol_point_limit
return limit is not None and data_length > limit
def _point_count(self) -> int:
"""
Number of points in the curve's current dataset.
Returns:
int: The number of points, 0 for an empty curve.
"""
dataset = getattr(self, "_dataset", None)
return 0 if dataset is None else len(dataset.y)
@property
def dap_params(self):
"""
@@ -202,12 +291,6 @@ class Curve(BECConnector, pg.PlotDataItem):
"""Monotonic counter bumped on every ``setData`` call."""
return self._data_version
def setData(self, *args, **kwargs):
"""Wrap ``PlotDataItem.setData`` to track how often the data changes."""
# getattr guards against setData being reached before __init__ sets the field.
self._data_version = getattr(self, "_data_version", 0) + 1
super().setData(*args, **kwargs)
def set_data(self, x: list | np.ndarray, y: list | np.ndarray):
"""
Set the data of the curve.
@@ -237,6 +320,7 @@ class Curve(BECConnector, pg.PlotDataItem):
- symbol: str
- symbol_color: str
- symbol_size: int
- symbol_point_limit: int | None
- pen_width: int
- pen_style: Literal["solid", "dash", "dot", "dashdot"]
"""
@@ -248,6 +332,7 @@ class Curve(BECConnector, pg.PlotDataItem):
"symbol": self.set_symbol,
"symbol_color": self.set_symbol_color,
"symbol_size": self.set_symbol_size,
"symbol_point_limit": self._set_symbol_point_limit,
"pen_width": self.set_pen_width,
"pen_style": self.set_pen_style,
}
@@ -273,10 +358,15 @@ class Curve(BECConnector, pg.PlotDataItem):
"""
Change the symbol of the curve.
Above ``symbol_point_limit`` points the symbol stays hidden until the data
shrinks or the limit is raised with ``set(symbol_point_limit=...)``.
Args:
symbol(str): Symbol of the curve.
"""
self.config.symbol = symbol
# While the curve is above the symbol limit, updateItems() keeps the symbol
# hidden; setSymbol() records it for when the data drops below the limit.
self.setSymbol(symbol)
self.updateItems()
@@ -300,6 +390,16 @@ class Curve(BECConnector, pg.PlotDataItem):
self.config.symbol_size = symbol_size
self.apply_config()
def _set_symbol_point_limit(self, limit: int | None):
"""
Change the number of points above which the symbol of the curve is hidden.
Args:
limit(int | None): Number of points; None keeps the symbol at every data size.
"""
self.config.symbol_point_limit = limit
self.updateItems()
def set_pen_width(self, pen_width: int):
"""
Change the pen width of the curve.
@@ -317,6 +317,7 @@ class Waveform(PlotBase):
),
symbol=live.config.symbol,
symbol_size=live.config.symbol_size,
symbol_point_limit=live.config.symbol_point_limit,
pen_width=live.config.pen_width,
pen_style=live.config.pen_style,
)
@@ -2293,10 +2294,12 @@ class Waveform(PlotBase):
) -> None:
"""
Based on the length of the data this method will adjust the plotting settings of
Curve items, by deactivating the symbol and activating downsampling auto, method='mean',
Curve items, by thinning the pen and activating downsampling auto, method='mean',
if the data length exceeds N points. If the data length is less than N points, the
symbol will be activated and downsampling will be deactivated. Maximum points will be
5x the limit.
pen is restored and downsampling deactivated. Maximum points will be 5x the limit.
The symbol is not handled here: `Curve.updateItems` hides it above
`CurveConfig.symbol_point_limit` for every curve, sync or async.
Args:
curve(Curve): The curve to adjust.
@@ -2308,14 +2311,11 @@ class Waveform(PlotBase):
logger.warning("Limit must be greater than 1.")
return
if data_length > limit:
if curve.config.symbol is not None:
curve.set_symbol(None)
if curve.config.pen_width > 3:
curve.set_pen_width(3)
curve.setDownsampling(ds=None, auto=True, method=method)
curve.setClipToView(True)
elif data_length <= limit:
curve.set_symbol("o")
curve.set_pen_width(4)
curve.setDownsampling(ds=1, auto=None, method=method)
curve.setClipToView(True)
+251
View File
@@ -0,0 +1,251 @@
"""The symbol is dropped for dense curves; see Curve.updateItems."""
import numpy as np
import pytest
from bec_widgets.tests.client_mocks import mocked_client
from bec_widgets.widgets.plots.waveform.curve import Curve, CurveConfig
from bec_widgets.widgets.plots.waveform.waveform import Waveform
from .conftest import create_widget
LIMIT = 1000
@pytest.fixture
def curve(qtbot, mocked_client):
waveform = create_widget(qtbot, Waveform, client=mocked_client)
waveform.plot(arg1="bpm4i")
return waveform.curves[0]
def _data(n: int):
x = np.arange(n, dtype=np.float64)
return x, np.sin(x * 0.01)
def test_symbol_kept_at_or_below_limit(curve):
curve.setData(*_data(LIMIT))
assert curve.opts["symbol"] == "o"
def test_symbol_hidden_above_limit(curve):
curve.setData(*_data(LIMIT + 1))
assert curve.opts["symbol"] is None
def test_config_symbol_survives_suppression(curve):
"""The configured symbol is a user setting, not something to overwrite."""
curve.setData(*_data(LIMIT + 1))
assert curve.config.symbol == "o"
def test_symbol_restored_when_data_shrinks(curve):
curve.setData(*_data(LIMIT + 1))
assert curve.opts["symbol"] is None
curve.setData(*_data(10))
assert curve.opts["symbol"] == "o"
def test_custom_symbol_restored_not_default(curve):
"""Restoring must use the configured symbol, not a hardcoded 'o'."""
curve.set_symbol("t")
curve.setData(*_data(LIMIT + 1))
assert curve.opts["symbol"] is None
curve.setData(*_data(10))
assert curve.opts["symbol"] == "t"
def test_set_symbol_while_dense_defers_until_sparse(curve):
curve.setData(*_data(LIMIT + 1))
curve.set_symbol("x")
# still dense: the request is recorded but not shown
assert curve.config.symbol == "x"
assert curve.opts["symbol"] is None
curve.setData(*_data(10))
assert curve.opts["symbol"] == "x"
def test_apply_config_does_not_resurrect_symbol_while_dense(curve):
curve.setData(*_data(LIMIT + 1))
curve.apply_config()
assert curve.opts["symbol"] is None
def test_limit_none_keeps_symbol_at_any_size(qtbot, mocked_client):
waveform = create_widget(qtbot, Waveform, client=mocked_client)
waveform.plot(arg1="bpm4i")
curve = waveform.curves[0]
curve.config.symbol_point_limit = None
curve.setData(*_data(50_000))
assert curve.opts["symbol"] == "o"
def test_custom_limit_is_honoured(curve):
curve.config.symbol_point_limit = 10
curve.setData(*_data(11))
assert curve.opts["symbol"] is None
curve.setData(*_data(10))
assert curve.opts["symbol"] == "o"
def test_default_limit_is_1000():
assert CurveConfig(widget_class="Curve").symbol_point_limit == LIMIT
def test_shrinking_never_redraws_dense_data_with_symbols(curve):
"""The restored symbol must arrive together with the small dataset."""
curve.setData(*_data(50_000))
seen = []
original = curve.scatter.setData
def spy(*args, **kwargs):
seen.append(len(kwargs["x"]) if kwargs.get("x") is not None else 0)
return original(*args, **kwargs)
curve.scatter.setData = spy
curve.setData(*_data(5))
assert curve.opts["symbol"] == "o"
assert seen and max(seen) == 5
def test_limit_none_after_suppression_restores_symbol(curve):
curve.setData(*_data(LIMIT + 1))
assert curve.opts["symbol"] is None
curve.config.symbol_point_limit = None
curve.apply_config()
assert curve.opts["symbol"] == "o"
curve.setData(*_data(5))
assert curve.opts["symbol"] == "o"
def test_limit_none_restores_on_next_data(curve):
"""Without apply_config, the next setData recomputes from the new limit."""
curve.setData(*_data(LIMIT + 1))
curve.config.symbol_point_limit = None
curve.setData(*_data(LIMIT + 1))
assert curve.opts["symbol"] == "o"
def test_apply_config_hides_symbol_when_limit_drops_below_data(curve):
curve.setData(*_data(20))
curve.config.symbol_point_limit = 10
curve.apply_config()
assert curve.opts["symbol"] is None
curve.setData(*_data(5))
assert curve.opts["symbol"] == "o"
@pytest.mark.parametrize("style", [{"symbolSize": 5}, {"symbolBrush": "r"}, {"symbol": "t"}])
def test_style_keywords_cannot_bypass_limit(curve, style):
"""pyqtgraph defaults to an "o" symbol when symbolSize/Pen/Brush come without one."""
curve.setData(*_data(LIMIT + 1), **style)
assert curve.opts["symbol"] is None
curve.setData(*_data(LIMIT + 2))
assert curve.opts["symbol"] is None
def _call_forms(n: int):
"""Every setData input form pyqtgraph accepts, each plotting n points."""
x, y = _data(n)
return {
"y only": ((y,), {}),
"x and y": ((x, y), {}),
"list": ((list(y),), {}),
"Nx2 array": ((np.column_stack([x, y]),), {}),
"dict": (({"x": x, "y": y},), {}),
"dict y only": (({"y": list(y)},), {}),
"list of dicts": (([{"x": a, "y": b} for a, b in zip(x, y)],), {}),
"keywords": ((), {"x": x, "y": y}),
"keyword y overrides positional": ((np.arange(3.0),), {"y": y}),
}
@pytest.mark.parametrize("form", list(_call_forms(1)))
def test_every_input_form_crosses_the_limit_both_ways(curve, form):
"""The limit counts the points pyqtgraph actually plots, whatever the input form."""
args, kwargs = _call_forms(LIMIT + 1)[form]
curve.setData(*args, **kwargs)
assert len(curve.yData) == LIMIT + 1
assert curve.opts["symbol"] is None
args, kwargs = _call_forms(2)[form]
curve.setData(*args, **kwargs)
assert len(curve.yData) == 2
assert curve.opts["symbol"] == "o"
args, kwargs = _call_forms(LIMIT + 1)[form]
curve.setData(*args, **kwargs)
assert curve.opts["symbol"] is None
def test_clearing_a_dense_curve_restores_the_symbol(curve):
curve.setData(*_data(LIMIT + 1))
curve.setData()
assert curve.opts["symbol"] == "o"
def test_set_symbol_none_on_dense_curve_stays_none_when_sparse(curve):
curve.setData(*_data(LIMIT + 1))
curve.set_symbol(None)
curve.setData(*_data(5))
assert curve.opts["symbol"] is None
def test_symbol_passed_with_small_data_after_dense_data_is_drawn(curve):
"""A symbol given to setData is drawn, also right after the curve was dense."""
curve.setData(*_data(LIMIT + 1))
curve.setData(*_data(5), symbol="t")
assert curve.opts["symbol"] == "t"
def _request_symbol(curve, how: str, symbol: str, n: int):
if how == "setData":
curve.setData(*_data(n), symbol=symbol)
else:
curve.setData(*_data(n))
curve.setSymbol(symbol)
@pytest.mark.parametrize("how", ["setData", "setSymbol"])
def test_requested_symbol_survives_a_dense_round_trip(curve, how):
"""small -> dense -> small keeps the symbol requested through pyqtgraph's API."""
_request_symbol(curve, how, "t", 5)
assert curve.opts["symbol"] == "t"
curve.setData(*_data(LIMIT + 1))
assert curve.opts["symbol"] is None
curve.setData(*_data(5))
assert curve.opts["symbol"] == "t"
@pytest.mark.parametrize("how", ["setData", "setSymbol"])
def test_symbol_requested_while_dense_is_drawn_when_sparse(curve, how):
"""dense -> small draws the symbol requested while it was hidden."""
_request_symbol(curve, how, "t", LIMIT + 1)
assert curve.opts["symbol"] is None
curve.setData(*_data(5))
assert curve.opts["symbol"] == "t"
def test_style_keyword_fallback_symbol_matches_pyqtgraph(curve):
"""Without a symbol, pyqtgraph draws "o" for symbolSize; that holds across the limit."""
curve.set_symbol(None)
curve.setData(*_data(LIMIT + 1), symbolSize=5)
assert curve.opts["symbol"] is None
curve.setData(*_data(5))
assert curve.opts["symbol"] == "o"
def test_set_symbol_point_limit_restores_the_symbol_at_once(curve):
"""The limit of an existing curve can be changed through set(), e.g. from the client."""
curve.setData(*_data(LIMIT + 1))
assert curve.opts["symbol"] is None
curve.set(symbol_point_limit=None)
assert curve.config.symbol_point_limit is None
assert curve.opts["symbol"] == "o"
def test_set_symbol_point_limit_hides_the_symbol_at_once(curve):
curve.setData(*_data(20))
curve.set(symbol_point_limit=10)
assert curve.opts["symbol"] is None
curve.set(symbol_point_limit=20)
assert curve.opts["symbol"] == "o"
+26
View File
@@ -697,6 +697,32 @@ def test_accumulate_scans_evicts_oldest_overlays(
assert wf.get_curve(live.name()) is live
def test_accumulated_scan_keeps_the_live_symbol_limit(
qtbot, mocked_client, monkeypatch, scan_history_factory
):
"""The overlay copies the live curve's symbol limit along with its symbol."""
wf = create_widget(qtbot, Waveform, client=mocked_client)
wf.x_mode = "index"
live = wf.plot("bpm4i")
live.config.symbol_point_limit = None
monkeypatch.setattr(wf, "_load_overlay", mock.Mock())
monkeypatch.setattr(
wf.queue.scan_storage,
"find_scan_by_ID",
lambda scan_id: create_dummy_scan_item(scan_id=scan_id, scan_number=int(scan_id)),
)
wf.toolbar.components.get_action("accumulate_scans").action.trigger()
wf.on_scan_status({"scan_id": "1"}, {})
live.setData(np.arange(1001.0), np.arange(1001.0))
assert live.opts["symbol"] == "o"
wf.on_scan_status({"scan_id": "2"}, {})
overlay = wf.get_curve("bpm4i-bpm4i-scan-1")
assert overlay.config.symbol_point_limit is None
assert len(overlay.yData) == 1001
assert overlay.opts["symbol"] == "o"
def test_accumulated_scan_uses_saved_points_and_hides_on_incompatible_auto_axis(
qtbot, mocked_client, monkeypatch, scan_history_factory
):