diff --git a/packages/essreduce/src/ess/reduce/time_of_flight/resample.py b/packages/essreduce/src/ess/reduce/time_of_flight/resample.py index 17612ff0..8f4b8b26 100644 --- a/packages/essreduce/src/ess/reduce/time_of_flight/resample.py +++ b/packages/essreduce/src/ess/reduce/time_of_flight/resample.py @@ -84,14 +84,15 @@ def rebin_strictly_increasing(da: sc.DataArray, dim: str) -> sc.DataArray: da = da.rename_dims({da.coords[dim].dim: dim}) slices = find_strictly_increasing_sections(da.coords[dim]) if len(slices) == 1: - return da[dim, slices[0]] + # Slices refer to the indices in the coord, which are bin edges. + # For slicing data we need to stop at the last index minus one. + return da[dim, slices[0].start : slices[0].stop - 1] if not slices: raise ValueError("No strictly increasing sections found.") if da.coords[dim].dtype not in (sc.DType.float64, sc.DType.float32): # rebin does not like integer coords. da = da.assign_coords({dim: da.coords[dim].to(dtype='float64')}) - # Slices refer to the indices in the coord, which are bin edges. For slicing data - # we need to stop at the last index minus one. + # Same -1 as above in the len(slices) == 1 case. sections = [da[dim, section.start : section.stop - 1] for section in slices] edges = make_regular_grid(da.coords[dim], dim=dim, slices=slices) return sc.reduce([sc.rebin(section, {dim: edges}) for section in sections]).sum() diff --git a/packages/essreduce/tests/time_of_flight/resample_tests.py b/packages/essreduce/tests/time_of_flight/resample_test.py similarity index 91% rename from packages/essreduce/tests/time_of_flight/resample_tests.py rename to packages/essreduce/tests/time_of_flight/resample_test.py index 3984d166..6d54c5f9 100644 --- a/packages/essreduce/tests/time_of_flight/resample_tests.py +++ b/packages/essreduce/tests/time_of_flight/resample_test.py @@ -321,6 +321,51 @@ def test_with_single_increasing_section(self): # For a single increasing section, should return just that section assert sc.identical(result, da) + def test_with_single_increasing_section_decreasing_last(self): + tof = sc.array(dims=['tof'], values=[1, 2, 3, 4, 5, 4]) + data = sc.array(dims=['tof'], values=[10, 20, 30, 40, 50]) + da = sc.DataArray(data=data, coords={'tof': tof}) + + result = resample.rebin_strictly_increasing(da, 'tof') + + # Check the rebinned result has a regular grid from 1 to 5 + expected_tof = sc.array(dims=['tof'], values=[1, 2, 3, 4, 5]) + assert_identical(result.coords['tof'], expected_tof) + + # Check the data values are sliced + expected_data = sc.array(dims=['tof'], values=[10, 20, 30, 40]) + assert_identical(result.data, expected_data) + + def test_with_single_increasing_section_nan_at_end(self): + tof = sc.array(dims=['tof'], values=[1, 2, 3, 4, 5, np.nan]) + data = sc.array(dims=['tof'], values=[10, 20, 30, 40, 50]) + da = sc.DataArray(data=data, coords={'tof': tof}) + + result = resample.rebin_strictly_increasing(da, 'tof') + + # Check the rebinned result has a regular grid from 1 to 5 + expected_tof = sc.array(dims=['tof'], values=[1.0, 2, 3, 4, 5]) + assert_identical(result.coords['tof'], expected_tof) + + # Check the data values are sliced + expected_data = sc.array(dims=['tof'], values=[10, 20, 30, 40]) + assert_identical(result.data, expected_data) + + def test_with_single_increasing_section_2_nans_at_end(self): + tof = sc.array(dims=['tof'], values=[1, 2, 3, 4, np.nan, np.nan]) + data = sc.array(dims=['tof'], values=[10, 20, 30, 40, 50]) + da = sc.DataArray(data=data, coords={'tof': tof}) + + result = resample.rebin_strictly_increasing(da, 'tof') + + # Check the rebinned result has a regular grid from 1 to 4 + expected_tof = sc.array(dims=['tof'], values=[1.0, 2, 3, 4]) + assert_identical(result.coords['tof'], expected_tof) + + # Check the data values are sliced + expected_data = sc.array(dims=['tof'], values=[10, 20, 30]) + assert_identical(result.data, expected_data) + def test_with_single_section_minimum_length(self): """Test with a single section of the minimum length (2 points).""" tof = sc.array(dims=['tof'], values=[1, 2])