a
    
d8                     @   s   d dl mZmZ d dlZddlmZmZmZm	Z	m
Z
mZmZ ddlmZmZmZmZmZmZmZ ddlmZmZ g dZdddZdd ZdddZdd ZdddZdd ZdS )    )partialreduceN   )_prep_axes_wavedecnwavedecwavedec2wavedecnwaverecwaverec2waverecn)iswtiswt2iswtnswtswt2swt_max_levelswtn)_modes_per_axis_wavelets_per_axis)mramra2mranimraimra2imranr   periodizationc                 C   sX  |dkrR|dkrt dt||dd}ttf|dd|}ttfi |}d}	nN|dkrt|||d}ttfd	|i|}ttfi |}d
}	nt d||| }
g }t|
}|	rt	
|
d }|g| }ndd |
D }t|D ]j}|
| ||< ||}|j| jkr&|tdd | jD  }|| |	r@|||< qt	
|| ||< q|S )a  Forward 1D multiresolution analysis.

    It is a projection onto the wavelet subspaces.

    Parameters
    ----------
    data: array_like
        Input data
    wavelet : Wavelet object or name string
        Wavelet to use
    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. Currently only available when ``transform='dwt'``.
    transform : {'dwt', 'swt'}
        Whether to use the DWT or SWT for the transforms.
    mode : str, optional
        Signal extension mode, see `Modes` (default: 'symmetric'). This option
        is only used when transform='dwt'.

    Returns
    -------
    [cAn, {details_level_n}, ... {details_level_1}] : list
        For more information, see the detailed description in `wavedec`

    See Also
    --------
    imra, swt

    Notes
    -----
    This is sometimes referred to as an additive decomposition because the
    inverse transform (``imra``) is just the sum of the coefficient arrays
    [1]_. The decomposition using ``transform='dwt'`` corresponds to section
    2.2 while that using an undecimated transform (``transform='swt'``) is
    described in section 3.2 and appendix A.

    This transform does not share the variance partition property of ``swt``
    with `norm=True`. It does however, result in coefficients that are
    temporally aligned regardless of the symmetry of the wavelet used.

    The redundancy of this transform is ``(level + 1)``.

    References
    ----------
    .. [1] Donald B. Percival and Harold O. Mofjeld. Analysis of Subtidal
        Coastal Sea Level Fluctuations Using Wavelets. Journal of the American
        Statistical Association Vol. 92, No. 439 (Sep., 1997), pp. 868-880.
        https://doi.org/10.2307/2965551

    r   r   0transform swt only supports mode='periodization'T)waveletaxisnormlevelZtrim_approxZdwt)r   moder   r"   Funrecognized transform: {}r   c                 S   s   g | ]}t |qS  np
zeros_like.0cr%   r%   R/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/pywt/_mra.py
<listcomp>_       zmra.<locals>.<listcomp>c                 S   s   g | ]}t |qS r%   slicer*   szr%   r%   r,   r-   i   r.   )
ValueErrordictr   r   r   r   r	   formatlenr'   r(   rangeshapetupleappend)datar   r"   r   	transformr#   kwargsforwardinverseZis_swt
wav_coeffs
mra_coeffsncztmpjrecr%   r%   r,   r      s@    7

r   c                 C   s   t dd | S )aW  Inverse 1D multiresolution analysis via summation.

    Parameters
    ----------
    mra_coeffs : list of ndarray
        Multiresolution analysis coefficients as returned by `mra`.

    Returns
    -------
    rec : ndarray
        The reconstructed signal.

    See Also
    --------
    mra

    References
    ----------
    .. [1] Donald B. Percival and Harold O. Mofjeld. Analysis of Subtidal
        Coastal Sea Level Fluctuations Using Wavelets. Journal of the American
        Statistical Association Vol. 92, No. 439 (Sep., 1997), pp. 868-880.
        https://doi.org/10.2307/2965551
    c                 S   s   | | S Nr%   )xyr%   r%   r,   <lambda>   r.   zimra.<locals>.<lambda>)r   )rA   r%   r%   r,   r   t   s    r   r   r   c                 C   s  |dkrj|dkrt d|du r4tdd | jD }t||dd}ttf|dd	|}ttfi |}nJ|d
