Skip to content

Commit

Permalink
merge changes with imports,
Browse files Browse the repository at this point in the history
add local variable bounds_value to compare number of bounds, and rephrasing some API reference explanation.
  • Loading branch information
esmith1729 committed Dec 13, 2024
1 parent 62e90a8 commit 9294e97
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/ibex_bluesky_core/devices/simpledae/reducers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import asyncio
import logging
import math
from abc import ABCMeta, abstractmethod
from abc import ABC, abstractmethod
from collections.abc import Collection, Sequence
from typing import TYPE_CHECKING, Awaitable, Callable, Collection, Sequence
from typing import TYPE_CHECKING, Awaitable, Callable

Check failure on line 8 in src/ibex_bluesky_core/devices/simpledae/reducers.py

View workflow job for this annotation

GitHub Actions / call-linter-workflow / ruff

Ruff (UP035)

src/ibex_bluesky_core/devices/simpledae/reducers.py:8:1: UP035 Import from `collections.abc` instead: `Awaitable`

import scipp as sc
from ophyd_async.core import (
Expand Down Expand Up @@ -60,9 +60,10 @@ def tof_bounded_spectra(
More info on scipp arrays and scalars can be found here: https://scipp.github.io/generated/functions/scipp.scalar.html
"""
bounds_value = 2
if "tof" not in bounds.dims:
raise ValueError("Should contain tof dims")
if bounds.sizes["tof"] != 2:
if bounds.sizes["tof"] != bounds_value:
raise ValueError("Should contain lower and upper bound")

async def sum_spectra_with_tof(spectra: Collection[DaeSpectra]) -> sc.Variable | sc.DataArray:
Expand All @@ -77,15 +78,15 @@ async def sum_spectra_with_tof(spectra: Collection[DaeSpectra]) -> sc.Variable |


def wavelength_bounded_spectra(
bounds: sc.Variable, beam_total: sc.Variable
bounds: sc.Variable, total_flight_path_length: sc.Variable
) -> Callable[[Collection[DaeSpectra]], Awaitable[sc.Variable | sc.DataArray]]:
"""Sum a set of neutron spectra between the specified [tof/wavelength] bounds.
"""Sum a set of neutron spectra between the specified wavelength bounds.
Args:
bounds: A scipp array of size 2 of wavelength bounds, in units of angstrom,
where the second element must be larger than the first.
beam_total: A scipp scalar of Ltotal (total flight path length), the path length
from neutron source to detector or monitor, in units of meters.
total_flight_path_length: A scipp scalar of Ltotal (total flight path length), the path
length from neutron source to detector or monitor, in units of meters.
Time of flight is converted to wavelength using scipp neutron's library function
`wavelength_from_tof`, more info on which can be found here:
Expand All @@ -95,18 +96,22 @@ def wavelength_bounded_spectra(
and variance respectively of the summed counts.
"""
bounds_value = 2

if "tof" not in bounds.dims:
raise ValueError("Should contain tof dims")
if bounds.sizes["tof"] != bounds_value:
raise ValueError("Should contain lower and upper bound")

async def sum_spectra_with_wavelength(
spectra: Collection[DaeSpectra],
) -> sc.Variable | sc.DataArray:
"""Sum spectra and convert time of flight to wavelength."""
"""Sum a set of spectra between the specified wavelength bounds."""
summed_counts = sc.scalar(value=0, unit=sc.units.counts, dtype="float64")
for spec in asyncio.as_completed([s.read_spectrum_dataarray() for s in spectra]):
wavelength_bounded_spectra = await spec
wavelength_coord = conversion.tof.wavelength_from_tof(
tof=wavelength_bounded_spectra.coords["tof"], Ltotal=beam_total
tof=wavelength_bounded_spectra.coords["tof"], Ltotal=total_flight_path_length
)
wavelength_bounded_spectra.coords["tof"] = wavelength_coord
summed_counts += wavelength_bounded_spectra.rebin({"tof": bounds}).sum()
Expand All @@ -115,7 +120,7 @@ async def sum_spectra_with_wavelength(
return sum_spectra_with_wavelength


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__(
Expand Down

0 comments on commit 9294e97

Please sign in to comment.