MCPcopy Index your code
hub / github.com/numpy/numpy / get_data_variation

Method get_data_variation

numpy/_core/tests/test_casting_unittests.py:207–264  ·  view source on GitHub ↗

Returns a copy of arr1 that may be non-contiguous or unaligned, and a matching array for arr2 (although not a copy).

(self, arr1, arr2, aligned=True, contig=True)

Source from the content-addressed store, hash-verified

205 return arr1, arr2, values
206
207 def get_data_variation(self, arr1, arr2, aligned=True, contig=True):
208 """
209 Returns a copy of arr1 that may be non-contiguous or unaligned, and a
210 matching array for arr2 (although not a copy).
211 """
212 if contig:
213 stride1 = arr1.dtype.itemsize
214 stride2 = arr2.dtype.itemsize
215 elif aligned:
216 stride1 = 2 * arr1.dtype.itemsize
217 stride2 = 2 * arr2.dtype.itemsize
218 else:
219 stride1 = arr1.dtype.itemsize + 1
220 stride2 = arr2.dtype.itemsize + 1
221
222 max_size1 = len(arr1) * 3 * arr1.dtype.itemsize + 1
223 max_size2 = len(arr2) * 3 * arr2.dtype.itemsize + 1
224 from_bytes = np.zeros(max_size1, dtype=np.uint8)
225 to_bytes = np.zeros(max_size2, dtype=np.uint8)
226
227 # Sanity check that the above is large enough:
228 assert stride1 * len(arr1) <= from_bytes.nbytes
229 assert stride2 * len(arr2) <= to_bytes.nbytes
230
231 if aligned:
232 new1 = as_strided(from_bytes[:-1].view(arr1.dtype),
233 arr1.shape, (stride1,))
234 new2 = as_strided(to_bytes[:-1].view(arr2.dtype),
235 arr2.shape, (stride2,))
236 else:
237 new1 = as_strided(from_bytes[1:].view(arr1.dtype),
238 arr1.shape, (stride1,))
239 new2 = as_strided(to_bytes[1:].view(arr2.dtype),
240 arr2.shape, (stride2,))
241
242 new1[...] = arr1
243
244 if not contig:
245 # Ensure we did not overwrite bytes that should not be written:
246 offset = arr1.dtype.itemsize if aligned else 0
247 buf = from_bytes[offset::stride1].tobytes()
248 assert buf.count(b"\0") == len(buf)
249
250 if contig:
251 assert new1.flags.c_contiguous
252 assert new2.flags.c_contiguous
253 else:
254 assert not new1.flags.c_contiguous
255 assert not new2.flags.c_contiguous
256
257 if aligned:
258 assert new1.flags.aligned
259 assert new2.flags.aligned
260 else:
261 assert not new1.flags.aligned or new1.dtype.alignment == 1
262 assert not new2.flags.aligned or new2.dtype.alignment == 1
263
264 return new1, new2

Callers 4

test_numeric_to_timesMethod · 0.95
test_time_to_timeMethod · 0.95

Calls 4

as_stridedFunction · 0.90
viewMethod · 0.45
tobytesMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected