U
    Vc@                     @   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	Zd
ZdZdd Zdd Zdd ZG dd dejZdS )zAngular Version of Text Plugin.    N)wrappers)base_plugin)	http_util)metadata)plugin_util)providerz/tagsz/textd   c                 C   sX   t | tjstdt| j}|dk r.tddg|d  tdtdg }| t| S )a  Given a np.ndarray with nDims > 2, reduce it to 2d.

    It does this by selecting the zeroth coordinate for every dimension except
    the last two.

    Args:
      arr: a numpy ndarray of dimension at least 2.

    Returns:
      A two-dimensional subarray from the input array.

    Raises:
      ValueError: If the argument is not a numpy ndarray, or the dimensionality
        is too low.
    z%reduce_to_2d requires a numpy.ndarray   z4reduce_to_2d requires an array of dimensionality >=2r   N)
isinstancenpZndarray
ValueErrorlenshapeslicetuple)ZarrZndimsZslices r   N/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/plugins/text_v2/text_v2_plugin.pyreduce_to_2d$   s    
r   c                 C   sB   | j }d}|s|  ||fS t|dkr4d}t| } |  ||fS )a>  Take a numpy.ndarray containing strings, and convert it into a
    json-compatible list, also squashing it to two dimensions if necessary.

    If the ndarray contains a single scalar string, then that ndarray is
    converted to a list. If it contains an array of strings,
    that array is converted to a list.  If the array contains dimensionality
    greater than 2, all but two of the dimensions are removed, and a squashed
    boolean is set to true.  Returned is a list, the shape of the original
    array, and a boolean indicating squashsing has occured.

    Args:
        text_arr: A numpy.ndarray containing strings.

    Returns:
        a tuple containing:
            The JSON-compatible list
            The shape of the array (before being squashed)
            A boolean indicating if the array was squashed
    Fr	   T)r   tolistr   r   )Z
text_ndarroriginal_shape	truncatedr   r   r   reduce_and_jsonify?   s    r   c                 C   s   t |\}}}| ||||dS )zCConvert a text event into a JSON-compatible response with rank <= 2)	wall_timestepZstring_arrayr   r   )r   )r   r   Zstring_ndarrayZformatted_string_arrayr   r   r   r   r   create_event^   s    
r   c                   @   sd   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	e
jjdd Ze
jjdd Zdd ZdS )TextV2Pluginz#Angular Text Plugin For TensorBoardZtext_v2c                 C   s2   |j pi | jt| _|j| _tjddd| _	dS )zInstantiates Angular TextPlugin via TensorBoard core.

        Args:
          context: A base_plugin.TBContext instance.
        textr   )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contextr   r   r   __init__q   s    
 zTextV2Plugin.__init__c                 C   s   t jddddS )NTzText v2F)Zis_ng_componentZtab_nameZdisable_reload)r   ZFrontendMetadatar#   r   r   r   frontend_metadata   s
      zTextV2Plugin.frontend_metadatac                 C   s   dS )zDetermines whether this plugin is active.

        This plugin is only active if TensorBoard sampled any text summaries.

        Returns:
          Whether this plugin is active.
        Fr   r&   r   r   r   	is_active   s    zTextV2Plugin.is_activec           
      C   sx   | j j||tjd}dd |D }| D ]H\}}| D ]6\}}t|j}	| j|	j	||sbq:|| 
| q:q*|S )N)experiment_idr   c                 S   s   i | ]
}|g qS r   r   ).0runr   r   r   
<dictcomp>   s      z+TextV2Plugin.index_impl.<locals>.<dictcomp>)r!   Zlist_tensorsr   PLUGIN_NAMEitemsZparse_plugin_metadataZplugin_contentr"   okversionappend)
r#   ctx
experimentmappingresultr+   Ztag_to_contenttagZ	metadatumZmdr   r   r   
index_impl   s    zTextV2Plugin.index_implc              	   C   sV   | j j||tj| jtj|g|gdd}||i |d }|d krHg S dd |D S )N)runstags)r)   r   Z
downsampleZrun_tag_filterc                 S   s   g | ]}t |j|j|jqS r   )r   r   r   numpy)r*   dr   r   r   
<listcomp>   s     z*TextV2Plugin.text_impl.<locals>.<listcomp>)r!   Zread_tensorsr   r-   r    r   ZRunTagFilterr   )r#   r2   r+   r6   r3   Zall_textr   r   r   r   	text_impl   s    zTextV2Plugin.text_implc                 C   sN   t |j}t |j}|jd}|jd}| ||||}t||dS )Nr+   r6   application/json)	r   r$   environr)   argsr   r=   r   Respond)r#   requestr2   r3   r+   r6   responser   r   r   
text_route   s    zTextV2Plugin.text_routec                 C   s2   t |j}t |j}| ||}t||dS )Nr>   )r   r$   r?   r)   r7   r   rA   )r#   rB   r2   r3   indexr   r   r   
tags_route   s    zTextV2Plugin.tags_routec                 C   s   t | jt| jiS )N)
TAGS_ROUTErF   
TEXT_ROUTErD   r&   r   r   r   get_plugin_apps   s
      zTextV2Plugin.get_plugin_appsN)__name__
__module____qualname____doc__r   r%   r'   r(   r7   r=   r   RequestZapplicationrD   rF   rI   r   r   r   r   r   l   s   


r   )rM   r:   r   Zwerkzeugr   Ztensorboard.pluginsr   Ztensorboard.backendr   Ztensorboard.plugins.textr   Ztensorboardr   Ztensorboard.datar   rG   rH   r   r   r   r   ZTBPluginr   r   r   r   r   <module>   s   