U
    »mœd)M  ã                	   @   s0  d dl mZmZmZmZ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mZmZ ddlmZmZ d dlmZ d dlmZ ddd	d
dddddg	Zd+dd„Zi Zdd„ Z dd„ Z!dd	„ Z"dd
„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dd„ Z(d d!„ Z)d"d#„ Z*d,d%d„Z+d-d&d„Z,d.d(d„Z-d/d)d„Z.d*S )0é    )Úlogical_andÚasarrayÚpiÚ
zeros_likeÚ	piecewiseÚarrayÚarctan2ÚtanÚzerosÚarangeÚfloor)	ÚsqrtÚexpÚgreaterÚlessÚcosÚaddÚsinÚ
less_equalÚgreater_equalé   )Ú	cspline2dÚsepfir2d)Úcomb)Úfloat_factorialÚspline_filterÚbsplineÚgauss_splineÚcubicÚ	quadraticÚ	cspline1dÚ	qspline1dÚcspline1d_evalÚqspline1d_evalç      @c           	      C   s¨   | j j}tdddgdƒd }|dkrr|  d¡} t| j|ƒ}t| j|ƒ}t|||ƒ}t|||ƒ}|d|   |¡}n2|dkrœt| |ƒ}t|||ƒ}| |¡}ntd	ƒ‚|S )
a3  Smoothing spline (cubic) filtering of a rank-2 array.

    Filter an input data set, `Iin`, using a (cubic) smoothing spline of
    fall-off `lmbda`.

    Parameters
    ----------
    Iin : array_like
        input data set
    lmbda : float, optional
        spline smooghing fall-off value, default is `5.0`.

    Returns
    -------
    res : ndarray
        filterd input data

    Examples
    --------
    We can filter an multi dimentional signal (ex: 2D image) using cubic
    B-spline filter:

    >>> import numpy as np
    >>> from scipy.signal import spline_filter
    >>> import matplotlib.pyplot as plt
    >>> orig_img = np.eye(20)  # create an image
    >>> orig_img[10, :] = 1.0
    >>> sp_filter = spline_filter(orig_img, lmbda=0.1)
    >>> f, ax = plt.subplots(1, 2, sharex=True)
    >>> for ind, data in enumerate([[orig_img, "original image"],
    ...                             [sp_filter, "spline filter"]]):
    ...     ax[ind].imshow(data[0], cmap='gray_r')
    ...     ax[ind].set_title(data[1])
    >>> plt.tight_layout()
    >>> plt.show()

    ç      ð?g      @Úfç      @)ÚFÚDr(   y              ð?)r&   ÚdzInvalid data type for Iin)	ÚdtypeÚcharr   Úastyper   ÚrealÚimagr   Ú	TypeError)	ZIinZlmbdaZintypeZhcolZckrZckiZoutrZoutiÚout© r2   úO/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scipy/signal/_bsplines.pyr      s    &

c                    sè   z
t ˆ W S  tk
r   Y nX dd„ }ˆd d }ˆd rBd}nd}|dd|ƒg}|‰ td|d ƒD ]"}| |dˆ ˆ d ƒ¡ ˆ d ‰ qf| |ddˆd  d ƒ¡ tˆƒ‰‡ ‡‡fd	d
„‰‡fdd„t|ƒD ƒ}||ft ˆ< ||fS )a�  Returns the function defined over the left-side pieces for a bspline of
    a given order.

    The 0th piece is the first one less than 0. The last piece is a function
    identical to 0 (returned as the constant 0). (There are order//2 + 2 total
    pieces).

    Also returns the condition functions that when evaluated return boolean
    arrays for use with `numpy.piecewise`.
    c                    s<   | dkr‡ ‡fdd„S | dkr*‡fdd„S ‡ ‡fdd„S d S )Nr   c                    s   t t| ˆ ƒt| ˆƒƒS ©N)r   r   r   ©Úx©Úval1Úval2r2   r3   Ú<lambda>]   s   
