diff --git a/LICENSE b/LICENSE index 06070a5..f9b721a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ + BSD 3-Clause License -Copyright (c) 2024, Paul Scherrer Institute +Copyright (c) 2025, Paul Scherrer Institute Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -25,4 +26,4 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..20a11ba --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1 @@ +# Add anything you don't want to check in to git, e.g. very large files \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 3b1a399..0928bc8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,17 +5,26 @@ build-backend = "hatchling.build" [project] name = "xtreme_bec" version = "0.0.0" -description = "Custom device implementations based on the ophyd hardware abstraction layer" +description = "A plugin repository for BEC" requires-python = ">=3.10" classifiers = [ "Development Status :: 3 - Alpha", "Programming Language :: Python :: 3", "Topic :: Scientific/Engineering", ] -dependencies = ["ophyd", "bec_server", "ophyd_devices"] +dependencies = [] [project.optional-dependencies] -dev = ["black", "isort", "coverage", "pylint", "pytest", "pytest-random-order"] +dev = [ + "black", + "isort", + "coverage", + "pylint", + "pytest", + "pytest-random-order", + "ophyd_devices", + "bec_server", +] [project.entry-points."bec"] plugin_bec = "xtreme_bec" @@ -29,6 +38,9 @@ plugin_file_writer = "xtreme_bec.file_writer" [project.entry-points."bec.scans"] plugin_scans = "xtreme_bec.scans" +[project.entry-points."bec.scans.metadata_schema"] +plugin_metadata_schema = "xtreme_bec.scans.metadata_schema" + [project.entry-points."bec.ipython_client_startup"] plugin_ipython_client_pre = "xtreme_bec.bec_ipython_client.startup.pre_startup" plugin_ipython_client_post = "xtreme_bec.bec_ipython_client.startup" diff --git a/tests/tests_bec_ipython_client/README.md b/tests/tests_bec_ipython_client/README.md index 5762245..63d535f 100644 --- a/tests/tests_bec_ipython_client/README.md +++ b/tests/tests_bec_ipython_client/README.md @@ -1,31 +1,34 @@ # Getting Started with Testing using pytest -BEC is using the [pytest](https://docs.pytest.org/en/8.0.x/) framework. -It can be install via -``` bash +BEC is using the [pytest](https://docs.pytest.org/en/latest/) framework. +It can be installed via + +```bash pip install pytest ``` -in your *python environment*. + +in your _python environment_. We note that pytest is part of the optional-dependencies `[dev]` of the plugin package. ## Introduction Tests in this package should be stored in the `tests` directory. We suggest to sort tests of different submodules, i.e. `scans` or `devices` in the respective folder structure, and to folow a naming convention of ``. +It is mandatory for test files to begin with `test_` for pytest to discover them. -To run all tests, navigate to the directory of the plugin from the command line, and run the command +To run all tests, navigate to the directory of the plugin from the command line, and run the command -``` bash +```bash pytest -v --random-order ./tests ``` + Note, the python environment needs to be active. The additional arg `-v` allows pytest to run in verbose mode which provides more detailed information about the tests being run. -The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. +The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. ## Test examples -Writing tests can be quite specific for the given function. +Writing tests can be quite specific for the given function. We recommend writing tests as isolated as possible, i.e. try to test single functions instead of full classes. A very useful class to enable isolated testing is [MagicMock](https://docs.python.org/3/library/unittest.mock.html). In addition, we also recommend to take a look at the [How-to guides from pytest](https://docs.pytest.org/en/8.0.x/how-to/index.html). - diff --git a/tests/tests_bec_widgets/README.md b/tests/tests_bec_widgets/README.md index 5762245..63d535f 100644 --- a/tests/tests_bec_widgets/README.md +++ b/tests/tests_bec_widgets/README.md @@ -1,31 +1,34 @@ # Getting Started with Testing using pytest -BEC is using the [pytest](https://docs.pytest.org/en/8.0.x/) framework. -It can be install via -``` bash +BEC is using the [pytest](https://docs.pytest.org/en/latest/) framework. +It can be installed via + +```bash pip install pytest ``` -in your *python environment*. + +in your _python environment_. We note that pytest is part of the optional-dependencies `[dev]` of the plugin package. ## Introduction Tests in this package should be stored in the `tests` directory. We suggest to sort tests of different submodules, i.e. `scans` or `devices` in the respective folder structure, and to folow a naming convention of ``. +It is mandatory for test files to begin with `test_` for pytest to discover them. -To run all tests, navigate to the directory of the plugin from the command line, and run the command +To run all tests, navigate to the directory of the plugin from the command line, and run the command -``` bash +```bash pytest -v --random-order ./tests ``` + Note, the python environment needs to be active. The additional arg `-v` allows pytest to run in verbose mode which provides more detailed information about the tests being run. -The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. +The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. ## Test examples -Writing tests can be quite specific for the given function. +Writing tests can be quite specific for the given function. We recommend writing tests as isolated as possible, i.e. try to test single functions instead of full classes. A very useful class to enable isolated testing is [MagicMock](https://docs.python.org/3/library/unittest.mock.html). In addition, we also recommend to take a look at the [How-to guides from pytest](https://docs.pytest.org/en/8.0.x/how-to/index.html). - diff --git a/tests/tests_dap_services/README.md b/tests/tests_dap_services/README.md new file mode 100644 index 0000000..63d535f --- /dev/null +++ b/tests/tests_dap_services/README.md @@ -0,0 +1,34 @@ +# Getting Started with Testing using pytest + +BEC is using the [pytest](https://docs.pytest.org/en/latest/) framework. +It can be installed via + +```bash +pip install pytest +``` + +in your _python environment_. +We note that pytest is part of the optional-dependencies `[dev]` of the plugin package. + +## Introduction + +Tests in this package should be stored in the `tests` directory. +We suggest to sort tests of different submodules, i.e. `scans` or `devices` in the respective folder structure, and to folow a naming convention of ``. +It is mandatory for test files to begin with `test_` for pytest to discover them. + +To run all tests, navigate to the directory of the plugin from the command line, and run the command + +```bash +pytest -v --random-order ./tests +``` + +Note, the python environment needs to be active. +The additional arg `-v` allows pytest to run in verbose mode which provides more detailed information about the tests being run. +The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. + +## Test examples + +Writing tests can be quite specific for the given function. +We recommend writing tests as isolated as possible, i.e. try to test single functions instead of full classes. +A very useful class to enable isolated testing is [MagicMock](https://docs.python.org/3/library/unittest.mock.html). +In addition, we also recommend to take a look at the [How-to guides from pytest](https://docs.pytest.org/en/8.0.x/how-to/index.html). diff --git a/tests/tests_devices/README.md b/tests/tests_devices/README.md index 5762245..63d535f 100644 --- a/tests/tests_devices/README.md +++ b/tests/tests_devices/README.md @@ -1,31 +1,34 @@ # Getting Started with Testing using pytest -BEC is using the [pytest](https://docs.pytest.org/en/8.0.x/) framework. -It can be install via -``` bash +BEC is using the [pytest](https://docs.pytest.org/en/latest/) framework. +It can be installed via + +```bash pip install pytest ``` -in your *python environment*. + +in your _python environment_. We note that pytest is part of the optional-dependencies `[dev]` of the plugin package. ## Introduction Tests in this package should be stored in the `tests` directory. We suggest to sort tests of different submodules, i.e. `scans` or `devices` in the respective folder structure, and to folow a naming convention of ``. +It is mandatory for test files to begin with `test_` for pytest to discover them. -To run all tests, navigate to the directory of the plugin from the command line, and run the command +To run all tests, navigate to the directory of the plugin from the command line, and run the command -``` bash +```bash pytest -v --random-order ./tests ``` + Note, the python environment needs to be active. The additional arg `-v` allows pytest to run in verbose mode which provides more detailed information about the tests being run. -The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. +The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. ## Test examples -Writing tests can be quite specific for the given function. +Writing tests can be quite specific for the given function. We recommend writing tests as isolated as possible, i.e. try to test single functions instead of full classes. A very useful class to enable isolated testing is [MagicMock](https://docs.python.org/3/library/unittest.mock.html). In addition, we also recommend to take a look at the [How-to guides from pytest](https://docs.pytest.org/en/8.0.x/how-to/index.html). - diff --git a/tests/tests_file_writer/README.md b/tests/tests_file_writer/README.md index 5762245..63d535f 100644 --- a/tests/tests_file_writer/README.md +++ b/tests/tests_file_writer/README.md @@ -1,31 +1,34 @@ # Getting Started with Testing using pytest -BEC is using the [pytest](https://docs.pytest.org/en/8.0.x/) framework. -It can be install via -``` bash +BEC is using the [pytest](https://docs.pytest.org/en/latest/) framework. +It can be installed via + +```bash pip install pytest ``` -in your *python environment*. + +in your _python environment_. We note that pytest is part of the optional-dependencies `[dev]` of the plugin package. ## Introduction Tests in this package should be stored in the `tests` directory. We suggest to sort tests of different submodules, i.e. `scans` or `devices` in the respective folder structure, and to folow a naming convention of ``. +It is mandatory for test files to begin with `test_` for pytest to discover them. -To run all tests, navigate to the directory of the plugin from the command line, and run the command +To run all tests, navigate to the directory of the plugin from the command line, and run the command -``` bash +```bash pytest -v --random-order ./tests ``` + Note, the python environment needs to be active. The additional arg `-v` allows pytest to run in verbose mode which provides more detailed information about the tests being run. -The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. +The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. ## Test examples -Writing tests can be quite specific for the given function. +Writing tests can be quite specific for the given function. We recommend writing tests as isolated as possible, i.e. try to test single functions instead of full classes. A very useful class to enable isolated testing is [MagicMock](https://docs.python.org/3/library/unittest.mock.html). In addition, we also recommend to take a look at the [How-to guides from pytest](https://docs.pytest.org/en/8.0.x/how-to/index.html). - diff --git a/tests/tests_scans/README.md b/tests/tests_scans/README.md index 5762245..63d535f 100644 --- a/tests/tests_scans/README.md +++ b/tests/tests_scans/README.md @@ -1,31 +1,34 @@ # Getting Started with Testing using pytest -BEC is using the [pytest](https://docs.pytest.org/en/8.0.x/) framework. -It can be install via -``` bash +BEC is using the [pytest](https://docs.pytest.org/en/latest/) framework. +It can be installed via + +```bash pip install pytest ``` -in your *python environment*. + +in your _python environment_. We note that pytest is part of the optional-dependencies `[dev]` of the plugin package. ## Introduction Tests in this package should be stored in the `tests` directory. We suggest to sort tests of different submodules, i.e. `scans` or `devices` in the respective folder structure, and to folow a naming convention of ``. +It is mandatory for test files to begin with `test_` for pytest to discover them. -To run all tests, navigate to the directory of the plugin from the command line, and run the command +To run all tests, navigate to the directory of the plugin from the command line, and run the command -``` bash +```bash pytest -v --random-order ./tests ``` + Note, the python environment needs to be active. The additional arg `-v` allows pytest to run in verbose mode which provides more detailed information about the tests being run. -The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. +The argument `--random-order` instructs pytest to run the tests in random order, which is the default in the CI pipelines. ## Test examples -Writing tests can be quite specific for the given function. +Writing tests can be quite specific for the given function. We recommend writing tests as isolated as possible, i.e. try to test single functions instead of full classes. A very useful class to enable isolated testing is [MagicMock](https://docs.python.org/3/library/unittest.mock.html). In addition, we also recommend to take a look at the [How-to guides from pytest](https://docs.pytest.org/en/8.0.x/how-to/index.html). - diff --git a/xtreme_bec/bec_ipython_client/startup/post_startup.py b/xtreme_bec/bec_ipython_client/startup/post_startup.py index 4ec2bfd..07d6da4 100644 --- a/xtreme_bec/bec_ipython_client/startup/post_startup.py +++ b/xtreme_bec/bec_ipython_client/startup/post_startup.py @@ -10,7 +10,7 @@ While command-line arguments have to be set in the pre-startup script, the post-startup script can be used to load beamline specific information and to setup the prompts. - from bec_lib import bec_logger + from bec_lib.logger import bec_logger logger = bec_logger.logger diff --git a/xtreme_bec/bec_ipython_client/startup/pre_startup.py b/xtreme_bec/bec_ipython_client/startup/pre_startup.py index e22e554..972a34a 100644 --- a/xtreme_bec/bec_ipython_client/startup/pre_startup.py +++ b/xtreme_bec/bec_ipython_client/startup/pre_startup.py @@ -1,6 +1,6 @@ """ Pre-startup script for BEC client. This script is executed before the BEC client -is started. It can be used to add additional command line arguments. +is started. It can be used to add additional command line arguments. """ from bec_lib.service_config import ServiceConfig diff --git a/xtreme_bec/scans/metadata_schema/metadata_schema_registry.py b/xtreme_bec/scans/metadata_schema/metadata_schema_registry.py new file mode 100644 index 0000000..deb6ef4 --- /dev/null +++ b/xtreme_bec/scans/metadata_schema/metadata_schema_registry.py @@ -0,0 +1,12 @@ +# from .metadata_schema_template import ExampleSchema + +METADATA_SCHEMA_REGISTRY = { + # Add models which should be used to validate scan metadata here. + # Make a model according to the template, and import it as above + # Then associate it with a scan like so: + # "example_scan": ExampleSchema +} + +# Define a default schema type which should be used as the fallback for everything: + +DEFAULT_SCHEMA = None diff --git a/xtreme_bec/scans/metadata_schema/metadata_schema_template.py b/xtreme_bec/scans/metadata_schema/metadata_schema_template.py new file mode 100644 index 0000000..c12e364 --- /dev/null +++ b/xtreme_bec/scans/metadata_schema/metadata_schema_template.py @@ -0,0 +1,34 @@ +# # By inheriting from BasicScanMetadata you can define a schema by which metadata +# # supplied to a scan must be validated. +# # This schema is a Pydantic model: https://docs.pydantic.dev/latest/concepts/models/ +# # but by default it will still allow you to add any arbitrary information to it. +# # That is to say, when you run a scan with which such a model has been associated in the +# # metadata_schema_registry, you can supply any python dictionary with strings as keys +# # and built-in python types (strings, integers, floats) as values, and these will be +# # added to the experiment metadata, but it *must* contain the keys and values of the +# # types defined in the schema class. +# # +# # +# # For example, say that you would like to enforce recording information about sample +# # pretreatment, you could define the following: +# # +# +# from bec_lib.metadata_schema import BasicScanMetadata +# +# +# class ExampleSchema(BasicScanMetadata): +# treatment_description: str +# treatment_temperature_k: int +# +# +# # If this was used according to the example in metadata_schema_registry.py, +# # then when calling the scan, the user would need to write something like: +# >>> scans.example_scan( +# >>> motor, +# >>> 1, +# >>> 2, +# >>> 3, +# >>> metadata={"treatment_description": "oven overnight", "treatment_temperature_k": 575}, +# >>> ) +# +# # And the additional metadata would be saved in the HDF5 file created for the scan.