Coverage for install/scipp/reduction.py: 44%

39 statements  

« 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 

4import uuid 

5from collections.abc import Iterable 

6 

7from .core import concat, reduction 

8from .typing import VariableLike 

9 

10 

11class BinsReducer: 

12 def __init__(self, obj: VariableLike, dim: str): 

13 self._obj = obj 

14 self._dim = dim 

15 

16 def concat(self): 

17 """Element-wise 'concat' across bins of inputs passed to :py:func:`scipp.reduce`.""" # noqa: E501 

18 return self._obj.bins.concat(self._dim) 

19 

20 

21class Reducer: 

22 def __init__(self, x: list[VariableLike]): 

23 self._dim = uuid.uuid4().hex 

24 # concat in init avoids repeated costly step in case of multiple reductions 

25 self._obj = concat(x, dim=self._dim) 

26 

27 @property 

28 def bins(self): 

29 return BinsReducer(self._obj, self._dim) 

30 

31 def all(self): 

32 """Element-wise 'all' across inputs passed to :py:func:`scipp.reduce`.""" 

33 return reduction.all(self._obj, self._dim) 

34 

35 def any(self): 

36 """Element-wise 'any' across inputs passed to :py:func:`scipp.reduce`.""" 

37 return reduction.any(self._obj, self._dim) 

38 

39 def max(self): 

40 """Element-wise 'max' across inputs passed to :py:func:`scipp.reduce`.""" 

41 return reduction.max(self._obj, self._dim) 

42 

43 def min(self): 

44 """Element-wise 'min' across inputs passed to :py:func:`scipp.reduce`.""" 

45 return reduction.min(self._obj, self._dim) 

46 

47 def sum(self): 

48 """Element-wise 'sum' across inputs passed to :py:func:`scipp.reduce`.""" 

49 return reduction.sum(self._obj, self._dim) 

50 

51 def mean(self): 

52 """Element-wise 'mean' across inputs passed to :py:func:`scipp.reduce`.""" 

53 return reduction.mean(self._obj, self._dim) 

54 

55 def nanmax(self): 

56 """Element-wise 'nanmax' across inputs passed to :py:func:`scipp.reduce`.""" 

57 return reduction.nanmax(self._obj, self._dim) 

58 

59 def nanmin(self): 

60 """Element-wise 'nanmin' across inputs passed to :py:func:`scipp.reduce`.""" 

61 return reduction.nanmin(self._obj, self._dim) 

62 

63 def nansum(self): 

64 """Element-wise 'nansum' across inputs passed to :py:func:`scipp.reduce`.""" 

65 return reduction.nansum(self._obj, self._dim) 

66 

67 def nanmean(self): 

68 """Element-wise 'nanmean' across inputs passed to :py:func:`scipp.reduce`.""" 

69 return reduction.nanmean(self._obj, self._dim) 

70 

71 

72def reduce(x: Iterable[VariableLike]) -> Reducer: 

73 """Create helper object for reduction operations over list or tuple of inputs. 

74 

75 Usage examples: 

76 

77 >>> a = sc.linspace(dim='x', start=0, stop=1, num=4) 

78 >>> b = sc.linspace(dim='x', start=0.2, stop=0.8, num=4) 

79 >>> sc.reduce([a, b]).max() 

80 <scipp.Variable> (x: 4) float64 [dimensionless] [0.2, 0.4, 0.666667, 1] 

81 >>> sc.reduce([a, b]).sum() 

82 <scipp.Variable> (x: 4) float64 [dimensionless] [0.2, 0.733333, 1.26667, 1.8] 

83 

84 :param x: List or tuple of variables or data arrays 

85 :return: Reducer helper object with methods such as ``sum()`` or ``max()`` 

86 """ 

87 return Reducer(list(x))