krt|||d}ttfd|i|}ttfi |}nt d	||| }	g }
t
|	}t|	d }|g}td|D ]}|dd |	| D  q|	d |d< ||}|j| jkr>|tdd | jD  }|
| ||d< td|D ]}g }tdD ]j}|| | }|	| | || |< ||}|j| jkr|tdd | jD  }|| ||| |< qj|
t| qZ|
S )a  Forward 2D multiresolution analysis.

    It is a projection onto wavelet subspaces.

    Parameters
    ----------
    data: array_like
        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`.
    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.
        Currently only available when ``transform='dwt2'``.
    transform : {'dwt2', 'swt2'}
        Whether to use the DWT or SWT for the transforms.
    mode : str or 2-tuple of str, optional
        Signal extension mode, see `Modes` (default: 'symmetric'). This option
        is only used when transform='dwt2'.

    Returns
    -------
    coeffs : list
        For more information, see the detailed description in `wavedec2`

    Notes
    -----
    This is sometimes referred to as an additive decomposition because the
    inverse transform (``imra2``) is just the sum of the coefficient arrays
    [1]_. The decomposition using ``transform='dwt'`` corresponds to section
    2.2 while that using an undecimated transform (``transform='swt'``) is
    described in section 3.2 and appendix A.

    This transform does not share the variance partition property of ``swt2``
    with `norm=True`. It does however, result in coefficients that are
    temporally aligned regardless of the symmetry of the wavelet used.

    The redundancy of this transform is ``3 * level + 1``.

    See Also
    --------
    imra2, swt2

    References
    ----------
    .. [1] Donald B. Percival and Harold O. Mofjeld. Analysis of Subtidal
        Coastal Sea Level Fluctuations Using Wavelets. Journal of the American
        Statistical Association Vol. 92, No. 439 (Sep., 1997), pp. 868-880.
        https://doi.org/10.2307/2965551
    r   r   r   Nc                 s   s   | ]}t |V  qd S rG   r   r*   sr%   r%   r,   	<genexpr>   r.   zmra2.<locals>.<genexpr>Tr   axesr    r!   Zdwt2r   r#   rR   r"   r$   r   r   c                 S   s   g | ]}t |qS r%   r&   r)   r%   r%   r,   r-      r.   zmra2.<locals>.<listcomp>c                 S   s   g | ]}t |qS r%   r/   r1   r%   r%   r,   r-      r.      c                 S   s   g | ]}t |qS r%   r/   r1   r%   r%   r,   r-      r.   )r3   minr8   r4   r   r   r   r   r
   r5   r6   r'   r(   r7   r:   r9   )r;   r   r"   rR   r<   r#   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   dcoeffsnr%   r%   r,   r      sP    7

r   c                 C   s>   | d }t dt| D ]"}t dD ]}|| | | 7 }q"q|S )aN  Inverse 2D multiresolution analysis via summation.

    Parameters
    ----------
    mra_coeffs : list
        Multiresolution analysis coefficients as returned by `mra2`.

    Returns
    -------
    rec : ndarray
        The reconstructed signal.

    See Also
    --------
    mra2

    References
    ----------
    .. [1] Donald B. Percival and Harold O. Mofjeld. Analysis of Subtidal
        Coastal Sea Level Fluctuations Using Wavelets. Journal of the American
        Statistical Association Vol. 92, No. 439 (Sep., 1997), pp. 868-880.
        https://doi.org/10.2307/2965551
    r   r   rT   )r7   r6   )rA   rF   rE   rW   r%   r%   r,   r      s
    r   r   c                 C   s  t | j|\}}}t||}|dkr|dkr4td|du rPtdd | jD }t||dd}	ttf|dd	|	}
ttfi |	}nT|d
krt	||}t|||d}	tt
fd|i|	}
ttfi |	}ntd||
| }g }t|}t|d }|g}td|D ]"}|dd ||  D  q|d |d< ||}|j| jkrj|tdd | jD  }|| ||d< td|D ]}i }t||  }|D ]h}|| | }|| | || |< ||}|j| jkr|tdd | jD  }|||< ||| |< q|| q|S )a  Forward nD multiresolution analysis.

    It is a projection onto the wavelet subspaces.

    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`.
    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 : tuple of ints, optional
        Axes over which to compute the DWT. Repeated elements are not allowed.
    transform : {'dwtn', 'swtn'}
        Whether to use the DWT or SWT for the transforms.
    mode : str or tuple of str, optional
        Signal extension mode, see `Modes` (default: 'symmetric'). This option
        is only used when transform='dwtn'.

    Returns
    -------
    coeffs : list
        For more information, see the detailed description in `wavedecn`.

    See Also
    --------
    imran, swtn

    Notes
    -----
    This is sometimes referred to as an additive decomposition because the
    inverse transform (``imran``) is just the sum of the coefficient arrays
    [1]_. The decomposition using ``transform='dwt'`` corresponds to section
    2.2 while that using an undecimated transform (``transform='swt'``) is
    described in section 3.2 and appendix A.

    This transform does not share the variance partition property of ``swtn``
    with `norm=True`. It does however, result in coefficients that are
    temporally aligned regardless of the symmetry of the wavelet used.

    The redundancy of this transform is ``(2**n - 1) * level + 1`` where ``n``
    corresponds to the number of axes transformed.

    References
    ----------
    .. [1] Donald B. Percival and Harold O. Mofjeld. Analysis of Subtidal
        Coastal Sea Level Fluctuations Using Wavelets. Journal of the American
        Statistical Association Vol. 92, No. 439 (Sep., 1997), pp. 868-880.
        https://doi.org/10.2307/2965551
    r   r   r   Nc                 s   s   | ]}t |V  qd S rG   rM   rN   r%   r%   r,   rP   Z  r.   zmran.<locals>.<genexpr>TrQ   r!   ZdwtnrS   r"   r$   r   r   c                 S   s   i | ]\}}|t |qS r%   r&   )r*   kvr%   r%   r,   
