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
« 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
5from .._scipp import core as _cpp
6from ..typing import VariableLikeType
7from ._cpp_wrapper_util import call_func as _call_cpp_func
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]
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]
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]
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]
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]
35def to_unit(
36 x: VariableLikeType, unit: _cpp.Unit | str, *, copy: bool = True
37) -> VariableLikeType:
38 """Convert the variable to a different unit.
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`.
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.
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.
57 Returns
58 -------
59 :
60 Input converted to the given unit.
62 Examples
63 --------
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]