Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into qt_create_axes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Willemsen committed Dec 2, 2024
2 parents 5eba4b1 + 78a4110 commit e6d7f6c
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 89 deletions.
2 changes: 1 addition & 1 deletion manual_system_tests/dae_scan.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Demonstration plan showing basic bluesky functionality."""

import os
from collections.abc import Generator
from pathlib import Path
from typing import Generator

import bluesky.plan_stubs as bps
import bluesky.plans as bp
Expand Down
2 changes: 1 addition & 1 deletion manual_system_tests/interruption.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Demonstration plan showing basic bluesky functionality."""

import os
from typing import Generator
from collections.abc import Generator

import bluesky.plan_stubs as bps
from bluesky.utils import Msg
Expand Down
49 changes: 35 additions & 14 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,56 @@ line-length = 100
indent-width = 4

[lint]
preview = true
extend-select = [
"N", # pep8-naming
"D", # pydocstyle (can use this later but for now causes too many errors)
"D", # pydocstyle
"I", # isort (for imports)
"E501", # Line too long ({width} > {limit})
"E",
"F",
"W",
"ANN",
"ASYNC", # Asyncio-specific checks
"B",
"NPY", # Numpy-specific rules
"RUF", # Ruff-specific checks, include some useful asyncio rules
"E", # Pycodestyle errors
"W", # Pycodestyle warnings
"F", # Pyflakes
"PL", # Pylint
"B", # Flake8-bugbear
"PIE", # Flake8-pie
"ANN", # Annotations
"ASYNC", # Asyncio-specific checks
"NPY", # Numpy-specific rules
"RUF", # Ruff-specific checks, include some useful asyncio rules
"FURB", # Rules from refurb
"ERA", # Commented-out code
"PT", # Pytest-specific rules
"LOG", # Logging-specific rules
"G", # Logging-specific rules
"UP", # Pyupgrade
"SLF", # Private-member usage
"PERF", # Performance-related rules
]
ignore = [
"D406", # Section name should end with a newline ("{name}")
"D407", # Missing dashed underline after section ("{name}")
"D213", # Incompatible with D212
"D203", # Incompatible with D211
"D213", # Incompatible with D212
"D203", # Incompatible with D211
"B901", # This is a standard, expected, pattern in bluesky
"PLR6301" # Too noisy
]
[lint.per-file-ignores]
"tests/*" = [
"N802",
"D", # Don't require method documentation for test methods
"ANN" # Don't require tests to use type annotations
"N802", # Allow test names to be long / not pep8
"D", # Don't require method documentation for test methods
"ANN", # Don't require tests to use type annotations
"PLR2004", # Allow magic numbers in tests
"PLR0915", # Allow complex tests
"PLR0914", # Allow complex tests
"PLC2701", # Allow tests to import "private" things
"SLF001", # Allow tests to use "private" things
]
"doc/conf.py" = [
"D100"
]

[lint.pep8-naming]
extend-ignore-names = ["RE"] # Conventional name used for RunEngine

[lint.pylint]
max-args = 6
2 changes: 1 addition & 1 deletion src/ibex_bluesky_core/callbacks/document_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ def __call__(self, name: str, document: dict[str, Any]) -> None:

to_write: dict[str, Any] = {"type": name, "document": document}

with open(self.filename, "a") as outfile:
with open(self.filename, "a", encoding="utf8") as outfile:
outfile.write(f"{json.dumps(to_write)}\n")
4 changes: 2 additions & 2 deletions src/ibex_bluesky_core/callbacks/file_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def start(self, doc: RunStart) -> None:
)
header_data[START_TIME] = formatted_time

with open(self.filename, "a", newline="") as outfile:
with open(self.filename, "a", newline="", encoding="utf-8") as outfile:
for key, value in header_data.items():
outfile.write(f"{key}: {value}\n")

Expand Down Expand Up @@ -102,7 +102,7 @@ def event(self, doc: Event) -> Event:
else value
)

