Coverage for install/scipp/core/unary.py: 40%

15 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 Matthew Andrew 

4 

5from .._scipp import core as _cpp 

6from ..typing import VariableLikeType 

7from ._cpp_wrapper_util import call_func as _call_cpp_func 

8 

9 

10def isnan(x: VariableLikeType) -> VariableLikeType: 

11 """Element-wise isnan (true if an element is nan).""" 

12 return _call_cpp_func(_cpp.isnan, x) # type: ignore[return-value] 

13 

14 

15def isinf(x: VariableLikeType) -> VariableLikeType: 

16 """Element-wise isinf (true if an element is inf).""" 

17 return _call_cpp_func(_cpp.isinf, x) # type: ignore[return-value] 

18 

19 

20def isfinite(x: VariableLikeType) -> VariableLikeType: 

21 """Element-wise isfinite (true if an element is finite).""" 

22 return _call_cpp_func(_cpp.isfinite, x) # type: ignore[return-value] 

23 

24 

25def isposinf(x: VariableLikeType) -> VariableLikeType: 

26 """Element-wise isposinf (true if an element is a positive infinity).""" 

27 return _call_cpp_func(_cpp.isposinf, x) # type: ignore[return-value] 

28 

29 

30def isneginf(x: VariableLikeType) -> VariableLikeType: 

31 """Element-wise isneginf (true if an element is a negative infinity).""" 

32 return _call_cpp_func(_cpp.isneginf, x) # type: ignore[return-value] 

33 

34 

35def to_unit( 

36 x: VariableLikeType, unit: _cpp.Unit | str, *, copy: bool = True 

37) -> VariableLikeType: 

38 """Convert the variable to a different unit. 

39 

40 Raises a :class:`scipp.UnitError` if the input unit is not compatible 

41 with the provided unit, e.g., `m` cannot be converted to `s`. 

42 

43 If the input dtype is an integer type or datetime64, the output is rounded 

44 and returned with the same dtype as the input. 

45 

46 Parameters 

47 ---------- 

48 x: VariableLike 

49 Input variable. 

50 unit: 

51 Desired target unit. 

52 copy: 

53 If `False`, return the input object if possible, i.e. 

54 if the unit is unchanged. 

55 If `True`, the function always returns a new object. 

56 

57 Returns 

58 ------- 

59 : 

60 Input converted to the given unit. 

61 

62 Examples 

63 -------- 

64 

65 >>> var = 1.2 * sc.Unit('m') 

66 >>> sc.to_unit(var, unit='mm') 

67 <scipp.Variable> () float64 [mm] 1200 

68 """ 

69 return _call_cpp_func(_cpp.to_unit, x=x, unit=unit, copy=copy) # type: ignore[return-value]