Coverage for install/scipp/core/domains.py: 69%
16 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-17 01:51 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-17 01:51 +0000
1# SPDX-License-Identifier: BSD-3-Clause
2# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3# @author Simon Heybrock
4from __future__ import annotations
6from .._scipp.core import DataArray, DimensionError
7from .shape import concat
8from .variable import empty
11def merge_equal_adjacent(da: DataArray) -> DataArray:
12 """Merges adjacent bins that have identical values.
14 Parameters
15 ----------
16 da:
17 Input data array, must be a 1-D histogram.
19 Returns
20 -------
21 :
22 Data array with bins spanning domains of input.
23 """
24 try:
25 dim = da.dim
26 except DimensionError as e:
27 raise DimensionError(
28 "Cannot merge equal adjacent bins non-1-D data array"
29 ) from e
30 tmp = DataArray(da.data, coords={dim: da.coords[dim][dim, 1:]})
31 condition = empty(dims=da.dims, shape=da.shape, dtype='bool')
32 condition[dim, -1] = True
33 condition[dim, :-1] = da.data[dim, 1:] != da.data[dim, :-1]
34 out = tmp[condition]
35 out.coords[dim] = concat([da.coords[dim][dim, 0:1], out.coords[dim]], dim)
36 return out