a
    
d,                     @   s   d Z ddlmZmZmZ ddlmZ ddlZddl	m
Z
 ddlmZmZ ddlmZmZ g d	ZdddZdddZdddZdd ZdddZdS )zP
2D and nD Discrete Wavelet Transforms and Inverse Discrete Wavelet Transforms.
    )divisionprint_functionabsolute_import)productN   )_have_c99_complex)dwt_axis	idwt_axis)_wavelets_per_axis_modes_per_axis)dwt2idwt2dwtnidwtn	symmetricc                 C   sn   t |}t| } t|dkr&td| jtt|k rBtdt| |||}|d |d |d |d ffS )a  
    2D Discrete Wavelet Transform.

    Parameters
    ----------
    data : array_like
        2D array with input data
    wavelet : Wavelet object or name string, or 2-tuple of wavelets
        Wavelet to use.  This can also be a tuple containing a wavelet to
        apply along each axis in ``axes``.
    mode : str or 2-tuple of strings, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`. This can
        also be a tuple of modes specifying the mode to use on each axis in
        ``axes``.
    axes : 2-tuple of ints, optional
        Axes over which to compute the DWT. Repeated elements mean the DWT will
        be performed multiple times along these axes.

    Returns
    -------
    (cA, (cH, cV, cD)) : tuple
        Approximation, horizontal detail, vertical detail and diagonal
        detail coefficients respectively.  Horizontal refers to array axis 0
        (or ``axes[0]`` for user-specified ``axes``).

    Examples
    --------
    >>> import numpy as np
    >>> import pywt
    >>> data = np.ones((4,4), dtype=np.float64)
    >>> coeffs = pywt.dwt2(data, 'haar')
    >>> cA, (cH, cV, cD) = coeffs
    >>> cA
    array([[ 2.,  2.],
           [ 2.,  2.]])
    >>> cV
    array([[ 0.,  0.],
           [ 0.,  0.]])

       Expected 2 axesz8Input array has fewer dimensions than the specified axesaadaaddd)tuplenpasarraylen
ValueErrorndimuniquer   )datawaveletmodeaxesZcoefs r%   W/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pywt/_multidim.pyr      s    )
r   c                 C   sF   | \}\}}}t |}t|dkr*td||||d} t| |||S )a  
    2-D Inverse Discrete Wavelet Transform.

    Reconstructs data from coefficient arrays.

    Parameters
    ----------
    coeffs : tuple
        (cA, (cH, cV, cD)) A tuple with approximation coefficients and three
        details coefficients 2D arrays like from ``dwt2``.  If any of these
        components are set to ``None``, it will be treated as zeros.
    wavelet : Wavelet object or name string, or 2-tuple of wavelets
        Wavelet to use.  This can also be a tuple containing a wavelet to
        apply along each axis in ``axes``.
    mode : str or 2-tuple of strings, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`. This can
        also be a tuple of modes specifying the mode to use on each axis in
        ``axes``.
    axes : 2-tuple of ints, optional
        Axes over which to compute the IDWT. Repeated elements mean the IDWT
        will be performed multiple times along these axes.

    Examples
    --------
    >>> import numpy as np
    >>> import pywt
    >>> data = np.array([[1,2], [3,4]], dtype=np.float64)
    >>> coeffs = pywt.dwt2(data, 'haar')
    >>> pywt.idwt2(coeffs, 'haar')
    array([[ 1.,  2.],
           [ 3.,  4.]])

    r   r   )r   r   r   r   )r   r   r   r   )coeffsr"   r#   r$   ZLLZHLZLHZHHr%   r%   r&   r   M   s    #r   c                    s&  t   tsTt  rTt j|||t j|||tfdd D S  j	t 	dkrlt
d jdk r~td|du rt j} fdd	|D }t||}t||}d
 fg}t|||D ]P\}}}g }	|D ]8\}
}t||||\}}|	|
d |f|
d |fg q|	}qt|S )aO  
    Single-level n-dimensional Discrete Wavelet Transform.

    Parameters
    ----------
    data : array_like
        n-dimensional array with input data.
    wavelet : Wavelet object or name string, or tuple of wavelets
        Wavelet to use.  This can also be a tuple containing a wavelet to
        apply along each axis in ``axes``.
    mode : str or tuple of string, optional
        Signal extension mode used in the decomposition,
        see :ref:`Modes <ref-modes>`. This can also be a tuple of modes
        specifying the mode to use on each axis in ``axes``.
    axes : sequence of ints, optional
        Axes over which to compute the DWT. Repeated elements mean the DWT will
        be performed multiple times along these axes. A value of ``None`` (the
        default) selects all axes.

        Axes may be repeated, but information about the original size may be
        lost if it is not divisible by ``2 ** nrepeats``. The reconstruction
        will be larger, with additional values derived according to the
        ``mode`` parameter. ``pywt.wavedecn`` should be used for multilevel
        decomposition.

    Returns
    -------
    coeffs : dict
        Results are arranged in a dictionary, where key specifies
        the transform type on each dimension and value is a n-dimensional
        coefficients array.

        For example, for a 2D case the result will look something like this::

            {'aa': <coeffs>  # A(LL) - approx. on 1st dim, approx. on 2nd dim
             'ad': <coeffs>  # V(LH) - approx. on 1st dim, det. on 2nd dim
             'da': <coeffs>  # H(HL) - det. on 1st dim, approx. on 2nd dim
             'dd': <coeffs>  # D(HH) - det. on 1st dim, det. on 2nd dim
            }

        For user-specified ``axes``, the order of the characters in the
        dictionary keys map to the specified ``axes``.

    c                 3   s&   | ]}|| d  |   fV  qdS )              ?Nr%   .0k)imagrealr%   r&   	<genexpr>       zdwtn.<locals>.<genexpr>objectz"Input must be a numeric array-liker   zInput data must be at least 1DNc                    s"   g | ]}|d k r| j  n|qS r   r   r*   a)r!   r%   r&   
