
    bMh                     d   d Z ddlZddlZddlmZ ddlmZ ddlZddlm	Z	m
Z
mZ ddlmZ ddlmZmZ dd	lmZmZmZmZmZ dd
lmZmZmZmZ g dZd Zd)dZd*dZd+dZ d,dZ!d Z"d-dZ#d Z$d.dZ%d Z&d Z'd Z(d Z)d/dZ*d0dZ+d-dZ,d  Z-d1d!Z.d1d"Z/d0d#Z0d$ Z1 G d% d&          Z2d-d'Z3d( Z4dS )2zY
Multilevel 1D and 2D Discrete Wavelet Transform
and Inverse Discrete Wavelet Transform.
    N)copy)product   )dwtdwt_coeff_lenidwtdwt_max_level)ModesWavelet)_fix_coeffsdwt2dwtnidwt2idwtn)	AxisError_as_wavelet_modes_per_axis_wavelets_per_axis)wavedecwaverecwavedec2waverec2wavedecnwaverecncoeffs_to_arrayarray_to_coeffsravel_coeffsunravel_coeffsdwtn_max_levelwavedecn_sizewavedecn_shapes
fswavedecn
fswaverecnFswavedecnResultc                 0   t          j        |           r| f} t          j        |          r|f}t          j        d t          | |          D                       }||}n6|dk     rt	          d|z            ||k    rt          j        d| d           |S )Nc                 4    g | ]\  }}t          ||          S  r	   ).0sds      P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pywt/_multilevel.py
<listcomp>z _check_level.<locals>.<listcomp>$   s&    MMM1a++MMM    r   z2Level value of %d is too low . Minimum level is 0.zLevel value of z@ is too high: all coefficients will experience boundary effects.)npisscalarminzip
ValueErrorwarningswarn)sizesdec_lenslevel	max_levels       r,   _check_levelr:      s    	{5 		{8  <MME88L8LMMMNNI}	@5HJ J 	J			!e ! ! !	" 	" 	" Lr.   	symmetricc                    t          j        |           } t          |          }	 | j        |         }n# t          $ r t          d          w xY wt          ||j        |          }g }| }t          |          D ],}t          ||||          \  }}	|
                    |	           -|
                    |           |                                 |S )a  
    Multilevel 1D Discrete Wavelet Transform of data.

    Parameters
    ----------
    data: array_like
        Input data
    wavelet : Wavelet object or name string
        Wavelet to use
    mode : str, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`.
    level : int, optional
        Decomposition level (must be >= 0). If level is None (default) then it
        will be calculated using the ``dwt_max_level`` function.
    axis: int, optional
        Axis over which to compute the DWT. If not given, the
        last axis is used.

    Returns
    -------
    [cA_n, cD_n, cD_n-1, ..., cD2, cD1] : list
        Ordered list of coefficients arrays
        where ``n`` denotes the level of decomposition. The first element
        (``cA_n``) of the result is approximation coefficients array and the
        following elements (``cD_n`` - ``cD_1``) are details coefficients
        arrays.

    Examples
    --------
    >>> from pywt import wavedec
    >>> coeffs = wavedec([1,2,3,4,5,6,7,8], 'db1', level=2)
    >>> cA2, cD2, cD1 = coeffs
    >>> cD1
    array([-0.70710678, -0.70710678, -0.70710678, -0.70710678])
    >>> cD2
    array([-2., -2.])
    >>> cA2
    array([  5.,  13.])

    !Axis greater than data dimensions)r/   asarrayr   shape
IndexErrorr   r:   dec_lenranger   appendreverse)
datawaveletmoder8   axis
axes_shapecoeffs_listair+   s
             r,   r   r   1   s    R :dD'""G=Z%

 = = =;<<<=W_e<<EKA5\\  1gtT**11qs	   3 Ac                    t          | t          t          f          st          d          t	          |           dk     rt          d          t	          |           dk    r| d         S | d         | dd         }}|D ]}|:t          |t
          j                  s t          dt          |           d          ||	 |j        |         |j        |         dz   k    r%|t          d |j        D                                }n+|j        |         |j        |         k    rt          d	          n# t          $ r t          d
          w xY wt          |||||          }|S )a2  
    Multilevel 1D Inverse Discrete Wavelet Transform.

    Parameters
    ----------
    coeffs : array_like
        Coefficients list [cAn, cDn, cDn-1, ..., cD2, cD1]
    wavelet : Wavelet object or name string
        Wavelet to use
    mode : str, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`.
    axis: int, optional
        Axis over which to compute the inverse DWT. If not given, the
        last axis is used.

    Notes
    -----
    It may sometimes be desired to run ``waverec`` with some sets of
    coefficients omitted.  This can best be done by setting the corresponding
    arrays to zero arrays of matching shape and dtype.  Explicitly removing
    list entries or setting them to None is not supported.

    Specifically, to ignore detail coefficients at level 2, one could do::

        coeffs[-2] = np.zeros_like(coeffs[-2])

    Examples
    --------
    >>> import pywt
    >>> coeffs = pywt.wavedec([1,2,3,4,5,6,7,8], 'db1', level=2)
    >>> pywt.waverec(coeffs, 'db1')
    array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.])
    (Expected sequence of coefficient arrays.r   z7Coefficient list too short (minimum 1 arrays required).r   N$Unexpected detail coefficient type: z. Detail coefficients must be arrays as returned by wavedec. If you are using pywt.array_to_coeffs or pywt.unravel_coeffs, please specify output_format='wavedec'c              3   4   K   | ]}t          |          V  d S Nslicer)   r*   s     r,   	<genexpr>zwaverec.<locals>.<genexpr>   s(      ::Qa::::::r.   zcoefficient shape mismatchz(Axis greater than coefficient dimensions)
isinstancelisttupler3   lenr/   ndarraytyper@   rA   r   r   )coeffsrG   rH   rI   rL   dsr+   s          r,   r   r   p   s   F ftUm,, ECDDD
6{{QEG G 	G	V		ay1IvabbzrA , ,=Arz!:!:=*tAww * * *+ + +
 ML74=AGDMA$555%::!':::::;AAWT]agdm33$%ABBB L L L JKKKLAwd++Hs    A/D00E
r<   c                     t          j                     j        dk     rt          d          t	          |          }t          |          dk    rt          d          t          |          t          t          |                    k    rt          d          	  fd|D             }n# t          $ r t          d          w xY wt          ||          }d |D             }t          |||          }g } }	t          |          D ],}
t          |	|||          \  }	}|                    |           -|                    |	           |                                 |S )a}  
    Multilevel 2D Discrete Wavelet Transform.

    Parameters
    ----------
    data : ndarray
        2D 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 str, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`. This can
        also be a tuple containing a mode to apply along each axis in ``axes``.
    level : int, optional
        Decomposition level (must be >= 0). If level is None (default) then it
        will be calculated using the ``dwt_max_level`` function.
    axes : 2-tuple of ints, optional
        Axes over which to compute the DWT. Repeated elements are not allowed.

    Returns
    -------
    [cAn, (cHn, cVn, cDn), ... (cH1, cV1, cD1)] : list
        Coefficients list.  For user-specified ``axes``, ``cH*``
        corresponds to ``axes[0]`` while ``cV*`` corresponds to ``axes[1]``.
        The first element returned is the approximation coefficients for the
        nth level of decomposition.  Remaining elements are tuples of detail
        coefficients in descending order of decomposition level.
        (i.e. ``cH1`` are the horizontal detail coefficients at the first
        level)

    Examples
    --------
    >>> import pywt
    >>> import numpy as np
    >>> coeffs = pywt.wavedec2(np.ones((4,4)), 'db1')
    >>> # Levels:
    >>> len(coeffs)-1
    2
    >>> pywt.waverec2(coeffs, 'db1')
    array([[ 1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.]])
       z2Expected input data to have at least 2 dimensions.zExpected 2 axesz+The axes passed to wavedec2 must be unique.c                 *    g | ]}j         |         S r(   r@   r)   axrF   s     r,   r-   zwavedec2.<locals>.<listcomp>   s    444djn444r.   r>   c                     g | ]	}|j         
S r(   rB   r)   ws     r,   r-   zwavedec2.<locals>.<listcomp>       ///19///r.   )r/   r?   ndimr3   rY   rZ   setrA   r   r   r:   rC   r   rD   rE   )rF   rG   rH   r8   axes
axes_sizeswaveletsdec_lengthsrK   rL   rM   r^   s   `           r,   r   r      s|   Z :dDy1}}MNNN;;D
4yyA~~*+++
4yyCD		NN""FGGG=4444t444

 = = =;<<<= "'400H//h///K[%88EKA5\\  Qt,,22qs   B- -Cc                   	 t          | t          t          f          st          d          t	          |          t	          t          |                    k    rt          d          t	          |           dk     rt          d          t	          |           dk    r| d         S | d         | dd         }}t          j        |          }|D ]&}t          |t          t          f          rt	          |          dk    r t          dt          |           d	          t          d
 |D                       }d |D             }	 t          |          	t          	fd|D                       st          d          t          d t          |j        	          D                       }n.# t          $ r! t          d          t          d          f}Y nw xY wt          ||         |f|||          }(|S )a  
    Multilevel 2D Inverse Discrete Wavelet Transform.

    coeffs : list or tuple
        Coefficients list [cAn, (cHn, cVn, cDn), ... (cH1, cV1, cD1)]
    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 str, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`. This can
        also be a tuple containing a mode to apply along each axis in ``axes``.
    axes : 2-tuple of ints, optional
        Axes over which to compute the IDWT. Repeated elements are not allowed.

    Returns
    -------
    2D array of reconstructed data.

    Notes
    -----
    It may sometimes be desired to run ``waverec2`` with some sets of
    coefficients omitted.  This can best be done by setting the corresponding
    arrays to zero arrays of matching shape and dtype.  Explicitly removing
    list or tuple entries or setting them to None is not supported.

    Specifically, to ignore all detail coefficients at level 2, one could do::

        coeffs[-2] == tuple([np.zeros_like(v) for v in coeffs[-2]])

    Examples
    --------
    >>> import pywt
    >>> import numpy as np
    >>> coeffs = pywt.wavedec2(np.ones((4,4)), 'db1')
    >>> # Levels:
    >>> len(coeffs)-1
    2
    >>> pywt.waverec2(coeffs, 'db1')
    array([[ 1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.]])
    rO   z+The axes passed to waverec2 must be unique.r   6Coefficient list too short (minimum 1 array required).r   N   rP   z. Detail coefficients must be a 3-tuple of arrays as returned by wavedec2. If you are using pywt.array_to_coeffs or pywt.unravel_coeffs, please specify output_format='wavedec2'c              3   F   K   | ]}|t          j        |          nd V  d S rR   )r/   r?   r)   coeffs     r,   rV   zwaverec2.<locals>.<genexpr>C  sJ       " " (-'8"*U###d " " " " " "r.   c              3   (   K   | ]}||j         V  d S rR   rd   rv   s     r,   rV   zwaverec2.<locals>.<genexpr>E  s)      DDE%2CEK2C2C2C2CDDr.   c              3   $   K   | ]
}|k    V  d S rR   r(   )r)   r*   d_shapes     r,   rV   zwaverec2.<locals>.<genexpr>K  s'      66qG|666666r.   z*All detail shapes must be the same length.c              3   R   K   | ]"\  }}t          d ||dz   k    rdnd           V  #d S )Nr   r<   rS   )r)   a_lend_lens      r,   rV   zwaverec2.<locals>.<genexpr>M  s^       D D)UE t5EAI+=+=RR4HH D D D D D Dr.   )rW   rX   rY   r3   rZ   rm   r/   r?   r\   nextallr2   r@   StopIterationrT   r   )
r]   rG   rH   rn   rL   r^   r+   d_shapesidxsrz   s
            @r,   r   r      s@   X ftUm,, ECDDD
4yyCD		NN""FGGG
6{{QDF F 	F	V		ay1IvabbzrA

1A 5 5!dE]++ 	;s1vv{{:tAww : : :; ; ;
  " " " " " " "DDQDDD	D8nnG 6666X66666 O !MNNN D D-0'-B-BD D D D DDD  	, 	, 	,;;d+DDD	, 1T7A,t44Hs   F,,(GGc                     t                     dk     rt          d          t                     }t          j        |          r|f}|t	          |          }nt          |          }t          |          t          t          |                    k    rt          d          	  fd|D             }n# t          $ r t          d          w xY wt          |          }|||fS )Nr   z Expected at least 1D input data.z+The axes passed to wavedecn must be unique.c                      g | ]
}|         S r(   r(   )r)   rf   r@   s     r,   r-   z'_prep_axes_wavedecn.<locals>.<listcomp>a  s    000RuRy000r.   r>   )	rZ   r3   r/   r0   rC   rY   rm   rA   r   )r@   rn   rl   axes_shapesndim_transforms   `    r,   _prep_axes_wavedecnr   T  s    
5zzA~~;<<<u::D	{4 x|T{{T{{
4yyCD		NN""FGGG=00004000 = = =;<<<=YYNn,,s   (B7 7Cc                    t          j        |           } t          | j        |          \  }}}t	          ||          }d |D             }t          |||          }g }	| }
t          |          D ]A}t          |
|||          }|                    d|z            }
|		                    |           B|		                    |
           |	
                                 |	S )a	  
    Multilevel nD Discrete Wavelet Transform.

    Parameters
    ----------
    data : ndarray
        nD 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 str, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`. This can
        also be a tuple containing a mode to apply along each axis in ``axes``.
    level : int, optional
        Decomposition level (must be >= 0). If level is None (default) then it
        will be calculated using the ``dwt_max_level`` function.
    axes : sequence of ints, optional
        Axes over which to compute the DWT. Axes may not be repeated. The
        default is None, which means transform all axes
        (``axes = range(data.ndim)``).

    Returns
    -------
    [cAn, {details_level_n}, ... {details_level_1}] : list
        Coefficients list.  Coefficients are listed in descending order of
        decomposition level.  ``cAn`` are the approximation coefficients at
        level ``n``.  Each ``details_level_i`` element is a dictionary
        containing detail coefficients at level ``i`` of the decomposition. As
        a concrete example, a 3D decomposition would have the following set of
        keys in each ``details_level_i`` dictionary::

            {'aad', 'ada', 'daa', 'add', 'dad', 'dda', 'ddd'}

        where the order of the characters in each key map to the specified
        ``axes``.

    Examples
    --------
    >>> import numpy as np
    >>> from pywt import wavedecn, waverecn
    >>> coeffs = wavedecn(np.ones((4, 4, 4)), 'db1')
    >>> # Levels:
    >>> len(coeffs)-1
    2
    >>> waverecn(coeffs, 'db1')
    array([[[ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.]],
           [[ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.]],
           [[ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.]],
           [[ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.]]])

    c                     g | ]	}|j         
S r(   rh   ri   s     r,   r-   zwavedecn.<locals>.<listcomp>  rk   r.   rL   )r/   r?   r   r@   r   r:   rC   r   poprD   rE   )rF   rG   rH   r8   rn   r   r   rp   rq   rK   rL   rM   r]   s                r,   r   r   h  s    @ :dD(;DJ(M(M%D+~!'400H//h///Kk599EKA5\\ # #a$--JJs^+,,6""""qr.   c                 V   | d S |s| S |t          t          |                             }t          j        | j        |j                  }t          j        |dk     |dk    z            rt          |           t          d          | t          d |j        D                                S )Nr   r   z$incompatible coefficient array sizesc              3   4   K   | ]}t          |          V  d S rR   rS   rU   s     r,   rV   z$_match_coeff_dims.<locals>.<genexpr>  s(      99aq999999r.   )	r~   iterr/   subtractr@   anyprintr3   rY   )a_coeffd_coeff_dictd_coeff
size_diffss       r,   _match_coeff_dimsr     s     t 4\ 2 2334GW]GM::J	vzA~*q.122 Aj?@@@5997=99999::r.   c                 r   t          |           dk     rt          d          | d         | dd         }}t          |          dk    r?t          d |D                       s&t          dt          |d                    d          t	          t          t          |                    }|s| d         S |t          |          st          d          g }|.t          j	        |          }|
                    |j                   |D ]#}|d	 |                                D             z  }$t          j        |          }t          |          dk    r	|d         }	nt          d
          t          j        |          r|f}|t          |	          }nt!          |          }t          |          t          t#          |                    k    rt          d          t          |          }
t%          |          D ]:\  }}||s
|dk    rt'          ||          }||d|
z  <   t)          ||||          };|S )a  
    Multilevel nD Inverse Discrete Wavelet Transform.

    coeffs : array_like
        Coefficients list [cAn, {details_level_n}, ... {details_level_1}]
    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 str, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`. This can
        also be a tuple containing a mode to apply along each axis in ``axes``.
    axes : sequence of ints, optional
        Axes over which to compute the IDWT.  Axes may not be repeated.

    Returns
    -------
    nD array of reconstructed data.

    Notes
    -----
    It may sometimes be desired to run ``waverecn`` with some sets of
    coefficients omitted.  This can best be done by setting the corresponding
    arrays to zero arrays of matching shape and dtype.  Explicitly removing
    list or dictionary entries or setting them to None is not supported.

    Specifically, to ignore all detail coefficients at level 2, one could do::

        coeffs[-2] = {k: np.zeros_like(v) for k, v in coeffs[-2].items()}

    Examples
    --------
    >>> import numpy as np
    >>> from pywt import wavedecn, waverecn
    >>> coeffs = wavedecn(np.ones((4, 4, 4)), 'db1')
    >>> # Levels:
    >>> len(coeffs)-1
    2
    >>> waverecn(coeffs, 'db1')
    array([[[ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.]],
           [[ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.]],
           [[ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.]],
           [[ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.],
            [ 1.,  1.,  1.,  1.]]])

    r   rs   r   Nc              3   @   K   | ]}t          |t                    V  d S rR   )rW   dictr)   r+   s     r,   rV   zwaverecn.<locals>.<genexpr>  s,      ??qz!T22??????r.   rP   z. Detail coefficients must be a dictionary of arrays as returned by wavedecn. If you are using pywt.array_to_coeffs or pywt.unravel_coeffs, please specify output_format='wavedecn'z4At least one coefficient must contain a valid value.c                 "    g | ]\  }}|j         S r(   )rl   )r)   kvs      r,   r-   zwaverecn.<locals>.<listcomp>#  s    55541a555r.   z:All coefficients must have a matching number of dimensionsz+The axes passed to waverecn must be unique.rL   )rZ   r3   r   r\   rX   mapr   r   r/   r?   rD   rl   itemsuniquer0   rC   rY   rm   	enumerater   r   )r]   rG   rH   rn   rL   r^   coeff_ndimsr+   unique_coeff_ndimsrl   r   idxs               r,   r   r     su   r 6{{QDF F 	F 1IvabbzrA 2ww{{3??B?????{641;; 6 6 67 7 	7 
