U
    •Ê¹jZ‚  ã                4   @   sZ  d dl Z d dlZd dlZd dlmZ d dlZd dlZd dlm	Z	 d dl
mZmZm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
dlmZ d
dlmZmZ dddddddddddddddddddgZe e¡Z ej!d ej"d ej#d!ej$d"ej%d"ej&d#ej'd#ej(d$ej)d%ej*d%ej+d&ej,d&ej-d'ej.d'ej/d'ej0d(ej1d)ej2d)ej3d*ej4d*ej5d+ej6d,ej7d,ej)d-ej8d-ej9d-iZ:d.d/„ Z;dQd2d3„Z<dRd4d„Z=dSd6d„Z>d7d„ Z?d8d„ Z@dTd9d„ZAdUd:d„ZBdVd<d„ZCdWd>d„ZDdXd?d„ZEdYd@d„ZFdZdBd„ZGdCd„ ZHd[dEd„ZIdFd„ ZJdGd„ ZKd\dId„ZLd]dJd„ZMd^dKd„ZNdLdM„ ZOdNdO„ ZPd_dPd„ZQdS )`é    N)ÚOptional)Ú
struct_pb2)ÚHistogramProtoÚSummaryÚSummaryMetadata)ÚTensorProto)ÚTensorShapeProto)Ú
layout_pb2)ÚPrCurvePluginData)ÚTextPluginDataé   )Úmake_np)Ú_prepare_videoÚconvert_to_HWCÚhparamsÚscalarÚhistogram_rawÚ	histogramÚmake_histogramÚimageÚimage_boxesÚ
draw_boxesÚ
make_imageÚvideoÚ
make_videoÚaudioÚcustom_scalarsÚtextÚtensor_protoÚpr_curve_rawÚpr_curveÚcompute_curveÚmesh)ZDT_HALFZhalf_val)Z	DT_BFLOATÚ	float_val)ÚDT_FLOATr#   )Ú	DT_DOUBLEÚ
double_val)ZDT_INT8Úint_val)ÚDT_UINT8r'   )ZDT_INT16r'   )ZDT_INT32r'   )ZDT_INT64Z	int64_val)ZDT_COMPLEX32Úscomplex_val)ZDT_COMPLEX64r)   )ZDT_BOOLZbool_val)ZDT_COMPLEX128Údcomplex_val)r(   Z
uint32_valc                 C   s,   t | tjƒs|  ¡ n| }|jtjkr(dS dS )Nr   éÿ   )Ú
isinstanceÚnpZndarrayÚnumpyÚdtypeÚuint8)ÚtensorZ	converted© r2   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/utils/tensorboard/summary.pyÚ_calc_scale_factorR   s    r4   Úblacké   c	                 C   sØ   ddl m}	m}
 |
 ¡ }|	 | ¡}||||f\}}}}|j||f||f||f||f||fg||d� |rÔ|}| |¡\}}t d| ¡}|j	||| d|  f|| |fg|d� |j
|| || | f|||d� | S )Nr   )Ú	ImageDrawÚ	ImageFont)ÚwidthÚfillgš™™™™™©?r6   )r:   )r:   Úfont)ÚPILr7   r8   Zload_defaultZDrawÚlineÚgetsizer-   ÚceilZ	rectangler   )r   ZxminZyminZxmaxZymaxÚdisplay_strÚcolorZ
color_textZ	thicknessr7   r8   r;   ZdrawÚleftÚrightÚtopÚbottomZtext_bottomÚ
text_widthZtext_heightÚmarginr2   r2   r3   Ú_draw_single_boxW   s4    
 ý
