
    1Vh92                     h    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Zdd	Zdd
ZddZy)a  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]`, where the first `k - 1` buckets are closed-open and the
last bucket is closed-closed.

In general, the shape of the output histogram is always constant (`[k, 3]`).
In the case of empty data, the output will be an all-zero histogram of shape
`[k, 3]`, where all edges and counts are zeros. If there is data but all points
have the same value, then all buckets' left and right edges are the same and only
the last bucket has nonzero count.
    N)tf2)summary_pb2)metadata)lazy_tensor_creator)tensor_util   c                 F   |t         n|}t        j                  |      j                         j	                  t
              }|dk(  s|j                  dk(  rt        j                  |df      }nt        j                  |      }t        j                  |      }||z
  }|dk(  rmt        j                  |g|z        x}	}
t        j                  dg|dz
  z  |j                  gz         }t        j                  |	|
|g      j                         }n$||z  }||z
  }t        j                  ||z        j	                  t              }t        j                  ||dz
        }t        j                  |g      j                         t        j                  d|      k(  }|j                  |j                  |fk(  sJ |j                  |j                  |ff       t        j                   |d      }t        j"                  |||dz         }|dd }	|dd }
t        j                  |	|
|g      j                         }t%        j&                  |t        j(                        }t+        j,                  d|      }t/        j0                         }|j2                  j5                  | ||	       |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 shape will always be
        [buckets, 3]. If there is no data, then an all-zero array of shape
        [buckets, 3] will be returned. If there is data but all points have
        the same value, then all buckets' left and right endpoints are the
        same and only the last bucket has nonzero count. Defaults to 30 if
        not specified.
      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         )axisdtypedisplay_namedescription)tagr   tensor)DEFAULT_BUCKET_COUNTnparrayflattenastypefloatsizezerosminmax	transposefloorintminimumarangeshapesumlinspacer   make_tensor_protofloat64r   create_summary_metadatar   Summaryvalueadd)r   databucketsr   bucket_counthistogram_bucketsmin_max_range_
left_edgesright_edgesbucket_countsbucket_widthoffsetsbucket_indicesclamped_indicesone_hotsedgesr   summary_metadatasummarys                        X/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/plugins/histogram/summary_v2.pyhistogram_pbr@   )   sV   & ,3?'L88D>!!#**51DqDIINHHlA%67vvd|vvd|Q;')xx0E'FFJHHaSL1,<%=%KLM "[-8!ik  "L0LTkGXXg&<=DDSIN jj9IJOxx 12<<>"))<C H >>dii%>> L)A > FF8!4MKKdL1,<=EsJ)K "[-8!ik  **+<BJJOF77{ !!#GMM#(8HN    c                    t        j                        t        j                  d|      }t	        t         j
                  j                  dd      xs t         j
                  j                  } || d|g      5 \  }}t        j                  fd       }	t         j
                  j                  ||	||      cddd       S # 1 sw Y   yxY w)aU  Write a histogram summary.

    See also `tf.summary.scalar`, `tf.summary.SummaryWriter`.

    Writes a histogram to the current default summary writer, for later analysis
    in TensorBoard's 'Histograms' and 'Distributions' dashboards (data written
    using this API will appear in both places). Like `tf.summary.scalar` points,
    each histogram is associated with a `step` and a `name`. All the histograms
    with the same `name` constitute a time series of histograms.

    The histogram is calculated over all the elements of the given `Tensor`
    without regard to its shape or rank.

    This example writes 2 histograms:

    ```python
    w = tf.summary.create_file_writer('test/logs')
    with w.as_default():
        tf.summary.histogram("activations", tf.random.uniform([100, 50]), step=0)
        tf.summary.histogram("initial_weights", tf.random.normal([1000]), step=0)
    ```

    A common use case is to examine the changing activation patterns (or lack
    thereof) at specific layers in a neural network, over time.

    ```python
    w = tf.summary.create_file_writer('test/logs')
    with w.as_default():
    for step in range(100):
        # Generate fake "activations".
        activations = [
            tf.random.normal([1000], mean=step, stddev=1),
            tf.random.normal([1000], mean=step, stddev=10),
            tf.random.normal([1000], mean=step, stddev=100),
        ]

        tf.summary.histogram("layer1/activate", activations[0], step=step)
        tf.summary.histogram("layer2/activate", activations[1], step=step)
        tf.summary.histogram("layer3/activate", activations[2], step=step)
    ```

    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. The histogram is computed over its elements,
        which 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 all buckets' left and right endpoints are the same
        and only the last bucket has nonzero count. Defaults to 30 if not
        specified.
      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.
    Nr   summary_scopehistogram_summary)valuesc                      t               S N)_buckets)r.   r-   s   r?   lazy_tensorzhistogram.<locals>.lazy_tensor   s    D'**rA   )r   r   stepr   )tfstop_gradientr   r)   getattrr>   experimentalrC   r   LazyTensorCreatorwrite)
namer-   rJ   r.   r   r=   rC   r   _rI   s
    ` `      r?   	histogramrS   h   s    L D!D77{
 	

''$? 	$::##  
!4$*?
 
	#q
 
	.	.	+ 
/	+ zz%	   

 
 
s   >?CCc                     t         t        j                  d      5  t        j                  j	                         t        j                  j                  t        j                         t        j                  j                  d      t        j                   dg       t        j                   t        j                         t        j                         t        j                  t        j                  d      t        j                  d            }fd} fd}t        j                   |||      cddd       S # 1 sw Y   yxY w)	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 non-negative `int` or scalar `int32` `Tensor`,
        defaults to 30.
    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 `0` (when input data
      is empty).
    Nr.   r   r   )r$   )inputc                  R    t        j                   dft         j                        S )a6  When input data is empty or bucket_count is zero.

            1. If bucket_count is specified as zero, an empty tensor of shape
              (0, 3) will be returned.
            2. If the input data is empty, a tensor of shape (bucket_count, 3)
              of all zero values will be returned.
            r
   r   )rK   r   r(   )r/   s   r?   
when_emptyz_buckets.<locals>.when_empty   s     88\1-RZZ@@rA   c                      t        j                        t        j                        z
  t        j                  d      } fd}fd}t        j                  | ||      S )N)input_tensorr   c                     t        j                  	t         j                        z  } 
z
  }t        j                  t        j                  || z        t         j                        }t        j
                  |	dz
        }t        j                  |	t         j                        }t        j                  t        j                  |d      t         j                        }t        j                  	dz         }t        j                  |dd ggd      }|dd }|dd }t        j                  t        j                  |||g            S )	z)When input data contains multiple values.r   r   )depthr   r   )rY   r   Nr   a)rK   castr(   r    int32r"   one_hot
reduce_sumr&   concatr   stack)r7   r8   r9   r:   r;   r6   r<   r4   r5   r/   r-   r2   r1   r3   s            r?   when_multiple_valuesz=_buckets.<locals>.when_nonempty.<locals>.when_multiple_values   s   %bjj(II+!#HHW|34BHH" #%**^\A=M"N ::#<rzz !#MMxa@**! D$q0@A 		5":v"6:"3BZ
#ABi||hh
KGH rA   c                  <   t        j                  g      } t        j                  gd      }t        j                  t        j                  |dd ggd      d t         j                        }t        j
                  t        j                  | | |g            S )z/When input data contains a single unique value.r   Nr   r   r\   )rK   fillr^   rb   r(   r   rc   )r<   zeroesr6   r/   	data_sizer2   s      r?   when_single_valuez:_buckets.<locals>.when_nonempty.<locals>.when_single_value  s     5 ,3 "IIvcr{YK8!<]lK**! ||bhhum/L&MNNrA   )rK   
reduce_min
reduce_maxequalcond)	has_single_valuerd   ri   r2   r1   r3   r/   r-   rh   s	      @@@r?   when_nonemptyz_buckets.<locals>.when_nonempty   se    ==d3D==d3DD[F!xx2 8O 77 "35I rA   )r   rK   
name_scope	debuggingassert_scalarassert_typer_   mathmaximumreshaper^   r(   r   
logical_orrl   
less_equalrm   )r-   r/   is_emptyrW   ro   rh   s   ``   @r?   rH   rH      s     +	y	! J<
""<0
  rxx8wwq,7zz$rd+wwtRZZ(GG$'	==HHY"BMM,$B
	A2	h wwx];UJ< J< J<s   D EE)NN)NNNrG   )__doc__numpyr   tensorboard.compatr   rK   tensorboard.compat.protor   tensorboard.plugins.histogramr   tensorboard.utilr   r   r   r@   rS   rH    rA   r?   <module>r      s9     ( 0 2 0 (  ;~a
HY<rA   