Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/essreduce/src/ess/reduce/unwrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
LookupTableFilename,
LookupTableRelativeErrorThreshold,
MonitorLtotal,
MonitorWavelengthBins,
PulseStrideOffset,
TofDetector,
TofMonitor,
Expand All @@ -61,6 +62,7 @@
"LookupTableWorkflow",
"LtotalRange",
"MonitorLtotal",
"MonitorWavelengthBins",
"NumberOfSimulatedNeutrons",
"PulsePeriod",
"PulseStride",
Expand Down
28 changes: 19 additions & 9 deletions packages/essreduce/src/ess/reduce/unwrap/to_wavelength.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
LookupTable,
LookupTableRelativeErrorThreshold,
MonitorLtotal,
MonitorWavelengthBins,
PulseStrideOffset,
WavelengthDetector,
WavelengthMonitor,
Expand Down Expand Up @@ -591,6 +592,7 @@ def monitor_wavelength_data(
ltotal: MonitorLtotal[RunType, MonitorType],
pulse_stride_offset: PulseStrideOffset,
keep_event_time_offset: KeepEventTimeOffset,
wavelength_bins: MonitorWavelengthBins[MonitorType],
backend: FrameUnwrapBackend = FrameUnwrapBackend.numba,
) -> WavelengthMonitor[RunType, MonitorType]:
"""
Expand All @@ -614,19 +616,27 @@ def monitor_wavelength_data(
keep_event_time_offset:
Whether to keep the event_time_offset coordinate after converting to
wavelength.
wavelength_bins:
Optional one-dimensional edges for histogramming or rebinning the converted
monitor. The dimension name is arbitrary. None preserves the events or bins.
backend:
Backend used to interpolate the wavelength lookup table.
"""
return WavelengthMonitor[RunType, MonitorType](
_compute_wavelength_data(
da=monitor_data,
lookup=lookup,
ltotal=ltotal,
pulse_stride_offset=pulse_stride_offset,
backend=backend,
keep_event_time_offset=keep_event_time_offset,
)
out = _compute_wavelength_data(
da=monitor_data,
lookup=lookup,
ltotal=ltotal,
pulse_stride_offset=pulse_stride_offset,
backend=backend,
keep_event_time_offset=keep_event_time_offset,
)
if wavelength_bins is not None:
edges = wavelength_bins.rename_dims({wavelength_bins.dim: 'wavelength'})
if not out.is_binned:
out = out.rebin(wavelength=edges)
else:
out = out.hist(wavelength=edges)
return WavelengthMonitor[RunType, MonitorType](out)


def providers() -> tuple[Callable]:
Expand Down
9 changes: 9 additions & 0 deletions packages/essreduce/src/ess/reduce/unwrap/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,13 @@ class WavelengthMonitor(sl.Scope[RunType, MonitorType, sc.DataArray], sc.DataArr
"""Monitor data with wavelength coordinate."""


class MonitorWavelengthBins(sl.Scope[MonitorType, sc.Variable], sc.Variable):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use

Suggested change
class MonitorWavelengthBins(sl.Scope[MonitorType, sc.Variable], sc.Variable):
class WavelengthBins(sl.Scope[Component, sc.Variable], sc.Variable):

That would allow any monitor type and detector.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WavelengthBins collides with a type already defined in esssans, but we can use Component instead of MonitorType

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather change the more specialised type to use a longer name as that has a smaller impact than the generic name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically I agree about that. But that would be a breaking change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, I don't think we should have different wavelength binnings in a workflow, unless there is a clear motivation for it.

"""Optional wavelength bin edges for a monitor, shared across run types.

If None, preserve the converted monitor's events or histogram bins. Otherwise,
histogram events or rebin a histogram using these one-dimensional edges.
The dimension name is arbitrary.
"""


KeepEventTimeOffset = NewType("KeepEventTimeOffset", bool)
9 changes: 8 additions & 1 deletion packages/essreduce/src/ess/reduce/unwrap/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

from ..nexus import GenericNeXusWorkflow
from ..nexus.types import AnyRun
from . import FrameUnwrapBackend, WavelengthLutMode, lut, to_wavelength
from . import (
FrameUnwrapBackend,
MonitorWavelengthBins,
WavelengthLutMode,
lut,
to_wavelength,
)


def GenericUnwrapWorkflow(
Expand Down Expand Up @@ -62,6 +68,7 @@ def GenericUnwrapWorkflow(
for key, value in lut.default_parameters(wavelength_from=wavelength_from).items():
wf[key] = value
wf[FrameUnwrapBackend] = FrameUnwrapBackend.numba
wf[MonitorWavelengthBins] = None

return wf

Expand Down
89 changes: 89 additions & 0 deletions packages/essreduce/tests/unwrap/workflow_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
NeXusName,
Position,
RawDetector,
RawMonitor,
SampleRun,
)
from ess.reduce.unwrap import (
Expand All @@ -32,6 +33,7 @@
sl = pytest.importorskip("sciline")

Monitor0 = NewType("Monitor0", int)
Monitor1 = NewType("Monitor1", int)


def test_GenericUnwrapWorkflow_defaults_to_numba_backend():
Expand All @@ -40,6 +42,93 @@ def test_GenericUnwrapWorkflow_defaults_to_numba_backend():
assert wf.compute(FrameUnwrapBackend) == FrameUnwrapBackend.numba


def test_monitor_wavelength_bins_are_independent_and_default_to_none():
wf = GenericUnwrapWorkflow(
run_types=[SampleRun], monitor_types=[Monitor0, Monitor1]
)
assert wf.compute(unwrap.MonitorWavelengthBins[Monitor0]) is None
assert wf.compute(unwrap.MonitorWavelengthBins[Monitor1]) is None

edges = sc.linspace('wavelength', 1.0, 5.0, 3, unit='angstrom')
wf[unwrap.MonitorWavelengthBins[Monitor0]] = edges
assert_identical(wf.compute(unwrap.MonitorWavelengthBins[Monitor0]), edges)
assert wf.compute(unwrap.MonitorWavelengthBins[Monitor1]) is None


@pytest.mark.parametrize('bin_dim', ['wavelength', 'incident_wavelength', 'wavelenght'])
def test_monitor_wavelength_bins_rebin_histograms(bin_dim):
wf = _make_workflow(wavelength_from='analytical')
target = unwrap.WavelengthMonitor[SampleRun, Monitor0]
original = wf.compute(target)
mid = original.sizes['wavelength'] // 2
wavelengths = original.coords['wavelength']
edges = sc.concat([wavelengths[0], wavelengths[mid], wavelengths[-1]], bin_dim)
expected = sc.concat(
[
original['wavelength', :mid].sum('wavelength').data,
original['wavelength', mid:].sum('wavelength').data,
],
'wavelength',
).transpose(original.dims)

wf[unwrap.MonitorWavelengthBins[Monitor0]] = edges
result = wf.compute(target)

sc.testing.assert_allclose(result.data, expected)
assert_identical(
result.coords['wavelength'], edges.rename_dims({bin_dim: 'wavelength'})
)
assert edges.dim == bin_dim


@pytest.mark.parametrize('bin_dim', ['wavelength', 'incident_wavelength', 'wavelenght'])
def test_monitor_wavelength_bins_histogram_events(bin_dim):
wf = _make_workflow(wavelength_from='analytical')
wf[DiskChoppers[SampleRun]] = {}
events = sc.DataArray(
sc.ones(dims=['event'], shape=[4], unit='counts', with_variances=True),
coords={
'event_time_offset': sc.array(
dims=['event'], values=[15.0, 15.0, 30.0, 45.0], unit='ms'
)
},
)
wf[RawMonitor[SampleRun, Monitor0]] = sc.DataArray(
sc.bins(data=events, dim='event', begin=sc.index(0), end=sc.index(4))
)
target = unwrap.WavelengthMonitor[SampleRun, Monitor0]
original = wf.compute(target)
assert original.is_binned
wavelengths = original.bins.constituents['data'].coords['wavelength']
edges = sc.concat(
[
sc.scalar(0.0, unit='angstrom'),
(wavelengths[1] + wavelengths[2]) / 2,
(wavelengths[2] + wavelengths[3]) / 2,
sc.scalar(15.0, unit='angstrom'),
],
bin_dim,
)
wf[unwrap.MonitorWavelengthBins[Monitor0]] = edges

result = wf.compute(target)

assert not result.is_binned
assert_identical(
result.data,
sc.array(
dims=['wavelength'],
values=[2.0, 1.0, 1.0],
variances=[2.0, 1.0, 1.0],
unit='counts',
),
)
assert_identical(
result.coords['wavelength'], edges.rename_dims({bin_dim: 'wavelength'})
)
assert edges.dim == bin_dim


def _make_workflow(
wavelength_from,
*,
Expand Down
8 changes: 7 additions & 1 deletion packages/essspectroscopy/src/ess/bifrost/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def normalize_by_monitor_and_proton_charge(
detector:
Detector events to normalize.
monitor:
Monitor histogram.
Monitor histogram in incident wavelength.
proton_charge:
Accumulated proton charge.
Scalar or per setting (a3 / a4).
Expand All @@ -63,6 +63,12 @@ def normalize_by_monitor_and_proton_charge(
# We should make this function public or move the normalization to ESSreduce.
from ess.reduce.normalization import _mask_detector_for_norm

if monitor.is_binned:
raise ValueError(
'Set MonitorWavelengthBins[NormalizationMonitor] to histogram the event '
'monitor before normalization.'
)

if 'time' in monitor.dims:
# Use a combined monitor distribution for all instrument settings.
monitor = monitor.sum('time')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
MonitorCoordTransformGraph,
MonitorLtotal,
MonitorType,
MonitorWavelengthBins,
NeXusDetectorName,
PulseStrideOffset,
RawDetector,
Expand Down Expand Up @@ -78,6 +79,7 @@ def monitor_wavelength_data(
lookup: ErrorLimitedLookupTable[RunType, MonitorType],
ltotal: MonitorLtotal[RunType, MonitorType],
pulse_stride_offset: PulseStrideOffset,
wavelength_bins: MonitorWavelengthBins[MonitorType],
) -> WavelengthMonitor[RunType, MonitorType]:
"""
Convert the time-of-arrival data to wavelength data using a lookup table.
Expand All @@ -98,8 +100,12 @@ def monitor_wavelength_data(
ltotal=ltotal,
pulse_stride_offset=pulse_stride_offset,
keep_event_time_offset=False,
wavelength_bins=wavelength_bins,
)
result = result.rename(wavelength='incident_wavelength')
if not result.is_binned:
result = result.rename(wavelength='incident_wavelength')
else:
result.bins.coords['incident_wavelength'] = result.bins.coords.pop('wavelength')
return result


Expand Down
1 change: 1 addition & 0 deletions packages/essspectroscopy/src/ess/spectroscopy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

WavelengthDetector = unwrap.WavelengthDetector
WavelengthMonitor = unwrap.WavelengthMonitor
MonitorWavelengthBins = unwrap.MonitorWavelengthBins
LookupTableRelativeErrorThreshold = unwrap.LookupTableRelativeErrorThreshold
MonitorLtotal = unwrap.MonitorLtotal
PulseStride = unwrap.PulseStride
Expand Down
93 changes: 93 additions & 0 deletions packages/essspectroscopy/tests/bifrost/normalization_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2026 Scipp contributors (https://github.com/scipp)

import numpy as np
import pytest
import scipp as sc
import scipp.testing
from ess.bifrost.normalization import normalize_by_monitor_and_proton_charge
from ess.spectroscopy.types import UncertaintyBroadcastMode


@pytest.fixture
def detector() -> sc.DataArray:
events = sc.DataArray(
sc.ones(dims=['event'], shape=[3], unit='counts', with_variances=True),
coords={
'incident_wavelength': sc.array(
dims=['event'], values=[1.5, 2.5, 3.5], unit='angstrom'
)
},
)
return sc.DataArray(
sc.bins(data=events, dim='event', begin=sc.index(0), end=sc.index(3))
)


@pytest.fixture(params=[False, True], ids=['scalar', 'time-dependent'])
def event_monitor(request: pytest.FixtureRequest) -> sc.DataArray:
events = sc.DataArray(
sc.ones(dims=['event'], shape=[4], unit='counts', with_variances=True),
coords={
'incident_wavelength': sc.array(
dims=['event'], values=[1.5, 1.5, 2.5, 3.5], unit='angstrom'
)
},
)
if request.param:
events.coords['time'] = sc.array(dims=['event'], values=[0, 1, 0, 1], unit='s')
return events.group('time')
return sc.DataArray(
sc.bins(data=events, dim='event', begin=sc.index(0), end=sc.index(4))
)


@pytest.mark.parametrize(
('edges', 'expected_weights'),
[
([1.0, 3.0, 4.0], [4 / 3, 4 / 3, 2.0]),
([1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 2.0]),
],
)
def test_normalize_monitor(
detector: sc.DataArray,
event_monitor: sc.DataArray,
edges: list[float],
expected_weights: list[float],
) -> None:
monitor = event_monitor.hist(
incident_wavelength=sc.array(
dims=['incident_wavelength'], values=edges, unit='angstrom'
)
)

result = normalize_by_monitor_and_proton_charge(
detector=detector,
monitor=monitor,
proton_charge=sc.DataArray(sc.scalar(2.0, unit='pC')),
uncertainty_broadcast_mode=UncertaintyBroadcastMode.drop,
)

sc.testing.assert_allclose(
result.bins.constituents['data'].data,
sc.array(
dims=['event'],
values=expected_weights,
variances=np.square(expected_weights),
unit='counts*angstrom/pC',
),
)


def test_event_monitor_requires_bins(
detector: sc.DataArray, event_monitor: sc.DataArray
) -> None:
with pytest.raises(
ValueError, match=r'MonitorWavelengthBins\[NormalizationMonitor\]'
):
normalize_by_monitor_and_proton_charge(
detector=detector,
monitor=event_monitor,
proton_charge=sc.DataArray(sc.scalar(2.0, unit='pC')),
uncertainty_broadcast_mode=UncertaintyBroadcastMode.drop,
)
Loading
Loading