þûürH   c                    s„  ddl }ddlm}m}m}m‰ m‰m} ddlm	}m
}	m}
m}m} ddlm}m}m} tˆtƒstt d¡ tdƒ‚t|tƒs�t d¡ tdƒ‚|p–i }t|tƒsªtdƒ‚| ¡ D ]H\‰}ˆˆksät|tƒrät‡‡fd	d
„|D ƒƒs²tdˆ› dˆ› d�ƒ‚q²g }|ƒ }ˆ ¡ D �]†\‰}|dk�r&�qt|ttfƒ�rŠ||jˆ _ˆ|k�rhtjdd„ |ˆ D ƒd�}nd}|  |ˆ| !d¡|d�¡ �qt|t"ƒ�rê||jˆ _#ˆ|k�rÈtjdd„ |ˆ D ƒd�}nd}|  |ˆ| !d¡|d�¡ �qt|t$ƒ�rJ||jˆ _%ˆ|k�r(tjdd„ |ˆ D ƒd�}nd}|  |ˆ| !d¡|d�¡ �qt||j&ƒ�rŒt'|ƒd }||jˆ _|  |ˆ| !d¡d�¡ �qt(dƒ‚�q|||	d�}t)t)j*|
| +¡ d�d�}t,t,j!||d�gd�}‡ ‡fdd„| -¡ D ƒ}|||d�}|||	d �}t)t)j*|
| +¡ d�d�}t,t,j!||d�gd�}|| !d!¡d"�}|||	d#�}t)t)j*|
| +¡ d�d�}t,t,j!||d�gd�}|||fS )$a2  Outputs three `Summary` protocol buffers needed by hparams plugin.
    `Experiment` keeps the metadata of an experiment, such as the name of the
      hyperparameters and the name of the metrics.
    `SessionStartInfo` keeps key-value pairs of the hyperparameters
    `SessionEndInfo` describes status of the experiment e.g. STATUS_SUCCESS

    Args:
      hparam_dict: A dictionary that contains names of the hyperparameters
        and their values.
      metric_dict: A dictionary that contains names of the metrics
        and their values.
      hparam_domain_discrete: (Optional[Dict[str, List[Any]]]) A dictionary that
        contains names of the hyperparameters and all discrete values they can hold

    Returns:
      The `Summary` protobufs for Experiment, SessionStartInfo and
        SessionEndInfo
    r   N)ÚDataTypeÚ
ExperimentÚ
HParamInfoÚ
MetricInfoÚ
MetricNameÚStatus)ÚEXPERIMENT_TAGÚPLUGIN_DATA_VERSIONÚPLUGIN_NAMEÚSESSION_END_INFO_TAGÚSESSION_START_INFO_TAG)ÚHParamsPluginDataÚSessionEndInfoÚSessionStartInfoz>parameter: hparam_dict should be a dictionary, nothing logged.z>parameter: metric_dict should be a dictionary, nothing logged.zIparameter: hparam_domain_discrete should be a dictionary, nothing logged.c                 3   s    | ]}t |tˆ ˆ ƒƒV  qd S )N)r,   Útype©Ú.0Úd)Úhparam_dictÚkr2   r3   Ú	<genexpr>Ç   s     zhparams.<locals>.<genexpr>z"parameter: hparam_domain_discrete[z/] should be a list of same type as hparam_dict[z].c                 S   s   g | ]}t j|d �‘qS ))Únumber_value©r   ÚValuerX   r2   r2   r3   Ú
<listcomp>×   s   ÿzhparams.<locals>.<listcomp>)ÚvaluesZDATA_TYPE_FLOAT64)ÚnamerW   Údomain_discretec                 S   s   g | ]}t j|d �‘qS ))Ústring_valuer_   rX   r2   r2   r3   ra   í   s   ÿZDATA_TYPE_STRINGc                 S   s   g | ]}t j|d �‘qS ))Ú
bool_valuer_   rX   r2   r2   r3   ra     s   ÿZDATA_TYPE_BOOL)rc   rW   z=value should be one of int, float, str, bool, or torch.Tensor)Zsession_start_infoÚversion©Úplugin_nameÚcontent©Úplugin_data)ÚtagÚmetadata©Úvaluec                    s   g | ]}ˆ ˆ|d �d�‘qS )©rm   )rc   r2   )rY   r\   )rL   rM   r2   r3   ra   %  s     )Zhparam_infosZmetric_infos)Z
experimentrg   ZSTATUS_SUCCESS)Ústatus)Zsession_end_inforg   ).ÚtorchZ#tensorboard.plugins.hparams.api_pb2rI   rJ   rK   rL   rM   rN   Z$tensorboard.plugins.hparams.metadatarO   rP   rQ   rR   rS   Z+tensorboard.plugins.hparams.plugin_data_pb2rT   rU   rV   r,   ÚdictÚloggerÚwarningÚ	TypeErrorÚitemsÚlistÚallÚintÚfloatr   r^   r   Z	ListValueÚappendr`   Ústrre   Úboolrf   ZTensorr   Ú
ValueErrorr   Ú
PluginDataÚSerializeToStringr   Úkeys)r[   Zmetric_dictZhparam_domain_discreters   rI   rJ   rK   rN   rO   rP   rQ   rR   rS   rT   rU   rV   ÚvZhpsZssird   rj   ÚsmdZmtsÚexpZseir2   )rL   rM   r[   r\   r3   r   �   sò     

ÿ

ÿ
ÿÿþýÿ

þÿýÿ
þÿýÿ
þÿýÿÿ ÿÿ ÿÿ ÿÿFc           	      C   s¤   t |ƒ ¡ }|jdks0td|j› d|j› d�ƒ‚t|ƒ}|rŠt|gdd�}|r\t|gdd�}tjd	d
�}t|d�}t	t	j
| ||d�gd�S t	t	j
| |d�gd�S dS )a  Outputs a `Summary` protocol buffer containing a single scalar value.
    The generated Summary has a Tensor.proto containing the input Tensor.
    Args:
      name: A name for the generated node. Will also serve as the series name in
        TensorBoard.
      tensor: A real numeric Tensor containing a single value.
      collections: Optional list of graph collections keys. The new summary op is
        added to these collections. Defaults to `[GraphKeys.SUMMARIES]`.
      new_style: Whether to use new style (tensor field) or old style (simple_value
        field). New style could lead to faster data loading.
    Returns:
      A scalar `Tensor` of type `string`. Which contains a `Summary` protobuf.
    Raises:
      ValueError: If tensor has the wrong shape or type.
    r   zBTensor should contain one element (0 dimensions). Was given size: z and z dimensions.r$   )r#   r/   r%   )r&   r/   Zscalars©ri   rk   ©rm   r1   rn   ro   )rm   Zsimple_valueN)r   ÚsqueezeÚndimÚAssertionErrorÚsizer|   r   r   r�   r   r`   )	rc   r1   ÚcollectionsÚ	new_styleZdouble_precisionr   r   rl   r…   r2   r2   r3   r   =  s*    ÿþ
