Skip to content

Commit 99fa7f9

Browse files
committed
Use simple sum, not integral
1 parent 53b861f commit 99fa7f9

File tree

2 files changed

+12
-51
lines changed

2 files changed

+12
-51
lines changed

src/ess/reduce/normalization.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@ def normalize_by_monitor_integrated(
9696
9797
.. math::
9898
99-
d_i^\\text{Norm} &= d_i / M \\\\
100-
M &= \\sum_j\\, m_j (x_{j+1} - x_j)\\,,
99+
d_i^\\text{Norm} = \\frac{d_i}{\\sum_j\\, m_j}
101100
102-
where :math:`m_j` is the monitor counts in bin :math:`j` and
103-
:math:`x_j` is the lower bin edge of that bin.
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.
104106
105107
Parameters
106108
----------
@@ -126,8 +128,7 @@ def normalize_by_monitor_integrated(
126128
"""
127129
_check_monitor_range_contains_detector(monitor=monitor, detector=detector)
128130
detector = _mask_detector_for_norm(detector=detector, monitor=monitor)
129-
coord = monitor.coords[monitor.dim]
130-
norm = (monitor * (coord[1:] - coord[:-1])).data.sum()
131+
norm = monitor.data.sum()
131132
norm = broadcast_uncertainties(
132133
norm, prototype=detector, mode=uncertainty_broadcast_mode
133134
)

tests/normalization_test.py

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,7 @@ def test_expected_results_bin(self) -> None:
569569
monitor=monitor,
570570
uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail,
571571
)
572-
expected = detector / sc.scalar(
573-
4 * 0.5 + 5 * 1.5 + 6 * 1 + 10 * 1, unit='counts * Å'
574-
)
572+
expected = detector / monitor.sum()
575573
sc.testing.assert_identical(normalized, expected)
576574

577575
def test_expected_results_hist(self) -> None:
@@ -588,45 +586,7 @@ def test_expected_results_hist(self) -> None:
588586
monitor=monitor,
589587
uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail,
590588
)
591-
expected = detector / sc.scalar(
592-
4 * 0.5 + 5 * 1.5 + 6 * 1 + 10 * 1, unit='counts * Å'
593-
)
594-
sc.testing.assert_identical(normalized, expected)
595-
596-
@pytest.mark.parametrize('event_coord', [True, False])
597-
def test_uses_monitor_values_at_boundary(
598-
self,
599-
event_coord: bool,
600-
) -> None:
601-
detector = sc.DataArray(
602-
sc.arange('wavelength', 4, unit='counts'),
603-
coords={'wavelength': sc.arange('wavelength', 4.0, unit='Å')},
604-
)
605-
if event_coord:
606-
# Make sure event at 3 is included
607-
detector = detector.bin(
608-
wavelength=sc.array(dims=['wavelength'], values=[0.0, 2, 3.1], unit='Å')
609-
)
610-
del detector.coords['wavelength']
611-
else:
612-
detector = detector.bin(
613-
wavelength=sc.array(dims=['wavelength'], values=[0.0, 2, 3], unit='Å')
614-
)
615-
del detector.bins.coords['wavelength']
616-
monitor = sc.DataArray(
617-
sc.array(dims=['wavelength'], values=[4.0, 10.0], unit='counts'),
618-
coords={
619-
'wavelength': sc.array(
620-
dims=['wavelength'], values=[0.0, 2, 4], unit='Å'
621-
)
622-
},
623-
)
624-
normalized = normalize_by_monitor_integrated(
625-
detector,
626-
monitor=monitor,
627-
uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail,
628-
)
629-
expected = detector / sc.scalar(4.0 * 2 + 10.0 * 2, unit='counts')
589+
expected = detector / monitor.sum()
630590
sc.testing.assert_identical(normalized, expected)
631591

632592
def test_raises_if_monitor_range_too_narrow(
@@ -651,18 +611,18 @@ def test_raises_if_monitor_range_too_narrow(
651611
uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail,
652612
)
653613

654-
def test_independent_of_monitor_binning_bin(self) -> None:
614+
def test_independent_of_monitor_binning(self) -> None:
655615
detector = sc.DataArray(
656616
sc.array(dims=['w'], values=[3, 10, 20, 30], unit='counts'),
657617
coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')},
658618
).bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å'))
659619

660620
monitor1 = sc.DataArray(
661621
sc.array(dims=['w'], values=[5.0, 6.0, 7.0], unit='counts'),
662-
coords={'w': sc.array(dims=['w'], values=[1.0, 2, 4, 8], unit='Å')},
622+
coords={'w': sc.array(dims=['w'], values=[1.0, 2, 4, 7], unit='Å')},
663623
)
664624
monitor2 = monitor1.rebin(
665-
w=sc.array(dims=['w'], values=[1.0, 2, 3, 4, 7], unit='Å')
625+
w=sc.array(dims=['w'], values=[1.0, 2, 3, 5, 7], unit='Å')
666626
)
667627

668628
normalized1 = normalize_by_monitor_integrated(

0 commit comments

Comments
 (0)