<listcomp>   r/   zdwtn.<locals>.<listcomp> r4   d)r   r   r   iscomplexobjr   r-   r,   dictkeysdtype	TypeErrorr   r   ranger   r
   zipr   extend)r!   r"   r#   r$   modeswaveletsr'   axiswav
new_coeffsZsubbandxZcAZcDr%   )r!   r,   r-   r&   r   y   s0    -






r   c                 C   s   dd |   D }|r$td|dd |   D }|rHtd|dd |  D }tt|dkrttdtd	d
 |   D S )Nc                 S   s   g | ]\}}|d u r|qS Nr%   r*   r+   vr%   r%   r&   r5      r/   z_fix_coeffs.<locals>.<listcomp>zThe following detail coefficients were set to None:
{0}
For multilevel transforms, rather than setting
	coeffs[key] = None
use
	coeffs[key] = np.zeros_like(coeffs[key])
c                 S   s$   g | ]\}}t |t d ks|qS )r   )setrG   r%   r%   r&   r5      s   zOThe following invalid keys were found in the detail coefficient dictionary: {}.c                 S   s   g | ]}t |qS r%   r   r)   r%   r%   r&   r5      r/   r   z4All detail coefficient names must have equal length.c                 s   s    | ]\}}|t |fV  qd S rF   )r   r   rG   r%   r%   r&   r.      r/   z_fix_coeffs.<locals>.<genexpr>)itemsr   formatr:   r   r   r    r9   )r'   Zmissing_keysZinvalid_keysZkey_lengthsr%   r%   r&   _fix_coeffs   s(    	rM   c                    sj  t dd |  D } t dd |  D } t| } tstdd |  D rt dd |  D }t dd |  D }t||||dt||||  S tdd |  D z"fd	d|  D }t	| W n t
y   td
Y n0 t fdd|D rtd|du r&t}nt fdd|D }t||}t||}tttt|||D ]\}	\}
}}|
dk s|
krtdi }dd td|	dD }|D ]}| |d d}| |d d}|durB|durB|j|jkrB|jjdks|jjdkr tj}ntj}tj||d}tj||d}t|||||
||< q|} ql| d S )aV  
    Single-level n-dimensional Inverse Discrete Wavelet Transform.

    Parameters
    ----------
    coeffs: dict
        Dictionary as in output of ``dwtn``. Missing or ``None`` items
        will be treated as zeros.
    wavelet : Wavelet object or name string, or tuple of wavelets
        Wavelet to use.  This can also be a tuple containing a wavelet to
        apply along each axis in ``axes``.
    mode : str or list of string, optional
        Signal extension mode used in the decomposition,
        see :ref:`Modes <ref-modes>`. This can also be a tuple of modes
        specifying the mode to use on each axis in ``axes``.
    axes : sequence of ints, optional
        Axes over which to compute the IDWT. Repeated elements mean the IDWT
        will be performed multiple times along these axes. A value of ``None``
        (the default) selects all axes.

        For the most accurate reconstruction, the axes should be provided in
        the same order as they were provided to ``dwtn``.

    Returns
    -------
    data: ndarray
        Original signal reconstructed from input data.

    c                 s   s"   | ]\}}|d ur||fV  qd S rF   r%   rG   r%   r%   r&   r.      r/   zidwtn.<locals>.<genexpr>c                 s   s"   | ]\}}|d ur||fV  qd S rF   r%   rG   r%   r%   r&   r.     r/   c                 s   s   | ]}t |V  qd S rF   )r   r8   )r*   rH   r%   r%   r&   r.     r/   c                 s   s   | ]\}}||j fV  qd S rF   )r-   rG   r%   r%   r&   r.     r/   c                 s   s   | ]\}}||j fV  qd S rF   )r,   rG   r%   r%   r&   r.   	  r/   r(   c                 s   s   | ]}t |V  qd S rF   rJ   )r*   keyr%   r%   r&   r.     r/   c                 3   s,   | ]$\}}|d urt | kr|jV  qd S rF   )r   shaperG   )ndim_transformr%   r&   r.     s   
z8`coeffs` must contain at least one non-null wavelet bandc                 3   s   | ]}| kV  qd S rF   r%   )r*   s)coeff_shaper%   r&   r.     r/   z,`coeffs` must all be of equal size (or None)Nc                    s    g | ]}|d k r|  n|qS r1   r%   r3   r2   r%   r&   r5     r/   zidwtn.<locals>.<listcomp>r   z!Axis greater than data dimensionsc                 S   s   g | ]}d  |qS )r6   )join)r*   Zcoefr%   r%   r&   r5   )  r/   r   )repeatr4   r7   c)r;   r6   )r9   rK   rM   r   anyvaluesr   maxr:   nextStopIterationr   r=   r   r   r
   reversedlist	enumerater>   r   Z	AxisErrorr   getr;   kindZ
complex128float64r   r	   )r'   r"   r#   r$   Zreal_coeffsZimag_coeffsZcoeff_shapesr@   rA   Z
key_lengthrB   rC   rD   Znew_keysrN   LHr;   r%   )rR   r   rP   r&   r      s\     



r   )r   r   )r   r   )r   N)r   N)__doc__
__future__r   r   r   	itertoolsr   numpyr   Z_c99_configr   Z_extensions._dwtr   r	   _utilsr
   r   __all__r   r   r   rM   r   r%   r%   r%   r&   <module>   s   
5
,
J