Skip to content

Commit

Permalink
Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
jackbdoughty committed Oct 10, 2024
1 parent f0af973 commit f7410ba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
27 changes: 19 additions & 8 deletions src/ibex_bluesky_core/devices/simpledae/reducers.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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="")

Expand Down Expand Up @@ -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="")

Expand All @@ -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))
Expand Down
36 changes: 29 additions & 7 deletions tests/devices/simpledae/test_reducers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])},
)
)
Expand All @@ -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
Expand Down Expand Up @@ -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])},
)
)
Expand All @@ -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(
Expand Down

0 comments on commit f7410ba

Please sign in to comment.