Coverage for install/scipp/core/like.py: 47%
17 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)
4from typing import Any, TypeVar
6from .concepts import rewrap_output_data
7from .cpp_classes import DataArray, Variable
8from .variable import empty, full, ones, zeros
10_T = TypeVar('_T', Variable, DataArray)
13def zeros_like(obj: _T, /) -> _T:
14 """Return a new object with the same dims, shape, unit,
15 and dtype as the input and all elements initialized to 0.
17 If the input has variances, all variances in the output are set to 0.
18 If the input is a :class:`DataArray`, coordinates and attributes are shallow-copied
19 and masks are deep-copied.
21 Parameters
22 ----------
23 obj: scipp.Variable | scipp.DataArray
24 Input object defining dims, shape, unit, and dtype of the output.
26 Returns
27 -------
28 : Same type as input
29 New object of zeros.
31 See Also
32 --------
33 scipp.zeros:
34 Create zeros but based on given dims and shape.
35 scipp.ones_like:
36 Create an object initialized with ones.
37 scipp.full_like:
38 Create an object filled with a given value.
39 scipp.empty_like:
40 Create an object with uninitialized elements.
41 """
42 new_values = zeros(
43 dims=obj.dims,
44 shape=obj.shape,
45 unit=obj.unit,
46 dtype=obj.dtype,
47 with_variances=obj.variances is not None,
48 )
49 return rewrap_output_data(obj, new_values)
52def ones_like(obj: _T, /) -> _T:
53 """Return a new object with the same dims, shape, unit,
54 and dtype as the input and all elements initialized to 1.
56 If the input has variances, all variances in the output are set to 1.
57 If the input is a :class:`DataArray`, coordinates and attributes are shallow-copied
58 and masks are deep-copied.
60 Parameters
61 ----------
62 obj: scipp.Variable | scipp.DataArray
63 Input object defining dims, shape, unit, and dtype of the output.
65 Returns
66 -------
67 : Same type as input
68 New object of ones.
70 See Also
71 --------
72 scipp.ones:
73 Create ones but based on given dims and shape.
74 scipp.zeros_like:
75 Create an object initialized with zeros.
76 scipp.full_like:
77 Create an object filled with a given value.
78 scipp.empty_like:
79 Create an object with uninitialized elements.
80 """
81 new_values = ones(
82 dims=obj.dims,
83 shape=obj.shape,
84 unit=obj.unit,
85 dtype=obj.dtype,
86 with_variances=obj.variances is not None,
87 )
88 return rewrap_output_data(obj, new_values)
91def empty_like(obj: _T, /) -> _T:
92 """Return a new object with the same dims, shape, unit,
93 and dtype as the input and all elements uninitialized.
95 If the input has variances, all variances in the output exist but are uninitialized.
96 If the input is a :class:`DataArray`, coordinates and attributes are shallow-copied
97 and masks are deep-copied.
99 Warning
100 -------
101 Reading from any elements before writing to them produces undefined results.
103 Parameters
104 ----------
105 obj: scipp.Variable | scipp.DataArray
106 Input object defining dims, shape, unit, and dtype of the output
108 Returns
109 -------
110 : Same type as input
111 New object with uninitialized values and maybe variances.
113 See Also
114 --------
115 scipp.empty:
116 Create an uninitialized object based on given dims and shape.
117 scipp.zeros_like:
118 Create an object initialized with zeros.
119 scipp.ones_like:
120 Create an object initialized with ones.
121 scipp.full_like:
122 Create an object filled with a given value.
123 """
124 new_values = empty(
125 dims=obj.dims,
126 shape=obj.shape,
127 unit=obj.unit,
128 dtype=obj.dtype,
129 with_variances=obj.variances is not None,
130 )
131 return rewrap_output_data(obj, new_values)
134def full_like(obj: _T, /, value: Any, *, variance: Any = None) -> _T:
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.
138 If the input is a :class:`DataArray`, coordinates and attributes are shallow-copied
139 and masks are deep-copied.
141 Parameters
142 ----------
143 obj: scipp.Variable | 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.
151 Returns
152 -------
153 : Same type as input
154 New object with elements set to given values and variances.
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)