Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
_array_utils_impl.py63 linesDownload Raw Back to lib
1"""
2Miscellaneous utils.
3"""
4from numpy._core import asarray
5from numpy._core.numeric import normalize_axis_index, normalize_axis_tuple
6from numpy._utils import set_module
7
8__all__ = ["byte_bounds", "normalize_axis_tuple", "normalize_axis_index"]
9
10
11@set_module("numpy.lib.array_utils")
12def byte_bounds(a):
13    """
14    Returns pointers to the end-points of an array.
15
16    Parameters
17    ----------
18    a : ndarray
19        Input array. It must conform to the Python-side of the array
20        interface.
21
22    Returns
23    -------
24    (low, high) : tuple of 2 integers
25        The first integer is the first byte of the array, the second
26        integer is just past the last byte of the array.  If `a` is not
27        contiguous it will not use every byte between the (`low`, `high`)
28        values.
29
30    Examples
31    --------
32    >>> import numpy as np
33    >>> I = np.eye(2, dtype='f'); I.dtype
34    dtype('float32')
35    >>> low, high = np.lib.array_utils.byte_bounds(I)
36    >>> high - low == I.size*I.itemsize
37    True
38    >>> I = np.eye(2); I.dtype
39    dtype('float64')
40    >>> low, high = np.lib.array_utils.byte_bounds(I)
41    >>> high - low == I.size*I.itemsize
42    True
43
44    """
45    ai = a.__array_interface__
46    a_data = ai['data'][0]
47    astrides = ai['strides']
48    ashape = ai['shape']
49    bytes_a = asarray(a).dtype.itemsize
50
51    a_low = a_high = a_data
52    if astrides is None:
53        # contiguous case
54        a_high += a.size * bytes_a
55    else:
56        for shape, stride in zip(ashape, astrides):
57            if stride < 0:
58                a_low += (shape - 1) * stride
59            else:
60                a_high += (shape - 1) * stride
61        a_high += bytes_a
62    return a_low, a_high
63 
codekingpro/portable-devtools · Team Ai