a
    d=ic8                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddl	mZ
 ddlmZ ddlmZ ddlmZ dd Zd	d
 Zdd Zdd Zdd Zdd Zdd ZdS )z?Utilities to migrate legacy protos to their modern equivalents.    N)	event_pb2)summary_pb2)metadata)tensor_utilc                 C   s   |  ds| S | jj}dd |D }t|t|krPtdd t||D rP| S t }||  |jjd d = |jj	| |S )Nsummaryc                 S   s   g | ]}t |qS  )migrate_value).0valuer   r   h/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/tensorboard/data_compat.py
<listcomp>!       z!migrate_event.<locals>.<listcomp>c                 s   s   | ]\}}||u V  qd S )Nr   )r	   xyr   r   r   	<genexpr>#   s   z migrate_event.<locals>.<genexpr>)
ZHasFieldr   r
   lenallzipr   EventZCopyFromextend)eventZ
old_values
new_valuesresultr   r   r   migrate_event   s    

r   c                 C   s*   t tttd| d}|r&|| S | S )a@  Convert `value` to a new-style value, if necessary and possible.

    An "old-style" value is a value that uses any `value` field other than
    the `tensor` field. A "new-style" value is a value that uses the
    `tensor` field. TensorBoard continues to support old-style values on
    disk; this method converts them to new-style values so that further
    code need only deal with one data format.

    Arguments:
      value: A `Summary.Value` object. This argument is not modified.

    Returns:
      If the `value` is an old-style value for which there is a new-style
      equivalent, the result is the new-style value. Otherwise---if the
      value is already new-style or does not yet have a new-style
      equivalent---the value will be returned unchanged.

    :type value: Summary.Value
    :rtype: Summary.Value
    )histoimageaudiosimple_valuer
   )_migrate_histogram_value_migrate_image_value_migrate_audio_value_migrate_scalar_valuegetZ
WhichOneof)r
   handlerr   r   r   r   .   s    r   c                 C   s   t |}tjj| ||dS )N)tagr   Ztensor)r   Zmake_tensor_protor   SummaryValue)r$   r   dataZtensor_protor   r   r   make_summaryL   s    
r(   c           
         s   | j }|j t }t fddt|D |}t fddtt|D d}||krntjddgtjd}nP ||d   |j	|| }|j
g| }||jg }tj|| gtjd }tj| jjp| j| jjd	}	t| j|	|S )
a  Convert `old-style` histogram value to `new-style`.

    The "old-style" format can have outermost bucket limits of -DBL_MAX and
    DBL_MAX, which are problematic for visualization. We replace those here
    with the actual min and max values seen in the input data, but then in
    order to avoid introducing "backwards" buckets (where left edge > right
    edge), we first must drop all empty buckets on the left and right ends.
    c                 3   s   | ]} | d kr|V  qdS r   Nr   r	   iZbucket_countsr   r   r   `   r   z+_migrate_histogram_value.<locals>.<genexpr>c                 3   s   | ]} | d kr|V  qdS r)   r   r*   r,   r   r   r   a   r   r      )Zdtype   display_namedescription)r   Zbucketr   nextrangereversednpZzerosfloat32Zbucket_limitminmaxarrayZ	transposehistogram_metadatacreate_summary_metadatar   r1   r$   summary_descriptionr(   )
r
   Zhistogram_valuenstartendZbucketsZinner_edgesZbucket_leftsZbucket_rightssummary_metadatar   r,   r   r   S   s&    	 
r   c                 C   sV   | j }t|jdt|jd|jg}tj| jj	p:| j
| jjdd}t| j
||S )NasciiT)r1   r2   converted_to_tensor)r   strwidthencodeheightZencoded_image_stringimage_metadatar<   r   r1   r$   r=   r(   )r
   Zimage_valuer'   rA   r   r   r   r   ~   s    r   c                 C   sH   | j }|jdgg}tj| jjp"| j| jjtj	ddd}t
| j||S )Nr   ZWAVT)r1   r2   encodingrC   )r   Zencoded_audio_stringaudio_metadatar<   r   r1   r$   r=   ZEncodingr&   r(   )r
   Zaudio_valuer'   rA   r   r   r   r       s    
r    c                 C   s0   | j }tj| jjp| j| jjd}t| j||S )Nr0   )r   scalar_metadatar<   r   r1   r$   r=   r(   )r
   Zscalar_valuerA   r   r   r   r!      s    r!   )__doc__numpyr6   Ztensorboard.compat.protor   r   Ztensorboard.plugins.audior   rJ   Ztensorboard.plugins.histogramr;   Ztensorboard.plugins.imagerH   Ztensorboard.plugins.scalarrK   Ztensorboard.utilr   r   r   r(   r   r   r    r!   r   r   r   r   <module>   s   +