Skip to content

Commit 7b2a9b0

Browse files
committed
Handle binning in two_theta wo event coord
1 parent 9fbcfc0 commit 7b2a9b0

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/ess/powder/correction.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""Correction algorithms for powder diffraction."""
44

55
import enum
6+
from collections.abc import Mapping, Sequence
67
from typing import TypeVar
78

89
import sciline
@@ -31,6 +32,8 @@
3132
WavelengthMonitor,
3233
)
3334

35+
_T = TypeVar("_T")
36+
3437

3538
def normalize_by_monitor_histogram(
3639
detector: CorrectedDspacing[RunType],
@@ -146,15 +149,32 @@ def _mask_out_of_monitor_range_data(
146149
return detector, False
147150

148151

152+
def _filter_keys(mapping: Mapping[str, _T], keys: Sequence[str]) -> dict[str, _T]:
153+
return {key: value for key, value in mapping.items() if key in keys}
154+
155+
156+
def _histogram_vanadium(vanadium: sc.DataArray, sample: sc.DataArray) -> sc.DataArray:
157+
target_coords = _filter_keys(sample.coords, ("dspacing", "two_theta"))
158+
if "two_theta" in target_coords and "two_theta" not in vanadium.bins.coords:
159+
# Need to provide a two_theta event coord so we can histogram.
160+
if sc.identical(vanadium.coords["two_theta"], target_coords["two_theta"]):
161+
# Skip the expensive event coord if possible:
162+
return vanadium.hist(dspacing=sample.coords["dspacing"])
163+
return vanadium.bins.assign_coords(
164+
two_theta=sc.bins_like(vanadium, vanadium.coords["two_theta"])
165+
).hist(target_coords)
166+
return vanadium.hist(target_coords)
167+
168+
149169
def _normalize_by_vanadium(
150170
data: sc.DataArray,
151171
vanadium: sc.DataArray,
152172
uncertainty_broadcast_mode: UncertaintyBroadcastMode,
153173
) -> sc.DataArray:
154174
norm = (
155-
vanadium.hist(data.coords)
175+
_histogram_vanadium(vanadium, sample=data)
156176
if vanadium.is_binned
157-
else vanadium.rebin(data.coords)
177+
else vanadium.rebin(_filter_keys(data.coords, ("dspacing", "two_theta")))
158178
)
159179
norm = broadcast_uncertainties(
160180
norm, prototype=data, mode=uncertainty_broadcast_mode

0 commit comments

Comments
 (0)