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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.    )absolute_import)division)print_functionN)wrappers)base_plugin)	http_util)metadata)plugin_util)providerz/tagsz/textd   c             C   sT   t | tjstdt| j}|dk r.tddg|d  tdtdg }| | 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)ZarrZndimsZslices r   Y/home/dcms/DCMS/lib/python3.7/site-packages/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_jsonifyD   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_eventc   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   s"   |j pi | jt| _|j| _dS )zInstantiates Angular TextPlugin 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contextr   r   r   __init__v   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   s(   | j j||tjd}dd t|D S )N)experiment_idr   c             S   s   i | ]\}}t ||qS r   )list).0runZtag_to_contentr   r   r   
<dictcomp>   s   z+TextV2Plugin.index_impl.<locals>.<dictcomp>)r"   Zlist_tensorsr   PLUGIN_NAMEsix	iteritems)r#   ctx
experimentmappingr   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#   r0   r+   tagr1   Zall_texttextr   r   r   	text_impl   s    zTextV2Plugin.text_implc             C   sN   t |j}t |j}|jd}|jd}| ||||}t||dS )Nr+   r9   zapplication/json)	r	   r$   environr(   argsr   r;   r   Respond)r#   requestr0   r1   r+   r9   responser   r   r   
text_route   s    zTextV2Plugin.text_routec             C   s2   t |j}t |j}| ||}t||dS )Nzapplication/json)r	   r$   r<   r(   r3   r   r>   )r#   r?   r0   r1   indexr   r   r   
tags_route   s    zTextV2Plugin.tags_routec             C   s   t | jt| jiS )N)
TAGS_ROUTErC   
TEXT_ROUTErA   )r#   r   r   r   get_plugin_apps   s    zTextV2Plugin.get_plugin_appsN)__name__
__module____qualname____doc__r   r%   r&   r'   r3   r;   r   RequestZapplicationrA   rC   rF   r   r   r   r   r   q   s   
		r   )rJ   
__future__r   r   r   r6   r   r.   Zwerkzeugr   Ztensorboard.pluginsr   Ztensorboard.backendr   Ztensorboard.plugins.textr   Ztensorboardr	   Ztensorboard.datar
   rD   rE   r    r   r   r   ZTBPluginr   r   r   r   r   <module>   s$   