o
    c²“iù	  ã                   @   s*   d dl Zd dlmZ eG dd„ dƒƒZdS )é    N)ÚOldAPIStackc                   @   s`   e Zd ZdZdedefdd„Zddd	„Zdefd
d„Z	defdd„Z
dejfdd„Zdd„ ZdS )Ú
WindowStata˜  Handles/stores incoming dataset and provides window-based statistics.

    .. testcode::
        :skipif: True

        win_stats = WindowStat("level", 3)
        win_stats.push(5.0)
        win_stats.push(7.0)
        win_stats.push(7.0)
        win_stats.push(10.0)
        # Expect 8.0 as the mean of the last 3 values: (7+7+10)/3=8.0
        print(win_stats.mean())

    .. testoutput::

        8.0
    ÚnameÚnc                 C   s*   || _ || _dg| j  | _d| _d| _dS )zþInitializes a WindowStat instance.

        Args:
            name: The name of the stats to collect and return stats for.
            n: The window size. Statistics will be computed for the last n
                items received from the stream.
        Nr   )Úwindow_sizer   ÚitemsÚidxÚcount)Úselfr   r   © r   úW/home/ubuntu/.local/lib/python3.10/site-packages/ray/rllib/utils/metrics/window_stat.pyÚ__init__   s
   	
zWindowStat.__init__ÚreturnNc                 C   s@   || j | j< |  jd7  _|  jd7  _|  jt| j ƒ;  _dS )z/Pushes a new value/object into the data buffer.é   N)r   r   r	   Úlen)r
   Úobjr   r   r   Úpush-   s   zWindowStat.pushc                 C   ó   t t | jd| j… ¡ƒS )z<Returns the (NaN-)mean of the last `self.window_size` items.N)ÚfloatÚnpÚnanmeanr   r	   ©r
   r   r   r   Úmean8   ó   zWindowStat.meanc                 C   r   )z>Returns the (NaN)-stddev of the last `self.window_size` items.N)r   r   Únanstdr   r	   r   r   r   r   Ústd<   r   zWindowStat.stdc                 C   s6   | j stjg tjdS t | jd| j … g d¢¡ ¡ S )z8Returns ndarray with 0, 10, 50, 90, and 100 percentiles.)ÚdtypeN)r   é
   é2   éZ   éd   )r	   r   ÚndarrayÚfloat32Únanpercentiler   Útolistr   r   r   r   Ú	quantiles@   s   ÿþzWindowStat.quantilesc              	   C   s>   | j d t| jƒ| j d |  ¡ | j d |  ¡ | j d |  ¡ iS )NÚ_countÚ_meanÚ_stdÚ
_quantiles)r   Úintr	   r   r   r%   r   r   r   r   ÚstatsI   s
   üzWindowStat.stats)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr*   r   r   r   r   r   r   r!   r%   r+   r   r   r   r   r      s    
	r   )Únumpyr   Úray.rllib.utils.annotationsr   r   r   r   r   r   Ú<module>   s    