diff --git a/bec_widgets/widgets/plots/waveform/waveform.py b/bec_widgets/widgets/plots/waveform/waveform.py index 432209c7..a9d69d9f 100644 --- a/bec_widgets/widgets/plots/waveform/waveform.py +++ b/bec_widgets/widgets/plots/waveform/waveform.py @@ -65,6 +65,7 @@ else: _OverlayAxis = Literal["index", "timestamp"] | tuple[str, str] +_MAX_ACCUMULATED_CURVES = 50 @dataclass @@ -295,6 +296,11 @@ class Waveform(PlotBase): is_async = live in self._async_curves axis = auto_axis if self.x_mode == "auto" else self._overlay_axis(is_async) snapshot_axis = self._curve_data_axes.get(live) + oldest = ( + next(iter(self._accumulated_curves)) + if len(self._accumulated_curves) >= _MAX_ACCUMULATED_CURVES + else None + ) config = CurveConfig( label=label, widget_class="Curve", @@ -304,7 +310,11 @@ class Waveform(PlotBase): scan_id=self.scan_id, scan_number=scan_number, current_x_mode=self.x_mode, - color=self._generate_color_from_palette(), + color=( + oldest.config.color + if oldest is not None + else self._generate_color_from_palette() + ), symbol=live.config.symbol, symbol_size=live.config.symbol_size, pen_width=live.config.pen_width, @@ -315,6 +325,9 @@ class Waveform(PlotBase): curve = self._add_curve_object(name=label, config=config) if has_data: curve.setData(x.copy(), y.copy()) + if oldest is not None: + self.plot_item.removeItem(oldest) + self._curve_clean_up(oldest) self._history_curves.append(curve) self._accumulated_curves[curve] = _ScanOverlay( auto_axis, snapshot_axis if has_data else None, axis, is_async diff --git a/tests/unit_tests/test_waveform.py b/tests/unit_tests/test_waveform.py index f00ab755..6c5cde5a 100644 --- a/tests/unit_tests/test_waveform.py +++ b/tests/unit_tests/test_waveform.py @@ -655,6 +655,48 @@ def test_accumulate_scans_overlays_previous_device_data( assert wf.get_curve(saved.name()) is not None +def test_accumulate_scans_evicts_oldest_overlays( + qtbot, mocked_client, monkeypatch, scan_history_factory +): + wf = create_widget(qtbot, Waveform, client=mocked_client) + inject_scan_history(wf, scan_history_factory, ("saved", 100)) + wf.x_mode = "index" + saved = wf.plot("bpm4i", scan_id="saved") + live = wf.plot("bpm4i") + 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"}, {}) + + def start_scan(number): + live.setData([0, 1], [number - 1, number]) + wf.on_scan_status({"scan_id": str(number)}, {}) + + def overlay_ids(): + return [ + curve.config.scan_id + for curve in wf.curves + if curve.config.source == "history" and curve is not saved + ] + + for number in range(2, 52): + start_scan(number) + assert overlay_ids() == [str(number) for number in range(1, 51)] + oldest = wf.get_curve("bpm4i-bpm4i-scan-1") + + start_scan(52) + assert overlay_ids() == [str(number) for number in range(2, 52)] + assert wf.get_curve("bpm4i-bpm4i-scan-51").config.color == oldest.config.color + start_scan(53) + assert overlay_ids() == [str(number) for number in range(3, 53)] + assert wf.get_curve(saved.name()) is saved + assert wf.get_curve(live.name()) is live + + def test_accumulated_scan_uses_saved_points_and_hides_on_incompatible_auto_axis( qtbot, mocked_client, monkeypatch, scan_history_factory ):