
     e,                         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ZddZddZd Z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                 `   t          |          }t          j        |           } t          |          dk    rt	          d          | j        t          t          j        |                    k     rt	          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axescoefss        .lib/python3.11/site-packages/pywt/_multidim.pyr   r      s    R ;;D:dD
4yyA~~*+++y3ry''''   ! ! 	! wd++E;teDk5;???    c                     | \  }\  }}}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&   LLHLLHHHs           r(   r   r   M   sc    F BR;;D
4yyA~~*+++b"55F$---r)   c                     t          j                    t          spt          j                   r\t	           j        |||          t	           j        |||          t          fd                                D                       S  j	        t          j	        d          k    rt          d           j        dk     rt          d          |t           j                  } fd|D             }t          ||          }t          ||          }d fg}t!          |||          D ]H\  }}}g }	|D ];\  }
}t#          ||||          \  }}|	                    |
d	z   |f|
d
z   |fg           <|	}It          |          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   D   K   | ]}||         d |         z  z   fV  dS )              ?N ).0kimagreals     r(   	<genexpr>zdwtn.<locals>.<genexpr>   s9      EEAQQ"tAw,./EEEEEEr)   objectz"Input must be a numeric array-liker   zInput data must be at least 1DNc                 4    g | ]}|d k     r
|j         z   n|S r   )r!   )r4   ar#   s     r(   
<listcomp>zdwtn.<locals>.<listcomp>   s+    888aQUUA	MM888r)    r<   d)r   r   r   iscomplexobjr   r7   r6   dictkeysdtype	TypeErrorr!   r    ranger   r   zipr	   extend)r#   r$   r%   r&   modeswaveletsr+   axiswav
new_coeffssubbandxcAcDr6   r7   s   `             @@r(   r   r   y   s   Z :dD F!6!6 FDIwd33DIwd33EEEEEEEEEEEzRXh''''<===y1}}9:::|TY88884888DD$''E!'400H4j\FtXu55  c4
  	5 	5JGQadD11FB#r2 '#r24 5 5 5 5<<r)   c                    d |                                  D             }|r"t          d                    |                    d |                                  D             }|r"t          d                    |                    d |                                 D             }t	          t          j        |                    dk    rt          d          t          d |                                  D                       S )	Nc                     g | ]	\  }}||
S Nr3   r4   r5   vs      r(   r=   z_fix_coeffs.<locals>.<listcomp>   s    >>>$!QAIAIIIr)   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                 X    g | ]'\  }}t          |          t          d           k    %|(S )r   )setrT   s      r(   r=   z_fix_coeffs.<locals>.<listcomp>   s<     - - -$!QA#d))++ +++r)   zOThe following invalid keys were found in the detail coefficient dictionary: {}.c                 ,    g | ]}t          |          S r3   r   )r4   r5   s     r(   r=   z_fix_coeffs.<locals>.<listcomp>   s    111a3q66111r)   r   z4All detail coefficient names must have equal length.c              3   H   K   | ]\  }}|t          j        |          fV  d S rS   )r   r   rT   s      r(   r8   z_fix_coeffs.<locals>.<genexpr>   s3      >>tq!BJqMM">>>>>>r)   )itemsr    formatrB   r   r   r"   rA   )r+   missing_keysinvalid_keyskey_lengthss       r(   _fix_coeffsr`      s   >>&,,..>>>L ;
 <B6< <  	- -&,,.. - - -L @**0&*>*>@ @ 	@ 216;;==111K
29[!!""Q&&BD D 	D >>v||~~>>>>>>r)   c                    t          d |                                 D                       } t          d |                                 D                       } t          |           } t          st	          d |                                 D                       r~t          d |                                 D                       }t          d |                                 D                       }t          ||||          dt          ||||          z  z   S t          d |                                 D                       	 fd|                                 D             }t          |          n# t          $ r t          d	          w xY wt	          fd
|D                       rt          d          |t                    }nt                    fd|D             }t          ||          }t          ||          }t!          t#          t%          t'          |||                                        D ]\  }	\  }
}}|
dk     s|
k    rt)          j        d          i }d t-          d|	          D             }|D ]}|                     |dz   d          }|                     |dz   d          }|w|u|j        |j        k    re|j        j        dk    s|j        j        dk    rt(          j        }nt(          j        }t)          j        ||          }t)          j        ||          }t;          |||||
          ||<   |} | 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              3   (   K   | ]\  }}|||fV  d S rS   r3   rT   s      r(   r8   zidwtn.<locals>.<genexpr>   *      EETQq}1a&}}}}EEr)   c              3   (   K   | ]\  }}|||fV  d S rS   r3   rT   s      r(   r8   zidwtn.<locals>.<genexpr>  rc   r)   c              3   >   K   | ]}t          j        |          V  d S rS   )r   r@   )r4   rU   s     r(   r8   zidwtn.<locals>.<genexpr>  s,      <<q""<<<<<<r)   c              3   .   K   | ]\  }}||j         fV  d S rS   )r7   rT   s      r(   r8   zidwtn.<locals>.<genexpr>  ,      BB41aAqv;BBBBBBr)   c              3   .   K   | ]\  }}||j         fV  d S rS   )r6   rT   s      r(   r8   zidwtn.<locals>.<genexpr>	  rg   r)   r2   c              3   4   K   | ]}t          |          V  d S rS   rY   )r4   keys     r(   r8   zidwtn.<locals>.<genexpr>  s(      ;;cS;;;;;;r)   c              3   V   K   | ]#\  }}|t          |          k    |j        V  $d S rS   )r   shape)r4   r5   rU   ndim_transforms      r(   r8   zidwtn.<locals>.<genexpr>  sM       G GDAq=SVV~-E-E -E-E-E-EG Gr)   z8`coeffs` must contain at least one non-null wavelet bandc              3   $   K   | ]
}|k    V  d S rS   r3   )r4   scoeff_shapes     r(   r8   zidwtn.<locals>.<genexpr>  s(      
2
21
2
2
2
2
2
2r)   z,`coeffs` must all be of equal size (or None)Nc                 *    g | ]}|d k     r|z   n|S r;   r3   )r4   r<   r!   s     r(   r=   zidwtn.<locals>.<listcomp>  s)    333AAHH1333r)   r   z!Axis greater than data dimensionsc                 8    g | ]}d                      |          S )r>   )join)r4   coefs     r(   r=   zidwtn.<locals>.<listcomp>)  s"    OOOdBGGDMMOOOr)   r   )repeatr<   r?   c)rC   r>   )rA   r[   r`   r   anyvaluesr   maxrB   nextStopIterationr    rE   r   r   r   reversedlist	enumeraterF   r   	AxisErrorr   getrC   kind
complex128float64r   r
   )r+   r$   r%   r&   real_coeffsimag_coeffscoeff_shapesrH   rI   
key_lengthrJ   rK   rL   new_keysrj   LHrC   rp   r!   rm   s                     @@@r(   r   r      s   @ EEV\\^^EEEEEF EEV\\^^EEEEEF   F ><<FMMOO<<<<<>BB6<<>>BBBBBBB6<<>>BBBBBk7D$77U;t<<<= 	> ;;V[[]];;;;;N!G G G GFLLNN G G G<(( ! ! !   ! ! 	!! 
2
2
2
2\
2
2
222 IGHHH|^$$;3333d333DD$''E!'400H)13tXu556677*9 *9  %
%T3!88tt||,BCCC
OOgd:.N.N.NOOO 	? 	?C

39d++A

39d++A}7ag%%w|s**aglc.A.A " "

1E222A
1E222A'1c4>>JsOO":s   /E5 5F)r   r   )r   N)__doc__
__future__r   r   r   	itertoolsr   numpyr   _c99_configr   _extensions._dwtr	   r
   _utilsr   r   __all__r   r   r   r`   r   r3   r)   r(   <module>r      s    A @ @ @ @ @ @ @ @ @           * * * * * * 1 1 1 1 1 1 1 1 7 7 7 7 7 7 7 7 -
,
,2@ 2@ 2@ 2@j). ). ). ).XG G G GT? ? ?6\ \ \ \ \ \r)   