o
    i}                     @   s8   dddZ dd Zdd Zddd	Zd
d Zdd ZdS )Tc                    s   ddl zddlm  m ddlm W n ty"   td Y nw  fddt| t	r?fdd| D }
|S | }|S )aA  Render matplotlib figure to numpy format.

    Note that this requires the ``matplotlib`` package.

    Args:
        figure (matplotlib.pyplot.figure) or list of figures: figure or a list of figures
        close (bool): Flag to automatically close the figure

    Returns:
        numpy.array: image in [CHW] order
        Nzplease install matplotlibc                    s|    | }|  j| jd}| j \}}|||dgd d d d ddf }j|ddd} r<	|  |S )Ndtype   r         )sourcedestination)
FigureCanvasAggdraw
frombufferbuffer_rgbauint8canvasget_width_heightreshapemoveaxisclose)figurer   datawh	image_hwc	image_chw)r   nppltplt_backend_agg F/home/ubuntu/.local/lib/python3.10/site-packages/tensorboardX/utils.pyrender_to_rgb   s   
&
z&figure_to_image.<locals>.render_to_rgbc                    s   g | ]} |qS r   r   ).0r   )r   r   r   
<listcomp>!   s    z#figure_to_image.<locals>.<listcomp>)numpymatplotlib.backends.backend_aggbackendsbackend_aggmatplotlib.pyplotpyplotModuleNotFoundErrorprint
isinstanceliststack)figuresr   imagesimager   )r   r   r   r   r   r   figure_to_image   s   

r/   c                   C   s   d S )Nr   r   r   r   r   graphviz_to_image(   s   r0   c                 C   s   dd l }| j\}}}}}| j|jkr|| d } dd }|| jd sFtd| jd   | jd  }|j| |j|||||fdfdd} d| d d  }	| jd |	 }
|j	| |	|
||||fd	} |j
| d
d} |j	| ||	| |
| |fd	} | S )Nr   g     o@c                 S   s   | dko| | d @ dkS )Nr      r   )numr   r   r   	is_power23   s   z!_prepare_video.<locals>.is_power2r   )shape)axisr1   )newshape)r   r   r   r1      r   )axes)r!   r4   r   r   float32int
bit_lengthconcatenatezerosr   	transpose)Vr   btcr   r   r3   len_additionn_rowsn_colsr   r   r   _prepare_video,   s     rF      c              	   C   s6  dd l }t| |jsJ d| jd dkr|| | | gd} | jdkr*| jd dks3| jd dks3J | jd }| jd }| jd }t||}t|t	|| }|j
| jd || || f| jd}d}t|D ].}	t|D ]'}
||krx n | | |d d |	| |	d | |
| |
d | f< |d }qpqj|S )Nr   z*plugin error, should pass numpy array herer1   r   r   r   r   )r!   r)   ndarrayr4   r<   ndimminr:   ceilfloatr=   r   range)Incolsr   nimgHWnrowsr   iyxr   r   r   	make_gridF   s0   *



$6
rW   c                    s   t  dks
J dt t t  ksJ d  t | jt  ks/J d| j d      fdddD }| |}|S )	Nr7   zOnly 5D tensor is supported.JYou can not use the same dimension shorthand twice.         input_format: Ksize of input tensor and input format are different.         tensor shape: , input_format: c                       g | ]}  |qS r   findr   rB   input_formatr   r   r    e       z$convert_to_NTCHW.<locals>.<listcomp>NTCHW)lensetr4   upperr>   )tensorr`   indextensor_NTCHWr   r_   r   convert_to_NTCHW^   s   
ri   c                    s&  dd l }tt t ksJ d  t| jt ks)J d| j d     t dkrL fdddD }| |}t|}|d	d
dS t dkrr fdddD }| |}|jd
 d	krp||||gd
}|S t d
kr fdddD }| |} || | | gd
} | S d S )Nr   rX   rY   rZ   r   c                    r[   r   r\   r^   r_   r   r   r    s   ra   z"convert_to_HWC.<locals>.<listcomp>NCHWr1   r   r   c                    r[   r   r\   r^   r_   r   r   r    y   ra   HWCc                    r[   r   r\   r^   r_   r   r   r       ra   HW)	r!   rc   rd   r4   re   r>   rW   r<   r+   )rf   r`   r   rg   tensor_NCHW
tensor_CHW
tensor_HWCr   r_   r   convert_to_HWCj   s6   


rp   N)T)rG   )r/   r0   rF   rW   ri   rp   r   r   r   r   <module>   s   
&
