From 33f7be42c512402dab3fdd9781a8234e3ec5f4ba Mon Sep 17 00:00:00 2001 From: wyzula-jan Date: Fri, 7 Jun 2024 12:29:32 +0200 Subject: [PATCH] fix(curve): color_map_z setting works --- bec_widgets/cli/client.py | 2 +- .../jupyter_console/jupyter_console_window.py | 5 ++-- .../widgets/figure/plots/waveform/waveform.py | 4 +-- .../figure/plots/waveform/waveform_curve.py | 29 +++++++++++++++---- tests/unit_tests/test_waveform1d.py | 6 ++-- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/bec_widgets/cli/client.py b/bec_widgets/cli/client.py index 1aa691d0..5205fe05 100644 --- a/bec_widgets/cli/client.py +++ b/bec_widgets/cli/client.py @@ -753,7 +753,7 @@ class BECCurve(RPCBase): """ @rpc_call - def set_colormap(self, colormap: "str"): + def set_colormap_z(self, colormap: "str"): """ Set the colormap for the scatter plot z gradient. diff --git a/bec_widgets/examples/jupyter_console/jupyter_console_window.py b/bec_widgets/examples/jupyter_console/jupyter_console_window.py index 87734d29..c8736be2 100644 --- a/bec_widgets/examples/jupyter_console/jupyter_console_window.py +++ b/bec_widgets/examples/jupyter_console/jupyter_console_window.py @@ -86,9 +86,10 @@ class JupyterConsoleWindow(QWidget): # pragma: no cover: self.console_layout.addWidget(self.console) def _init_figure(self): - self.figure.plot(x_name="samx", y_name="bpm4d") + self.figure.plot(x_name="samx", y_name="samy", z_name="bpm4i", color_map_z="cividis") self.figure.motor_map("samx", "samy") self.figure.image("eiger", color_map="viridis", vrange=(0, 100)) + self.figure.add_plot(x_name="samx", y_name="samy", z_name="bpm4i", color_map_z="magma") self.figure.change_layout(2, 2) @@ -97,8 +98,6 @@ class JupyterConsoleWindow(QWidget): # pragma: no cover: self.w3 = self.figure[1, 0] # curves for w1 - self.w1.plot(x_name="samx", y_name="samy", z_name="bpm4i") - self.w1.plot(x_name="samx", y_name="samy", z_name="bpm3a") self.c1 = self.w1.get_config() def _init_dock(self): diff --git a/bec_widgets/widgets/figure/plots/waveform/waveform.py b/bec_widgets/widgets/figure/plots/waveform/waveform.py index 4405d689..2f1b27b6 100644 --- a/bec_widgets/widgets/figure/plots/waveform/waveform.py +++ b/bec_widgets/widgets/figure/plots/waveform/waveform.py @@ -364,7 +364,7 @@ class BECWaveform(BECPlotBase): parent_id=self.gui_id, label=label, color=color, - color_map=color_map_z, + color_map_z=color_map_z, source=curve_source, signals=Signal( source=curve_source, @@ -564,7 +564,7 @@ class BECWaveform(BECPlotBase): if curve.config.signals.z: data_z = data[z_name][z_entry].val color_z = self._make_z_gradient( - data_z, curve.config.colormap + data_z, curve.config.color_map_z ) # TODO decide how to implement custom gradient except TypeError: continue diff --git a/bec_widgets/widgets/figure/plots/waveform/waveform_curve.py b/bec_widgets/widgets/figure/plots/waveform/waveform_curve.py index 35d3c7ef..e9daf22f 100644 --- a/bec_widgets/widgets/figure/plots/waveform/waveform_curve.py +++ b/bec_widgets/widgets/figure/plots/waveform/waveform_curve.py @@ -3,7 +3,8 @@ from __future__ import annotations from typing import Any, Literal, Optional import pyqtgraph as pg -from pydantic import BaseModel, Field +from pydantic import BaseModel, Field, field_validator +from pydantic_core import PydanticCustomError from qtpy import QtCore from bec_widgets.utils import BECConnector, ConnectionConfig @@ -43,7 +44,22 @@ class CurveConfig(ConnectionConfig): ) source: Optional[str] = Field(None, description="The source of the curve.") signals: Optional[Signal] = Field(None, description="The signal of the curve.") - colormap: Optional[str] = Field("plasma", description="The colormap of the curves z gradient.") + color_map_z: Optional[str] = Field( + "plasma", description="The colormap of the curves z gradient.", validate_default=True + ) + model_config: dict = {"validate_assignment": True} + + @field_validator("color_map_z") + def validate_color_map(cls, v, values): + if v is not None and v != "": + available_colormaps = pg.colormap.listMaps() + if v not in available_colormaps: + raise PydanticCustomError( + "unsupported colormap", + f"Colormap '{v}' not found in the current installation of pyqtgraph. Choose on the following: {available_colormaps}.", + {"wrong_value": v}, + ) + return v class BECCurve(BECConnector, pg.PlotDataItem): @@ -54,7 +70,7 @@ class BECCurve(BECConnector, pg.PlotDataItem): "set", "set_data", "set_color", - "set_colormap", + "set_colormap_z", "set_symbol", "set_symbol_color", "set_symbol_size", @@ -130,7 +146,7 @@ class BECCurve(BECConnector, pg.PlotDataItem): # Mapping of keywords to setter methods method_map = { "color": self.set_color, - "colormap": self.set_colormap, + "color_map_z": self.set_color_map_z, "symbol": self.set_symbol, "symbol_color": self.set_symbol_color, "symbol_size": self.set_symbol_size, @@ -205,14 +221,15 @@ class BECCurve(BECConnector, pg.PlotDataItem): self.config.pen_style = pen_style self.apply_config() - def set_colormap(self, colormap: str): + def set_color_map_z(self, colormap: str): """ Set the colormap for the scatter plot z gradient. Args: colormap(str): Colormap for the scatter plot. """ - self.config.colormap = colormap + self.config.color_map_z = colormap + self.apply_config() def get_data(self) -> tuple[np.ndarray, np.ndarray]: """ diff --git a/tests/unit_tests/test_waveform1d.py b/tests/unit_tests/test_waveform1d.py index 6c36d7a2..792963e0 100644 --- a/tests/unit_tests/test_waveform1d.py +++ b/tests/unit_tests/test_waveform1d.py @@ -73,7 +73,7 @@ def test_create_waveform1D_by_config(bec_figure): "parent_id": "widget_1", "label": "bpm4i-bpm4i", "color": "#cc4778", - "colormap": "plasma", + "color_map_z": "plasma", "symbol": "o", "symbol_color": None, "symbol_size": 5, @@ -105,7 +105,7 @@ def test_create_waveform1D_by_config(bec_figure): "parent_id": "widget_1", "label": "curve-custom", "color": "blue", - "colormap": "plasma", + "color_map_z": "plasma", "symbol": "o", "symbol_color": None, "symbol_size": 5, @@ -360,7 +360,7 @@ def test_curve_add_by_config(bec_figure): "parent_id": "widget_1", "label": "bpm4i-bpm4i", "color": "#cc4778", - "colormap": "plasma", + "color_map_z": "plasma", "symbol": "o", "symbol_color": None, "symbol_size": 5,