
    qi                     P   d dl Z d dlZd dlZd dlZd dlZej        j         G d d                      Z G d d          Z	 G d dej
                  Zdeeef         d	efd
Zded	eeef         fdZdej        ez  deeef         d	dfdZdej        ez  d	eeef         fdZdS )    Nc                   t    e Zd ZU ej        ed<   ej        ed<   dZej        dz  ed<   dZej        dz  ed<   dS )	NormStatsmeanstdNq01q99)__name__
__module____qualname__numpydanticNDArray__annotations__r   r        8/home/robot-lab/Pi0.5_yam/src/openpi/shared/normalize.pyr   r   	   s`         

		&*C	t	#***&*C	t	#*****r   r   c                   d    e Zd ZdZd Zdej        ddfdZdefdZ	d Z
dej        ddfd	Zd
 ZdS )RunningStatsz1Compute running statistics of a batch of vectors.c                 v    d| _         d | _        d | _        d | _        d | _        d | _        d | _        d| _        d S )Nr   i  )_count_mean_mean_of_squares_min_max_histograms
_bin_edges_num_quantile_bins)selfs    r   __init__zRunningStats.__init__   sC    
 $		"&r   batchreturnNc                     |                     d|j        d                   }|j        \  }} j        dk    rt          j        |d           _        t          j        |dz  d           _        t          j        |d           _        t          j	        |d           _
         fdt          |          D              _         fdt          |          D              _        n| j        j        k    rt          d          t          j	        |d          }t          j        |d          }t          j        | j
        k              }t          j        | j        k               }t          j         j
        |           _
        t          j         j        |           _        |s|r                                   xj        |z  c_        t          j        |d          }t          j        |dz  d          }	 xj        | j        z
  | j        z  z  z  c_         xj        |	 j        z
  | j        z  z  z  c_                             |           dS )	z
        Update the running statistics with a batch of vectors.

        Args:
            vectors (np.ndarray): An array where all dimensions except the last are batch dimensions.
        r   )axis   c                 B    g | ]}t          j        j                  S r   )npzerosr   ).0_r   s     r   
<listcomp>z'RunningStats.update.<locals>.<listcomp>,   s&    ```a)@ A A```r   c                     g | ]<}t          j        j        |         d z
  j        |         d z   j        dz             =S )g|=   )r&   linspacer   r   r   )r(   ir   s     r   r*   z'RunningStats.update.<locals>.<listcomp>-   sV        DIaL50$)A,2FH_bcHcdd  r   zGThe length of new vectors does not match the initialized vector length.N)reshapeshaper   r&   r   r   r   minr   maxr   ranger   r   size
ValueErroranymaximumminimum_adjust_histograms_update_histograms)
r   r   num_elementsvector_lengthnew_maxnew_minmax_changedmin_changed
batch_meanbatch_mean_of_squaress
   `         r   updatezRunningStats.update   sT    b%+b/22&+k#m;!Q///DJ$&GE1H1$=$=$=D!u1---DIu1---DI````5Q^K_K_```D   }--  DOO
 
// !jkkkfU+++GfU+++G&49!455K&49!455K
49g66DI
49g66DI *k *'')))|#WU+++
 "qq 9 9 9 	

zDJ.<$+3MNN

"7$:O"OT`cgcnTn!oo&&&&&r   c                    | j         dk     rt          d          | j        | j        dz  z
  }t	          j        t	          j        d|                    }|                     ddg          \  }}t          | j        |||          S )z
        Compute and return the statistics of the vectors processed so far.

        Returns:
            dict: A dictionary containing the computed statistics.
        r$   z2Cannot compute statistics for less than 2 vectors.r   g{Gz?gGz?)r   r   r   r   )	r   r5   r   r   r&   sqrtr7   _compute_quantilesr   )r   variancestddevr   r   s        r   get_statisticszRunningStats.get_statisticsI   s     ;??QRRR(4:q=8Ax0011**D$<88Sdjf#3GGGGr   c                 R   t          t          | j                            D ]}| j        |         }t	          j        | j        |         | j        |         | j        dz             }t	          j	        |dd         || j        |                   \  }}|| j        |<   || j        |<   dS )z*Adjust histograms when min or max changes.r,   Nr"   )binsweights)
r3   lenr   r   r&   r-   r   r   r   	histogram)r   r.   	old_edges	new_edgesnew_histr)   s         r   r9   zRunningStats._adjust_histogramsX   s    s4+,,-- 	+ 	+A*IDIaL$)A,@WZ[@[\\I ,y"~ItO_`aObcccKHa"*DQ!*DOA	+ 	+r   c                     t          |j        d                   D ]E}t          j        |dd|f         | j        |                   \  }}| j        |xx         |z  cc<   FdS )z#Update histograms with new vectors.r,   N)rK   )r3   r0   r&   rN   r   r   )r   r   r.   histr)   s        r   r:   zRunningStats._update_histogramsd   sv    u{1~&& 	( 	(Al5A;T_Q5GHHHGD!Q4'	( 	(r   c                 F   g }|D ]}|| j         z  }g }t          | j        | j        d          D ]I\  }}t	          j        |          }t	          j        ||          }	|                    ||	                    J|                    t	          j        |                     |S )z&Compute quantiles based on histograms.T)strict)	r   zipr   r   r&   cumsumsearchsortedappendarray)
r   	quantilesresultsqtarget_countq_valuesrS   edgesrW   idxs
             r   rF   zRunningStats._compute_quantilesj   s     	/ 	/At{?LH"4#3T_TRRR , ,e4ofl;;c
++++NN28H--....r   )r	   r
   r   __doc__r   r&   ndarrayrC   r   rI   r9   r:   rF   r   r   r   r   r      s        ;;' ' ')'BJ )'4 )' )' )' )'VH	 H H H H
+ 
+ 
+(
 (t ( ( ( (    r   r   c                   *    e Zd ZU eeef         ed<   dS )_NormStatsDict
norm_statsN)r	   r
   r   dictstrr   r   r   r   r   re   re   x   s'         S)^$$$$$$r   re   rf   r    c                 J    t          |                               d          S )z2Serialize the running statistics to a JSON string.rf   r$   )indent)re   model_dump_jsonrj   s    r   serialize_jsonrm   |   s$    Z000@@@JJJr   datac                 H    t          di t          j        |           j        S )z6Deserialize the running statistics from a JSON string.r   )re   jsonloadsrf   )rn   s    r   deserialize_jsonrr      s#    --DJt,,--88r   	directoryc                     t          j        |           dz  }|j                            dd           |                    t          |                     dS )z,Save the normalization stats to a directory.norm_stats.jsonT)parentsexist_okN)pathlibPathparentmkdir
write_textrm   )rs   rf   paths      r   saver~      sR    <	""%66DKdT222OON:../////r   c                     t          j        |           dz  }|                                st          d|           t	          |                                          S )z.Load the normalization stats from a directory.ru   zNorm stats file not found at: )rx   ry   existsFileNotFoundErrorrr   	read_text)rs   r}   s     r   loadr      sW    <	""%66D;;== I G G GHHHDNN,,---r   )rp   rx   numpyr&   r   pydanticdataclasses	dataclassr   r   	BaseModelre   rg   rh   rm   rr   ry   r~   r   r   r   r   <module>r      s              
+ + + + + + +  +d d d d d d d dN% % % % %X' % % %KtCN3 K K K K K
93 94Y#7 9 9 9 9
0GL3& 0Di4H 0T 0 0 0 0.GL3& .4Y+? . . . . . .r   