U
    e»{jŠ  ã                   @   sV   d Z ddlZddlmZ dddgZdd	„ Zd
d„ Zeejfdd„ƒZ	eddd„ƒZ
dS )z=
This module includes helper functions for array operations.
é    Né   )Úsupport_nddataÚreshape_as_blocksÚblock_reduceÚblock_replicatec                 C   sŒ   t  | ¡} t  |¡}t  |dk¡r*tdƒ‚| jdkrNt|ƒdkrNt  || j¡}t|ƒ| jkrdtdƒ‚| t	¡}t  ||k¡r„tdƒ‚| |fS )Nr   z-block_size elements must be strictly positiver   zTblock_size must be a scalar or have the same length as the number of data dimensionsz$block_size elements must be integers)
ÚnpZ
asanyarrayZ
atleast_1dÚanyÚ
ValueErrorÚndimÚlenÚrepeatZastypeÚint)ÚdataÚ
block_sizeZblock_size_int© r   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/nddata/blocks.pyÚ_process_block_inputs   s    

ÿ
r   c                 C   s’   t | |ƒ\} }t t | j|¡dk¡r.tdƒ‚t | j¡| }tdd„ t||ƒD ƒƒ}tt	dt
|ƒdƒƒ}tt	dt
|ƒdƒƒ}|  |¡ || ¡S )a  
    Reshape a data array into blocks.

    This is useful to efficiently apply functions on block subsets of
    the data instead of using loops.  The reshaped array is a view of
    the input data array.

    .. versionadded:: 4.1

    Parameters
    ----------
    data : ndarray
        The input data array.

    block_size : int or array-like (int)
        The integer block size along each axis.  If ``block_size`` is a
        scalar and ``data`` has more than one dimension, then
        ``block_size`` will be used for for every axis.  Each dimension
        of ``block_size`` must divide evenly into the corresponding
        dimension of ``data``.

    Returns
    -------
    output : ndarray
        The reshaped array as a view of the input ``data`` array.

    Examples
    --------
    >>> import numpy as np
    >>> from astropy.nddata import reshape_as_blocks
    >>> data = np.arange(16).reshape(4, 4)
    >>> data
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])
    >>> reshape_as_blocks(data, (2, 2))
    array([[[[ 0,  1],
             [ 4,  5]],
            [[ 2,  3],
             [ 6,  7]]],
           [[[ 8,  9],
             [12, 13]],
            [[10, 11],
             [14, 15]]]])
    r   zXEach dimension of block_size must divide evenly into the corresponding dimension of datac                 s   s   | ]}|D ]
}|V  q
qd S )Nr   )Ú.0ZijÚkr   r   r   Ú	<genexpr>\   s       z$reshape_as_blocks.<locals>.<genexpr>é   r   )r   r   r   ÚmodÚshaper	   ÚarrayÚtupleÚzipÚranger   ZreshapeZ	transpose)r   r   ÚnblocksZ	new_shapeZnblocks_idxZ	block_idxr   r   r   r   $   s    /ÿc                 C   s˜   t | |ƒ\} }t | j¡| }|| }t| jƒD ]>}| j| || kr0|  d|¡} | d|| … } |  d|¡} q0t| |ƒ}tt| j|jƒƒ}|||d�S )a%  
    Downsample a data array by applying a function to local blocks.

    If ``data`` is not perfectly divisible by ``block_size`` along a
    given axis then the data will be trimmed (from the end) along that
    axis.

    Parameters
    ----------
    data : array-like
        The data to be resampled.

    block_size : int or array-like (int)
        The integer block size along each axis.  If ``block_size`` is a
        scalar and ``data`` has more than one dimension, then
        ``block_size`` will be used for for every axis.

    func : callable, optional
        The method to use to downsample the data. Must be a callable
        that takes in a 4D `~numpy.ndarray` (the 2D `~numpy.ndarray`
        input into `block_reduce` gets reshaped as 4D) and has an
        ``axis`` keyword that accepts tuples. This function will be
        called with ``axis=(2, 3)`` and it should return a 2D array. The
        default is `~numpy.sum`, which provides block summation (and
        conserves the data sum).

    Returns
    -------
    output : array-like
        The resampled data. Note the depending on the input ``func``,
        the dtype of the output array may not match the input array.

    Examples
    --------
    >>> import numpy as np
    >>> from astropy.nddata import block_reduce
    >>> data = np.arange(16).reshape(4, 4)
    >>> block_reduce(data, 2)  # doctest: +FLOAT_CMP
    array([[10, 18],
           [42, 50]])

    >>> block_reduce(data, 2, func=np.mean)  # doctest: +FLOAT_CMP
    array([[  2.5,   4.5],
           [ 10.5,  12.5]])
    r   N©Úaxis)	r   r   r   r   r   r
   Zswapaxesr   r   )r   r   Úfuncr   Z	size_initr   Zreshapedr   r   r   r   c   s    /
Tc                 C   sH   t | |ƒ\} }t| jƒD ]}tj| || |d�} q|rD| t |¡ } | S )aû  
    Upsample a data array by block replication.

    Parameters
    ----------
    data : array-like
        The data to be block replicated.

    block_size : int or array-like (int)
        The integer block size along each axis.  If ``block_size`` is a
        scalar and ``data`` has more than one dimension, then
        ``block_size`` will be used for for every axis.

    conserve_sum : bool, optional
        If `True` (the default) then the sum of the output
        block-replicated data will equal the sum of the input ``data``.

    Returns
    -------
    output : array-like
        The block-replicated data. Note that when ``conserve_sum`` is
        `True`, the dtype of the output array will be float.

    Examples
    --------
    >>> import numpy as np
    >>> from astropy.nddata import block_replicate
    >>> data = np.array([[0., 1.], [2., 3.]])
    >>> block_replicate(data, 2)  # doctest: +FLOAT_CMP
    array([[0.  , 0.  , 0.25, 0.25],
           [0.  , 0.  , 0.25, 0.25],
           [0.5 , 0.5 , 0.75, 0.75],
           [0.5 , 0.5 , 0.75, 0.75]])

    >>> block_replicate(data, 2, conserve_sum=False)  # doctest: +FLOAT_CMP
    array([[0., 0., 1., 1.],
           [0., 0., 1., 1.],
           [2., 2., 3., 3.],
           [2., 2., 3., 3.]])
    r   )r   r   r
   r   r   Úprod)r   r   Zconserve_sumÚir   r   r   r   £   s    *)T)Ú__doc__Únumpyr   Z
decoratorsr   Ú__all__r   r   Úsumr   r   r   r   r   r   Ú<module>   s   
??