U
    Vcj                     @   sr   d 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	ZG d
d dejZdS )zzThe TensorBoard Histograms plugin.

See `http_api.md` in this directory for specifications of the routes for
this plugin.
    )wrappers)errors)plugin_util)	http_util)provider)base_plugin)metadatai  c                   @   sl   e Zd ZdZejZdZdd Zdd Z	dd Z
d	d
 Zdd ZdddZejjdd Zejjdd ZdS )HistogramsPlugina  Histograms Plugin for TensorBoard.

    This supports both old-style summaries (created with TensorFlow ops
    that output directly to the `histo` field of the proto) and new-
    style summaries (as created by the
    `tensorboard.plugins.histogram.summary` module).
    3   c                 C   s2   |j pi | jt| _|j| _tjddd| _	dS )zInstantiates HistogramsPlugin via TensorBoard core.

        Args:
          context: A base_plugin.TBContext instance.
        Z	histogramr   )Z	data_kindZlatest_known_versionN)
Zsampling_hintsgetplugin_name_DEFAULT_DOWNSAMPLING_downsample_toZdata_provider_data_providerr   Z_MetadataVersionChecker_version_checker)selfcontext r   S/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/plugins/histogram/histograms_plugin.py__init__2   s    
 zHistogramsPlugin.__init__c                 C   s   | j | jdS )N)z/histogramsz/tags)histograms_route
tags_router   r   r   r   get_plugin_appsA   s    z HistogramsPlugin.get_plugin_appsc                 C   s   dS )NFr   r   r   r   r   	is_activeG   s    zHistogramsPlugin.is_activec                 C   s   | j j||tjd}dd |D }| D ]Z\}}| D ]H\}}t|j}	t|j	}
| j
|
j||snq:|j|	d|| |< q:q*|S )zJReturn {runName: {tagName: {displayName: ..., description:
        ...}}}.)experiment_idr   c                 S   s   i | ]
}|i qS r   r   ).0runr   r   r   
<dictcomp>R   s      z/HistogramsPlugin.index_impl.<locals>.<dictcomp>)ZdisplayNamedescription)r   Zlist_tensorsr   PLUGIN_NAMEitemsr   Zmarkdown_to_safe_htmlr   Zparse_plugin_metadataZplugin_contentr   okversionZdisplay_name)r   ctx
experimentmappingresultr   Ztag_to_contenttagZ	metadatumr   Zmdr   r   r   
index_implJ   s$    zHistogramsPlugin.index_implc                 C   s   t jddS )Nztf-histogram-dashboard)Zelement_name)r   ZFrontendMetadatar   r   r   r   frontend_metadataa   s    z"HistogramsPlugin.frontend_metadataNc           
   	   C   s|   |dk	r|n| j }| jj||tj|tj|g|gdd}||i |d}|dkrft	d||f dd |D }	|	dfS )a  Result of the form `(body, mime_type)`.

        At most `downsample_to` events will be returned. If this value is
        `None`, then default downsampling will be performed.

        Raises:
          tensorboard.errors.PublicError: On invalid request.
        N)runstags)r   r   Z
downsampleZrun_tag_filterzNo histogram tag %r for run %rc                 S   s    g | ]}|j |j|j fqS r   )Z	wall_timestepZnumpytolist)r   er   r   r   
<listcomp>~   s     z4HistogramsPlugin.histograms_impl.<locals>.<listcomp>application/json)
r   r   Zread_tensorsr   r    r   ZRunTagFilterr   r   ZNotFoundError)
r   r$   r(   r   r%   downsample_toZsample_countZall_histogramsZ
histogramseventsr   r   r   histograms_implf   s     

z HistogramsPlugin.histograms_implc                 C   s4   t |j}t |j}| j||d}t||dS )N)r%   r1   )r   r   environr   r)   r   Respond)r   requestr$   r%   indexr   r   r   r      s    zHistogramsPlugin.tags_routec                 C   sX   t |j}t |j}|jd}|jd}| j||||| jd\}}t	|||S )z=Given a tag and single run, return array of histogram values.r(   r   )r%   r2   )
r   r   r5   r   argsr   r4   SAMPLE_SIZEr   r6   )r   r7   r$   r%   r(   r   bodyZ	mime_typer   r   r   r      s        
z!HistogramsPlugin.histograms_route)N)__name__
__module____qualname____doc__r   r    r   r:   r   r   r   r)   r*   r4   r   RequestZapplicationr   r   r   r   r   r   r	   #   s   

r	   N)r?   Zwerkzeugr   Ztensorboardr   r   Ztensorboard.backendr   Ztensorboard.datar   Ztensorboard.pluginsr   Ztensorboard.plugins.histogramr   r   ZTBPluginr	   r   r   r   r   <module>   s   