ýÿÿ
c                    s  ˆ   ¡ ˆ j dkrtdƒ‚ˆ jtkrÎtˆ j d }|dksB|dkrVt ˆ ¡ ¡  ¡ }n6ˆ   ¡ dkrnˆ  	¡ g}nˆ   ¡ dkr€g }nˆ  ¡  ¡ }dtˆ j d dt
‡ fd	d
„tˆ  ¡ ƒD ƒd�||i}tf |Ž}nt| › dˆ j› �ƒ‚tjdd�}t|d�}ttj| ||d�gd�S )a÷  Outputs a `Summary` protocol buffer containing the full tensor.
    The generated Summary has a Tensor.proto containing the input Tensor.
    Args:
      name: A name for the generated node. Will also serve as the series name in
        TensorBoard.
      tensor: Tensor to be converted to protobuf
    Returns:
      A tensor protobuf in a `Summary` protobuf.
    Raises:
      ValueError: If tensor is too big to be converted to protobuf, or
                     tensor data type is not supported
    l        zAtensor is bigger than protocol buffer's hard limit of 2GB in sizer   r)   r*   r   r/   Útensor_shapec                    s   g | ]}t jˆ j| d �‘qS )©rŒ   )r   ÚDimÚshape)rY   Úi©r1   r2   r3   ra   ˆ  s   ÿz tensor_proto.<locals>.<listcomp>©Údimz has unsupported tensor dtype r1   r‡   rk   ©rm   rn   r1   ro   )ZnumelÚitemsizer€   r/   Ú_TENSOR_TYPE_MAPrs   Zview_as_realÚflattenÚtolistÚitemr   Úranger–   r   r   r�   r   r`   )rm   r1   Zproto_val_fieldZproto_val_contentsZtensor_proto_argsr   rl   r…   r2   r”   r3   r   g  s:    ÿ
  