ÿz>_bspline_piecefunctions.<locals>.condfuncgen.<locals>.<lambda>é   c                    s
   t | ˆ ƒS r4   )r   r5   )r9   r2   r3   r:   `   ó    c                    s   t t| ˆ ƒt| ˆƒƒS r4   )r   r   r   r5   r7   r2   r3   r:   b   s   
ÿr2   )Únumr8   r9   r2   r7   r3   Úcondfuncgen[   s
    z,_bspline_piecefunctions.<locals>.condfuncgenr;   g      ð¿g      à¿r   r   ç       @c                    sd   ˆd |  ‰ ˆ dk rdS ‡‡fdd„t ˆ d ƒD ƒ‰‡fdd„t ˆ d ƒD ƒ‰‡ ‡‡‡fdd„}|S )	Nr;   r   c              	      s6   g | ].}d d|d   t tˆd  |d d�ƒ ˆ  ‘qS )r   r;   )Úexact)Úfloatr   ©Ú.0Úk)ÚfvalÚorderr2   r3   Ú
<listcomp>}   s   ÿzA_bspline_piecefunctions.<locals>.piecefuncgen.<locals>.<listcomp>r   c                    s   g | ]}ˆ  | ‘qS r2   r2   rB   )Úboundr2   r3   rG      s     c                    s6   d}t ˆ d ƒD ] }|ˆ| | ˆ|  ˆ  7 }q|S )Nç        r   ©Úrange)r6   ÚresrD   )ÚMkÚcoeffsrF   Úshiftsr2   r3   Úthefunc�   s    z>_bspline_piecefunctions.<locals>.piecefuncgen.<locals>.thefuncrJ   )r=   rP   )rH   rE   rF   )rM   rN   rO   r3   Úpiecefuncgeny   s    
ÿz-_bspline_piecefunctions.<locals>.piecefuncgenc                    s   g | ]}ˆ |ƒ‘qS r2   r2   rB   )rQ   r2   r3   rG   ˆ   s     z+_bspline_piecefunctions.<locals>.<listcomp>)Ú_splinefunc_cacheÚKeyErrorrK   Úappendr   )rF   r>   ÚlastZ
startboundÚ	condfuncsr=   Úfunclistr2   )rH   rE   rF   rQ   r3   Ú_bspline_piecefunctionsK   s(    


rX   c                    s8   t t| ƒƒ ‰ t|ƒ\}}‡ fdd„|D ƒ}tˆ ||ƒS )aw  B-spline basis function of order n.

    Parameters
    ----------
    x : array_like
        a knot vector
    n : int
        The order of the spline. Must be non-negative, i.e., n >= 0

    Returns
    -------
    res : ndarray
        B-spline basis function values

    See Also
    --------
    cubic : A cubic B-spline.
    quadratic : A quadratic B-spline.

    Notes
    -----
    Uses numpy.piecewise and automatic function-generator.

    Examples
    --------
    We can calculate B-Spline basis function of several orders:

    >>> import numpy as np
    >>> from scipy.signal import bspline, cubic, quadratic
    >>> bspline(0.0, 1)
    1

    >>> knots = [-1.0, 0.0, -1.0]
    >>> bspline(knots, 2)
    array([0.125, 0.75, 0.125])

    >>> np.array_equal(bspline(knots, 2), quadratic(knots))
    True

    >>> np.array_equal(bspline(knots, 3), cubic(knots))
    True

    c                    s   g | ]}|ˆ ƒ‘qS r2   r2   )rC   Úfunc©Úaxr2   r3   rG   ¾   s     zbspline.<locals>.<listcomp>)Úabsr   rX   r   )r6   ÚnrW   rV   Zcondlistr2   rZ   r3   r   �   s    ,c                 C   s>   t | ƒ} |d d }dtdt | ƒ t| d  d | ƒ S )a÷  Gaussian approximation to B-spline basis function of order n.

    Parameters
    ----------
    x : array_like
        a knot vector
    n : int
        The order of the spline. Must be non-negative, i.e., n >= 0

    Returns
    -------
    res : ndarray
        B-spline basis function values approximated by a zero-mean Gaussian
        function.

    Notes
    -----
    The B-spline basis function can be approximated well by a zero-mean
    Gaussian function with standard-deviation equal to :math:`\sigma=(n+1)/12`
    for large `n` :

    .. math::  \frac{1}{\sqrt {2\pi\sigma^2}}exp(-\frac{x^2}{2\sigma})

    References
    ----------
    .. [1] Bouma H., Vilanova A., Bescos J.O., ter Haar Romeny B.M., Gerritsen
       F.A. (2007) Fast and Accurate Gaussian Derivatives Based on B-Splines. In:
       Sgallari F., Murli A., Paragios N. (eds) Scale Space and Variational
       Methods in Computer Vision. SSVM 2007. Lecture Notes in Computer
       Science, vol 4485. Springer, Berlin, Heidelberg
    .. [2] http://folk.uio.no/inf3330/scripting/doc/python/SciPy/tutorial/old/node24.html

    Examples
    --------
    We can calculate B-Spline basis functions approximated by a gaussian
    distribution:

    >>> import numpy as np
    >>> from scipy.signal import gauss_spline, bspline
    >>> knots = np.array([-1.0, 0.0, -1.0])
    >>> gauss_spline(knots, 3)
    array([0.15418033, 0.6909883, 0.15418033])  # may vary

    >>> bspline(knots, 3)
    array([0.16666667, 0.66666667, 0.16666667])  # may vary

    r   g      (@r;   )r   r   r   r   )r6   r]   Zsignsqr2   r2   r3   r   Â   s    0c                 C   s‚   t t| ƒƒ}t|ƒ}t|dƒ}| ¡ rJ|| }dd|d  d|   ||< | t|dƒ@ }| ¡ r~|| }dd| d  ||< |S )a-  A cubic B-spline.

    This is a special case of `bspline`, and equivalent to ``bspline(x, 3)``.

    Parameters
    ----------
    x : array_like
        a knot vector

    Returns
    -------
    res : ndarray
        Cubic B-spline basis function values

    See Also
    --------
    bspline : B-spline basis function of order n
    quadratic : A quadratic B-spline.

    Examples
    --------
    We can calculate B-Spline basis function of several orders:

    >>> import numpy as np
    >>> from scipy.signal import bspline, cubic, quadratic
    >>> bspline(0.0, 1)
    1

    >>> knots = [-1.0, 0.0, -1.0]
    >>> bspline(knots, 2)
    array([0.125, 0.75, 0.125])

    >>> np.array_equal(bspline(knots, 2), quadratic(knots))
    True

    >>> np.array_equal(bspline(knots, 3), cubic(knots))
    True

    r   gUUUUUUå?ç      à?r;   gUUUUUUÅ?é   ©r\   r   r   r   Úany©r6   r[   rL   Úcond1Zax1Úcond2Zax2r2   r2   r3   r   ÷   s    (
c                 C   sv   t t| ƒƒ}t|ƒ}t|dƒ}| ¡ r>|| }d|d  ||< | t|dƒ@ }| ¡ rr|| }|d d d ||< |S )a-  A quadratic B-spline.

    This is a special case of `bspline`, and equivalent to ``bspline(x, 2)``.

    Parameters
    ----------
    x : array_like
        a knot vector

    Returns
    -------
    res : ndarray
        Quadratic B-spline basis function values

    See Also
    --------
    bspline : B-spline basis function of order n
    cubic : A cubic B-spline.

    Examples
    --------
    We can calculate B-Spline basis function of several orders:

    >>> import numpy as np
    >>> from scipy.signal import bspline, cubic, quadratic
    >>> bspline(0.0, 1)
    1

    >>> knots = [-1.0, 0.0, -1.0]
    >>> bspline(knots, 2)
    array([0.125, 0.75, 0.125])

    >>> np.array_equal(bspline(knots, 2), quadratic(knots))
    True

    >>> np.array_equal(bspline(knots, 3), cubic(knots))
    True

    r^   g      è?r;   ç      ø?r?   r`   rb   r2   r2   r3   r   ,  s    (
c                 C   sŽ   dd|   d|  t dd|   ƒ  }tt d|  d ƒt |ƒƒ}d|  d t |ƒ d|   }|t d|  d|  t dd|   ƒ  | ƒ }||fS )Nr   é`   é   r_   é�   é0   )r   r   )ZlamÚxiZomegÚrhor2   r2   r3   Ú_coeff_smootha  s
    $,rl   c                 C   s.   |t |ƒ ||   t || d  ƒ t| dƒ S )Nr   éÿÿÿÿ)r   r   )rD   Úcsrk   Úomegar2   r2   r3   Ú_hci  s    "ÿrp   c                 C   s”   || d||   d||   dd| | t d| ƒ  |d   }d||  d||   t|ƒ }t| ƒ}|||  t || ƒ|t|| ƒ   S )Nr   r;   é   )r   r	   r\   r   )rD   rn   rk   ro   Zc0ÚgammaZakr2   r2   r3   Ú_hsn  s    "ÿ rs   c           
      C   s  t |ƒ\}}dd| t|ƒ  ||  }t| ƒ}t|f| jjƒ}t|ƒ}td|||ƒ| d  t 	t|d |||ƒ|  ¡ |d< td|||ƒ| d  td|||ƒ| d   t 	t|d |||ƒ|  ¡ |d< t
d|ƒD ]D}|| |  d| t|ƒ ||d    || ||d    ||< qÔt|f| jjƒ}	t 	t||||ƒt|d |||ƒ | d d d…  ¡|	|d < t 	t|d |||ƒt|d |||ƒ | d d d…  ¡|	|d < t
|d ddƒD ]F}|||  d| t|ƒ |	|d    || |	|d    |	|< �q¶|	S )Nr   r;   r   rm   r_   )rl   r   Úlenr
   r+   r,   r   rp   r   ÚreducerK   rs   )
ÚsignalÚlambrk   ro   rn   ÚKZyprD   r]   Úyr2   r2   r3   Ú_cubic_smooth_coeffv  sB    ÿÿþ&ÿ
ÿÿÿÿ&ÿrz   c                 C   sâ   dt dƒ }t| ƒ}t|f| jjƒ}|t|ƒ }| d |t ||  ¡  |d< td|ƒD ] }| | |||d    ||< qXt|f| jƒ}||d  ||d   ||d < t|d ddƒD ] }|||d  ||   ||< q¸|d S )Néþÿÿÿr_   r   r   r;   rm   r'   ©	r   rt   r
   r+   r,   r   r   ru   rK   ©rv   Zzirx   ZyplusZpowersrD   Úoutputr2   r2   r3   Ú_cubic_coeff•  s     r   c                 C   sè   ddt dƒ  }t| ƒ}t|f| jjƒ}|t|ƒ }| d |t ||  ¡  |d< td|ƒD ] }| | |||d    ||< q\t|f| jjƒ}||d  ||d   ||d < t|d ddƒD ] }|||d  ||   ||< q¾|d S )Néýÿÿÿr;   r?   r   r   rm   g       @r|   r}   r2   r2   r3   Ú_quadratic_coeff¤  s     r�   rI   c                 C   s   |dkrt | |ƒS t| ƒS dS )a  
    Compute cubic spline coefficients for rank-1 array.

    Find the cubic spline coefficients for a 1-D signal assuming
    mirror-symmetric boundary conditions. To obtain the signal back from the
    spline representation mirror-symmetric-convolve these coefficients with a
    length 3 FIR window [1.0, 4.0, 1.0]/ 6.0 .

    Parameters
    ----------
    signal : ndarray
        A rank-1 array representing samples of a signal.
    lamb : float, optional
        Smoothing coefficient, default is 0.0.

    Returns
    -------
    c : ndarray
        Cubic spline coefficients.

    See Also
    --------
    cspline1d_eval : Evaluate a cubic spline at the new set of points.

    Examples
    --------
    We can filter a signal to reduce and smooth out high-frequency noise with
    a cubic spline:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.signal import cspline1d, cspline1d_eval
    >>> rng = np.random.default_rng()
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += rng.standard_normal(len(sig))*0.05  # add noise
    >>> time = np.linspace(0, len(sig))
    >>> filtered = cspline1d_eval(cspline1d(sig), time)
    >>> plt.plot(sig, label="signal")
    >>> plt.plot(time, filtered, label="filtered")
    >>> plt.legend()
    >>> plt.show()

    rI   N)rz   r   ©rv   rw   r2   r2   r3   r    ³  s    ,
c                 C   s   |dkrt dƒ‚nt| ƒS dS )aF  Compute quadratic spline coefficients for rank-1 array.

    Parameters
    ----------
    signal : ndarray
        A rank-1 array representing samples of a signal.
    lamb : float, optional
        Smoothing coefficient (must be zero for now).

    Returns
    -------
    c : ndarray
        Quadratic spline coefficients.

    See Also
    --------
    qspline1d_eval : Evaluate a quadratic spline at the new set of points.

    Notes
    -----
    Find the quadratic spline coefficients for a 1-D signal assuming
    mirror-symmetric boundary conditions. To obtain the signal back from the
    spline representation mirror-symmetric-convolve these coefficients with a
    length 3 FIR window [1.0, 6.0, 1.0]/ 8.0 .

    Examples
    --------
    We can filter a signal to reduce and smooth out high-frequency noise with
    a quadratic spline:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.signal import qspline1d, qspline1d_eval
    >>> rng = np.random.default_rng()
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += rng.standard_normal(len(sig))*0.05  # add noise
    >>> time = np.linspace(0, len(sig))
    >>> filtered = qspline1d_eval(qspline1d(sig), time)
    >>> plt.plot(sig, label="signal")
    >>> plt.plot(time, filtered, label="filtered")
    >>> plt.legend()
    >>> plt.show()

    rI   z.Smoothing quadratic splines not supported yet.N)Ú
ValueErrorr�   r‚   r2   r2   r3   r!   å  s    -
r%   c                 C   s  t |ƒ| t|ƒ }t|| jd�}|jdkr0|S t| ƒ}|dk }||d k}||B  }t| ||  ƒ||< t| d|d  ||  ƒ||< || }|jdkrž|S t|| jd�}	t|d ƒ t	¡d }
t
dƒD ]4}|
| }| d|d ¡}|	| | t|| ƒ 7 }	qÊ|	||< |S )a±  Evaluate a cubic spline at the new set of points.

    `dx` is the old sample-spacing while `x0` was the old origin. In
    other-words the old-sample points (knot-points) for which the `cj`
    represent spline coefficients were at equally-spaced points of:

      oldx = x0 + j*dx  j=0...N-1, with N=len(cj)

    Edges are handled using mirror-symmetric boundary conditions.

    Parameters
    ----------
    cj : ndarray
        cublic spline coefficients
    newx : ndarray
        New set of points.
    dx : float, optional
        Old sample-spacing, the default value is 1.0.
    x0 : int, optional
        Old origin, the default value is 0.

    Returns
    -------
    res : ndarray
        Evaluated a cubic spline points.

    See Also
    --------
    cspline1d : Compute cubic spline coefficients for rank-1 array.

    Examples
    --------
    We can filter a signal to reduce and smooth out high-frequency noise with
    a cubic spline:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.signal import cspline1d, cspline1d_eval
    >>> rng = np.random.default_rng()
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += rng.standard_normal(len(sig))*0.05  # add noise
    >>> time = np.linspace(0, len(sig))
    >>> filtered = cspline1d_eval(cspline1d(sig), time)
    >>> plt.plot(sig, label="signal")
    >>> plt.plot(time, filtered, label="filtered")
    >>> plt.legend()
    >>> plt.show()

    )r+   r   r   r;   rq   )r   rA   r   r+   Úsizert   r"   r   r-   ÚintrK   Úclipr   ©ÚcjZnewxZdxZx0rL   ÚNrc   rd   Zcond3ÚresultZjlowerÚiZthisjZindjr2   r2   r3   r"     s*    2


c                 C   sü   t |ƒ| | }t|ƒ}|jdkr&|S t| ƒ}|dk }||d k}||B  }t| ||  ƒ||< t| d|d  ||  ƒ||< || }|jdkr”|S t|ƒ}	t|d ƒ t¡d }
tdƒD ]4}|
| }| 	d|d ¡}|	| | t
|| ƒ 7 }	qº|	||< |S )aÙ  Evaluate a quadratic spline at the new set of points.

    Parameters
    ----------
    cj : ndarray
        Quadratic spline coefficients
    newx : ndarray
        New set of points.
    dx : float, optional
        Old sample-spacing, the default value is 1.0.
    x0 : int, optional
        Old origin, the default value is 0.

    Returns
    -------
    res : ndarray
        Evaluated a quadratic spline points.

    See Also
    --------
    qspline1d : Compute quadratic spline coefficients for rank-1 array.

    Notes
    -----
    `dx` is the old sample-spacing while `x0` was the old origin. In
    other-words the old-sample points (knot-points) for which the `cj`
    represent spline coefficients were at equally-spaced points of::

      oldx = x0 + j*dx  j=0...N-1, with N=len(cj)

    Edges are handled using mirror-symmetric boundary conditions.

    Examples
    --------
    We can filter a signal to reduce and smooth out high-frequency noise with
    a quadratic spline:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> from scipy.signal import qspline1d, qspline1d_eval
    >>> rng = np.random.default_rng()
    >>> sig = np.repeat([0., 1., 0.], 100)
    >>> sig += rng.standard_normal(len(sig))*0.05  # add noise
    >>> time = np.linspace(0, len(sig))
    >>> filtered = qspline1d_eval(qspline1d(sig), time)
    >>> plt.plot(sig, label="signal")
    >>> plt.plot(time, filtered, label="filtered")
    >>> plt.legend()
    >>> plt.show()

    r   r   r;   re   r_   )r   r   r„   rt   r#   r   r-   r…   rK   r†   r   r‡   r2   r2   r3   r#   b  s*    4


N)r$   )rI   )rI   )r%   r   )r%   r   )/Únumpyr   r   r   r   r   r   r   r	   r
   r   r   Znumpy.core.umathr   r   r   r   r   r   r   r   r   Z_spliner   r   Zscipy.specialr   Zscipy._lib._utilr   Ú__all__r   rR   rX   r   r   r   r   rl   rp   rs   rz   r   r�   r    r!   r"   r#   r2   r2   r2   r3   Ú<module>   s6   4,
   ÿ
8D3555
2
3
J