o
     ݱiJ                     @   s&   d dl ZddlmZ G dd dZdS )    N   )CompressedTrajectoryBufferc                   @   sV   e Zd ZdZ	ddededejfddZdefd	d
Z	dede
eejf fddZdS )TrajectorySamplerz^
    A class that samples sequences of observations and actions from a trajectory buffer.
    Nbufferseq_lenepisode_maskc           	      C   s   || _ || _t| j  | _g }d}t| j jD ]#\}}|du s$|| r9t||d | D ]}|||| g q-|}qtj	|tj
d| _tdt| j  dS )a"  
        Initialize the trajectory sampler.

        Args:
            buffer: The trajectory buffer containing the data.
            seq_len: The length of the sequences to sample.
            episode_mask: A binary mask indicating valid episodes. If None, all episodes are valid.
        r   Nr   )dtypez!Total number of valid sequences: )r   r   listkeys	enumerateepisode_endsrangeappendnparrayint64indicesprintlen)	selfr   r   r   r   Zepisode_startiZepisode_endj r   B/data/cameron/vidgen/unified-world-model/datasets/utils/sampler.py__init__   s   zTrajectorySampler.__init__returnc                 C   s
   t | jS N)r   r   )r   r   r   r   __len__(   s   
zTrajectorySampler.__len__indexc                 C   s@   | j | \}}i }| jD ]}| j| }||| }|||< q|S r   )r   r
   r   )r   r   startenddatakeyarrvaluer   r   r   sample_sequence+   s   


z!TrajectorySampler.sample_sequencer   )__name__
__module____qualname____doc__r   intr   ndarrayr   r   dictstrr%   r   r   r   r   r      s    
 r   )numpyr   r   r   r   r   r   r   r   <module>   s    