þÿ ø
c           	   	   C   s,   t |||||||d�}ttj| |d�gd�S )a  Outputs a `Summary` protocol buffer with a histogram.
    The generated
    [`Summary`](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto)
    has one summary value containing a histogram for `values`.
    Args:
      name: A name for the generated node. Will also serve as a series name in
        TensorBoard.
      min: A float or int min value
      max: A float or int max value
      num: Int number of values
      sum: Float or int sum of all values
      sum_squares: Float or int sum of squares for all values
      bucket_limits: A numeric `Tensor` with upper value per bucket
      bucket_counts: A numeric `Tensor` with number of values per bucket
    Returns:
      A scalar `Tensor` of type `string`. The serialized `Summary` protocol
      buffer.
    ©ÚminÚmaxÚnumÚsumÚsum_squaresZbucket_limitZbucket©rm   Zhistoro   )r   r   r`   )	rc   rŸ   r    r¡   r¢   r£   Zbucket_limitsZbucket_countsÚhistr2   r2   r3   r   š  s    ù	c                 C   s0   t |ƒ}t| t¡||ƒ}ttj| |d�gd�S )ad  Outputs a `Summary` protocol buffer with a histogram.
    The generated
    [`Summary`](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto)
    has one summary value containing a histogram for `values`.
    This op reports an `InvalidArgument` error if any value is not finite.
    Args:
      name: A name for the generated node. Will also serve as a series name in
        TensorBoard.
      values: A real numeric `Tensor`. Any shape. Values to use to
        build the histogram.
    Returns:
      A scalar `Tensor` of type `string`. The serialized `Summary` protocol
      buffer.
    r¤   ro   )r   r   Úastyper|   r   r`   )rc   rb   ÚbinsÚmax_binsr¥   r2   r2   r3   r   º  s    c              	   C   s¬  | j dkrtdƒ‚|  d¡} tj| |d�\}}t|ƒ}|dk	rÊ||krÊ|| }|| }|dkrztj|d|| ggddd�}| d|¡jdd�}t |j d	 f|j	¡}|dd|… |dd…< |d |d< |}t 
t |d¡¡}	tj|	d|	d d	 gd
d�\}
}t|
ƒ}
t|ƒd	 }~	|
dk�r,||
d	 |… nt dg|d|… g¡}||
|d	 … }|j dk�sl|j dk�rttdƒ‚|  | ¡}t|  ¡ |  ¡ t| ƒ|  ¡ || ¡ | ¡ d�S )zDConvert values into a histogram proto using logic from histogram.cc.r   zThe input has no element.éÿÿÿÿ)r§   NZconstant)Z	pad_widthÚmodeZconstant_values)Zaxisr   rC   )Zsidez1The histogram is empty, please file a bug report.rž   )rŒ   r€   Úreshaper-   r   ÚlenÚpadr¢   Úemptyr/   ÚcumsumZgreaterZsearchsortedr{   ZconcatenateÚdotr   rŸ   r    r›   )rb   r§   r¨   ÚcountsZlimitsZnum_binsZsubsamplingZsubsampling_remainderZ
new_limitsZ
cum_countsÚstartÚendZsum_sqr2   r2   r3   r   Ï  sP    

ü 0ÿ
ùÚNCHWc                 C   s`   t |ƒ}t||ƒ}t|ƒ}| tj¡}||  dd¡ tj¡}t||d�}t	t	j
| |d�gd�S )aJ  Outputs a `Summary` protocol buffer with images.
    The summary has up to `max_images` summary values containing images. The
    images are built from `tensor` which must be 3-D with shape `[height, width,
    channels]` and where `channels` can be:
    *  1: `tensor` is interpreted as Grayscale.
    *  3: `tensor` is interpreted as RGB.
    *  4: `tensor` is interpreted as RGBA.
    The `name` in the outputted Summary.Value protobufs is generated based on the
    name, with a suffix depending on the max_outputs setting:
    *  If `max_outputs` is 1, the summary value tag is '*name*/image'.
    *  If `max_outputs` is greater than 1, the summary value tags are
       generated sequentially as '*name*/image/0', '*name*/image/1', etc.
    Args:
      tag: A name for the generated node. Will also serve as a series name in
        TensorBoard.
      tensor: A 3-D `uint8` or `float32` `Tensor` of shape `[height, width,
        channels]` where `channels` is 1, 3, or 4.
        'tensor' can either have values in [0, 1] (float32) or [0, 255] (uint8).
        The image() function will scale the image values to [0, 255] by applying
        a scale factor of either 1 (uint8) or 255 (float32). Out-of-range values
        will be clipped.
    Returns:
      A scalar `Tensor` of type `string`. The serialized `Summary` protocol
      buffer.
    r   r+   )Úrescale©rm   r   ro   )r   r   r4   r¦   r-   Úfloat32Úclipr0   r   r   r`   )rm   r1   rµ   ÚdataformatsÚscale_factorr   r2   r2   r3   r     s    