c+r""	#	#B ayyRyBD D 	D K}JqMM16""" 6 655177995555 ;//
!##!!$HJ J 	J 
{4 x|T{{T{{
4yyCD		NN""FGGGYYNB-- 	* 	*Q9Q9 77!!Q''A"##
!WdD))Hr.   c                     t          |           dk    r| S t          |           } t          dt          |                     D ]8}| |         | |         j        dk    rt	          d          d| |         i| |<   9| S )z4Convert wavedec coefficients to the wavedecn format.r   r   Nzexpected a 1D coefficient arrayr+   )rZ   r   rC   rl   r3   )r]   ns     r,   _coeffs_wavedec_to_wavedecnr   E  s    
6{{a&\\F1c&kk"" % %!9!9>Q>???&)$q		Mr.   c                 v   t          |           dk    r| S t          |           } t          dt          |                     D ]v}t          | |         t          t
          f          rt          | |                   dk    rt          d          | |         \  }}}|||t          d          |||d| |<   w| S )z5Convert wavedec2 coefficients to the wavedecn format.r   r   rt   z)expected a 3-tuple of detail coefficientsNz\Expected numpy arrays of detail coefficients. Setting coefficients to None is not supported.)addadd)rZ   r   rC   rW   rY   rX   r3   )r]   r   r   r   r   s        r,   _coeffs_wavedec2_to_wavedecnr   S  s    
