U
    VcA                     @   sL   d Z ddlZddlZdZedddgZefddZefd	d
Zdd Z	dS )z"Package for histogram compression.    N)	r   i  i3  i  i  i  i   it$  i'  CompressedHistogramValueZbasis_pointvaluec                 C   sR  | 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 rN|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   .0b r
   O/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/plugins/distribution/compressor.py
<listcomp>5   s     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   )Zhistobpsr   r   weightsvaluesjir   cumsum_prevlhsrhsweightr
   r
   r   compress_histogram_proto"   s8    
r)   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 rJt j||| d	d
}	|	t|k rJ||	 }
|	dkr||	d  nd}|
|kr|	d7 }	q|	r|s|}nt||	d  |}t	||	 |}t
|| ||
||}|t|| | |d7 }qqqJq|t|k rx|t|| | |d7 }qJ|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   j   s     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   )Zbucketsr    ZminminZmaxmaxcountsZright_edgesr!   resultZbp_indexr$   r   r%   r&   r'   r(   r
   r
   r   compress_histogramT   s<    
r.   c                 C   s    || | t ||  ||   S )z)Affinely map from [x0, x1] onto [y0, y1].)float)xZx0x1Zy0y1r
   r
   r   r      s    r   )
__doc__collectionsZnumpyr   ZNORMAL_HISTOGRAM_BPS
namedtupler   r)   r.   r   r
   r
   r
   r   <module>   s    27