
    IR-e                     n    d Z ddlZddlmZ g dZd Zd Zeej        fd            Z	edd
            Z
dS )z=
This module includes helper functions for array operations.
    N   )support_nddata)reshape_as_blocksblock_reduceblock_replicatec                    t          j        |           } t          j        |          }t          j        |dk              rt	          d          | j        dk    r-t          |          dk    rt          j        || j                  }t          |          | j        k    rt	          d          |                    t                    }t          j        ||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)
np
asanyarray
atleast_1dany
ValueErrorndimlenrepeatastypeint)data
block_sizeblock_size_ints      5lib/python3.11/site-packages/astropy/nddata/blocks.py_process_block_inputsr      s    =Dz**J	vjAo JHIIIy1}}ZA--Yz4955

:$)##6
 
 	

  &&s++N	vn
*++ A?@@@    c                 .   t          | |          \  } }t          j        t          j        | j        |          dk              rt          d          t          j        | j                  |z  }t          d t          ||          D                       }t          t          dt          |          d                    }t          t          dt          |          d                    }|                     |                              ||z             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              3   $   K   | ]}|D ]}|V  d S )N ).0ijks      r   	<genexpr>z$reshape_as_blocks.<locals>.<genexpr>\   s/      GGBBGGqaGGGGGGGr      r   )r   r	   r   modshaper   arraytuplezipranger   reshape	transpose)r   r   nblocks	new_shapenblocks_idx	block_idxs         r   r   r   $   s    ^ -T:>>D*	vbfTZ,,122 
7
 
 	

 htz""j0GGG#gz":":GGGGGIaY3344KeAs9~~q1122I<<	"",,[9-DEEEr   c                    t          | |          \  } }t          j        | j                  |z  }||z  }t	          | j                  D ]U}| j        |         ||         k    r<|                     d|          } | d||                  } |                     d|          } Vt          | |          }t          t	          | j        |j                            } |||          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   swapaxesr   r$   )r   r   funcr)   	size_initr/   reshapeds          r   r   r   c   s    ^ -T:>>D*htz""j0G*$I di   * *:dy..==D))D))D/)*D==D))D z22Hty(-0011D4t$$$$r   Tc                     t          | |          \  } }t          | j                  D ]}t          j        | ||         |          }  |r| t          j        |          z  } | 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   conserve_sumis       r   r   r      so    T -T:>>D*49 6 6yz!}1555 *bgj)))Kr   )T)__doc__numpyr	   
decoratorsr   __all__r   r   sumr   r   r   r   r   <module>r=      s         & & & & & &
B
B
B     .<F <F <F~ (* <% <% <% <%~ 1 1 1 1 1 1r   