6{{a&\\F1c&kk"" 3 3&)eT]33 	Js6!9~~7J7JHIIIayR:rz9: : : Rr22q		Mr.   c           	         t          j        | d         j                  }t          j        |          }t          |          }| d         j        }| dd          D ]\}||xx         t          j        |d|z           j                  |         z  cc<   |                                D ]\  }}||j        z  }]t          |                                          }t          j        |          |k    }||fS )Nr   r   r+   )	r/   r?   r@   rZ   sizer   rY   tolistprod)	r]   rn   	arr_shaper   ncoeffsr+   r   r   is_tight_packings	            r,   _determine_coeff_array_shaper   d  s    
6!9?++I:dDYYNQinGABBZ  $2:aN(:&;&ABB4HHGGII 	 	DAqqvGG	i&&(())I 	**g5&&&r.   c                    t          | t                    rt          |           dk    rt          d          | d         t          d          t          | d         t          j                  st          d          | d         j        }t          |           dk    rt          | d         t                    rnqt          | d         t          j                  rt          |           } nAt          | d         t          t          f          rt          |           } nt          d          t          |           dk    r| ||dfS t          t          | d                                                   d                   }|)||k     rt          d          t	          j        |          }t          |          |k    rt          d	          | |||fS )
zwHelper function to check type of coeffs and axes.

    This code is used by both coeffs_to_array and ravel_coeffs.
    r   z2input must be a list of coefficients from wavedecnN6coeffs_to_array does not support missing coefficients.z(first list element must be a numpy arrayr   zinvalid coefficient listzlcoeffs corresponds to a DWT performed over only a subset of the axes.  In this case, axes must be specified.zFThe length of axes doesn't match the number of dimensions transformed.)rW   rX   rZ   r3   r/   r[   rl   r   r   rY   r   keysarange)r]   rn   rl   r   s       r,   _prepare_coeffs_axesr   t  s   
 fd## Os6{{a'7'7MNNNay ) * * 	*fQi,, ECDDD!9>D
6{{QfQi&& 	9q	2:.. 	9088FFq	E4=11 	91&99FF7888
6{{atT4'' fQinn..//233N|D  CD D D y
4yyN""  	 4~--r.   c           	         t          | |          \  } }}}| d         }|j        t          |           dk    r#|t          t	          d          g|z            gfS t          | |          \  }}|-|st          d          t          j        ||j	                  }nt          j
        |||j	                  }t          d D                       }	|||	<   g }
