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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ddZdddZdddZdS )aP  Histogram summaries and TensorFlow operations to create them, V2 versions.

A histogram summary stores a list of buckets. Each bucket is encoded as
a triple `[left_edge, right_edge, count]`. Thus, a full histogram is
encoded as a tensor of dimension `[k, 3]`.

In general, the value of `k` (the number of buckets) will be a constant,
like 30. There are two edge cases: if there is no data, then there are
no buckets (the shape is `[0, 3]`); and if there is data but all points
have the same value, then there is one bucket whose left and right
endpoints are the same (the shape is `[1, 3]`).
    )absolute_import)division)print_functionN)tf2)summary_pb2)metadata)lazy_tensor_creator)tensor_util   c                sz   t jd|dttjjddp$tjj fdd}ttj	 tjjj
tjj
frltjjj||||S ||||S )a  Write a histogram summary.

    Arguments:
      name: A name for this summary. The summary tag used for TensorBoard will
        be this name prefixed by any active name scopes.
      data: A `Tensor` of any shape. Must be castable to `float64`.
      step: Explicit `int64`-castable monotonic step value for this summary. If
        omitted, this defaults to `tf.summary.experimental.get_step()`, which must
        not be None.
      buckets: Optional positive `int`. The output will have this
        many buckets, except in two edge cases. If there is no data, then
        there are no buckets. If there is data but all points have the
        same value, then there is one bucket whose left and right
        endpoints are the same.
      description: Optional long-form description for this summary, as a
        constant `str`. Markdown is supported. Defaults to empty.

    Returns:
      True on success, or false if no summary was emitted because no default
      summary writer was available.

    Raises:
      ValueError: if a default writer exists, but no step was provided and
        `tf.summary.experimental.get_step()` is None.
    N)display_namedescriptionsummary_scopec          	      sL   d |gd.\}}t j fdd}tjj|||dS Q R X d S )Nhistogram_summary)valuesc                  s
   t  S )N)_buckets )bucketsdatar   W/home/dcms/DCMS/lib/python3.7/site-packages/tensorboard/plugins/histogram/summary_v2.pylazy_tensorV   s    z9histogram.<locals>.histogram_summary.<locals>.lazy_tensor)tagtensorstepr   )r   ZLazyTensorCreatortfsummarywrite)r   r   Zhistogram_metadatar   r   _r   )namesummary_metadatar   )r   r   r   r   O   s    z$histogram.<locals>.histogram_summary)r   create_summary_metadatagetattrr   r   Zexperimentalr   
isinstanceZ
distributeZget_strategyZTPUStrategycompatZv1ZtpuZoutside_compilation)r   r   r   r   r   r   r   )r   r   r   r   	histogram,   s    

r#   c          	      s    dkrt  tdt tj  tj tj tjdgdttj	t
tjdd}dd } fd	d
}t|||S Q R X dS )a  Create a TensorFlow op to group data into histogram buckets.

    Arguments:
      data: A `Tensor` of any shape. Must be castable to `float64`.
      bucket_count: Optional positive `int` or scalar `int32` `Tensor`.
    Returns:
      A `Tensor` of shape `[k, 3]` and type `float64`. The `i`th row is
      a triple `[left_edge, right_edge, count]` for a single bucket.
      The value of `k` is either `bucket_count` or `1` or `0`.
    Nr   )shape)inputr   c               S   s   t jg dt jdS )N)r      )r%   dtype)r   Zconstantfloat64r   r   r   r   
when_empty   s    z_buckets.<locals>.when_emptyc                 s\   t jdt jd   t d}  fdd}fdd}t | ||S )N)input_tensorr   c        	         s   t  t j }  }t jt ||  t jd}t | d }t j| d}t jt j|ddt jd}t  d }t 	|d d ggd}|d d }|dd  }t j
t |||gdS )N)r(      )depthr   )r+   axisr$   )a)r   castr)   floorint32minimumZone_hotZ
reduce_sumlinspaceconcat	transposestack)	bucket_widthoffsetsbucket_indicesclamped_indicesone_hotsbucket_countsedges
left_edgesright_edges)bucket_countr   max_min_range_r   r   when_nonsingular   s    
z9_buckets.<locals>.when_nonempty.<locals>.when_nonsingularc                 sZ   } t | d g}t | d g}t t t j dt jg}t jt |||gdS )Ng      ?)r&   )r/   )r   r7   r0   sizer)   r6   )centerZbucket_startsZbucket_endsr=   )r   rC   r   r   when_singular   s    z6_buckets.<locals>.when_nonempty.<locals>.when_singular)r   Z
reduce_minZ
reduce_maxequalcond)Zis_singularrE   rH   )rA   r   )rB   rC   rD   r   when_nonempty   s    z_buckets.<locals>.when_nonempty)DEFAULT_BUCKET_COUNTr   Z
name_scope	debuggingZassert_scalarZassert_typer2   reshaper0   r)   rI   rF   rJ   )r   rA   Zis_emptyr*   rK   r   )rA   r   r   r   o   s    )r   c             C   s  |dkrt n|}t| t}|jdkrBtg d}nt|}t	|}|| }|dkr|}t|d |d t|jgg}n|| }	|| }
t
|
|	 t}t||d }t|g td|k}|j|j|fkst|j|j|fftj|dd}t|||d }|dd }|dd }t|||g }tj|tjd}tjd|d	}t }|jj| ||d
 |S )a  Create a histogram summary protobuf.

    Arguments:
      tag: String tag for the summary.
      data: A `np.array` or array-like form of any shape. Must have type
        castable to `float`.
      buckets: Optional positive `int`. The output will have this
        many buckets, except in two edge cases. If there is no data, then
        there are no buckets. If there is data but all points have the
        same value, then there is one bucket whose left and right
        endpoints are the same.
      description: Optional long-form description for this summary, as a
        `str`. Markdown is supported. Defaults to empty.

    Returns:
      A `summary_pb2.Summary` protobuf object.
    Nr   )r   r'   g      ?r,   )r.   r$   )r(   )r   r   )r   r   r   )rL   nparrayflattenZastypefloatrF   rN   minmaxr1   intr3   r6   Zaranger%   AssertionErrorsumr4   r	   Zmake_tensor_protor)   r   r   r   ZSummaryvalueadd)r   r   r   r   rA   rC   rB   rD   rG   r8   r9   r:   r;   r<   r=   r>   r?   r@   r   r   r   r   r   r   histogram_pb   s>    


"

rZ   )NNN)N)NN)__doc__
__future__r   r   r   numpyrO   Ztensorboard.compatr   r   Ztensorboard.compat.protor   Ztensorboard.plugins.histogramr   Ztensorboard.utilr   r	   rL   r#   r   rZ   r   r   r   r   <module>   s   
C
C