From f7410ba94c24dee9e75b5bca9913c8e1bc9f8548 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Thu, 10 Oct 2024 13:29:34 +0100 Subject: [PATCH] Ruff --- .../devices/simpledae/reducers.py | 27 +++++++++----- tests/devices/simpledae/test_reducers.py | 36 +++++++++++++++---- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/ibex_bluesky_core/devices/simpledae/reducers.py b/src/ibex_bluesky_core/devices/simpledae/reducers.py index 4bf6208..b47004c 100644 --- a/src/ibex_bluesky_core/devices/simpledae/reducers.py +++ b/src/ibex_bluesky_core/devices/simpledae/reducers.py @@ -1,12 +1,11 @@ """DAE data reduction strategies.""" import asyncio +import math from abc import ABCMeta, abstractmethod from typing import TYPE_CHECKING, Collection, Sequence -import numpy as np import scipp as sc -import math from ophyd_async.core import ( Device, DeviceVector, @@ -55,8 +54,12 @@ def __init__(self, prefix: str, detector_spectra: Sequence[int]) -> None: self.det_counts, self._det_counts_setter = soft_signal_r_and_setter(float, 0.0) self.intensity, self._intensity_setter = soft_signal_r_and_setter(float, 0.0, precision=6) - self.det_counts_stddev, self._det_counts_stddev_setter = soft_signal_r_and_setter(float, 0.0) - self.intensity_stddev, self._intensity_stddev_setter = soft_signal_r_and_setter(float, 0.0, precision=6) + self.det_counts_stddev, self._det_counts_stddev_setter = soft_signal_r_and_setter( + float, 0.0 + ) + self.intensity_stddev, self._intensity_stddev_setter = soft_signal_r_and_setter( + float, 0.0, precision=6 + ) super().__init__(name="") @@ -135,9 +138,15 @@ def __init__( self.mon_counts, self._mon_counts_setter = soft_signal_r_and_setter(float, 0.0) self.intensity, self._intensity_setter = soft_signal_r_and_setter(float, 0.0, precision=6) - self.det_counts_stddev, self._det_counts_stddev_setter = soft_signal_r_and_setter(float, 0.0) - self.mon_counts_stddev, self._mon_counts_stddev_setter = soft_signal_r_and_setter(float, 0.0) - self.intensity_stddev, self._intensity_stddev_setter = soft_signal_r_and_setter(float, 0.0, precision=6) + self.det_counts_stddev, self._det_counts_stddev_setter = soft_signal_r_and_setter( + float, 0.0 + ) + self.mon_counts_stddev, self._mon_counts_stddev_setter = soft_signal_r_and_setter( + float, 0.0 + ) + self.intensity_stddev, self._intensity_stddev_setter = soft_signal_r_and_setter( + float, 0.0, precision=6 + ) super().__init__(name="") @@ -154,7 +163,9 @@ async def reduce_data(self, dae: "SimpleDae") -> None: detector_counts_var = 0.0 if detector_counts.variance is None else detector_counts.variance monitor_counts_var = 0.0 if monitor_counts.variance is None else monitor_counts.variance - intensity_var = 0.0 if monitor_counts_var == 0.0 else (detector_counts / monitor_counts).variance + intensity_var = ( + 0.0 if monitor_counts_var == 0.0 else (detector_counts / monitor_counts).variance + ) self._det_counts_stddev_setter(math.sqrt(detector_counts_var)) self._mon_counts_stddev_setter(math.sqrt(monitor_counts_var)) diff --git a/tests/devices/simpledae/test_reducers.py b/tests/devices/simpledae/test_reducers.py index 1a5f3df..85f9e84 100644 --- a/tests/devices/simpledae/test_reducers.py +++ b/tests/devices/simpledae/test_reducers.py @@ -117,13 +117,23 @@ async def test_period_good_frames_normalizer_uncertainties( period_good_frames_reducer.detectors[1].read_spectrum_dataarray = AsyncMock( return_value=sc.DataArray( - data=sc.Variable(dims=["tof"], values=[1000.0, 2000.0, 3000.0], variances=[1000.0, 2000.0, 3000.0], unit=sc.units.counts), + data=sc.Variable( + dims=["tof"], + values=[1000.0, 2000.0, 3000.0], + variances=[1000.0, 2000.0, 3000.0], + unit=sc.units.counts, + ), coords={"tof": sc.array(dims=["tof"], values=[0, 1, 2, 3])}, ) ) period_good_frames_reducer.detectors[2].read_spectrum_dataarray = AsyncMock( return_value=sc.DataArray( - data=sc.Variable(dims=["tof"], values=[4000.0, 5000.0, 6000.0], variances=[4000.0, 5000.0, 6000.0], unit=sc.units.counts), + data=sc.Variable( + dims=["tof"], + values=[4000.0, 5000.0, 6000.0], + variances=[4000.0, 5000.0, 6000.0], + unit=sc.units.counts, + ), coords={"tof": sc.array(dims=["tof"], values=[0, 1, 2, 3])}, ) ) @@ -134,7 +144,7 @@ async def test_period_good_frames_normalizer_uncertainties( intensity_stddev = await period_good_frames_reducer.intensity_stddev.get_value() assert det_counts_stddev == math.sqrt(21000) - assert intensity_stddev == pytest.approx(math.sqrt((21000 + (123**2 / 21000) ) / 123**2), 1e-4) + assert intensity_stddev == pytest.approx(math.sqrt((21000 + (123**2 / 21000)) / 123**2), 1e-4) # Monitor Normalizer @@ -165,16 +175,28 @@ async def test_monitor_normalizer(simpledae: SimpleDae, monitor_normalizer: Moni assert intensity == pytest.approx(6000 / 15000) -async def test_monitor_normalizer_uncertainties(simpledae: SimpleDae, monitor_normalizer: MonitorNormalizer): +async def test_monitor_normalizer_uncertainties( + simpledae: SimpleDae, monitor_normalizer: MonitorNormalizer +): monitor_normalizer.detectors[1].read_spectrum_dataarray = AsyncMock( return_value=sc.DataArray( - data=sc.Variable(dims=["tof"], values=[1000.0, 2000.0, 3000.0], variances=[1000.0, 2000.0, 3000.0], unit=sc.units.counts), + data=sc.Variable( + dims=["tof"], + values=[1000.0, 2000.0, 3000.0], + variances=[1000.0, 2000.0, 3000.0], + unit=sc.units.counts, + ), coords={"tof": sc.array(dims=["tof"], values=[0, 1, 2, 3])}, ) ) monitor_normalizer.monitors[2].read_spectrum_dataarray = AsyncMock( return_value=sc.DataArray( - data=sc.Variable(dims=["tof"], values=[4000.0, 5000.0, 6000.0], variances=[4000.0, 5000.0, 6000.0], unit=sc.units.counts), + data=sc.Variable( + dims=["tof"], + values=[4000.0, 5000.0, 6000.0], + variances=[4000.0, 5000.0, 6000.0], + unit=sc.units.counts, + ), coords={"tof": sc.array(dims=["tof"], values=[0, 1, 2, 3])}, ) ) @@ -187,7 +209,7 @@ async def test_monitor_normalizer_uncertainties(simpledae: SimpleDae, monitor_no assert det_counts_stddev == math.sqrt(6000) assert mon_counts_stddev == math.sqrt(15000) - assert intensity_stddev == pytest.approx(math.sqrt((6000 + (6000**2 / 15000) ) / 15000**2), 1e-4) + assert intensity_stddev == pytest.approx(math.sqrt((6000 + (6000**2 / 15000)) / 15000**2), 1e-4) async def test_monitor_normalizer_publishes_raw_and_normalized_counts(