with open(self.filename, "a", newline="") as outfile:
with open(self.filename, "a", newline="", encoding="utf-8") as outfile:
file_delimiter = ","
if doc[SEQ_NUM] == 1:
# If this is the first event, write out the units before writing event data.
Expand Down
7 changes: 3 additions & 4 deletions src/ibex_bluesky_core/callbacks/fitting/fitting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def model(cls, *args: int) -> lmfit.Model:
(x-values: NDArray, parameters: np.float64 -> y-values: NDArray)
"""
pass

@classmethod
@abstractmethod
Expand All @@ -47,7 +46,6 @@ def guess(
(x-values: NDArray, y-values: NDArray -> parameters: Dict[str, lmfit.Parameter])
"""
pass

@classmethod
def fit(cls, *args: int) -> FitMethod:
Expand Down Expand Up @@ -198,8 +196,9 @@ class Polynomial(Fit):
@classmethod
def _check_degree(cls, args: tuple[int, ...]) -> int:
"""Check that polynomial degree is valid."""
degree = args[0] if args else 7
if not (0 <= degree <= 7):
max_degree = 7
degree = args[0] if args else max_degree
if not (0 <= degree <= max_degree):
raise ValueError("The polynomial degree should be at least 0 and smaller than 8.")
return degree

Expand Down
2 changes: 1 addition & 1 deletion src/ibex_bluesky_core/callbacks/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
"""
super().__init__(y=y, x=x, *args, **kwargs) # noqa: B026
if yerr is not None:
self.yerr, *others = get_obj_fields([yerr])
self.yerr, *_others = get_obj_fields([yerr])
else:
self.yerr = None
self.yerr_data = []
Expand Down
6 changes: 3 additions & 3 deletions src/ibex_bluesky_core/devices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import binascii
import os
import zlib
from typing import Type, TypeVar
from typing import TypeVar

from ophyd_async.core import SignalDatatype, SignalRW
from ophyd_async.epics.core import epics_signal_rw
Expand All @@ -18,7 +18,7 @@ def get_pv_prefix() -> str:
prefix = os.getenv("MYPVPREFIX")

if prefix is None:
raise EnvironmentError("MYPVPREFIX environment variable not available - please define")
raise OSError("MYPVPREFIX environment variable not available - please define")

return prefix

Expand Down Expand Up @@ -48,7 +48,7 @@ def compress_and_hex(value: str) -> bytes:
return binascii.hexlify(compr)


def isis_epics_signal_rw(datatype: Type[T], read_pv: str, name: str = "") -> SignalRW[T]:
def isis_epics_signal_rw(datatype: type[T], read_pv: str, name: str = "") -> SignalRW[T]:
"""Make a RW signal with ISIS' PV naming standard ie. read_pv as TITLE, write_pv as TITLE:SP."""
write_pv = f"{read_pv}:SP"
return epics_signal_rw(datatype, read_pv, write_pv, name)
14 changes: 7 additions & 7 deletions src/ibex_bluesky_core/devices/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import asyncio
import logging
from dataclasses import dataclass
from typing import Callable, Generic, Type, TypeVar
from typing import Callable, Generic, TypeVar

