Ejemplo n.º 1
0
def view_element_first_byte(x):
    """Construct an array viewing the first byte of each element of `x`"""
    from numpy.lib.stride_tricks import DummyArray
    interface = dict(x.__array_interface__)
    interface['typestr'] = '|b1'
    interface['descr'] = [('', '|b1')]
    return np.asarray(DummyArray(interface, x))
Ejemplo n.º 2
0
def view_element_first_byte(x):
    """Construct an array viewing the first byte of each element of `x`"""
    from numpy.lib.stride_tricks import DummyArray

    interface = dict(x.__array_interface__)
    interface["typestr"] = "|b1"
    interface["descr"] = [("", "|b1")]
    return np.asarray(DummyArray(interface, x))
Ejemplo n.º 3
0
    def as_strided(x, shape=None, strides=None, subok=False):
        """ Make an ndarray from the given array with the given shape and strides.
        """
        # first convert input to array, possibly keeping subclass
        x = np.array(x, copy=False, subok=subok)
        interface = dict(x.__array_interface__)
        if shape is not None:
            interface['shape'] = tuple(shape)
        if strides is not None:
            interface['strides'] = tuple(strides)
        array = np.asarray(DummyArray(interface, base=x))

        if array.dtype.fields is None and x.dtype.fields is not None:
            # This should only happen if x.dtype is [('', 'Vx')]
            array.dtype = x.dtype

        return _maybe_view_as_subclass(x, array)
Ejemplo n.º 4
0
 def as_strided(x, shape=None, strides=None, subok=False):
     """ Make an ndarray from the given array with the given shape and strides.
     """
     # first convert input to array, possibly keeping subclass
     x = np.array(x, copy=False, subok=subok)
     interface = dict(x.__array_interface__)
     if shape is not None:
         interface['shape'] = tuple(shape)
     if strides is not None:
         interface['strides'] = tuple(strides)
     array = np.asarray(DummyArray(interface, base=x))
     # Make sure dtype is correct in case of custom dtype
     if array.dtype.kind == 'V':
         array.dtype = x.dtype
     if type(x) is not type(array):
         # if input was an ndarray subclass and subclasses were OK,
         # then view the result as that subclass.
         array = array.view(type=type(x))
         # Since we have done something akin to a view from x, we should let
         # the subclass finalize (if it has it implemented, i.e., is not None).
         if array.__array_finalize__:
             array.__array_finalize__(x)
     return array