essreduce 25.11.2__py3-none-any.whl → 25.11.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ess/reduce/__init__.py +2 -2
- ess/reduce/normalization.py +215 -0
- {essreduce-25.11.2.dist-info → essreduce-25.11.3.dist-info}/METADATA +1 -1
- {essreduce-25.11.2.dist-info → essreduce-25.11.3.dist-info}/RECORD +8 -7
- {essreduce-25.11.2.dist-info → essreduce-25.11.3.dist-info}/WHEEL +0 -0
- {essreduce-25.11.2.dist-info → essreduce-25.11.3.dist-info}/entry_points.txt +0 -0
- {essreduce-25.11.2.dist-info → essreduce-25.11.3.dist-info}/licenses/LICENSE +0 -0
- {essreduce-25.11.2.dist-info → essreduce-25.11.3.dist-info}/top_level.txt +0 -0
ess/reduce/__init__.py
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import importlib.metadata
|
|
6
6
|
|
|
7
|
-
from . import nexus, time_of_flight, uncertainty
|
|
7
|
+
from . import nexus, normalization, time_of_flight, uncertainty
|
|
8
8
|
|
|
9
9
|
try:
|
|
10
10
|
__version__ = importlib.metadata.version("essreduce")
|
|
@@ -13,4 +13,4 @@ except importlib.metadata.PackageNotFoundError:
|
|
|
13
13
|
|
|
14
14
|
del importlib
|
|
15
15
|
|
|
16
|
-
__all__ = ["nexus", "time_of_flight", "uncertainty"]
|
|
16
|
+
__all__ = ["nexus", "normalization", "time_of_flight", "uncertainty"]
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
# Copyright (c) 2025 Scipp contributors (https://github.com/scipp)
|
|
3
|
+
"""Normalization routines for neutron data reduction."""
|
|
4
|
+
|
|
5
|
+
import functools
|
|
6
|
+
|
|
7
|
+
import scipp as sc
|
|
8
|
+
|
|
9
|
+
from .uncertainty import UncertaintyBroadcastMode, broadcast_uncertainties
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def normalize_by_monitor_histogram(
|
|
13
|
+
detector: sc.DataArray,
|
|
14
|
+
*,
|
|
15
|
+
monitor: sc.DataArray,
|
|
16
|
+
uncertainty_broadcast_mode: UncertaintyBroadcastMode,
|
|
17
|
+
) -> sc.DataArray:
|
|
18
|
+
"""Normalize detector data by a normalized histogrammed monitor.
|
|
19
|
+
|
|
20
|
+
This normalization accounts for both the (wavelength) profile of the incident beam
|
|
21
|
+
and the integrated neutron flux, meaning measurement duration and source strength.
|
|
22
|
+
|
|
23
|
+
- For *event* detectors, the monitor values are mapped to the detector
|
|
24
|
+
using :func:`scipp.lookup`. That is, for detector event :math:`d_i`,
|
|
25
|
+
:math:`m_i` is the monitor bin value at the same coordinate.
|
|
26
|
+
- For *histogram* detectors, the monitor is rebinned using to the detector
|
|
27
|
+
binning using :func:`scipp.rebin`. Thus, detector value :math:`d_i` and
|
|
28
|
+
monitor value :math:`m_i` correspond to the same bin.
|
|
29
|
+
|
|
30
|
+
In both cases, let :math:`x_i` be the lower bound of monitor bin :math:`i`
|
|
31
|
+
and let :math:`\\Delta x_i = x_{i+1} - x_i` be the width of that bin.
|
|
32
|
+
|
|
33
|
+
The detector is normalized according to
|
|
34
|
+
|
|
35
|
+
.. math::
|
|
36
|
+
|
|
37
|
+
d_i^\\text{Norm} = \\frac{d_i}{m_i} \\Delta x_i
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
detector:
|
|
42
|
+
Input detector data.
|
|
43
|
+
Must have a coordinate named ``monitor.dim``, that is, the single
|
|
44
|
+
dimension name of the **monitor**.
|
|
45
|
+
monitor:
|
|
46
|
+
A histogrammed monitor.
|
|
47
|
+
Must be one-dimensional and have a dimension coordinate, typically "wavelength".
|
|
48
|
+
uncertainty_broadcast_mode:
|
|
49
|
+
Choose how uncertainties of the monitor are broadcast to the sample data.
|
|
50
|
+
|
|
51
|
+
Returns
|
|
52
|
+
-------
|
|
53
|
+
:
|
|
54
|
+
``detector`` normalized by ``monitor``.
|
|
55
|
+
If the monitor has masks or contains non-finite values, the output has a mask
|
|
56
|
+
called '_monitor_mask' constructed from the monitor masks and non-finite values.
|
|
57
|
+
|
|
58
|
+
See also
|
|
59
|
+
--------
|
|
60
|
+
normalize_by_monitor_integrated:
|
|
61
|
+
Normalize by an integrated monitor.
|
|
62
|
+
"""
|
|
63
|
+
_check_monitor_range_contains_detector(monitor=monitor, detector=detector)
|
|
64
|
+
|
|
65
|
+
dim = monitor.dim
|
|
66
|
+
|
|
67
|
+
if detector.bins is None:
|
|
68
|
+
monitor = monitor.rebin({dim: detector.coords[dim]})
|
|
69
|
+
detector = _mask_detector_for_norm(detector=detector, monitor=monitor)
|
|
70
|
+
coord = monitor.coords[dim]
|
|
71
|
+
delta_w = sc.DataArray(coord[1:] - coord[:-1], masks=monitor.masks)
|
|
72
|
+
norm = broadcast_uncertainties(
|
|
73
|
+
monitor / delta_w, prototype=detector, mode=uncertainty_broadcast_mode
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
if detector.bins is None:
|
|
77
|
+
return detector / norm.rebin({dim: detector.coords[dim]})
|
|
78
|
+
return detector.bins / sc.lookup(norm, dim=dim)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def normalize_by_monitor_integrated(
|
|
82
|
+
detector: sc.DataArray,
|
|
83
|
+
*,
|
|
84
|
+
monitor: sc.DataArray,
|
|
85
|
+
uncertainty_broadcast_mode: UncertaintyBroadcastMode,
|
|
86
|
+
) -> sc.DataArray:
|
|
87
|
+
"""Normalize detector data by an integrated monitor.
|
|
88
|
+
|
|
89
|
+
This normalization accounts only for the integrated neutron flux,
|
|
90
|
+
meaning measurement duration and source strength.
|
|
91
|
+
It does *not* account for the (wavelength) profile of the incident beam.
|
|
92
|
+
For that, see :func:`normalize_by_monitor_histogram`.
|
|
93
|
+
|
|
94
|
+
Let :math:`d_i` be a detector event or the counts in a detector bin.
|
|
95
|
+
The normalized detector is
|
|
96
|
+
|
|
97
|
+
.. math::
|
|
98
|
+
|
|
99
|
+
d_i^\\text{Norm} = \\frac{d_i}{\\sum_j\\, m_j}
|
|
100
|
+
|
|
101
|
+
where :math:`m_j` is the monitor counts in bin :math:`j`.
|
|
102
|
+
Note that this is not a true integral but only a sum over monitor events.
|
|
103
|
+
|
|
104
|
+
The result depends on the range of the monitor but not its
|
|
105
|
+
binning within that range.
|
|
106
|
+
|
|
107
|
+
Parameters
|
|
108
|
+
----------
|
|
109
|
+
detector:
|
|
110
|
+
Input detector data.
|
|
111
|
+
monitor:
|
|
112
|
+
A histogrammed monitor.
|
|
113
|
+
Must be one-dimensional and have a dimension coordinate, typically "wavelength".
|
|
114
|
+
uncertainty_broadcast_mode:
|
|
115
|
+
Choose how uncertainties of the monitor are broadcast to the sample data.
|
|
116
|
+
|
|
117
|
+
Returns
|
|
118
|
+
-------
|
|
119
|
+
:
|
|
120
|
+
`detector` normalized by a monitor.
|
|
121
|
+
If the monitor has masks or contains non-finite values, the output has a mask
|
|
122
|
+
called '_monitor_mask' constructed from the monitor masks and non-finite values.
|
|
123
|
+
|
|
124
|
+
See also
|
|
125
|
+
--------
|
|
126
|
+
normalize_by_monitor_histogram:
|
|
127
|
+
Normalize by a monitor histogram.
|
|
128
|
+
"""
|
|
129
|
+
_check_monitor_range_contains_detector(monitor=monitor, detector=detector)
|
|
130
|
+
detector = _mask_detector_for_norm(detector=detector, monitor=monitor)
|
|
131
|
+
norm = monitor.nansum().data
|
|
132
|
+
norm = broadcast_uncertainties(
|
|
133
|
+
norm, prototype=detector, mode=uncertainty_broadcast_mode
|
|
134
|
+
)
|
|
135
|
+
return detector / norm
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _check_monitor_range_contains_detector(
|
|
139
|
+
*, monitor: sc.DataArray, detector: sc.DataArray
|
|
140
|
+
) -> None:
|
|
141
|
+
dim = monitor.dim
|
|
142
|
+
if not monitor.coords.is_edges(dim):
|
|
143
|
+
raise sc.CoordError(
|
|
144
|
+
f"Monitor coordinate '{dim}' must be bin-edges to integrate the monitor."
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
# Prefer a bin coord over an event coord because this makes the behavior for binned
|
|
148
|
+
# and histogrammed data consistent. If we used an event coord, we might allow a
|
|
149
|
+
# monitor range that is less than the detector bins which is fine for the events,
|
|
150
|
+
# but would be wrong if the detector was subsequently histogrammed.
|
|
151
|
+
if (det_coord := detector.coords.get(dim)) is not None:
|
|
152
|
+
lo = det_coord[dim, :-1].nanmin()
|
|
153
|
+
hi = det_coord[dim, 1:].nanmax()
|
|
154
|
+
elif (det_coord := detector.bins.coords.get(dim)) is not None:
|
|
155
|
+
lo = det_coord.nanmin()
|
|
156
|
+
hi = det_coord.nanmax()
|
|
157
|
+
else:
|
|
158
|
+
raise sc.CoordError(
|
|
159
|
+
f"Missing '{dim}' coordinate in detector for monitor normalization."
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
if monitor.coords[dim].min() > lo or monitor.coords[dim].max() < hi:
|
|
163
|
+
raise ValueError(
|
|
164
|
+
f"Cannot normalize by monitor: The {dim} range of the monitor "
|
|
165
|
+
f"({monitor.coords[dim].min():c} to {monitor.coords[dim].max():c}) "
|
|
166
|
+
f"is smaller than the range of the detector ({lo:c} to {hi:c})."
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _mask_detector_for_norm(
|
|
171
|
+
*, detector: sc.DataArray, monitor: sc.DataArray
|
|
172
|
+
) -> sc.DataArray:
|
|
173
|
+
"""Mask the detector where the monitor is masked.
|
|
174
|
+
|
|
175
|
+
For performance, this applies the monitor mask to the detector bins.
|
|
176
|
+
This can lead to masking more events than strictly necessary if we
|
|
177
|
+
used an event mask.
|
|
178
|
+
"""
|
|
179
|
+
dim = monitor.dim
|
|
180
|
+
|
|
181
|
+
if (monitor_mask := _monitor_mask(monitor)) is None:
|
|
182
|
+
return detector
|
|
183
|
+
|
|
184
|
+
if (detector_coord := detector.coords.get(monitor.dim)) is not None:
|
|
185
|
+
# Apply the mask to the bins or a dense detector.
|
|
186
|
+
# Use rebin to reshape the mask to the detector.
|
|
187
|
+
mask = sc.DataArray(monitor_mask, coords={dim: monitor.coords[dim]}).rebin(
|
|
188
|
+
{dim: detector_coord}
|
|
189
|
+
).data != sc.scalar(0, unit=None)
|
|
190
|
+
return detector.assign_masks({"_monitor_mask": mask})
|
|
191
|
+
|
|
192
|
+
# else: Apply the mask to the events.
|
|
193
|
+
if dim not in detector.bins.coords:
|
|
194
|
+
raise sc.CoordError(
|
|
195
|
+
f"Detector must have coordinate '{dim}' to mask by monitor."
|
|
196
|
+
)
|
|
197
|
+
event_mask = sc.lookup(
|
|
198
|
+
sc.DataArray(monitor_mask, coords={dim: monitor.coords[dim]})
|
|
199
|
+
)[detector.bins.coords[dim]]
|
|
200
|
+
return detector.bins.assign_masks({"_monitor_mask": event_mask})
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _monitor_mask(monitor: sc.DataArray) -> sc.Variable | None:
|
|
204
|
+
"""Mask nonfinite and zero monitor values and combine all masks."""
|
|
205
|
+
masks = list(monitor.masks.values())
|
|
206
|
+
|
|
207
|
+
finite = sc.isfinite(monitor.data)
|
|
208
|
+
nonzero = monitor.data != sc.scalar(0, unit=monitor.unit)
|
|
209
|
+
valid = finite & nonzero
|
|
210
|
+
if not valid.all():
|
|
211
|
+
masks.append(~valid)
|
|
212
|
+
|
|
213
|
+
if not masks:
|
|
214
|
+
return None
|
|
215
|
+
return functools.reduce(sc.logical_or, masks)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
ess/reduce/__init__.py,sha256=
|
|
1
|
+
ess/reduce/__init__.py,sha256=9iqQ57K3stwyujDzOk30hj7WqZt1Ycnb9AVDDDmk3K0,451
|
|
2
2
|
ess/reduce/logging.py,sha256=6n8Czq4LZ3OK9ENlKsWSI1M3KvKv6_HSoUiV4__IUlU,357
|
|
3
|
+
ess/reduce/normalization.py,sha256=B4O5W3CV_ti-zeU7tyQEAXk5pCUebZ0BG30YN2I3TyY,7844
|
|
3
4
|
ess/reduce/parameter.py,sha256=4sCfoKOI2HuO_Q7JLH_jAXnEOFANSn5P3NdaOBzhJxc,4635
|
|
4
5
|
ess/reduce/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
6
|
ess/reduce/streaming.py,sha256=zbqxQz5dASDq4ZVyx-TdbapBXMyBttImCYz_6WOj4pg,17978
|
|
@@ -40,9 +41,9 @@ ess/reduce/widgets/_spinner.py,sha256=2VY4Fhfa7HMXox2O7UbofcdKsYG-AJGrsgGJB85nDX
|
|
|
40
41
|
ess/reduce/widgets/_string_widget.py,sha256=iPAdfANyXHf-nkfhgkyH6gQDklia0LebLTmwi3m-iYQ,1482
|
|
41
42
|
ess/reduce/widgets/_switchable_widget.py,sha256=fjKz99SKLhIF1BLgGVBSKKn3Lu_jYBwDYGeAjbJY3Q8,2390
|
|
42
43
|
ess/reduce/widgets/_vector_widget.py,sha256=aTaBqCFHZQhrIoX6-sSqFWCPePEW8HQt5kUio8jP1t8,1203
|
|
43
|
-
essreduce-25.11.
|
|
44
|
-
essreduce-25.11.
|
|
45
|
-
essreduce-25.11.
|
|
46
|
-
essreduce-25.11.
|
|
47
|
-
essreduce-25.11.
|
|
48
|
-
essreduce-25.11.
|
|
44
|
+
essreduce-25.11.3.dist-info/licenses/LICENSE,sha256=nVEiume4Qj6jMYfSRjHTM2jtJ4FGu0g-5Sdh7osfEYw,1553
|
|
45
|
+
essreduce-25.11.3.dist-info/METADATA,sha256=RG8gLrZiQjprpTwz70dd-300cLF2jZeOEIigmwWFBOY,1937
|
|
46
|
+
essreduce-25.11.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
47
|
+
essreduce-25.11.3.dist-info/entry_points.txt,sha256=PMZOIYzCifHMTe4pK3HbhxUwxjFaZizYlLD0td4Isb0,66
|
|
48
|
+
essreduce-25.11.3.dist-info/top_level.txt,sha256=0JxTCgMKPLKtp14wb1-RKisQPQWX7i96innZNvHBr-s,4
|
|
49
|
+
essreduce-25.11.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|