
    &`i                         d dl mZ d dlmZ d dlmZmZmZmZm	Z	 d dl
Zd dlmZ d dlmZ d dlmZ e G d d	e                      ZdS )
    )deque)chain)AnyDictListOptionalUnionN)	StatsBase)batch_values_to_cpu)DeveloperAPIc            	       L    e Zd ZdZdZddee         f fdZd Zde	e
ef         f fdZd	e	e
ef         ddf fd
ZdeddfdZddedeed f         fdZdefdZ	 ddededeee         ef         fdZded          ddfdZedde	e
ef         fd            Zde
fdZ xZS )ItemSeriesStatsa  A Stats object that tracks a series of items.

    Use this if you want to track a series of items that should not be reduced.
    An example would be to log actions and translate them into a chart to visualize
    the distribution of actions outside of RLlib.

    This class will check if logged items are GPU tensors.
    If they are, they will be converted to CPU memory.

    Note that at the root level, the internal item list can grow to `window * len(incoming_stats)`.
    item_seriesNwindowc                      t                      j        |i | || _        g | _        |                     g            d S N)super__init___windowitems
_set_items)selfr   argskwargs	__class__s       }/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/utils/metrics/stats/item_series.pyr   zItemSeriesStats.__init__   sE    $)&)))35
    c                 j    | j         r$| j        rt          || j                   | _        d S || _        d S )N)maxlen)r   is_leafr   r   )r   	new_itemss     r   r   zItemSeriesStats._set_items"   s>     < 	#DL 	#y>>>DJJJ #DJJJr   returnc                 p    t                                                      }| j        |d<   | j        |d<   |S Nr   r   )r   	get_stater   r   r   stater   s     r   r%   zItemSeriesStats.get_state,   s3    !!##g,hr   r'   c                 ~    t                                          |           |d         | _        |d         | _        d S r$   )r   	set_stater   r   r&   s     r   r)   zItemSeriesStats.set_state2   s5    %   7^
Xr   itemc                     | j                             |           | j        r8t          | j                   | j        k    r| j                                          dS dS dS )zPushes a item into this Stats object.

        This method does not handle GPU tensors.

        Args:
            item: The item to push. Can be of any type but data should be in CPU memory.
        N)r   appendr   lenpopleft)r   r*   s     r   pushzItemSeriesStats.push7   sb     	
$< 	!C
OOdl::J     	! 	!::r   Tcompilec                     | j         }|                     g            t          |          }|r|S |                                 }|                    |           |S )a#  Reduces the internal values list according to the constructor settings.

        Args:
            compile: Argument is ignored for ItemSeriesStats.

        Returns:
            The reduced value (can be of any type, depending on the input values and
            reduction method).
        )r   r   r   clone)r   r0   r   return_statss       r   reducezItemSeriesStats.reduceC   s^     
#E** 	Lzz||&&&r   c                 *    t          | j                  S )z.Returns the length of the internal items list.)r-   r   r   s    r   __len__zItemSeriesStats.__len__Y   s    4:r   Flatest_merged_onlyc                     |r| j         rt          d          |r| j        |rt          j        S g S | j        }n| j        }t          |          }|rt          |          dk    rt          j        S |S )aR  Returns the internal items list.

        This does not alter the internal items list.

        Args:
            compile: If True and items list is empty, returns np.nan. Otherwise returns the items list.
            latest_merged_only: If True, only considers the latest merged values.
                This parameter only works on aggregation stats (root or intermediate nodes).
                When enabled, peek() will only use the items from the most recent merge operation.

        Returns:
            The internal items list, or np.nan if compile=True and items list is empty.
        zPlatest_merged_only can only be used on aggregation stats objects (is_leaf=False)Nr   )r    
ValueErrorlatest_mergednpnanr   r   r-   )r   r0   r8   r   s       r   peekzItemSeriesStats.peek]   s    "  	$, 	"    
	!) "6M	&EE JE#E**  	s5zzQ6Mr   incoming_statsc                     | j         r
J d            d |D             }t          t          j        |                    }t          | j                  |z   }|| _        | j         s	|| _        dS dS )zMerges ItemSeriesStats objects.

        Args:
            incoming_stats: The list of ItemSeriesStats objects to merge.

        Returns:
            None. The merge operation modifies self in place.
        zRItemSeriesStats should only be merged at aggregation stages (root or intermediate)c                     g | ]	}|j         
S  )r   ).0ss     r   
<listcomp>z)ItemSeriesStats.merge.<locals>.<listcomp>   s    555QW555r   N)r    listr   from_iterabler   r;   )r   r?   r!   	all_itemss       r   mergezItemSeriesStats.merge   s     	` 	`_	` 	` 65n555	,Y7788	$$y0	
 | 	+!*D	+ 	+r   c                 p    t          j        | |          }|i |d|d         iS | i |d| j        iS |S )z;Returns the initialization arguments for this Stats object.)stats_objectr'   Nr   )r
   _get_init_argsr   )rK   r'   
super_argss      r   rL   zItemSeriesStats._get_init_args   sx     -<uUUU
%/   %,.   r   c                 8    d| j          dt          |            dS )NzItemSeriesStats(window=z; len=))r   r-   r6   s    r   __repr__zItemSeriesStats.__repr__   s#    IIISYYIIIIr   r   )T)TF)NN)__name__
__module____qualname____doc__stats_cls_identifierr   intr   r   r   strr   r%   r)   r/   boolr	   r4   r7   r   r>   rI   staticmethodrL   rP   __classcell__)r   s   @r   r   r      s       
 
 ) x}      # # #4S>      'tCH~ '$ ' ' ' ' ' '

! 
! 
! 
! 
! 
! d eC9J4J.K    ,    
 @E* **8<*	tCy#~	* * * *X+D):$; + + + + +.  c3h    \J# J J J J J J J Jr   r   )collectionsr   	itertoolsr   typingr   r   r   r   r	   numpyr<   "ray.rllib.utils.metrics.stats.baser
   #ray.rllib.utils.metrics.stats.utilsr   ray.util.annotationsr   r   rB   r   r   <module>rb      s                3 3 3 3 3 3 3 3 3 3 3 3 3 3     8 8 8 8 8 8 C C C C C C - - - - - - dJ dJ dJ dJ dJi dJ dJ dJ dJ dJr   