From 98eaa4d1dea968d29a44e1ab8ac621feca7f4ca9 Mon Sep 17 00:00:00 2001 From: Klaus Wakonig Date: Fri, 7 Jul 2023 10:21:25 +0200 Subject: [PATCH] refactor: changes for new plugin structure --- .gitignore | 173 +++++++++++++++++- .gitmodules | 3 - bec | 1 - bec_plugins/__init__.py | 0 .../configs}/config_session_start_e20632.yaml | 0 .../configs}/e21125_lamni_config.yaml | 0 bec_plugins/plugins/__init__.py | 0 .../{ => plugins}/bec_client/hli/spec_hli.py | 0 .../bec_client/plugins/LamNI/LamNI_logo.png | Bin .../bec_client/plugins/LamNI/__init__.py | 0 .../bec_client/plugins/LamNI/bl_show_all.mac | 0 .../plugins/LamNI/lamni_optics_mixin.py | 0 .../LamNI/load_additional_correction.py | 0 .../plugins/LamNI/x_ray_eye_align.py | 0 .../bec_client/plugins/cSAXS/__init__.py | 0 .../plugins/cSAXS/cSAXS_beamline.py | 0 .../bec_client/startup/post-startup.py | 0 .../plugins/bec_client/startup/pre-startup.py | 0 .../scan_plugins}/LamNIFermatScan.py | 41 ++++- .../{ => plugins}/scibec/lamni_config.py | 0 .../scibec/test_config_cSAXS.yaml | 0 setup.cfg | 21 +++ setup.py | 7 + 23 files changed, 233 insertions(+), 13 deletions(-) delete mode 100644 .gitmodules delete mode 160000 bec create mode 100644 bec_plugins/__init__.py rename {configs => bec_plugins/configs}/config_session_start_e20632.yaml (100%) rename {configs => bec_plugins/configs}/e21125_lamni_config.yaml (100%) create mode 100644 bec_plugins/plugins/__init__.py rename bec_plugins/{ => plugins}/bec_client/hli/spec_hli.py (100%) rename bec_plugins/{ => plugins}/bec_client/plugins/LamNI/LamNI_logo.png (100%) rename bec_plugins/{ => plugins}/bec_client/plugins/LamNI/__init__.py (100%) rename bec_plugins/{ => plugins}/bec_client/plugins/LamNI/bl_show_all.mac (100%) rename bec_plugins/{ => plugins}/bec_client/plugins/LamNI/lamni_optics_mixin.py (100%) rename bec_plugins/{ => plugins}/bec_client/plugins/LamNI/load_additional_correction.py (100%) rename bec_plugins/{ => plugins}/bec_client/plugins/LamNI/x_ray_eye_align.py (100%) rename bec_plugins/{ => plugins}/bec_client/plugins/cSAXS/__init__.py (100%) rename bec_plugins/{ => plugins}/bec_client/plugins/cSAXS/cSAXS_beamline.py (100%) create mode 100644 bec_plugins/plugins/bec_client/startup/post-startup.py create mode 100644 bec_plugins/plugins/bec_client/startup/pre-startup.py rename bec_plugins/{scan_server/plugins => plugins/scan_server/scan_plugins}/LamNIFermatScan.py (92%) rename bec_plugins/{ => plugins}/scibec/lamni_config.py (100%) rename bec_plugins/{ => plugins}/scibec/test_config_cSAXS.yaml (100%) create mode 100644 setup.cfg create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index 0814ce9..74235c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,177 @@ -**/venv +**/*_venv **/.idea *.log **/__pycache__ -.DS_Store +**/.DS_Store **/out **/.vscode **/.pytest_cache +**/*.egg* + +# file writer data +**.h5 + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/**/_build/ +docs/**/autodoc/ +docs/**/_autosummary/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +**.prof + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 3c39377..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "bec"] - path = bec - url = https://gitlab.psi.ch/bec/bec.git diff --git a/bec b/bec deleted file mode 160000 index 28bfad3..0000000 --- a/bec +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 28bfad33a3bb51a61ff6fb4e389a876081331036 diff --git a/bec_plugins/__init__.py b/bec_plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/configs/config_session_start_e20632.yaml b/bec_plugins/configs/config_session_start_e20632.yaml similarity index 100% rename from configs/config_session_start_e20632.yaml rename to bec_plugins/configs/config_session_start_e20632.yaml diff --git a/configs/e21125_lamni_config.yaml b/bec_plugins/configs/e21125_lamni_config.yaml similarity index 100% rename from configs/e21125_lamni_config.yaml rename to bec_plugins/configs/e21125_lamni_config.yaml diff --git a/bec_plugins/plugins/__init__.py b/bec_plugins/plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bec_plugins/bec_client/hli/spec_hli.py b/bec_plugins/plugins/bec_client/hli/spec_hli.py similarity index 100% rename from bec_plugins/bec_client/hli/spec_hli.py rename to bec_plugins/plugins/bec_client/hli/spec_hli.py diff --git a/bec_plugins/bec_client/plugins/LamNI/LamNI_logo.png b/bec_plugins/plugins/bec_client/plugins/LamNI/LamNI_logo.png similarity index 100% rename from bec_plugins/bec_client/plugins/LamNI/LamNI_logo.png rename to bec_plugins/plugins/bec_client/plugins/LamNI/LamNI_logo.png diff --git a/bec_plugins/bec_client/plugins/LamNI/__init__.py b/bec_plugins/plugins/bec_client/plugins/LamNI/__init__.py similarity index 100% rename from bec_plugins/bec_client/plugins/LamNI/__init__.py rename to bec_plugins/plugins/bec_client/plugins/LamNI/__init__.py diff --git a/bec_plugins/bec_client/plugins/LamNI/bl_show_all.mac b/bec_plugins/plugins/bec_client/plugins/LamNI/bl_show_all.mac similarity index 100% rename from bec_plugins/bec_client/plugins/LamNI/bl_show_all.mac rename to bec_plugins/plugins/bec_client/plugins/LamNI/bl_show_all.mac diff --git a/bec_plugins/bec_client/plugins/LamNI/lamni_optics_mixin.py b/bec_plugins/plugins/bec_client/plugins/LamNI/lamni_optics_mixin.py similarity index 100% rename from bec_plugins/bec_client/plugins/LamNI/lamni_optics_mixin.py rename to bec_plugins/plugins/bec_client/plugins/LamNI/lamni_optics_mixin.py diff --git a/bec_plugins/bec_client/plugins/LamNI/load_additional_correction.py b/bec_plugins/plugins/bec_client/plugins/LamNI/load_additional_correction.py similarity index 100% rename from bec_plugins/bec_client/plugins/LamNI/load_additional_correction.py rename to bec_plugins/plugins/bec_client/plugins/LamNI/load_additional_correction.py diff --git a/bec_plugins/bec_client/plugins/LamNI/x_ray_eye_align.py b/bec_plugins/plugins/bec_client/plugins/LamNI/x_ray_eye_align.py similarity index 100% rename from bec_plugins/bec_client/plugins/LamNI/x_ray_eye_align.py rename to bec_plugins/plugins/bec_client/plugins/LamNI/x_ray_eye_align.py diff --git a/bec_plugins/bec_client/plugins/cSAXS/__init__.py b/bec_plugins/plugins/bec_client/plugins/cSAXS/__init__.py similarity index 100% rename from bec_plugins/bec_client/plugins/cSAXS/__init__.py rename to bec_plugins/plugins/bec_client/plugins/cSAXS/__init__.py diff --git a/bec_plugins/bec_client/plugins/cSAXS/cSAXS_beamline.py b/bec_plugins/plugins/bec_client/plugins/cSAXS/cSAXS_beamline.py similarity index 100% rename from bec_plugins/bec_client/plugins/cSAXS/cSAXS_beamline.py rename to bec_plugins/plugins/bec_client/plugins/cSAXS/cSAXS_beamline.py diff --git a/bec_plugins/plugins/bec_client/startup/post-startup.py b/bec_plugins/plugins/bec_client/startup/post-startup.py new file mode 100644 index 0000000..e69de29 diff --git a/bec_plugins/plugins/bec_client/startup/pre-startup.py b/bec_plugins/plugins/bec_client/startup/pre-startup.py new file mode 100644 index 0000000..e69de29 diff --git a/bec_plugins/scan_server/plugins/LamNIFermatScan.py b/bec_plugins/plugins/scan_server/scan_plugins/LamNIFermatScan.py similarity index 92% rename from bec_plugins/scan_server/plugins/LamNIFermatScan.py rename to bec_plugins/plugins/scan_server/scan_plugins/LamNIFermatScan.py index df715a4..b2223a6 100644 --- a/bec_plugins/scan_server/plugins/LamNIFermatScan.py +++ b/bec_plugins/plugins/scan_server/scan_plugins/LamNIFermatScan.py @@ -13,7 +13,7 @@ but they are executed in a specific order: - self.open_scan # send an open_scan message including the scan name, the number of points and the scan motor names - self.stage # stage all devices for the upcoming acquisiton - self.run_baseline_readings # read all devices to get a baseline for the upcoming scan -- self.scan_core # run a loop over all position +- self.scan_core # run a loop over all position - self._at_each_point(ind, pos) # called at each position with the current index and the target positions as arguments - self.finalize # clean up the scan, e.g. move back to the start position; wait everything to finish - self.unstage # unstage all devices that have been staged before @@ -23,8 +23,8 @@ but they are executed in a specific order: import time import numpy as np -from bec_utils import BECMessage, MessageEndpoints, bec_logger - +from bec_lib.core import BECMessage, MessageEndpoints, bec_logger +from scan_server.errors import ScanAbortion from scan_server.scans import RequestBase, ScanArgType, ScanBase MOVEMENT_SCALE_X = np.sin(np.radians(15)) * np.cos(np.radians(30)) @@ -243,17 +243,30 @@ class LamNIFermatScan(ScanBase, LamNIMixin): self.fov_circular = scan_kwargs.get("fov_circular", 0) self.stitch_overlap = scan_kwargs.get("stitch_overlap", 1) # self.keep_plot = scan_kwargs.get("keep_plot", 0) - # self.optim_trajectory = scan_kwargs.get("optim_trajectory", "corridor") + self.optim_trajectory = scan_kwargs.get("optim_trajectory", "corridor") + self.optim_trajectory_corridor = scan_kwargs.get("optim_trajectory_corridor") def initialize(self): self.scan_motors = ["rtx", "rty"] + def _optimize_trajectory(self): + self.positions = self.optimize_corridor( + self.positions, corridor_size=self.optim_trajectory_corridor + ) + def prepare_positions(self): self._calculate_positions() self._optimize_trajectory() # self._sort_positions() self.num_pos = len(self.positions) + self._check_min_positions() + + def _check_min_positions(self): + if self.num_pos < 20: + raise ScanAbortion( + f"The number of positions must exceed 20. Currently: {self.num_pos}." + ) def _lamni_check_pos_in_fov_range_and_circ_fov(self, x, y) -> bool: # this function checks if positions are reachable in a scan @@ -411,6 +424,16 @@ class LamNIFermatScan(ScanBase, LamNIMixin): logger.info("No rotation required") else: logger.info("Rotating to requested angle") + yield from self.stubs.scan_report_instruction( + { + "readback": { + "RID": self.metadata["RID"], + "devices": ["lsamrot"], + "start": [lsamrot_current_setpoint], + "end": [angle], + } + } + ) yield from self.stubs.set_and_wait(device=["lsamrot"], positions=[angle]) def scan_core(self): @@ -428,10 +451,14 @@ class LamNIFermatScan(ScanBase, LamNIMixin): msg = self.device_manager.producer.get(MessageEndpoints.device_status("rt_scan")) if msg: status = BECMessage.DeviceStatusMessage.loads(msg) - if status.content.get("status", 1) == 0 and self.metadata.get( - "RID" - ) == status.metadata.get("RID"): + status_id = status.content.get("status", 1) + request_id = status.metadata.get("RID") + if status_id == 0 and self.metadata.get("RID") == request_id: break + if status_id == 2 and self.metadata.get("RID") == request_id: + raise ScanAbortion( + f"An error occured during the LamNI readout: {status.metadata.get('error')}" + ) time.sleep(1) logger.debug("reading monitors") diff --git a/bec_plugins/scibec/lamni_config.py b/bec_plugins/plugins/scibec/lamni_config.py similarity index 100% rename from bec_plugins/scibec/lamni_config.py rename to bec_plugins/plugins/scibec/lamni_config.py diff --git a/bec_plugins/scibec/test_config_cSAXS.yaml b/bec_plugins/plugins/scibec/test_config_cSAXS.yaml similarity index 100% rename from bec_plugins/scibec/test_config_cSAXS.yaml rename to bec_plugins/plugins/scibec/test_config_cSAXS.yaml diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..bf71b83 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,21 @@ +[metadata] +name = bec_plugins +description = BEC plugins to modify the behaviour of services within the BEC framework +long_description = file: README.md +long_description_content_type = text/markdown +url = https://gitlab.psi.ch/bec/bec +project_urls = + Bug Tracker = https://gitlab.psi.ch/bec/bec/issues +classifiers = + Programming Language :: Python :: 3 + Development Status :: 3 - Alpha + Topic :: Scientific/Engineering + +[options] +package_dir = + = . +packages = find: +python_requires = >=3.8 + +[options.packages.find] +where = . diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..4ee4437 --- /dev/null +++ b/setup.py @@ -0,0 +1,7 @@ +from setuptools import setup + +if __name__ == "__main__": + setup( + install_requires=[], + extras_require={"dev": ["pytest", "pytest-random-order", "coverage"]}, + )