a
    Df                     @   s   d Z ddlZddlmZ ddlZddlZddlmZm	Z	 ddl
mZ ddlmZ d	d
 Zdd Zdd Zdd ZeedZG dd deZdS )a@  
Implements downsampling algorithms for large 1D datasets.

The algorithms implemented in this module have been adapted from
https://github.com/predict-idlab/plotly-resampler and are reproduced
along with the original license:

MIT License

Copyright (c) 2022 Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
    N)partial   )	NdOverlayOverlay)Area   )ResampleOperation1Dc                 C   s2   t |||  |||    | | ||    S )a-  Vectorized triangular area argmax computation.
    Parameters
    ----------
    prev_x : float
        The previous selected point is x value.
    prev_y : float
        The previous selected point its y value.
    avg_next_x : float
        The x mean of the next bucket
    avg_next_y : float
        The y mean of the next bucket
    x_bucket : np.ndarray
        All x values in the bucket
    y_bucket : np.ndarray
        All y values in the bucket
    Returns
    -------
    int
        The index of the point with the largest triangular area.
    )npabsZargmax)Zprev_xZprev_yZ
avg_next_xZ
avg_next_yZx_bucketZy_bucket r   k/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/operation/downsample.py_argmax_area*   s    

r   c           
   
   C   s   d}t |d D ]~}|| ||d  ||d    }}}	t| | || | ||	  |||	  | || ||| ||  }|||d < qt| | || | d |d | |d |d  ||d |d  |d  |d< d S )Nr      r   r   )ranger   mean)
xyn_out	sampled_xoffsetaiZo0Zo1Zo2r   r   r   _lttb_innerF   s4    $

r   c                 C   s   |j d d |d  }tjd|j d |dtj}tj|tjd}d|d< | j d d |d< | jjdkrv| tj} |jjdkr|tj}t	| |||| |S )a7  
    Downsample the data using the LTTB algorithm (python implementation).

    Args:
        x (np.ndarray): The x-values of the data.
        y (np.ndarray): The y-values of the data.
        n_out (int): The number of output points.
    Returns:
        np.array: The indexes of the selected datapoints.
    r   r   r   )startstopstep)dtyper   M)
shaper	   arangeastypeint64emptyr   kindviewr   )r   r   r   
block_sizer   r   r   r   r   _lttbf   s    r(   c              
   C   s&   t | }td|tdt|| S )a   
    Downsampling by selecting every n-th datapoint

    Args:
        x (np.ndarray): The x-values of the data.
        y (np.ndarray): The y-values of the data.
        n_out (int): The number of output points.
    Returns:
        np.array: The indexes of the selected datapoints.
    r   r   )lenr	   r!   maxmathceil)r   r   r   Z	n_samplesr   r   r   
_nth_point   s    r-   )lttbnthc                   @   s,   e Zd ZdZejdddgdZdddZdS )	downsample1dz
    Implements downsampling of a regularly sampled 1D dataset.

    Supports multiple algorithms:

        - `lttb`: Largest Triangle Three Buckets downsample algorithm
        - `nth`: Selects every n-th point.
    r.   r/   )defaultZobjectsNc                    s   t ttfrZt| j|d t tr: fddD }n fdd D }|S | jjrrt	| jj  t
| jjkrS fddtdD \}}|jtjkr|tj}t| jj }| jjd	krt trtd
|||| jj}j| S )N)keyc                    s   g | ]}|  qS r   map).0v_processr   r   
<listcomp>       z)downsample1d._process.<locals>.<listcomp>c                    s   i | ]\}}||  qS r   r3   )r5   kr6   r7   r   r   
<dictcomp>   r:   z)downsample1d._process.<locals>.<dictcomp>c                 3   s   | ]}  |V  qd S )N)Zdimension_values)r5   r   )elementr   r   	<genexpr>   r:   z(downsample1d._process.<locals>.<genexpr>r   r.   z-LTTB algorithm is not implemented for hv.Area)
isinstancer   r   r   r8   itemsclonepZx_rangeslicer)   widthr   r   r	   Zbool_r"   Zint8_ALGORITHMS	algorithmr   NotImplementedErrorZiloc)selfr=   r2   elementsxsZysZ
downsampleZsamplesr   )r8   r=   r   r8      s(    

zdownsample1d._process)N)__name__
__module____qualname____doc__paramZSelectorrF   r8   r   r   r   r   r0      s   	r0   )rN   r+   	functoolsr   numpyr	   rO   corer   r   Zelement.chartr   Zresampler   r   r   r(   r-   rE   r0   r   r   r   r   <module>   s    "