o
    2hM                     @   sb   d Z ddlZddlZddlmZ dZejddG dd dZefd	d
Z	efddZ
dd ZdS )z"Package for histogram compression.    N)Tuple)	r   i  i3  i  i  i  i   it$  i'  T)frozenc                   @   s8   e Zd ZU dZeed< eed< deeef fddZdS )CompressedHistogramValuezRepresents a value in a compressed histogram.

    Attributes:
      basis_point: Compression point represented in basis point, 1/100th of a
        percent.
      value: Cumulative weight at the basis point.
    basis_pointvaluereturnc                 C   s   | j | jfS )z1Returns the basis point and the value as a tuple.)r   r   )self r	   f/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorboard/plugins/distribution/compressor.pyas_tuple*   s   z!CompressedHistogramValue.as_tupleN)__name__
__module____qualname____doc__float__annotations__r   r   r	   r	   r	   r
   r      s
   
 r   c                 C   sb  | j s
dd |D S t| j}t| j}||d  | pd  }g }d}|t|k rtj	||| dd}|t|k r|| }|dkrK||d  nd	}	||	krV|d7 }q7|rZ|	s^| j
}
n
t||d  | j
}
t
|| | j}t|| |	||
|}|t|| |  |d7 }nn|t|k s-|t|k r|t|| | j  |d7 }|t|k s|S )
a  Creates fixed size histogram by adding compression to accumulated state.

    This routine transforms a histogram at a particular step by interpolating its
    variable number of buckets to represent their cumulative weight at a constant
    number of compression points. This significantly reduces the size of the
    histogram and makes it suitable for a two-dimensional area plot where the
    output of this routine constitutes the ranges for a single x coordinate.

    Args:
      histo: A HistogramProto object.
      bps: Compression points represented in basis points, 1/100ths of a percent.
          Defaults to normal distribution.

    Returns:
      List of values for each basis point.
    c                 S      g | ]	}t |d  qS         r   r   .0br	   r	   r
   
<listcomp>C       z,compress_histogram_proto.<locals>.<listcomp>      ?r   rightside   r   )numnparraybucketlistbucket_limitsumcumsumlensearchsortedminmax_lerpappendr   r   )histobpsr$   r&   weightsvaluesjir(   cumsum_prevlhsrhsweightr	   r	   r
   compress_histogram_proto0   s<   
r9   c                 C   s  t | } | jsdd |D S | d d | d d }}| dddf }t| dddf }||d  | p8d  }g }d}|t|k rt j||| d	d
}	|	t|k r||	 }
|	dkrd||	d  nd}|
|kro|	d7 }	qP|	rs|sv|}n	t||	d  |}t	||	 |}t
|| ||
||}|t|| |  |d7 }nn|t|k sF|t|k r|t|| |  |d7 }|t|k s|S )a  Creates fixed size histogram by adding compression to accumulated state.

    This routine transforms a histogram at a particular step by linearly
    interpolating its variable number of buckets to represent their cumulative
    weight at a constant number of compression points. This significantly reduces
    the size of the histogram and makes it suitable for a two-dimensional area
    plot where the output of this routine constitutes the ranges for a single x
    coordinate.

    Args:
      buckets: A list of buckets, each of which is a 3-tuple of the form
        `(min, max, count)`.
      bps: Compression points represented in basis points, 1/100ths of a percent.
          Defaults to normal distribution.

    Returns:
      List of values for each basis point.
    c                 S   r   r   r   r   r	   r	   r
   r   x   r   z&compress_histogram.<locals>.<listcomp>r   r   r    N   r   r   r   r   )r"   r#   sizer%   r'   r(   r)   r*   r,   r+   r-   r.   r   r   )bucketsr0   minminmaxmaxcountsright_edgesr1   resultbp_indexr4   r(   r5   r6   r7   r8   r	   r	   r
   compress_histogramb   sH   
rC   c                 C   s    || | t ||  ||   S )z)Affinely map from [x0, x1] onto [y0, y1].)r   )xx0x1y0y1r	   r	   r
   r-      s    r-   )r   dataclassesnumpyr"   typingr   NORMAL_HISTOGRAM_BPS	dataclassr   r9   rC   r-   r	   r	   r	   r
   <module>   s   
2;