from bluesky.protocols import Locatable, Location, Movable, Triggerable
from ophyd_async.core import (
Expand Down Expand Up @@ -119,7 +119,7 @@ def __init__(self, prefix: str, name: str = "") -> None:
class BlockR(StandardReadable, Triggerable, Generic[T]):
"""Device representing an IBEX readable block of arbitrary data type."""

def __init__(self, datatype: Type[T], prefix: str, block_name: str) -> None:
def __init__(self, datatype: type[T], prefix: str, block_name: str) -> None:
"""Create a new read-only block.
Args:
Expand Down Expand Up @@ -154,7 +154,7 @@ class BlockRw(BlockR[T], Movable):

def __init__(
self,
datatype: Type[T],
datatype: type[T],
prefix: str,
block_name: str,
*,
Expand Down Expand Up @@ -231,7 +231,7 @@ class BlockRwRbv(BlockRw[T], Locatable):

def __init__(
self,
datatype: Type[T],
datatype: type[T],
prefix: str,
block_name: str,
*,
Expand Down Expand Up @@ -326,7 +326,7 @@ def __repr__(self) -> str:
return f"{self.__class__.__name__}(name={self.name})"


def block_r(datatype: Type[T], block_name: str) -> BlockR[T]:
def block_r(datatype: type[T], block_name: str) -> BlockR[T]:
"""Get a local read-only block for the current instrument.
See documentation of BlockR for more information.
Expand All @@ -335,7 +335,7 @@ def block_r(datatype: Type[T], block_name: str) -> BlockR[T]:


def block_rw(
datatype: Type[T], block_name: str, *, write_config: BlockWriteConfig[T] | None = None
datatype: type[T], block_name: str, *, write_config: BlockWriteConfig[T] | None = None
) -> BlockRw[T]:
"""Get a local read-write block for the current instrument.
Expand All @@ -347,7 +347,7 @@ def block_rw(


def block_rw_rbv(
datatype: Type[T], block_name: str, *, write_config: BlockWriteConfig[T] | None = None
datatype: type[T], block_name: str, *, write_config: BlockWriteConfig[T] | None = None
) -> BlockRwRbv[T]:
"""Get a local read/write/setpoint readback block for the current instrument.
Expand Down
8 changes: 4 additions & 4 deletions src/ibex_bluesky_core/devices/dae/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Utilities for the DAE device - mostly XML helpers."""

from enum import Enum
from typing import Any, Dict, List
from typing import Any
from xml.etree.ElementTree import Element


def convert_xml_to_names_and_values(xml: Element) -> Dict[str, str]:
def convert_xml_to_names_and_values(xml: Element) -> dict[str, str]:
"""Convert an XML element's children to a dict containing <Name>.text:<Val>.text."""
names_and_values = dict()
elements = get_all_elements_in_xml_with_child_called_name(xml)
Expand All @@ -16,7 +16,7 @@ def convert_xml_to_names_and_values(xml: Element) -> Dict[str, str]:
return names_and_values


def get_all_elements_in_xml_with_child_called_name(xml: Element) -> List[Element]:
def get_all_elements_in_xml_with_child_called_name(xml: Element) -> list[Element]:
"""Find all elements with a "name" element, but ignore the first one as it's the root."""
elements = xml.findall("*/Name/..")
return elements
Expand All @@ -32,7 +32,7 @@ def _get_names_and_values(element: Element) -> tuple[Any, Any] | tuple[None, Non


def set_value_in_dae_xml(
elements: List[Element], name: str, value: str | Enum | int | None | float
elements: list[Element], name: str, value: str | Enum | int | float | None
) -> None:
"""Find and set a value in the DAE XML, given a name and value.
Expand Down
11 changes: 5 additions & 6 deletions src/ibex_bluesky_core/devices/dae/dae_period_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import xml.etree.ElementTree as ET
from dataclasses import dataclass
from enum import Enum
from typing import List
from xml.etree.ElementTree import tostring

from bluesky.protocols import Locatable, Location, Movable
Expand Down Expand Up @@ -58,13 +57,13 @@ class SinglePeriodSettings:
class DaePeriodSettingsData:
"""Dataclass for the hardware period settings."""

periods_settings: List[SinglePeriodSettings] | None = None
periods_soft_num: None | int = None
periods_settings: list[SinglePeriodSettings] | None = None
periods_soft_num: int | None = None
periods_type: PeriodType | None = None
periods_src: PeriodSource | None = None
periods_file: None | str = None
periods_seq: None | int = None
periods_delay: None | int = None
periods_file: str | None = None
periods_seq: int | None = None
periods_delay: int | None = None


def _convert_xml_to_period_settings(value: str) -> DaePeriodSettingsData:
Expand Down
5 changes: 2 additions & 3 deletions src/ibex_bluesky_core/devices/dae/dae_tcb_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import xml.etree.ElementTree as ET
from dataclasses import dataclass
from enum import Enum
from typing import Dict
from xml.etree.ElementTree import tostring

from bluesky.protocols import Locatable, Location, Movable
Expand Down Expand Up @@ -66,14 +65,14 @@ class TimeRegimeRow:
class TimeRegime:
"""Time regime - contains a dict(rows) which is row_number:TimeRegimeRow."""

rows: Dict[int, TimeRegimeRow]
rows: dict[int, TimeRegimeRow]


@dataclass(kw_only=True)
class DaeTCBSettingsData:
"""Dataclass for the DAE TCB settings."""

tcb_tables: Dict[int, TimeRegime] | None = None
tcb_tables: dict[int, TimeRegime] | None = None
tcb_file: str | None = None
time_unit: TimeUnit | None = None
tcb_calculation_method: CalculationMethod | None = None
Expand Down
9 changes: 4 additions & 5 deletions src/ibex_bluesky_core/devices/simpledae/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def __init__(
"""
self.prefix = prefix
self.controller: "Controller" = controller
self.waiter: "Waiter" = waiter
self.reducer: "Reducer" = reducer
self.controller: Controller = controller
self.waiter: Waiter = waiter
self.reducer: Reducer = reducer

logger.info(
"created simpledae with prefix=%s, controller=%s, waiter=%s, reducer=%s",
Expand All @@ -72,8 +72,7 @@ def __init__(
# published when the top-level SimpleDae object is read.
extra_readables = set()
for strategy in [self.controller, self.waiter, self.reducer]:
for sig in strategy.additional_readable_signals(self):
extra_readables.add(sig)
extra_readables.update(strategy.additional_readable_signals(self))
logger.info("extra readables: %s", list(extra_readables))
self.add_readables(devices=list(extra_readables))

Expand Down
4 changes: 2 additions & 2 deletions src/ibex_bluesky_core/devices/simpledae/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def start_counting(self, dae: "SimpleDae") -> None:
await dae.controls.resume_run.trigger(wait=True, timeout=None)
await wait_for_value(
dae.run_state,
lambda v: v in [RunstateEnum.RUNNING, RunstateEnum.WAITING, RunstateEnum.VETOING],
lambda v: v in {RunstateEnum.RUNNING, RunstateEnum.WAITING, RunstateEnum.VETOING},
timeout=10,
)

Expand Down Expand Up @@ -130,7 +130,7 @@ async def start_counting(self, dae: "SimpleDae") -> None:
await dae.controls.begin_run.trigger(wait=True, timeout=None)
await wait_for_value(
dae.run_state,
lambda v: v in [RunstateEnum.RUNNING, RunstateEnum.WAITING, RunstateEnum.VETOING],
lambda v: v in {RunstateEnum.RUNNING, RunstateEnum.WAITING, RunstateEnum.VETOING},
timeout=10,
)

Expand Down
7 changes: 4 additions & 3 deletions src/ibex_bluesky_core/devices/simpledae/reducers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import asyncio
import logging
import math
from abc import ABCMeta, abstractmethod
from typing import TYPE_CHECKING, Collection, Sequence
from abc import ABC, abstractmethod
from collections.abc import Collection, Sequence
from typing import TYPE_CHECKING

import scipp as sc
from ophyd_async.core import (
Expand Down Expand Up @@ -41,7 +42,7 @@ async def sum_spectra(spectra: Collection[DaeSpectra]) -> sc.Variable | sc.DataA
return summed_counts


class ScalarNormalizer(Reducer, StandardReadable, metaclass=ABCMeta):
class ScalarNormalizer(Reducer, StandardReadable, ABC):
"""Sum a set of user-specified spectra, then normalize by a scalar signal."""

def __init__(self, prefix: str, detector_spectra: Sequence[int]) -> None:
Expand Down
Loading

0 comments on commit e6d7f6c

Please sign in to comment.