Coverage for install/scipp/core/cumulative.py: 42%

12 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 

4 

5from typing import Literal 

6 

7from .._scipp import core as _cpp 

8from ..typing import VariableLikeType 

9from ._cpp_wrapper_util import call_func as _call_cpp_func 

10from .concepts import transform_data 

11from .cpp_classes import Variable 

12 

13 

14def cumsum( 

15 a: VariableLikeType, 

16 dim: str | None = None, 

17 mode: Literal['exclusive', 'inclusive'] = 'inclusive', 

18) -> VariableLikeType: 

19 """Return the cumulative sum along the specified dimension. 

20 

21 See :py:func:`scipp.sum` on how rounding errors for float32 inputs are handled. 

22 

23 Parameters 

24 ---------- 

25 a: scipp.typing.VariableLike 

26 Input data. 

27 dim: 

28 Optional dimension along which to calculate the sum. If not 

29 given, the cumulative sum along all dimensions is calculated. 

30 mode: 

31 Include or exclude the ith element (along dim) in the sum. 

32 Options are 'inclusive' and 'exclusive'. Defaults to 'inclusive'. 

33 

34 Returns 

35 ------- 

36 : Same type as input 

37 The cumulative sum of the input values. 

38 """ 

39 

40 def _cumsum(data: Variable) -> Variable: 

41 if dim is None: 

42 return _call_cpp_func(_cpp.cumsum, data, mode=mode) # type: ignore[return-value] 

43 else: 

44 return _call_cpp_func(_cpp.cumsum, data, dim, mode=mode) # type: ignore[return-value] 

45 

46 return transform_data(a, _cumsum)