|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright (c) 2025 Scipp contributors (https://github.com/scipp) |
| 3 | + |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import scipp as sc |
| 7 | + |
| 8 | + |
| 9 | +def find_strictly_increasing_sections(var: sc.Variable) -> list[slice]: |
| 10 | + """ |
| 11 | + Find strictly increasing sections in a coordinate dimension (minimum length 2). |
| 12 | +
|
| 13 | + Parameters |
| 14 | + ---------- |
| 15 | + var: |
| 16 | + The variable to analyze, which should be one-dimensional. |
| 17 | +
|
| 18 | + Returns |
| 19 | + ------- |
| 20 | + sections: |
| 21 | + Slice objects that can be used extract strictly increasing sections. |
| 22 | + """ |
| 23 | + values = var.values |
| 24 | + finite = np.isfinite(values) |
| 25 | + increasing = (np.sign(np.diff(values)) > 0) & finite[:-1] & finite[1:] |
| 26 | + # 1 marks the start of an increasing section, -1 marks the end |
| 27 | + transitions = np.diff(np.concatenate(([False], increasing, [False])).astype(int)) |
| 28 | + section_starts = np.where(transitions == 1)[0] |
| 29 | + section_ends = np.where(transitions == -1)[0] + np.array(1) |
| 30 | + return [ |
| 31 | + slice(start, end) |
| 32 | + for start, end in zip(section_starts, section_ends, strict=True) |
| 33 | + if end - start >= 2 # Ensure section has at least 2 points |
| 34 | + ] |
| 35 | + |
| 36 | + |
| 37 | +def get_min_max( |
| 38 | + var: sc.Variable, *, dim: str, slices: list[slice] |
| 39 | +) -> tuple[sc.Variable, sc.Variable]: |
| 40 | + if not slices: |
| 41 | + raise ValueError("No strictly increasing sections found.") |
| 42 | + combined = sc.concat([var[dim, slice] for slice in slices], dim) |
| 43 | + return combined.min(), combined.max() |
| 44 | + |
| 45 | + |
| 46 | +def make_regular_grid( |
| 47 | + var: sc.Variable, *, dim: str, slices: list[slice] |
| 48 | +) -> sc.Variable: |
| 49 | + """ |
| 50 | + Create a regular grid variable based on the min and max of the slices. |
| 51 | +
|
| 52 | + The grid is constructed such that it includes the minimum and maximum values |
| 53 | + of the strictly increasing sections, with a step size equal to the difference |
| 54 | + between the first two values of the section with the minimum start value (which is |
| 55 | + not necessarily the first section). |
| 56 | + """ |
| 57 | + min_val, max_val = get_min_max(var, dim=dim, slices=slices) |
| 58 | + first: sc.Variable | None = None |
| 59 | + for s in slices: |
| 60 | + first = var[dim, s] |
| 61 | + if sc.identical(first[0], min_val): |
| 62 | + break |
| 63 | + if first is None: |
| 64 | + # This should not happen if slices are correctly identified and passed from |
| 65 | + # find_strictly_increasing_sections. |
| 66 | + raise ValueError("Section is not strictly increasing.") |
| 67 | + step = first[1] - first[0] |
| 68 | + return sc.arange( |
| 69 | + dim=dim, |
| 70 | + start=min_val.value, |
| 71 | + stop=max_val.value + step.value, # Ensure the last bin edge is included |
| 72 | + step=step.value, |
| 73 | + unit=step.unit, |
| 74 | + dtype=step.dtype, |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def rebin_strictly_increasing(da: sc.DataArray, dim: str) -> sc.DataArray: |
| 79 | + """ |
| 80 | + Find strictly monotonic sections in a coordinate dimension and rebin the data array |
| 81 | + into a regular grid based on these sections. |
| 82 | + """ |
| 83 | + # Ensure the dimension is named like the coordinate. |
| 84 | + da = da.rename_dims({da.coords[dim].dim: dim}) |
| 85 | + slices = find_strictly_increasing_sections(da.coords[dim]) |
| 86 | + if len(slices) == 1: |
| 87 | + return da[dim, slices[0]] |
| 88 | + if not slices: |
| 89 | + raise ValueError("No strictly increasing sections found.") |
| 90 | + if da.coords[dim].dtype not in (sc.DType.float64, sc.DType.float32): |
| 91 | + # rebin does not like integer coords. |
| 92 | + da = da.assign_coords({dim: da.coords[dim].to(dtype='float64')}) |
| 93 | + # Slices refer to the indices in the coord, which are bin edges. For slicing data |
| 94 | + # we need to stop at the last index minus one. |
| 95 | + sections = [da[dim, section.start : section.stop - 1] for section in slices] |
| 96 | + edges = make_regular_grid(da.coords[dim], dim=dim, slices=slices) |
| 97 | + return sc.reduce([sc.rebin(section, {dim: edges}) for section in sections]).sum() |
0 commit comments