ÚCHWc                 C   sd   t |ƒ}t||ƒ}t |ƒ}| tj¡t|ƒ }t| dd¡ tj¡|||d�}t	t	j
| |d�gd�S )z0Outputs a `Summary` protocol buffer with images.r   r+   )rµ   ÚroisÚlabelsr¶   ro   )r   r   r¦   r-   r·   r4   r   r¸   r0   r   r`   )rm   Ztensor_imageZtensor_boxesrµ   r¹   r½   r   r2   r2   r3   r   *  s    
üc              
   C   sf   |j d }t|ƒ}|D ]J}t| ||df ||df ||df ||df |d krRd n|| dd�} q| S )Nr   r   r6   é   ÚRed)r@   rA   )r’   r�   rH   )Z
disp_imageZboxesr½   Z	num_boxesZlist_gtr“   r2   r2   r3   r   ;  s    




ù	c                 C   sÈ   ddl m} | j\}}}t|| ƒ}t|| ƒ}	| | ¡}
|dk	rPt|
||d�}
z|jj}W n tk
rv   |j	}Y nX |
 
|	|f|¡}
ddl}| ¡ }|
j|dd� | ¡ }| ¡  tj||||d�S )z<Convert a numpy representation of an image to Image protobufr   )ÚImageN)r½   ZPNG)Úformat©Úheightr9   Z
colorspaceZencoded_image_string)r<   rÀ   r’   r{   Z	fromarrayr   Z
ResamplingZLANCZOSÚAttributeErrorÚ	ANTIALIASÚresizeÚioÚBytesIOÚsaveÚgetvalueÚcloser   )r1   rµ   r¼   r½   rÀ   rÃ   r9   ZchannelZscaled_heightZscaled_widthr   rÅ   rÇ   ÚoutputZimage_stringr2   r2   r3   r   L  s.    
üé   c                 C   s\   t |ƒ}t|ƒ}t|ƒ}| tj¡}||  dd¡ tj¡}t||ƒ}t	t	j
| |d�gd�S )Nr   r+   r¶   ro   )r   r   r4   r¦   r-   r·   r¸   r0   r   r   r`   )rm   r1   Úfpsrº   r   r2   r2   r3   r   i  s    
c                 C   sX  zdd l }W n tk
r*   tdƒ Y d S X zddl m} W n  tk
r\   tddƒ Y d S X dd l}| j\}}}}|jt| ƒ|d�}	|jddd	�j	}
z|	j
|
dd d
� W nL tk
rö   z|	j
|
ddd� W n" tk
rð   |	j
|
dd� Y nX Y nX t|
dƒ�}| ¡ }W 5 Q R X zt |
¡ W n  tk
�rD   t d¡ Y nX tj||||d�S )Nr   zadd_video needs package moviepy)Úeditorz6moviepy is installed, but can't import moviepy.editor.z2Some packages could be missing [imageio, requests])rÎ   z.gifF)ÚsuffixÚdelete)Úverboseru   )rÒ   Úprogress_bar)rÒ   Úrbz5The temporary file used by moviepy cannot be deleted.rÂ   )ÚmoviepyÚImportErrorÚprintrÏ   Útempfiler’   ZImageSequenceClipry   ÚNamedTemporaryFilerc   Z	write_gifrw   ÚopenÚreadÚosÚremoveÚOSErrorru   rv   r   rÀ   )r1   rÎ   rÕ   ZmpyrØ   ÚtÚhÚwÚcr¸   ÚfilenameÚfZtensor_stringr2   r2   r3   r   t  sH    þ   ÿéD¬  c           
   	   C   sø   t |ƒ}| ¡ }t|ƒ ¡ dkr4tdƒ | dd¡}|jdksFtdƒ‚|t 	tj