|
                    |	           | dd         }|D ]H}|
                    i            t          j        d |                                D                       rt          d          |d	|z           j        |D ]}||         }t	          d          g|z  }t          |          D ]v\  }}||         }|d
k    rt	          |j        |                   ||<   1|d	k    r.t	          |         |         |j        |         z             ||<   et          d|           t          |          }|||<   ||
d         |<   Èfdt          |          D             J||
fS )aC  
    Arrange a wavelet coefficient list from ``wavedecn`` into a single array.

    Parameters
    ----------

    coeffs : array-like
        Dictionary of wavelet coefficients as returned by pywt.wavedecn
    padding : float or None, optional
        The value to use for the background if the coefficients cannot be
        tightly packed. If None, raise an error if the coefficients cannot be
        tightly packed.
    axes : sequence of ints, optional
        Axes over which the DWT that created ``coeffs`` was performed.  The
        default value of None corresponds to all axes.

    Returns
    -------
    coeff_arr : array-like
        Wavelet transform coefficient array.
    coeff_slices : list
        List of slices corresponding to each coefficient.  As a 2D example,
        ``coeff_arr[coeff_slices[1]['dd']]`` would extract the first level
        detail coefficients from ``coeff_arr``.

    See Also
    --------
    array_to_coeffs : the inverse of coeffs_to_array

    Notes
    -----
    Assume a 2D coefficient dictionary, c, from a two-level transform.

    Then all 2D coefficients will be stacked into a single larger 2D array
    as follows::

        +---------------+---------------+-------------------------------+
        |               |               |                               |
        |     c[0]      |  c[1]['da']   |                               |
        |               |               |                               |
        +---------------+---------------+           c[2]['da']          |
        |               |               |                               |
        | c[1]['ad']    |  c[1]['dd']   |                               |
        |               |               |                               |
        +---------------+---------------+ ------------------------------+
        |                               |                               |
        |                               |                               |
        |                               |                               |
        |          c[2]['ad']           |           c[2]['dd']          |
        |                               |                               |
        |                               |                               |
        |                               |                               |
        +-------------------------------+-------------------------------+

    If the transform was not performed with mode "periodization" or the signal
    length was not a multiple of ``2**level``, coefficients at each subsequent
    scale will not be exactly 1/2 the size of those at the previous level due
    to additional coefficients retained to handle the boundary condition. In
    these cases, the default setting of `padding=0` indicates to pad the
    individual coefficient arrays with 0 as needed so that they can be stacked
    into a single, contiguous array.

    Examples
    --------
    >>> import pywt
    >>> cam = pywt.data.camera()
    >>> coeffs = pywt.wavedecn(cam, wavelet='db2', level=3)
    >>> arr, coeff_slices = pywt.coeffs_to_array(coeffs)

    r   r   Nz+array coefficients cannot be tightly packeddtypec                 ,    g | ]}t          |          S r(   rS   rU   s     r,   r-   z#coeffs_to_array.<locals>.<listcomp>  s    0001eAhh000r.   c                     g | ]}|d u S rR   r(   r   s     r,   r-   z#coeffs_to_array.<locals>.<listcomp>
      :::19:::r.   r   r+   rL   zunexpected letter: r<   c                 2    g | ]}|         |         z   S r(   r(   )r)   r   a_shaperz   s     r,   r-   z#coeffs_to_array.<locals>.<listcomp>  s&    @@@q71:
*@@@r.   )r   r@   rZ   rY   rT   r   r3   r/   emptyr   fullrD   r   valuesr   rC   )r]   paddingrn   rl   r   a_coeffsr   r   	coeff_arra_slicescoeff_slicesr^   
coeff_dictkeyr+   slice_arrayrM   letax_ir   rz   s                      @@r,   r   r     s   P *>fd)K)K&FD$ ayHnG
6{{a%t 455666 #?vt"L"LI  	LJKKKHYhn===		GIwhnEEE	0000011H"Ih L!!! 
B A A
B6::j&7&7&9&9:::;; 	. - . . .S>128 	0 	0C3A ;;/D0K#C.. B B3Aw#::(-agdm(<(<K%%CZZ(-gdm.5dmagdm.K)M )MK%% %%@3%@%@AAA,,K%&Ik"$/LS!!@@@@@E$KK@@@l""r.   r   c                 h   t          j        |           } g }t          |          dk    rt          d          |                    | |d                             t          dt          |                    D ]}|dk    r| ||         d                  }n|dk    r<| ||         d                  | ||         d                  | ||         d	                  f}nF|d
k    r.i }||                                         D ]\  }}| |         ||<   nt          d|           |                    |           |S )ai
  
    Convert a combined array of coefficients back to a list compatible with
    ``waverecn``.

    Parameters
    ----------

    arr : array-like
        An array containing all wavelet coefficients.  This should have been
        generated via ``coeffs_to_array``.
    coeff_slices : list of tuples
        List of slices corresponding to each coefficient as obtained from
        ``array_to_coeffs``.
    output_format : {'wavedec', 'wavedec2', 'wavedecn'}
        Make the form of the coefficients compatible with this type of
        multilevel transform.

    Returns
    -------
    coeffs: array-like
        Wavelet transform coefficient array.

    See Also
    --------
    coeffs_to_array : the inverse of array_to_coeffs

    Notes
    -----
    A single large array containing all coefficients will have subsets stored,
    into a ``waverecn`` list, c, as indicated below::

        +---------------+---------------+-------------------------------+
        |               |               |                               |
        |     c[0]      |  c[1]['da']   |                               |
        |               |               |                               |
        +---------------+---------------+           c[2]['da']          |
        |               |               |                               |
        | c[1]['ad']    |  c[1]['dd']   |                               |
        |               |               |                               |
        +---------------+---------------+ ------------------------------+
        |                               |                               |
        |                               |                               |
        |                               |                               |
        |          c[2]['ad']           |           c[2]['dd']          |
        |                               |                               |
        |                               |                               |
        |                               |                               |
        +-------------------------------+-------------------------------+

    Examples
    --------
    >>> import pywt
    >>> from numpy.testing import assert_array_almost_equal
    >>> cam = pywt.data.camera()
    >>> coeffs = pywt.wavedecn(cam, wavelet='db2', level=3)
    >>> arr, coeff_slices = pywt.coeffs_to_array(coeffs)
    >>> coeffs_from_arr = pywt.array_to_coeffs(arr, coeff_slices,
    ...                                        output_format='wavedecn')
    >>> cam_recon = pywt.waverecn(coeffs_from_arr, wavelet='db2')
    >>> assert_array_almost_equal(cam, cam_recon)

    r    empty list of coefficient slicesr   r   r+   r   r   r   r   r   Unrecognized output format: )r/   r?   rZ   r3   rD   rC   r   )arrr   output_formatr]   r   r+   r   r   s           r,   r   r   !  sc   ~ *S//CF
