
    bMh,                     |    d 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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.
    )productN   )_have_c99_complex)dwt_axis	idwt_axis)	AxisError_modes_per_axis_wavelets_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        N/var/www/html/test/jupyter/venv/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   K   sc    F BR;;D
4yyA~~*+++b"55F$---r'   c                     t          j                    t          sQt          j                   r=t	           j        |||          t	           j        |||          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                 :    i | ]}||         d |         z  z   S )              ? ).0kimagreals     r&   
<dictcomp>zdwtn.<locals>.<dictcomp>   s,    888a47R$q'\)888r'   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   )r2   ar!   s     r&   
<listcomp>zdwtn.<locals>.<listcomp>   s+    888aQUUA	MM888r'    r:   d)r   r   r   iscomplexobjr   r5   r4   dtype	TypeErrorr   r   ranger	   r
   zipr   extenddict)r!   r"   r#   r$   modeswaveletsr)   axiswav
new_coeffssubbandxcAcDr4   r5   s   `             @@r&   r   r   w   s   Z :dD 9!6!6 9DIwd33DIwd338888848888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 |                                  D             }|rt          d| d          d | D             }t          t          j        |                    dk    rt          d	          d
 |                                  D             S )Nc                     g | ]	\  }}||
S Nr1   r2   r3   vs      r&   r;   z_fix_coeffs.<locals>.<listcomp>   s    >>>$!QAIAIIIr'   z4The following detail coefficients were set to None:
zr
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   )setrQ   s      r&   r;   z_fix_coeffs.<locals>.<listcomp>   s<     - - -$!QA#d))++ +++r'   zLThe following invalid keys were found in the detail coefficient dictionary: .c                 ,    g | ]}t          |          S r1   r   )r2   r3   s     r&   r;   z_fix_coeffs.<locals>.<listcomp>   s    ***a3q66***r'   r   z4All detail coefficient names must have equal length.c                 >    i | ]\  }}|t          j        |          S r1   )r   r   rQ   s      r&   r6   z_fix_coeffs.<locals>.<dictcomp>   s&    888AArz!}}888r'   )itemsr   r   r   r    )r)   missing_keysinvalid_keyskey_lengthss       r&   _fix_coeffsr]      s   >>&,,..>>>L <;; ; ;< < 	<- -&,,.. - - -L 87'37 7 78 8 	8 +*6***K
29[!!""Q&&BD D 	D 988888r'   c                 &   d |                                  D             } d |                                  D             } t          |           } t          st          d |                                 D                       rdd |                                  D             }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%          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        ||          }t7          |||||
          ||<   |} | 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                     i | ]
\  }}|||S rP   r1   rQ   s      r&   r6   zidwtn.<locals>.<dictcomp>       ???tq!ar'   c                     i | ]
\  }}|||S rP   r1   rQ   s      r&   r6   zidwtn.<locals>.<dictcomp>   r`   r'   c              3   >   K   | ]}t          j        |          V  d S rP   )r   r>   )r2   rR   s     r&   	<genexpr>zidwtn.<locals>.<genexpr>  s,      <<q""<<<<<<r'   c                 $    i | ]\  }}||j         S r1   )r5   rQ   s      r&   r6   zidwtn.<locals>.<dictcomp>       <<<TQq!&<<<r'   c                 $    i | ]\  }}||j         S r1   )r4   rQ   s      r&   r6   zidwtn.<locals>.<dictcomp>  re   r'   r0   c              3   4   K   | ]}t          |          V  d S rP   rW   )r2   keys     r&   rc   zidwtn.<locals>.<genexpr>  s(      44cS444444r'   c              3   V   K   | ]#\  }}|t          |          k    |j        V  $d S rP   )r   shape)r2   r3   rR   ndim_transforms      r&   rc   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 rP   r1   )r2   scoeff_shapes     r&   rc   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 r9   r1   )r2   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)r2   coefs     r&   r;   zidwtn.<locals>.<listcomp>&  s"    OOOdBGGDMMOOOr'   r   )repeatr:   r=   c)r?   r<   )rY   r]   r   anyvaluesr   maxnextStopIterationr   rA   r   r	   r
   reversedlist	enumeraterB   r   r   getr?   kindr   
complex128float64r   r   )r)   r"   r#   r$   real_coeffsimag_coeffscoeff_shapesrE   rF   
key_lengthrG   rH   rI   new_keysrh   LHr?   rn   r   rk   s                     @@@r&   r   r      st   @ @?v||~~???F @?v||~~???F   F ><<FMMOO<<<<<><<V\\^^<<<<<V\\^^<<<k7D$77U;t<<<= 	> 44V44444N!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||?@@@
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   ?/D/ /E	)r   r   )r   N)__doc__	itertoolsr   numpyr   _c99_configr   _extensions._dwtr   r   _utilsr   r	   r
   __all__r   r   r   r]   r   r1   r'   r&   <module>r      s    
           * * * * * * 1 1 1 1 1 1 1 1 B B B B B B B B B B
,
,
,2@ 2@ 2@ 2@j). ). ). ).XG G G GT9 9 94\ \ \ \ \ \r'   