<dictcomp>m  r.   zmran.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r%   r/   r1   r%   r%   r,   r-   u  r.   zmran.<locals>.<listcomp>c                 S   s   g | ]}t |qS r%   r/   r1   r%   r%   r,   r-     r.   )r   r8   r   r3   rU   r4   r   r   r   r   r   r   r5   r6   r'   r(   r7   r:   itemsr9   listkeys)r;   r   r"   rR   r<   r#   Zaxes_shapesZndim_transformZwaveletsr=   r>   r?   modesr@   rA   rB   rC   rD   rE   rF   rV   ZdkeysrX   r%   r%   r,   r     sX    7

 
r   c                 C   s>   | d }t dt| D ]"}| |  D ]\}}||7 }q&q|S )aN  Inverse nD multiresolution analysis via summation.

    Parameters
    ----------
    mra_coeffs : list
        Multiresolution analysis coefficients as returned by `mra2`.

    Returns
    -------
    rec : ndarray
        The reconstructed signal.

    See Also
    --------
    mran

    References
    ----------
    .. [1] Donald B. Percival and Harold O. Mofjeld. Analysis of Subtidal
        Coastal Sea Level Fluctuations Using Wavelets. Journal of the American
        Statistical Association Vol. 92, No. 439 (Sep., 1997), pp. 868-880.
        https://doi.org/10.2307/2965551
    r   r   )r7   r6   r[   )rA   rF   rE   rX   rY   r%   r%   r,   r     s
    r   )Nr   r   r   )NrK   r   r   )NNr   r   )	functoolsr   r   numpyr'   Z_multilevelr   r   r   r   r	   r
   r   Z_swtr   r   r   r   r   r   r   _utilsr   r   __all__r   r   r   r   r   r   r%   r%   r%   r,   <module>   s"   $$  
g  
m  
t