diff --git a/CHANGELOG.md b/CHANGELOG.md index 26e56285..f8b5ca89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,21 @@ +## v0.17.1 (2023-09-08) + +### Fix + +* Start_device_consumer changed from EP device_status to scan_status ([`46a3981`](https://gitlab.psi.ch/bec/bec-widgets/-/commit/46a3981e7dfd5ded7b7f325301d2a25c47abd16f)) + +## v0.17.0 (2023-09-07) + +### Feature + +* Console arguments added for Redis port, device, and sub_device tag ([`fb52b2a`](https://gitlab.psi.ch/bec/bec-widgets/-/commit/fb52b2a8e59fca556764e0dc32bd4edc167e31d3)) +* Plot flips every second row ([`c368871`](https://gitlab.psi.ch/bec/bec-widgets/-/commit/c36887191914d23e85a1b480dac324be0eefb963)) +* Device_consumer is getting scanID and initialise stream_consumer ([`9271b91`](https://gitlab.psi.ch/bec/bec-widgets/-/commit/9271b91113a3bbd46f0bffdaef7b50b629e4f44f)) +* Simulation and simple 2D plot for mca card stream ([`bfef713`](https://gitlab.psi.ch/bec/bec-widgets/-/commit/bfef71382e6a1180d750d2c800650942c5da7a21)) + ## v0.16.4 (2023-09-06) ### Fix diff --git a/bec_widgets/examples/mca_readout/mca_plot.py b/bec_widgets/examples/mca_readout/mca_plot.py new file mode 100644 index 00000000..1e76b8ef --- /dev/null +++ b/bec_widgets/examples/mca_readout/mca_plot.py @@ -0,0 +1,145 @@ +# import simulation_progress as SP +import numpy as np +import pyqtgraph as pg +from PyQt5.QtCore import pyqtSignal, pyqtSlot +from PyQt5.QtWidgets import ( + QApplication, + QVBoxLayout, + QWidget, +) + +from bec_lib.core import MessageEndpoints, BECMessage + + +class StreamApp(QWidget): + update_signal = pyqtSignal() + new_scanID = pyqtSignal(str) + + def __init__(self, device, sub_device): + super().__init__() + + self.init_ui() + + self.data = None + self.scanID = None + self.stream_consumer = None + + self.device = device + self.sub_device = sub_device + + self.start_device_consumer() + + # self.start_device_consumer(self.device) # for simulation + + self.new_scanID.connect(self.create_new_stream_consumer) + self.update_signal.connect(self.plot_new) + + def init_ui(self): + # Create layout and add widgets + self.layout = QVBoxLayout() + self.setLayout(self.layout) + + # Create plot + # self.glw = pg.GraphicsLayoutWidget() + self.plot_widget = pg.PlotWidget(title="MCA readout") + self.image_item = pg.ImageItem() + self.plot_widget.addItem(self.image_item) + + # Add widgets to the layout + self.layout.addWidget(self.plot_widget) + + @pyqtSlot(str) + def create_new_stream_consumer(self, scanID: str): + print(f"Creating new stream consumer for scanID: {scanID}") + + self.connect_stream_consumer(scanID, self.device) + + def connect_stream_consumer(self, scanID, device): + if self.stream_consumer is not None: + self.stream_consumer.shutdown() + + self.stream_consumer = connector.stream_consumer( + topics=MessageEndpoints.device_async_readback(scanID=scanID, device=device), + cb=self._streamer_cb, + parent=self, + ) + + self.stream_consumer.start() + + def start_device_consumer(self): + self.device_consumer = connector.consumer( + topics=MessageEndpoints.scan_status(), cb=self._device_cv, parent=self + ) + + self.device_consumer.start() + + # def start_device_consumer(self, device): #for simulation + # self.device_consumer = connector.consumer( + # topics=MessageEndpoints.device_status(device), cb=self._device_cv, parent=self + # ) + # + # self.device_consumer.start() + + def plot_new(self): + self.image_item.setImage(self.data.T) + + @staticmethod + def _streamer_cb(msg, *, parent, **_kwargs) -> None: + msgMCS = BECMessage.DeviceMessage.loads(msg.value) + + row = msgMCS.content["signals"][parent.sub_device] + metadata = msgMCS.metadata + + # Check if the current number of rows is odd + if parent.data is not None and parent.data.shape[0] % 2 == 1: + row = np.flip(row) # Flip the row + + if parent.data is None: + parent.data = np.array([row]) + else: + parent.data = np.vstack((parent.data, row)) + + parent.update_signal.emit() + + @staticmethod + def _device_cv(msg, *, parent, **_kwargs) -> None: + print("Getting ScanID") + + msgDEV = BECMessage.ScanStatusMessage.loads(msg.value) + + current_scanID = msgDEV.content["scanID"] + + if parent.scanID is None: + parent.scanID = current_scanID + parent.new_scanID.emit(current_scanID) + print(f"New scanID: {current_scanID}") + + if current_scanID != parent.scanID: + parent.scanID = current_scanID + parent.data = None + parent.image_item.clear() + parent.new_scanID.emit(current_scanID) + + print(f"New scanID: {current_scanID}") + + +if __name__ == "__main__": + import argparse + from bec_lib.core import RedisConnector + + parser = argparse.ArgumentParser(description="Stream App.") + parser.add_argument( + "--port", type=str, default="localhost:6379", help="Port for RedisConnector" + ) + parser.add_argument("--device", type=str, default="mca", help="Device name") + parser.add_argument("--sub_device", type=str, default="mca1", help="Sub-device name") + + args = parser.parse_args() + + connector = RedisConnector(args.port) + + app = QApplication([]) + streamApp = StreamApp(device=args.device, sub_device=args.sub_device) + + streamApp.show() + app.exec_() diff --git a/bec_widgets/examples/mca_readout/mca_sim.py b/bec_widgets/examples/mca_readout/mca_sim.py new file mode 100644 index 00000000..b3609293 --- /dev/null +++ b/bec_widgets/examples/mca_readout/mca_sim.py @@ -0,0 +1,34 @@ +from bec_lib.core import BECMessage, MessageEndpoints, RedisConnector +import time + +connector = RedisConnector("localhost:6379") +producer = connector.producer() +metadata = {} + +scanID = "ScanID1" + +metadata.update( + { + "scanID": scanID, # this will be different for each scan + "async_update": "append", + } +) +for ii in range(20): + data = {"mca1": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "mca2": [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]} + msg = BECMessage.DeviceMessage( + signals=data, + metadata=metadata, + ).dumps() + + # producer.send(topic=MessageEndpoints.device_status(device="mca"), msg=msg) + + producer.xadd( + topic=MessageEndpoints.device_async_readback( + scanID=scanID, device="mca" + ), # scanID will be different for each scan + msg={"data": msg}, + expire=1800, + ) + + print(f"Sent {ii}") + time.sleep(0.5) diff --git a/setup.py b/setup.py index ea5310f6..0cc73534 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup -__version__ = "0.16.4" +__version__ = "0.17.1" if __name__ == "__main__": setup(