Coverage for install/scipp/typing.py: 0%

27 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-04-28 01:28 +0000

1# SPDX-License-Identifier: BSD-3-Clause 

2# Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 

3# @file 

4# @author Neil Vaytet 

5 

6import typing as _std_typing 

7 

8import numpy as np 

9import numpy.typing 

10 

11from ._scipp import core as sc 

12from .core.cpp_classes import DataArray, Dataset, DType, Variable 

13from .core.data_group import DataGroup 

14 

15 

16def is_scalar(obj: _std_typing.Any) -> bool: 

17 """ 

18 Return True if the input is a scalar. 

19 """ 

20 return obj.ndim == 0 

21 

22 

23def has_vector_type(obj: _std_typing.Any) -> bool: 

24 """ 

25 Return True if the object dtype is vector3. 

26 """ 

27 return obj.dtype == sc.DType.vector3 

28 

29 

30def has_string_type(obj: _std_typing.Any) -> bool: 

31 """ 

32 Return True if the object dtype is string. 

33 """ 

34 return obj.dtype == sc.DType.string 

35 

36 

37def has_datetime_type(obj: _std_typing.Any) -> bool: 

38 """ 

39 Return True if the object dtype is datetime64. 

40 """ 

41 return obj.dtype == sc.DType.datetime64 

42 

43 

44def has_numeric_type(obj: _std_typing.Any) -> bool: 

45 """ 

46 Return False if the dtype is either vector or string. 

47 """ 

48 return (not has_vector_type(obj)) and (not has_string_type(obj)) 

49 

50 

51Dims = _std_typing.Union[None, str, _std_typing.Sequence[str]] 

52""" 

53Describes dimensions to operate on. 

54 

55Can be a string (for a single dimension) or a sequence of strings (multiple dimensions). 

56A value of ``None`` indicates "all dimensions." 

57""" 

58 

59VariableLike = _std_typing.Union[Variable, DataArray, Dataset, DataGroup] 

60"""Any object that behaves like a :class:`scipp.Variable`. 

61 

62More concretely, an array with labeled dimensions which supports slicing and 

63arithmetic: 

64 

65- :class:`scipp.DataArray` 

66- :class:`scipp.DataGroup` 

67- :class:`scipp.Dataset` 

68- :class:`scipp.Variable` 

69""" 

70 

71MetaDataMap = _std_typing.MutableMapping[str, Variable] 

72"""dict-like object mapping dimension labels to Variables.""" 

73 

74VariableLikeType = _std_typing.TypeVar( 

75 'VariableLikeType', Variable, DataArray, Dataset, DataGroup 

76) 

77"""TypeVar for use in annotations. 

78 

79Should be hidden in rendered documentation in favor of VariableLike. 

80""" 

81 

82DTypeLike = _std_typing.Union[numpy.typing.DTypeLike, DType] 

83"""Anything that can be interpreted as a dtype. 

84 

85This includes 

86 

87- :class:`scipp.DType` 

88- everything that is supported by 

89 `numpy.DTypeLike <https://numpy.org/devdocs/reference/typing.html#numpy.typing.DTypeLike>`_ 

90 e.g. 

91 

92 - :class:`numpy.dtype` 

93 - :class:`type` objects like :class:`int` and :class:`float` 

94 - names of dtypes as strings like ``'int32'`` and ``'float64'`` 

95""" 

96 

97if _std_typing.TYPE_CHECKING: 

98 from enum import Enum 

99 

100 class ellipsis(Enum): 

101 Ellipsis = "..." 

102 

103else: 

104 ellipsis = type(Ellipsis) 

105 

106ScippIndex = _std_typing.Union[ 

107 ellipsis, 

108 int, 

109 tuple, 

110 slice, 

111 list, 

112 np.ndarray, 

113 _std_typing.Tuple[str, _std_typing.Union[int, slice, list, np.ndarray, Variable]], 

114 Variable, 

115]