Skip to content
Merged
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
7 changes: 4 additions & 3 deletions packages/essreduce/src/ess/reduce/time_of_flight/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
Loading