MCPcopy Create free account
hub / github.com/numpy/numpy / get_data

Method get_data

numpy/core/tests/test_casting_unittests.py:157–196  ·  view source on GitHub ↗
(self, dtype1, dtype2)

Source from the content-addressed store, hash-verified

155 size = 1500 # Best larger than NPY_LOWLEVEL_BUFFER_BLOCKSIZE * itemsize
156
157 def get_data(self, dtype1, dtype2):
158 if dtype2 is None or dtype1.itemsize >= dtype2.itemsize:
159 length = self.size // dtype1.itemsize
160 else:
161 length = self.size // dtype2.itemsize
162
163 # Assume that the base array is well enough aligned for all inputs.
164 arr1 = np.empty(length, dtype=dtype1)
165 assert arr1.flags.c_contiguous
166 assert arr1.flags.aligned
167
168 values = [random.randrange(-128, 128) for _ in range(length)]
169
170 for i, value in enumerate(values):
171 # Use item assignment to ensure this is not using casting:
172 if value < 0 and dtype1.kind == "u":
173 # Manually rollover unsigned integers (-1 -> int.max)
174 value = value + np.iinfo(dtype1).max + 1
175 arr1[i] = value
176
177 if dtype2 is None:
178 if dtype1.char == "?":
179 values = [bool(v) for v in values]
180 return arr1, values
181
182 if dtype2.char == "?":
183 values = [bool(v) for v in values]
184
185 arr2 = np.empty(length, dtype=dtype2)
186 assert arr2.flags.c_contiguous
187 assert arr2.flags.aligned
188
189 for i, value in enumerate(values):
190 # Use item assignment to ensure this is not using casting:
191 if value < 0 and dtype2.kind == "u":
192 # Manually rollover unsigned integers (-1 -> int.max)
193 value = value + np.iinfo(dtype2).max + 1
194 arr2[i] = value
195
196 return arr1, arr2, values
197
198 def get_data_variation(self, arr1, arr2, aligned=True, contig=True):
199 """

Calls

no outgoing calls

Tested by

no test coverage detected