B
    !`                 @   s   d Z ddlmZ ddlmZ ddlmZ ddl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ZG dd dejZdd ZdS )zzThe TensorBoard Histograms plugin.

See `http_api.md` in this directory for specifications of the routes for
this plugin.
    )absolute_import)division)print_functionN)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   s"   |j pi | jt| _|j| _dS )zInstantiates HistogramsPlugin via TensorBoard core.

        Args:
          context: A base_plugin.TBContext instance.
        N)Zsampling_hintsgetplugin_name_DEFAULT_DOWNSAMPLING_downsample_toZdata_provider_data_provider)selfcontext r   ^/home/dcms/DCMS/lib/python3.7/site-packages/tensorboard/plugins/histogram/histograms_plugin.py__init__8   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_appsC   s    z HistogramsPlugin.get_plugin_appsc             C   s   dS )NFr   )r   r   r   r   	is_activeI   s    zHistogramsPlugin.is_activec       
      C   sv   | j j||tjd}dd |D }xNt|D ]@\}}x6t|D ](\}}t|j}	|j	|	d|| |< qBW q.W |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six	iteritemsr   Zmarkdown_to_safe_htmlr    Zdisplay_name)
r   ctx
experimentmappingresultr   Ztag_to_contenttagZ	metadatumr    r   r   r   
index_implL   s    zHistogramsPlugin.index_implc             C   s   t jddS )Nztf-histogram-dashboard)Zelement_name)r
   ZFrontendMetadata)r   r   r   r   frontend_metadata^   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k	rt
d}	t|	||}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 %rr   c             S   s    g | ]}|j |j|j fqS r   )Z	wall_timestepnumpytolist)r   er   r   r   
<listcomp>   s    z4HistogramsPlugin.histograms_impl.<locals>.<listcomp>zapplication/json)r   r   Zread_tensorsr   r!   r	   ZRunTagFilterr   r   ZNotFoundErrorrandomRandom_downsample)r   r$   r(   r   r%   downsample_toZsample_countZall_histogramsZ
histogramsrngeventsr   r   r   histograms_implc   s     

z HistogramsPlugin.histograms_implc             C   s4   t |j}t |j}| j||d}t||dS )N)r%   zapplication/json)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%   r5   )
r   r   r9   r   argsr   r8   SAMPLE_SIZEr   r:   )r   r;   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*   r8   r   RequestZapplicationr   r   r   r   r   r   r   )   s   
!r   c                sF   |t  krt S | tjt  |}|   fdd|D S )a9  Uniformly choose a maximal at-most-`k`-subsequence of `xs`.

    If `k` is larger than `xs`, then the contents of `xs` itself will be
    returned.

    This differs from `random.sample` in that it returns a subsequence
    (i.e., order is preserved) and that it permits `k > len(xs)`.

    Args:
      rng: A `random` interface.
      xs: A sequence (`collections.abc.Sequence`).
      k: A non-negative integer.

    Returns:
      A new list whose elements are a subsequence of `xs` of length
      `min(k, len(xs))`, uniformly selected among such subsequences.
    c                s   g | ]} | qS r   r   )r   i)xsr   r   r1      s    z_downsample.<locals>.<listcomp>)lenlistsampler"   movesxrangesort)r6   rF   kindicesr   )rF   r   r4      s
    r4   )rC   
__future__r   r   r   r2   r"   Zwerkzeugr   Ztensorboardr   r   Ztensorboard.backendr   Ztensorboard.datar	   Ztensorboard.pluginsr
   Ztensorboard.plugins.histogramr   r   ZTBPluginr   r4   r   r   r   r   <module>   s   o