¡j  d¡}dd l}dd l}| ¡ }| |d¡�0}| d¡ | d¡ | |¡ | |j¡ W 5 Q R X | ¡ }| ¡  tj|d|jd |d	d
�}	ttj| |	d�gd�S )Nr   z4warning: audio amplitude out of range, auto clipped.r©   z%input tensor should be 1 dimensional.z<i2r   Úwbr6   z	audio/wav)Úsample_rateZnum_channelsZlength_framesZencoded_audio_stringÚcontent_type)rm   r   ro   )r   r‰   Úabsr    r×   r¸   rŠ   r‹   r-   ZiinfoÚint16r¦   rÇ   ÚwaverÈ   rÚ   ZsetnchannelsZsetsampwidthZsetframerateZwriteframesÚdatarÊ   rË   r   ZAudior’   r`   )
rm   r1   rç   ÚarrayrÇ   rë   ZfioZ
wave_writeZaudio_stringr   r2   r2   r3   r   Ÿ  s2    


ûc                 C   s  g }|   ¡ D ]²\}}g }|  ¡ D ]ˆ\}}|d }|d dkr„t|ƒdksLt‚tjtjj|d |d |d d�gd�}tj||d�}	ntj|d	�}
tj||
d
�}	| |	¡ q | tj	||d�¡ qtj
|d�} tjdd�}t|d�}td|  ¡ gtƒ d�}ttjd||d�gd�S )Nr   r   ZMarginr¾   r6   )rp   ÚlowerÚupper)Zseries)ÚtitlerG   rq   )rð   Ú	multiline)rð   Úchart)Úcategoryr   r‡   rk   Ú	DT_STRING©r/   Z
string_valr�   Zcustom_scalars__config__rˆ   ro   )rx   r¬   r‹   r	   ZMarginChartContentZSeriesZChartZMultilineChartContentr}   ZCategoryZLayoutr   r�   r   r‚   r   r   r`   )ZlayoutÚ
categoriesr\   r„   ZchartsZ
chart_nameZchart_meatadataÚtagsZmgccrò   Zmlccrl   r…   r1   r2   r2   r3   r   ½  s@      ÿÿÿ
ýÿÿc                 C   sf   t jdtdd� ¡ d�}t |d�}td|jdd�gttjd	d
�gd�d�}ttj	| d ||d�gd�S )Nr   r   )rg   rh   rk   rô   Úutf_8)Úencodingr   r�   r•   rõ   z/text_summaryr—   ro   )
r   r�   r   r‚   r   Úencoder   r‘   r   r`   )rm   r   rl   r…   r1   r2   r2   r3   r   â  s     ÿ
ýÿé   c	                 C   s    |dkrd}t  ||||||f¡}	td|d� ¡ }
tjd|
d�}t|d�}td|	 d¡ ¡ t	t	j
|	jd d	�t	j
|	jd
 d	�gd�d�}ttj| ||d�gd�S )Nrû   r   ©rg   Únum_thresholdsÚ	pr_curvesrh   rk   r$   r©   r�   r   r•   ©r/   r#   r�   r—   ro   )r-   Ústackr
   r‚   r   r�   r   r«   r›   r   r‘   r’   r   r`   )rm   ÚtpÚfpÚtnÚfnÚ	precisionÚrecallrý   Úweightsrì   Úpr_curve_plugin_datarl   r…   r1   r2   r2   r3   r   ñ  s,     ÿ
 ÿ
þÿý
c           
      C   s˜   t |dƒ}t||||d�}td|d� ¡ }tjd|d�}t|d�}td| d	¡ ¡ t	t	j
|jd d
�t	j
|jd d
�gd�d�}	ttj| ||	d�gd�S )Nrû   )rý   r  r   rü   rþ   rh   rk   r$   r©   r�   r   r•   rÿ   r—   ro   )rŸ   r!   r
   r‚   r   r�   r   r«   r›   r   r‘   r’   r   r`   )
rm   r½   Úpredictionsrý   r  rì   r  rl   r…   r1   r2   r2   r3   r      s4    
   ÿ ÿ
 ÿ
þÿý
c                 C   s
  d}|d krd}t  t  ||d  ¡¡}|  t j¡}d|d f}t j||||| d�\}}	t j|||d| | d�\}
}	t  |d d d… ¡d d d… }t  |
d d d… ¡d d d… }|d | }|d | }|t  ||| ¡ }|t  ||| ¡ }t  ||||||f¡S )NgH¯¼šò×z>g      ð?r   r   )r§   r�   r  r©   )	r-   Úint32Úfloorr¦   Úfloat64r   r¯   Úmaximumr   )r½   r	  rý   r  Z_MINIMUM_COUNTZbucket_indicesZfloat_labelsZhistogram_rangeZ
tp_bucketsÚ_Z
fp_bucketsr  r  r  r  r  r  r2   r2   r3   r!   &  s2    ü

ü
c              	   C   s¢   ddl }ddlm} | |¡}|j| ||||j||d�}	td| d¡ ¡ t	t	j
|jd d�t	j
|jd d�t	j
|jd	 d�gd
�d�}tj| | |¡||	d�}
|
S )a  Creates a tensor summary with summary metadata.

    Args:
      name: Uniquely identifiable name of the summary op. Could be replaced by
        combination of name and type to make it unique even outside of this
        summary.
      display_name: Will be used as the display name in TensorBoard.
        Defaults to `name`.
      description: A longform readable description of the summary data. Markdown
        is supported.
      tensor: Tensor to display in summary.
      content_type: Type of content inside the Tensor.
      components: Bitmask representing present parts (vertices, colors, etc.) that
        belong to the summary.
      json_config: A string, JSON-serialized dictionary of ThreeJS classes
        configuration.

    Returns:
      Tensor summary with metadata.
    r   N©rn   )Újson_configr$   r©   r�   r   r6   r•   rÿ   rˆ   )rs   Útensorboard.plugins.meshrn   Z	as_tensorZcreate_summary_metadatar’   r   r«   r›   r   r‘   r   r`   Zget_instance_name)rc   Údisplay_nameÚdescriptionr1   rè   Ú
