2 Commits

Author SHA1 Message Date
wakonig_k 9c164ea19f test(live_processing): change test mode to something more unlikely to be implemented 2025-06-23 18:34:56 +02:00
gac-x02da 096ac3ff43 Added process_average 2025-06-23 18:15:16 +02:00
14 changed files with 45 additions and 269 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
# It is needed to track the repo template version, and editing may break things.
# This file will be overwritten by copier on template updates.
_commit: v1.4.0
_commit: v1.0.0
_src_path: https://github.com/bec-project/plugin_copier_template.git
make_commit: false
project_name: tomcat_bec
-102
View File
@@ -1,102 +0,0 @@
name: CI for tomcat_bec
on:
push:
pull_request:
workflow_dispatch:
inputs:
BEC_WIDGETS_BRANCH:
description: "Branch of BEC Widgets to install"
required: false
type: string
default: "main"
BEC_CORE_BRANCH:
description: "Branch of BEC Core to install"
required: false
type: string
default: "main"
OPHYD_DEVICES_BRANCH:
description: "Branch of Ophyd Devices to install"
required: false
type: string
default: "main"
BEC_PLUGIN_REPO_BRANCH:
description: "Branch of the BEC Plugin Repository to install"
required: false
type: string
default: "main"
PYTHON_VERSION:
description: "Python version to use"
required: false
type: string
default: "3.12"
permissions:
pull-requests: write
jobs:
test:
runs-on: ubuntu-latest
env:
QTWEBENGINE_DISABLE_SANDBOX: 1
QT_QPA_PLATFORM: "offscreen"
steps:
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "${{ inputs.PYTHON_VERSION || '3.12' }}"
- name: Checkout BEC Plugin Repository
uses: actions/checkout@v4
with:
repository: bec/tomcat_bec
ref: "${{ inputs.BEC_PLUGIN_REPO_BRANCH || github.head_ref || github.sha }}"
path: ./tomcat_bec
- name: Lint for merge conflicts from template updates
shell: bash
# Find all Copier conflicts except this line
run: '! grep -r "<<<<<<< before updating" | grep -v "grep -r \"<<<<<<< before updating"'
- name: Checkout BEC Core
uses: actions/checkout@v4
with:
repository: bec/bec
ref: "${{ inputs.BEC_CORE_BRANCH || 'main' }}"
path: ./bec
- name: Checkout Ophyd Devices
uses: actions/checkout@v4
with:
repository: bec/ophyd_devices
ref: "${{ inputs.OPHYD_DEVICES_BRANCH || 'main' }}"
path: ./ophyd_devices
- name: Checkout BEC Widgets
uses: actions/checkout@v4
with:
repository: bec/bec_widgets
ref: "${{ inputs.BEC_WIDGETS_BRANCH || 'main' }}"
path: ./bec_widgets
- name: Install dependencies
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y libgl1 libegl1 x11-utils libxkbcommon-x11-0 libdbus-1-3 xvfb
sudo apt-get -y install libnss3 libxdamage1 libasound2t64 libatomic1 libxcursor1
- name: Install Python dependencies
shell: bash
run: |
pip install uv
uv pip install --system -e ./ophyd_devices
uv pip install --system -e ./bec/bec_lib[dev]
uv pip install --system -e ./bec/bec_ipython_client
uv pip install --system -e ./bec/bec_server[dev]
uv pip install --system -e ./bec_widgets[dev,pyside6]
uv pip install --system -e ./tomcat_bec[dev]
- name: Run Pytest with Coverage
id: coverage
run: pytest --random-order --cov=./tomcat_bec --cov-config=./tomcat_bec/pyproject.toml --cov-branch --cov-report=xml --no-cov-on-fail ./tomcat_bec/tests/ || test $? -eq 5
-70
View File
@@ -1,70 +0,0 @@
name: Create template upgrade PR for tomcat_bec
on:
workflow_dispatch:
permissions:
pull-requests: write
jobs:
create_update_branch_and_pr:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Checkout
uses: actions/checkout@v4
- name: Create virtualenv
run: |
python -m virtualenv .venv
- name: Install tools
run: |
source .venv/bin/activate
pip install copier PySide6 bec_lib
- name: Perform update
run: |
source .venv/bin/activate
git config --global user.email "bec_ci_staging@psi.ch"
git config --global user.name "BEC automated CI"
branch="chore/update-template-$(python -m uuid)"
echo "switching to branch $branch"
git checkout -b $branch
echo "Running copier update..."
copier update --trust --defaults --conflict inline 2>&1 | tee copier.log
status=${PIPESTATUS[0]}
output="$(cat copier.log)"
echo $output
msg="$(printf '%s\n' "$output" | head -n 1)"
if ! grep -q "make_commit: true" .copier-answers.yml ; then
echo "Autocommit not made, committing..."
git add -A
git commit -a -m "$msg"
fi
if diff-index --quiet HEAD ; then
echo "No changes detected"
exit 0
fi
git push -u origin $branch
curl -X POST "https://gitea.psi.ch/api/v1/repos/${{ gitea.repository }}/pulls" \
-H "Authorization: token ${{ secrets.CI_REPO_WRITE }}" \
-H "Content-Type: application/json" \
-d "{
\"title\": \"Template: $(echo $msg)\",
\"body\": \"This PR was created by Gitea Actions\",
\"head\": \"$(echo $branch)\",
\"base\": \"main\"
}"
+7
View File
@@ -0,0 +1,7 @@
include:
- file: /templates/plugin-repo-template.yml
inputs:
name: tomcat_bec
target: tomcat_bec
branch: $CHILD_PIPELINE_BRANCH
project: bec/awi_utils
+1 -4
View File
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
name = "tomcat_bec"
version = "0.0.0"
description = "The TOMCAT plugin repository for BEC"
requires-python = ">=3.11"
requires-python = ">=3.10"
classifiers = [
"Development Status :: 3 - Alpha",
"Programming Language :: Python :: 3",
@@ -48,9 +48,6 @@ plugin_file_writer = "tomcat_bec.file_writer"
[project.entry-points."bec.scans"]
plugin_scans = "tomcat_bec.scans"
[project.entry-points."bec.scans.scan_modifier"]
plugin_scan_modifier = "tomcat_bec.scans.scan_customization.scan_modifier"
[project.entry-points."bec.scans.metadata_schema"]
plugin_metadata_schema = "tomcat_bec.scans.metadata_schema"
@@ -11,7 +11,7 @@ from typeguard import TypeCheckError
from tomcat_bec.devices.std_daq.std_daq_live_processing import StdDaqLiveProcessing
def fake_redis_server(host, port, **kwargs):
def fake_redis_server(host, port):
redis = fakeredis.FakeRedis()
return redis
@@ -53,7 +53,7 @@ def test_std_daq_live_processing_set_mode(std_daq_live_processing):
std_daq_live_processing.set_mode("sum")
assert std_daq_live_processing.get_mode() == "sum"
with pytest.raises(TypeCheckError):
std_daq_live_processing.set_mode("average")
std_daq_live_processing.set_mode("mode_that_does_not_exist")
with pytest.raises(TypeCheckError):
std_daq_live_processing.set_mode(123)
@@ -3,12 +3,8 @@ 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.
"""
import os
from bec_lib.service_config import ServiceConfig
import tomcat_bec
def extend_command_line_args(parser):
"""
@@ -20,13 +16,8 @@ def extend_command_line_args(parser):
return parser
def get_config() -> ServiceConfig:
"""
Create and return the ServiceConfig for the plugin repository
"""
deployment_path = os.path.dirname(os.path.dirname(os.path.dirname(tomcat_bec.__file__)))
files = os.listdir(deployment_path)
if "bec_config.yaml" in files:
return ServiceConfig(config_path=os.path.join(deployment_path, "bec_config.yaml"))
else:
return ServiceConfig(redis={"host": "localhost", "port": 6379})
# def get_config() -> ServiceConfig:
# """
# Create and return the service configuration.
# """
# return ServiceConfig(redis={"host": "localhost", "port": 6379})
+11 -21
View File
@@ -468,15 +468,11 @@ class StdDaqClient:
Run the count callbacks with the given count.
The callbacks will be called with the count as argument.
"""
callbacks_ids = list(self._count_callbacks.keys())
for cb_id in callbacks_ids:
if cb_id not in self._count_callbacks:
continue
cb = self._count_callbacks[cb_id]
for cb in self._count_callbacks.values():
try:
cb(count)
except Exception as exc:
logger.error(f"Error in StdDAQ count callback with ID {cb_id}: {exc}")
logger.error(f"Error in StdDAQ count callback: {exc}")
def _run_status_callbacks(self):
"""
@@ -485,25 +481,19 @@ class StdDaqClient:
or exception, respectively and removed from the list of callbacks.
"""
status = self._status
status_callbacks_ids = list(self._status_callbacks.keys())
for status_id in status_callbacks_ids:
dev_status, success, error = self._status_callbacks.get(status_id, (None, [], []))
if dev_status is None:
continue
completed_callbacks = []
for dev_status, success, error in self._status_callbacks.values():
if dev_status.done:
logger.info("Status object already resolved. Skipping StdDaq callback.")
self._status_callbacks.pop(status_id)
logger.warning("Status object already resolved. Skipping StdDaq callback.")
continue
if status in success:
dev_status.set_finished()
logger.info(f"StdDaq status is {status}")
self._status_callbacks.pop(status_id)
continue
if status in error:
completed_callbacks.append(dev_status)
elif status in error:
logger.warning(f"StdDaq status is {status}")
dev_status.set_exception(StdDaqError(f"StdDaq status is {status}"))
self._status_callbacks.pop(status_id)
completed_callbacks.append(dev_status)
for cb in completed_callbacks:
self._status_callbacks.pop(id(cb))
@@ -45,14 +45,14 @@ class StdDaqLiveProcessing:
return self._mode
@typechecked
def set_mode(self, mode: Literal["sum"]):
def set_mode(self, mode: Literal["sum", "average"]):
"""
Set the processing mode.
Args:
mode (str): Processing mode, currently only "sum" is supported.
mode (str): Processing mode, currently only "sum" and "average" are supported.
"""
if mode not in ["sum"]:
raise ValueError("Unsupported mode. Only 'sum' is currently supported.")
if mode not in ["sum", "average"]:
raise ValueError("Unsupported mode. Only 'sum' and 'average' are currently supported.")
self._mode = mode
def set_enabled(self, value: bool):
@@ -83,6 +83,8 @@ class StdDaqLiveProcessing:
match self._mode:
case "sum":
self.process_sum(data)
case "average":
self.process_average(data)
case _:
raise ValueError(f"Unknown mode: {self._mode}")
@@ -98,6 +100,18 @@ class StdDaqLiveProcessing:
summed_data = np.sum(np.sum(data))
self.signal.put(summed_data)
def process_average(self, data: np.ndarray):
"""
Process data by averaging it.
Args:
data (np.ndarray): Data to average.
"""
if not isinstance(data, np.ndarray):
raise ValueError("Data must be a numpy array.")
averaged_data = np.mean(data)
self.signal.put(averaged_data)
########################################
## Flat and Dark Field References ######
########################################
-6
View File
@@ -1,6 +0,0 @@
# Macros
This directory is intended to store macros which will be loaded automatically when starting BEC.
Macros are small functions to make repetitive tasks easier. Functions defined in python files in this directory will be accessible from the BEC console.
Please do not put any code outside of function definitions here. If you wish for code to be automatically run when starting BEC, see the startup script at tomcat_bec/bec_ipython_client/startup/post_startup.py
For a guide on writing macros, please see: https://bec.readthedocs.io/en/latest/user/command_line_interface.html#how-to-write-a-macro
View File
@@ -1,12 +0,0 @@
"""
Scan components for tomcat_bec.
The scan components module allows you to define custom components that can be used in your scans.
These components can be used to encapsulate reusable logic, interact with devices, or perform specific actions during the scan lifecycle.
"""
from bec_server.scan_server.scans.scan_components import ScanComponents
class TomcatBecScanComponents(ScanComponents):
"""Scan components for tomcat_bec."""
@@ -1,33 +0,0 @@
"""
Scan modifier plugin for tomcat_bec.
The scan modifier allows you to modify the scan lifecycle and run custom actions before or after the scan hook or replace the scan hook entirely.
Note that the scan_modifier module must be registered as a plugin in the pyproject.toml file for it to be recognized by the BEC framework and that
there can only be one scan_modifier plugin registered at a time. If you need to run multiple scan modifiers, you can create a single scan
modifier plugin that runs multiple actions in sequence with conditional logic to determine which actions to run based on the scan context.
"""
from bec_server.scan_server.scans.scan_modifier import ScanModifier, scan_hook_impl
class TomcatBecScanModifier(ScanModifier):
"""
Scan modifier for tomcat_bec.
By inheriting from the ScanModifier base class, you get access to currently running scan (self.scan), the devices (self.dev), the scan info (self.scan_info),
the scan components (self.components) and the scan actions (self.actions).
"""
def __init__(self, **kwargs):
"""Initialize the scan modifier."""
super().__init__(**kwargs)
# Example of running code before the scan stage for a specific scan
# @scan_hook_impl("stage", "before")
# def before_stage(self):
# """Run before the stage hook."""
# self.actions.send_client_info("Custom stage logic executed by ScanModifier.")
# if self.scan_info.scan_name == "example_scan":
# self.dev.samx.set(20)