Coverage for install/scipp/core/like.py: 50%

16 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 

4from typing import Any 

5 

6from ..typing import VariableLikeType 

7from .concepts import rewrap_output_data 

8from .variable import empty, full, ones, zeros 

9 

10 

11def zeros_like(obj: VariableLikeType, /) -> VariableLikeType: 

12 """Return a new object with the same dims, shape, unit, 

13 and dtype as the input and all elements initialized to 0. 

14 

15 If the input has variances, all variances in the output are set to 0. 

16 If the input is a :class:`DataArray`, coordinates and attributes are shallow-copied 

17 and masks are deep-copied. 

18 

19 Parameters 

20 ---------- 

21 obj: scipp.Variable or scipp.DataArray 

22 Input object defining dims, shape, unit, and dtype of the output. 

23 

24 Returns 

25 ------- 

26 : Same type as input 

27 New object of zeros. 

28 

29 See Also 

30 -------- 

31 scipp.zeros: 

32 Create zeros but based on given dims and shape. 

33 scipp.ones_like: 

34 Create an object initialized with ones. 

35 scipp.full_like: 

36 Create an object filled with a given value. 

37 scipp.empty_like: 

38 Create an object with uninitialized elements. 

39 """ 

40 new_values = zeros( 

41 dims=obj.dims, 

42 shape=obj.shape, 

43 unit=obj.unit, 

44 dtype=obj.dtype, 

45 with_variances=obj.variances is not None, 

46 ) 

47 return rewrap_output_data(obj, new_values) 

48 

49 

50def ones_like(obj: VariableLikeType, /) -> VariableLikeType: 

51 """Return a new object with the same dims, shape, unit, 

52 and dtype as the input and all elements initialized to 1. 

53 

54 If the input has variances, all variances in the output are set to 1. 

55 If the input is a :class:`DataArray`, coordinates and attributes are shallow-copied 

56 and masks are deep-copied. 

57 

58 Parameters 

59 ---------- 

60 obj: scipp.Variable or scipp.DataArray 

61 Input object defining dims, shape, unit, and dtype of the output. 

62 

63 Returns 

64 ------- 

65 : Same type as input 

66 New object of ones. 

67 

68 See Also 

69 -------- 

70 scipp.ones: 

71 Create ones but based on given dims and shape. 

72 scipp.zeros_like: 

73 Create an object initialized with zeros. 

74 scipp.full_like: 

75 Create an object filled with a given value. 

76 scipp.empty_like: 

77 Create an object with uninitialized elements. 

78 """ 

79 new_values = ones( 

80 dims=obj.dims, 

81 shape=obj.shape, 

82 unit=obj.unit, 

83 dtype=obj.dtype, 

84 with_variances=obj.variances is not None, 

85 ) 

86 return rewrap_output_data(obj, new_values) 

87 

88 

89def empty_like(obj: VariableLikeType, /) -> VariableLikeType: 

90 """Return a new object with the same dims, shape, unit, 

91 and dtype as the input and all elements uninitialized. 

92 

93 If the input has variances, all variances in the output exist but are uninitialized. 

94 If the input is a :class:`DataArray`, coordinates and attributes are shallow-copied 

95 and masks are deep-copied. 

96 

97 Warning 

98 ------- 

99 Reading from any elements before writing to them produces undefined results. 

100 

101 Parameters 

102 ---------- 

103 obj: scipp.Variable or scipp.DataArray 

104 Input object defining dims, shape, unit, and dtype of the output 

105 

106 Returns 

107 ------- 

108 : Same type as input 

109 New object with uninitialized values and maybe variances. 

110 

111 See Also 

112 -------- 

113 scipp.empty: 

114 Create an uninitialized object based on given dims and shape. 

115 scipp.zeros_like: 

116 Create an object initialized with zeros. 

117 scipp.ones_like: 

118 Create an object initialized with ones. 

119 scipp.full_like: 

120 Create an object filled with a given value. 

121 """ 

122 new_values = empty( 

123 dims=obj.dims, 

124 shape=obj.shape, 

125 unit=obj.unit, 

126 dtype=obj.dtype, 

127 with_variances=obj.variances is not None, 

128 ) 

129 return rewrap_output_data(obj, new_values) 

130 

131 

132def full_like( 

133 obj: VariableLikeType, /, value: Any, *, variance: Any = None 

134) -> VariableLikeType: 

135 """Return a new object with the same dims, shape, unit, 

136 and dtype as the input and all elements initialized to the given value. 

137 

138 If the input is a :class:`DataArray`, coordinates and attributes are shallow-copied 

139 and masks are deep-copied. 

140 

141 Parameters 

142 ---------- 

143 obj: scipp.Variable or scipp.DataArray 

144 Input object defining dims, shape, unit, and dtype of the output 

145 value: 

146 The value to fill the data with. 

147 variance: 

148 The variance to fill the Variable with. If None 

149 or not provided, the variances will not be set. 

150 

151 Returns 

152 ------- 

153 : Same type as input 

154 New object with elements set to given values and variances. 

155 

156 See Also 

157 -------- 

158 scipp.full: 

159 Create an object filled with given value based on given dims and shape. 

160 scipp.zeros_like: 

161 Create an object initialized with zeros. 

162 scipp.ones_like: 

163 Create an object initialized with ones. 

164 scipp.empty_like: 

165 Create an object with uninitialized elements. 

166 """ 

167 new_values = full( 

168 dims=obj.dims, 

169 shape=obj.shape, 

170 unit=obj.unit, 

171 dtype=obj.dtype, 

172 value=value, 

173 variance=variance, 

174 ) 

175 return rewrap_output_data(obj, new_values)