a
    
dM                     @   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< eeef d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	   t/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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   sZ  | j sdd |D S t| j}t| j}||d  | p>d  }g }d}|t|k r"tj	||| dd}|t|k r"|| }|dkr||d  nd	}	||	kr|d7 }qp|r|	s| j
}
nt||d  | j
}
t
|| | j}t|| |	||
|}|t|| |  |d7 }qNqpq"qN|t|k rV|t|| | j  |d7 }q"|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   s   g | ]}t |d  qS         r   r   .0br	   r	   r
   
<listcomp>C       z,compress_histogram_proto.<locals>.<listcomp>      ?r   rightZside   r   )numnparraybucketlistbucket_limitsumcumsumlensearchsortedminmax_lerpappendr   r   )Zhistobpsr"   r$   weightsvaluesjir&   cumsum_prevlhsrhsweightr	   r	   r
   compress_histogram_proto0   s8    
r6   c                 C   s  t | } | jsdd |D S | d d | d d  }}| dddf }t| dddf }||d  | ppd  }g }d}|t|k rNt j||| d	d
}	|	t|k rN||	 }
|	dkr||	d  nd}|
|kr|	d7 }	q|	r|s|}nt||	d  |}t	||	 |}t
|| ||
||}|t|| |  |d7 }qqqNq|t|k r|t|| |  |d7 }qN|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   s   g | ]}t |d  qS 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   )Zbucketsr-   ZminminZmaxmaxcountsZright_edgesr.   resultZbp_indexr1   r&   r2   r3   r4   r5   r	   r	   r
   compress_histogramb   sD    
r;   c                 C   s    || | t ||  ||   S )z)Affinely map from [x0, x1] onto [y0, y1].)r   )xZx0x1Zy0y1r	   r	   r
   r+      s    r+   )r   Zdataclassesnumpyr    typingr   ZNORMAL_HISTOGRAM_BPSZ	dataclassr   r6   r;   r+   r	   r	   r	   r
   <module>   s   
2;