2 Commits

Author SHA1 Message Date
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
11 changed files with 45 additions and 213 deletions

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.2.8
_commit: v1.0.0
_src_path: https://github.com/bec-project/plugin_copier_template.git
make_commit: false
project_name: tomcat_bec

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

View File

@@ -1,62 +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: Install tools
run: |
pip install copier PySide6
- name: Checkout
uses: actions/checkout@v4
- name: Perform update
run: |
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..."
output="$(copier update --trust --defaults --conflict inline 2>&1)"
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
.gitlab-ci.yml Normal file
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

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",

View File

@@ -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)

View File

@@ -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})

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))

View File

@@ -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 ######
########################################

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