<A;<<<c,q/*+++ 1c,''((  I%%LOC()AAj((\!_T*+\!_T*+\!_T*+-AA j((A$Q--//  11v! >}>>@ @ @aMr.   c                      t           |          \  }}}t          ||          }t          ||          }d |D             }	t          t	          |          t          |	          |          }g }
t          |          D ]}d t          dt          |                    D             } fd|D             }t          |||          D ]3\  }}}t           |         |j        |          }|D ]}|||         |<   4|                                D ]\  }}t          |          ||<   |
                    |           |                    d|z             |
                                |
                                 |
S )an  Subband shapes for a multilevel nD discrete wavelet transform.

    Parameters
    ----------
    shape : sequence of ints
        The shape of the data to be transformed.
    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 str, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`. This can
        also be a tuple containing a mode to apply along each axis in ``axes``.
    level : int, optional
        Decomposition level (must be >= 0). If level is None (default) then it
        will be calculated using the ``dwt_max_level`` function.
    axes : sequence of ints, optional
        Axes over which to compute the DWT. Axes may not be repeated. The
        default is None, which means transform all axes
        (``axes = range(data.ndim)``).

    Returns
    -------
    shapes : [cAn, {details_level_n}, ... {details_level_1}] : list
        Coefficients shape list.  Mirrors the output of ``wavedecn``, except
        it contains only the shapes of the coefficient arrays rather than the
        arrays themselves.

    Examples
    --------
    >>> import pywt
    >>> pywt.wavedecn_shapes((64, 32), wavelet='db2', level=3, axes=(0, ))
    [(10, 32), {'d': (10, 32)}, {'d': (18, 32)}, {'d': (33, 32)}]
    c                     g | ]	}|j         
S r(   rh   ri   s     r,   r-   z#wavedecn_shapes.<locals>.<listcomp>  rk   r.   c                 8    g | ]}d                      |          S ) )join)r)   cs     r,   r-   z#wavedecn_shapes.<locals>.<listcomp>  s"    KKKarwwqzzKKKr.   r   )repeatc                 0    i | ]}|t                    S r(   )rX   )r)   r   r@   s     r,   
<dictcomp>z#wavedecn_shapes.<locals>.<dictcomp>  s!    :::ae:::r.   )
filter_lenrH   rL   )r   r   r   r:   r1   maxrC   r   rZ   r2   r   rB   r   rY   rD   r   rE   )r@   rG   rH   r8   rn   r   r   rp   modesrq   shapesrM   detail_keys
new_shapesrI   wavr*   r   r   s   `                  r,   r"   r"   z  s   D )<E4(H(H%D+~!'400HD$''E//h///K[))3{+;+;UCCEF5\\ 
5 
5KK74D		+J+J+JKKK::::k:::
"4599 	( 	(OD#teDkckMMMA  ( (&'
1d##($$&& 	% 	%DAq!!HHJqMMj!!!s^344
MM%
NNMr.   c                     d } || d                   }| dd         D ];}|                                 D ]$\  }}|t          d          | ||          z  }%<|S )a  Compute the total number of wavedecn coefficients.

    Parameters
    ----------
    shapes : list of coefficient shapes
        A set of coefficient shapes as returned by ``wavedecn_shapes``.
        Alternatively, the user can specify a set of coefficients as returned
        by ``wavedecn``.

    Returns
    -------
    size : int
        The total number of coefficients.

    Examples
    --------
    >>> import numpy as np
    >>> import pywt
    >>> data_shape = (64, 32)
    >>> shapes = pywt.wavedecn_shapes(data_shape, 'db2', mode='periodization')
    >>> pywt.wavedecn_size(shapes)
    2048
    >>> coeffs = pywt.wavedecn(np.ones(data_shape), 'sym4', mode='symmetric')
    >>> pywt.wavedecn_size(coeffs)
    3087
    c                 l    t          | t          j                  r| j        S t          j        |           S )z?Size corresponding to ``x`` as either a shape tuple or ndarray.)rW   r/   r[   r   r   )xs    r,   _sizezwavedecn_size.<locals>._size  s+    a$$ 	6M71::r.   r   r   Nz4Setting coefficient arrays to None is not supported.)r   r3   )r   r   r   r+   r   r   s         r,   r!   r!     s    6   eF1IGABBZ    GGII 	  	 DAqy JL L LuuQxxGG		 
 Nr.   c                     t          | |          \  }}}t          ||          }d t          ||          D             }t          |          S )aC  Compute the maximum level of decomposition for n-dimensional data.

    This returns the maximum number of levels of decomposition suitable for use
    with ``wavedec``, ``wavedec2`` or ``wavedecn``.

    Parameters
    ----------
    shape : sequence of ints
        Input data shape.
    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``.
    axes : sequence of ints, optional
        Axes over which to compute the DWT. Axes may not be repeated.

    Returns
    -------
    level : int
        Maximum level.

    Notes
    -----
    The level returned is the smallest ``dwt_max_level`` over all axes.

    Examples
    --------
    >>> import pywt
    >>> pywt.dwtn_max_level((64, 32), 'db2')
    3
    c                 >    g | ]\  }}t          ||j                  S r(   )r
   rB   )r)   r   r   s      r,   r-   z"dwtn_max_level.<locals>.<listcomp>  s8     < < <a  3;// < < <r.   )r   r   r2   r1   )r@   rG   rn   r   r   rp   
max_levelss          r,   r    r      s`    @ )<E4(H(H%D+~ "'400H< < #K : :< < <Jz??r.   c                    t          | |          \  } }}}| d         }|j        }t          |           dk    r+|                                t	          |          g|j        gfS t          |           }t          j        |f|j	                  }t	          |          }|                                ||<   g }	g }
|	
                    |           |

                    | d         j                   | dd         }|}|D ]}|	
                    i            |

                    i            t          j        d |                                D                       rt          d          t          |                                          }|D ]^}||         }t	          |||j        z             }||j        z  }|                                ||<   ||	d         |<   |j        |
d         |<   _||	|
fS )a  Ravel a set of multilevel wavelet coefficients into a single 1D array.

    Parameters
    ----------
    coeffs : array-like
        A list of multilevel wavelet coefficients as returned by
        ``wavedec``, ``wavedec2`` or ``wavedecn``. This function is also
        compatible with the output of ``swt``, ``swt2`` and ``swtn`` if those
        functions were called with ``trim_approx=True``.
    axes : sequence of ints, optional
        Axes over which the DWT that created ``coeffs`` was performed. The
        default value of None corresponds to all axes.

    Returns
    -------
    coeff_arr : array-like
        Wavelet transform coefficient array. All coefficients have been
        concatenated into a single array.
    coeff_slices : list
        List of slices corresponding to each coefficient. As a 2D example,
        ``coeff_arr[coeff_slices[1]['dd']]`` would extract the first level
        detail coefficients from ``coeff_arr``.
    coeff_shapes : list
        List of shapes corresponding to each coefficient. For example, in 2D,
        ``coeff_shapes[1]['dd']`` would contain the original shape of the first
        level detail coefficients array.

    See Also
    --------
    unravel_coeffs : the inverse of ravel_coeffs

    Examples
    --------
    >>> import pywt
    >>> cam = pywt.data.camera()
    >>> coeffs = pywt.wavedecn(cam, wavelet='db2', level=3)
    >>> arr, coeff_slices, coeff_shapes = pywt.ravel_coeffs(coeffs)

    r   r   r   Nc                     g | ]}|d u S rR   r(   r   s     r,   r-   z ravel_coeffs.<locals>.<listcomp>P  r   r.   r   r<   )r   r   rZ   ravelrT   r@   r!   r/   r   r   rD   r   r   r3   sortedr   )r]   rn   rl   r   r   a_sizearr_sizer   a_slicer   coeff_shapesr^   offsetr   r   r   r+   sls                     r,   r   r   
  s   P *>fd)K)K&FD$ ayH]F
6{{a~~%--!2X^4FFF V$$H(X^<<<IFmmG!))Ig LL   q	((( 
BF , ,
BB6::j&7&7&9&9:::;; 	. - . . . joo''(( 	, 	,C3Avv//BafFGGIIIbM$&LS!$%GLS!!	, lL00r.   c                    t          j        |           } g }t          |          dk    rt          d          t          |          dk    rt          d          t          |          t          |          k    rt          d          |                    | |d                                      |d                              t          dt          |                    D ]*}||         }||         }|dv r(| |d                                      |d                   }n|dv ru| |d	                                      |d	                   | |d
                                      |d
                   | |d                                      |d                   f}n]|dv rGi }||                                         D ])\  }	}
| |
                             ||	                   ||	<   *nt          d|           |                    |           ,|S )a  Unravel a raveled array of multilevel wavelet coefficients.

    Parameters
    ----------
    arr : array-like
        An array containing all wavelet coefficients. This should have been
        generated by applying ``ravel_coeffs`` to the output of ``wavedec``,
        ``wavedec2`` or ``wavedecn`` (or via ``swt``, ``swt2`` or ``swtn``
        with ``trim_approx=True``).
    coeff_slices : list of tuples
        List of slices corresponding to each coefficient as obtained from
        ``ravel_coeffs``.
    coeff_shapes : list of tuples
        List of shapes corresponding to each coefficient as obtained from
        ``ravel_coeffs``.
    output_format : {'wavedec', 'wavedec2', 'wavedecn', 'swt', 'swt2', 'swtn'}, optional
        Make the form of the unraveled coefficients compatible with this type
        of multilevel transform. The default is ``'wavedecn'``.

    Returns
    -------
    coeffs: list
        List of wavelet transform coefficients. The specific format of the list
        elements is determined by ``output_format``.

    See Also
    --------
    ravel_coeffs : the inverse of unravel_coeffs

    Examples
    --------
    >>> import pywt
    >>> from numpy.testing import assert_array_almost_equal
    >>> cam = pywt.data.camera()
    >>> coeffs = pywt.wavedecn(cam, wavelet='db2', level=3)
    >>> arr, coeff_slices, coeff_shapes = pywt.ravel_coeffs(coeffs)
    >>> coeffs_from_arr = pywt.unravel_coeffs(arr, coeff_slices, coeff_shapes,
    ...                                       output_format='wavedecn')
    >>> cam_recon = pywt.waverecn(coeffs_from_arr, wavelet='db2')
    >>> assert_array_almost_equal(cam, cam_recon)

    r   r   z empty list of coefficient shapesz1coeff_shapes and coeff_slices have unequal lengthr   )r   swtr+   )r   swt2r   r   r   )r   swtnr   )r/   r?   rZ   r3   rD   reshaperC   r   )r   r   r   r   r]   r   
slice_dict
shape_dictr+   r   r   s              r,   r   r   _  s!   V *S//CF
<A;<<<	\		a		;<<<	\		c,//	/	/LMMMc,q/*22<?CCDDD 1c,''((  !!_
!!_
...JsO$,,Z_==AA222Z%&..z$/?@@Z%&..z$/?@@Z%&..z$/?@@BAA 222A$Q--// 5 511v~~jm44!5 >}>>@ @ @aMr.   c                      t          |          t          t          |                    k    rt          d          	  fd|D              dS # t          $ r t          d          w xY w)z-Axes checks common to fswavedecn, fswaverecn.z-The axes passed to fswavedecn must be unique.c                 *    g | ]}j         |         S r(   rd   re   s     r,   r-   z*_check_fswavedecn_axes.<locals>.<listcomp>  s    '''BB'''r.   r>   N)rZ   rm   r   rA   )rF   rn   s   ` r,   _check_fswavedecn_axesr     s~    
4yyCD		NN""GHHH=''''$'''''' = = =;<<<=s   A A)c                   R   e Zd ZdZd Zed             Zej        d             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zed             Zed             Zd Zed             Zej        d             Zd Zd Zd Zd ZdS )r%   ao  Object representing fully separable wavelet transform coefficients.

    Parameters
    ----------
    coeffs : ndarray
        The coefficient array.
    coeff_slices : list
        List of slices corresponding to each detail or approximation
        coefficient array.
    wavelets : list of pywt.DiscreteWavelet objects
        The wavelets used.  Will be a list with length equal to
        ``len(axes)``.
    mode_enums : list of int
        The border modes used.  Will be a list with length equal to
        ``len(axes)``.
    axes : tuple of int
        The set of axes over which the transform was performed.

    c                     || _         || _        || _        t          j        d |D                       st          d          || _        t          j        d |D                       st          d          || _        d S )Nc              3   @   K   | ]}t          |t                    V  d S rR   )rW   r   ri   s     r,   rV   z,FswavedecnResult.__init__.<locals>.<genexpr>  s,      ??jG,,??????r.   z*wavelets must contain pywt.Wavelet objectsc              3   @   K   | ]}t          |t                    V  d S rR   )rW   int)r)   ms     r,   rV   z,FswavedecnResult.__init__.<locals>.<genexpr>  s,      ==QjC((======r.   zmode_enums must be integers)_coeffs_coeff_slices_axesr/   r   r3   	_wavelets_mode_enums)selfr]   r   rp   
mode_enumsrn   s         r,   __init__zFswavedecnResult.__init__  s    )
v??h????? 	><> > >!v==*===== 	/-/ / /%r.   c                     | j         S )z6ndarray: All coefficients stacked into a single array.)r  r	  s    r,   r]   zFswavedecnResult.coeffs  s     |r.   c                 \    |j         | j        j         k    rt          d          || _        d S )Nz?new coefficient array must match the existing coefficient shape)r@   r  r3   )r	  r   s     r,   r]   zFswavedecnResult.coeffs  s6    7dl((( 1 2 2 2r.   c                     | j         S )z!List: List of coefficient slices.)r  r  s    r,   r   zFswavedecnResult.coeff_slices  s     !!r.   c                     | j         j        S )zint: Number of data dimensions.)r]   rl   r  s    r,   rl   zFswavedecnResult.ndim  s     {r.   c                 *    t          | j                  S )z int: Number of axes transformed.)rZ   rn   r  s    r,   r   zFswavedecnResult.ndim_transform  s     49~~r.   c                     | j         S )z8List of str: The axes the transform was performed along.)r  r  s    r,   rn   zFswavedecnResult.axes  s     zr.   c                 $    d | j         D             S )zAList of int: Levels of decomposition along each transformed axis.c                 2    g | ]}t          |          d z
  S r   )rZ   rU   s     r,   r-   z+FswavedecnResult.levels.<locals>.<listcomp>  s"    666qA
666r.   )r   r  s    r,   levelszFswavedecnResult.levels  s     76D$56666r.   c                     | j         S )@List of pywt.DiscreteWavelet: wavelet for each transformed axis.r  r  s    r,   rp   zFswavedecnResult.wavelets  s     ~r.   c                 $    d | j         D             S )r  c                     g | ]	}|j         
S r(   )nameri   s     r,   r-   z2FswavedecnResult.wavelet_names.<locals>.<listcomp>  s    ///1///r.   r  r  s    r,   wavelet_nameszFswavedecnResult.wavelet_names   s     0/////r.   c                 V    d t           j        D             fd| j        D             S )z>List of str: The border mode used along each transformed axis.c                 :    i | ]}t          t          |          |S r(   )getattrr   )r)   rH   s     r,   r   z*FswavedecnResult.modes.<locals>.<dictcomp>  s4     / / / eT**D / / /r.   c                      g | ]
}|         S r(   r(   )r)   r  
names_dicts     r,   r-   z*FswavedecnResult.modes.<locals>.<listcomp>
  s    888!
1888r.   )r   r   r  )r	  r"  s    @r,   r   zFswavedecnResult.modes  s@    / /"'+/ / /
8888t'78888r.   c                     t          d           g| j        z  }t          t          | j        |                    D ]\  }\  }}| j        |         |         ||<   t          |          S rR   )rT   rl   r   r2   rn   r   rY   )r	  r  r   r   rf   levs         r,   _get_coef_slzFswavedecnResult._get_coef_sl  se    Dkk_ty(%c$)V&<&<== 	/ 	/LAyC&q)#.BrFFRyyr.   c                 V    |                      d| j        z            }| j        |         S )z(ndarray: The approximation coefficients.r   )r%  rl   r  )r	  r   s     r,   approxzFswavedecnResult.approx  s*     uTY//|Br.   c                     |                      d| j        z            }| j        |         j        |j        k    rt	          d          || j        |<   d S )Nr'  7x does not match the shape of the requested coefficient)r%  rl   r  r@   r3   )r	  rL   r   s      r,   r(  zFswavedecnResult.approx  s\    uTY//<!QW,,IK K KRr.   c                 .   t          |          }t          |          t          | j                  k    rt          d          t	          j        d |D                       rKt	          j        t	          j        |          dz  dk              st	          j        d |D                       rt          d          t          d |D                       }t	          j        d t          || j	                  D                       rt          d	          d S )
Nz0levels must match the number of transformed axesc                 B    g | ]}t          |t          j                  S r(   )rW   numbersNumberr)   r$  s     r,   r-   z4FswavedecnResult._validate_index.<locals>.<listcomp>(  s$    JJJC
377JJJr.   r   r   c                     g | ]}|d k     	S r'  r(   r/  s     r,   r-   z4FswavedecnResult._validate_index.<locals>.<listcomp>*  s    222Ca222r.   z.Index must be a tuple of non-negative integersc                 ,    g | ]}t          |          S r(   )r  r/  s     r,   r-   z4FswavedecnResult._validate_index.<locals>.<listcomp>-  s    333SC333r.   c                      g | ]\  }}||k    S r(   r(   )r)   r$  maxlevs      r,   r-   z4FswavedecnResult._validate_index.<locals>.<listcomp>0  s     LLLKC3<LLLr.   z8Specified indices exceed the number of transform levels.)
rY   rZ   rn   r3   r/   r   r   r?   r2   r  )r	  r  s     r,   _validate_indexz FswavedecnResult._validate_index   s,   vv;;#di..((BD D D JJ6JJJKK 	Orz&))A-122	O22622233	O MNNN33F33344 6LL3vt{3K3KLLLMM 	LJL L L	L 	Lr.   c                 p    |                      |           |                     |          }| j        |         S )zRetrieve a coefficient subband.

        Parameters
        ----------
        levels : tuple of int
            The number of degrees of decomposition along each transformed
            axis.
        )r4  r%  r  )r	  r  r   s      r,   __getitem__zFswavedecnResult.__getitem__4  s8     	V$$$v&&|Br.   c                 *   |                      |           |                     |          }| j        |         j        }| j        |         j        |j        k    rt          d          |j        |k    rt          j        d|            || j        |<   dS )ad  Assign values to a coefficient subband.

        Parameters
        ----------
        levels : tuple of int
            The number of degrees of decomposition along each transformed
            axis.
        x : ndarray
            The data corresponding to assign. It must match the expected
            shape and dtype of the specified subband.
        r*  z7dtype mismatch:  converting the provided array todtype N)r4  r%  r  r   r@   r3   r4   r5   )r	  r  r   r   current_dtypes        r,   __setitem__zFswavedecnResult.__setitem__A  s     	V$$$v&&R(.<!QW,,IK K K7m##M 3#03 3 4 4 4Rr.   c                     t          t          d | j        D                        }|                    dt	          | j                  z             t          |          S )zReturn a list of all detail coefficient keys.

        Returns
        -------
        keys : list of str
            List of all detail coefficient keys.
        c              3   :   K   | ]}t          |d z             V  dS )r   N)rC   )r)   ls     r,   rV   z/FswavedecnResult.detail_keys.<locals>.<genexpr>`  s,      >>QeAaCjj>>>>>>r.   r'  )rX   r   r  removerZ   rn   r   )r	  r   s     r,   r   zFswavedecnResult.detail_keysX  sR     G>>$+>>>?@@E#di..()))d||r.   N)__name__
__module____qualname____doc__r  propertyr]   setterr   rl   r   rn   r  rp   r  r   r%  r(  r4  r6  r9  r   r(   r.   r,   r%   r%     s        && & &   X ]  ] " " X"     X    X   X 7 7 X7   X 0 0 X0 9 9 X9       X 
 ]  ]L L L(       .
 
 
 
 
r.   r%   c                 B   t          j        |           } |&t          t          j        | j                            }t          | |           |t          j        |          r|gt          |          z  }t          |          t          |          k    rt          d          t          ||          }t          ||          }t          d          gt          |          z  }| }t          t          ||||                    D ]\  }	\  }
}}t          ||||
          }fd|D             }t          j        dg|z             fdt!          t          |                    D             ||	<   t          j        |          }t%          |||||          S )a3  Fully Separable Wavelet Decomposition.

    This is a variant of the multilevel discrete wavelet transform where all
    levels of decomposition are performed along a single axis prior to moving
    onto the next axis.  Unlike in ``wavedecn``, the number of levels of
    decomposition are not required to be the same along each axis which can be
    a benefit for anisotropic data.

    Parameters
    ----------
    data: array_like
        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 str, optional
        Signal extension mode, see :ref:`Modes <ref-modes>`. This can
        also be a tuple containing a mode to apply along each axis in ``axes``.
    levels : int or sequence of ints, optional
        Decomposition levels along each axis (must be >= 0). If an integer is
        provided, the same number of levels are used for all axes. If
        ``levels`` is None (default), ``dwt_max_level`` will be used to compute
        the maximum number of levels possible for each axis.
    axes : sequence of ints, optional
        Axes over which to compute the transform. Axes may not be repeated. The
        default is to transform along all axes.

    Returns
    -------
    fswavedecn_result : FswavedecnResult object
        Contains the wavelet coefficients, slice objects to allow obtaining
        the coefficients per detail or approximation level, and more.
        See ``FswavedecnResult`` for details.

    Examples
    --------
    >>> import numpy as np
    >>> from pywt import fswavedecn
    >>> fs_result = fswavedecn(np.ones((32, 32)), 'sym2', levels=(1, 3))
    >>> print(fs_result.detail_keys())
    [(0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3)]
    >>> approx_coeffs = fs_result.approx
    >>> detail_1_2 = fs_result[(1, 2)]


    Notes
    -----
    This transformation has been variously referred to as the (fully) separable
    wavelet transform (e.g. refs [1]_, [3]_), the tensor-product wavelet
    ([2]_) or the hyperbolic wavelet transform ([4]_).  It is well suited to
    data with anisotropic smoothness.

    In [2]_ it was demonstrated that fully separable transform performs at
    least as well as the DWT for image compression.  Computation time is a
    factor 2 larger than that for the DWT.

    See Also
    --------
    fswaverecn : inverse of fswavedecn

    References
    ----------
    .. [1] PH Westerink. Subband Coding of Images. Ph.D. dissertation, Dept.
       Elect. Eng., Inf. Theory Group, Delft Univ. Technol., Delft, The
       Netherlands, 1989.  (see Section 2.3)
       http://resolver.tudelft.nl/uuid:a4d195c3-1f89-4d66-913d-db9af0969509

    .. [2] CP Rosiene and TQ Nguyen. Tensor-product wavelet vs. Mallat
       decomposition: A comparative analysis, in Proc. IEEE Int. Symp.
       Circuits and Systems, Orlando, FL, Jun. 1999, pp. 431-434.

    .. [3] V Velisavljevic, B Beferull-Lozano, M Vetterli and PL Dragotti.
       Directionlets: Anisotropic Multidirectional Representation With
       Separable Filtering. IEEE Transactions on Image Processing, Vol. 15,
       No. 7, July 2006.

    .. [4] RA DeVore, SV Konyagin and VN Temlyakov. "Hyperbolic wavelet
       approximation," Constr. Approx. 14 (1998), 1-26.
    Nz-levels must match the length of the axes list)rH   r8   rI   c                 *    g | ]}|j                  S r(   rd   )r)   r   rf   s     r,   r-   zfswavedecn.<locals>.<listcomp>  s    000AAGBK000r.   r   c                 N    g | ]!}t          |         |d z                      "S r  rS   )r)   r+   	c_offsetss     r,   r-   zfswavedecn.<locals>.<listcomp>  sB     "O "O "O45E)A,	!A#//"O "O "Or.   )rI   )r/   r?   rY   r   rl   r   r0   rZ   r3   r   r   rT   r   r2   r   cumsumrC   concatenater%   )rF   rG   rH   r  rn   r   rp   r   
coeffs_arrax_countr$  r   r]   c_shapesrf   rG  s                 @@r,   r#   r#   e  s   ` :dD|RYty))**4&&&~V,,~c$ii'
6{{c$iiHIIID$''E!'400H$KK?SYY.LJ*3fh..+0 +0 5 5&&2sCSt3RHHH
 1000000Iqeh.//	"O "O "O "O9>s8}}9M9M"O "O "OX ^F444

JhtLLLr.   c                    | j         }| j        }| j        }| j        }| j        }t          ||           t          |          t          |          k    rt          d          |}t          d          g|j	        z  }t          t          |||                    D ]h\  }\  }	}
}g }||         D ]/}|||	<   |                    |t          |                              0t          d          ||	<   t          ||
||	          }i|S )a  Fully Separable Inverse Wavelet Reconstruction.

    Parameters
    ----------
    fswavedecn_result : FswavedecnResult object
        FswavedecnResult object from ``fswavedecn``.

    Returns
    -------
    reconstructed : ndarray
        Array of reconstructed data.

    Notes
    -----
    This transformation has been variously referred to as the (fully) separable
    wavelet transform (e.g. refs [1]_, [3]_), the tensor-product wavelet
    ([2]_) or the hyperbolic wavelet transform ([4]_).  It is well suited to
    data with anisotropic smoothness.

    In [2]_ it was demonstrated that the fully separable transform performs at
    least as well as the DWT for image compression. Computation time is a
    factor 2 larger than that for the DWT.

    See Also
    --------
    fswavedecn : inverse of fswaverecn

    References
    ----------
    .. [1] PH Westerink. Subband Coding of Images. Ph.D. dissertation, Dept.
       Elect. Eng., Inf. Theory Group, Delft Univ. Technol., Delft, The
       Netherlands, 1989.  (see Section 2.3)
       http://resolver.tudelft.nl/uuid:a4d195c3-1f89-4d66-913d-db9af0969509

    .. [2] CP Rosiene and TQ Nguyen. Tensor-product wavelet vs. Mallat
       decomposition: A comparative analysis, in Proc. IEEE Int. Symp.
       Circuits and Systems, Orlando, FL, Jun. 1999, pp. 431-434.

    .. [3] V Velisavljevic, B Beferull-Lozano, M Vetterli and PL Dragotti.
       Directionlets: Anisotropic Multidirectional Representation With
       Separable Filtering. IEEE Transactions on Image Processing, Vol. 15,
       No. 7, July 2006.

    .. [4] RA DeVore, SV Konyagin and VN Temlyakov. "Hyperbolic wavelet
       approximation," Constr. Approx. 14 (1998), 1-26.
    zdimension mismatchN)rH   rI   )r]   r   rn   r   rp   r   rZ   r3   rT   rl   r   r2   rD   rY   r   )fswavedecn_resultrJ  r   rn   r   rp   r   cslrK  rf   r   rH   r]   r   s                 r,   r$   r$     s   ^ #)J$1L!D#E )H:t,,,
4yyC%%%%-...
C;;/CH
$C &/s45/I/I%J%J 7 7!/2sDx( 	+ 	+BCGMM#eCjj/****++Bfc2666Jr.   )r;   Nr<   )r;   r<   )r;   Nr_   )r;   r_   )r;   NN)r;   N)r   N)r   rR   )5rA  r-  r4   r   	itertoolsr   numpyr/   _dwtr   r   r   _extensions._dwtr
   _extensions._pywtr   r   	_multidimr   r   r   r   r   _utilsr   r   r   r   __all__r:   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r!   r    r   r   r   r%   r#   r$   r(   r.   r,   <module>rX     s                      * * * * * * * * * * + + + + + + - - - - - - - - < < < < < < < < < < < < < < O O O O O O O O O O O ON N N  $< < < <~@ @ @ @FJ J J JZQ Q Q Qh- - -(R R R Rj; ; ; u u u up    "' ' ' +. +. +.\|# |# |# |#~V V V Vr7 7 7 7t( ( (V( ( ( (VR1 R1 R1 R1jH H H HV= = =n n n n n n n nbnM nM nM nMbD D D D Dr.   