componentsr  rs   rn   Ztensor_metadataZtensor_summaryr2   r2   r3   Ú_get_tensor_summaryG  s8    
ù
ýÿý
ýr  c                 C   s   d}| dk	rt j| dd�}|S )z6Parses and returns JSON string from python dictionary.z{}NT)Ú	sort_keys)ÚjsonÚdumps)Úconfig_dictr  r2   r2   r3   Ú_get_json_config‚  s    r  c                 C   s”   ddl m} ddlm} t|ƒ}	g }
||jf||jf||jfg}dd„ |D ƒ}| dd„ |D ƒ¡}|D ]"\}}|
 	t
| ||||||	ƒ¡ qft|
d�S )a(  Outputs a merged `Summary` protocol buffer with a mesh/point cloud.

    Args:
      tag: A name for this summary operation.
      vertices: Tensor of shape `[dim_1, ..., dim_n, 3]` representing the 3D
        coordinates of vertices.
      faces: Tensor of shape `[dim_1, ..., dim_n, 3]` containing indices of
        vertices within each triangle.
      colors: Tensor of shape `[dim_1, ..., dim_n, 3]` containing colors for each
        vertex.
      display_name: If set, will be used as the display name in TensorBoard.
        Defaults to `name`.
      description: A longform readable description of the summary data. Markdown
        is supported.
      config_dict: Dictionary with ThreeJS classes names and configuration.

    Returns:
      Merged summary for mesh/point cloud representation.
    r   r  )ÚMeshPluginDatac                 S   s   g | ]}|d  dk	r|‘qS )r   Nr2   )rY   r1   r2   r2   r3   ra   ¬  s      zmesh.<locals>.<listcomp>c                 S   s   g | ]\}}|‘qS r2   r2   )rY   r1   rè   r2   r2   r3   ra   ®  s     ro   )r  rn   Z(tensorboard.plugins.mesh.plugin_data_pb2r  r  ZVERTEXZFACEZCOLORZget_components_bitmaskr}   r  r   )rm   ZverticesÚcolorsZfacesr  r  r  rn   r  r  Z	summariesZtensorsr  r1   rè   r2   r2   r3   r"   ‹  s2    ýÿùÿ)r5   r5   r6   )NNN)NFF)N)N)r   r´   )r   r»   N)N)r   NN)rÍ   )rå   )rû   N)rû   N)NN)NN)Rr  ÚloggingrÜ   Útypingr   rs   r.   r-   Zgoogle.protobufr   Z$tensorboard.compat.proto.summary_pb2r   r   r   Z#tensorboard.compat.proto.tensor_pb2r   Z)tensorboard.compat.proto.tensor_shape_pb2r   Z!tensorboard.plugins.custom_scalarr	   Z,tensorboard.plugins.pr_curve.plugin_data_pb2r
   Z(tensorboard.plugins.text.plugin_data_pb2r   Z_convert_npr   Z_utilsr   r   Ú__all__Ú	getLoggerÚ__name__ru   ZhalfZfloat16Zbfloat16r·   r|   r  ÚdoubleZint8r0   Zqint8rê   Úshortr{   r
  Zqint32Zint64Z	complex32ZchalfZ	complex64Zcfloatr   Z
complex128ZcdoubleZquint8Zquint4x2r™   r4   rH   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r  r  r"   r2   r2   r2   r3   Ú<module>   sô   í
                          æ   ÷
*
 =
*3 

7
%     ÿ



+
%   ÿ


!;
   ÿ