o
    vZh[                     @   s<   d dl Zd dlmZ dddZdd Zddd	Zd
d ZdS )    NTc                    s`   ddl m ddlm  m  fddt| tr*fdd| D }t|S | }|S )aB  Render matplotlib figure to numpy format.

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

    Args:
        figures (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
    r   Nc                    s|    | }|  tj| tjd}| j \}}|||dgd d d d ddf }tj	|ddd} r<
|  |S )Ndtype   r         )sourcedestination)ZFigureCanvasAggZdrawnpZ
frombufferZbuffer_rgbauint8canvasZget_width_heightreshapeZmoveaxisclose)figurer   datawhZ	image_hwcZ	image_chw)r   pltplt_backend_agg M/var/www/auris/lib/python3.10/site-packages/torch/utils/tensorboard/_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>)	Zmatplotlib.pyplotZpyplotZmatplotlib.backends.backend_aggbackendsZbackend_agg
isinstancelistr	   stack)Zfiguresr   Zimagesimager   )r   r   r   r   r   figure_to_image   s   

r   c           
      C   s   | j \}}}}}| jtjkrt| d } dd }|| j d sBtd| j d   | j d  }tj| tj|||||fdfdd} d| d d  }| j d | }	tj	| ||	||||fd	} tj
| d
d} tj	| ||| |	| |fd	} | S )aL  
    Convert a 5D tensor into 4D tensor.

    Convesrion is done from [batchsize, time(frame), channel(color), height, width]  (5D tensor)
    to [time(frame), new_width, new_height, channel] (4D tensor).

    A batch of images are spreaded to a grid, which forms a frame.
    e.g. Video with batchsize 16 will have a 4x4 grid.
    g     o@c                 S   s   | dko| | d @ dkS )Nr      r   )numr   r   r   	is_power28   s   z!_prepare_video.<locals>.is_power2r   r   )shape)Zaxisr   )Znewshape)r   r   r   r      r   )Zaxes)r"   r   r	   r
   Zfloat32int
bit_lengthconcatenatezerosr   	transpose)
Vbtcr   r   r!   Zlen_additionZn_rowsZn_colsr   r   r   _prepare_video)   s   
 $r-      c           
   	   C   s  t | tjs
J d| jd dkrt| | | gd} | jdkr&| jd dks(J | jd }| jd }| jd }t||}ttt	|| }tj
d|| || f| jd}d}t|D ].}t|D ]'}	||krj n | | |d d || |d | |	| |	d | f< |d }qbq\|S )Nz*plugin error, should pass numpy array herer   r   r   r   r   r   )r   r	   Zndarrayr"   r&   ndimminr$   ceilfloatr'   r   range)
IZncolsZnimgHWZnrowsr   iyxr   r   r   	make_gridJ   s&   



6
r:   c                    s  t t t  ksJ d  t | jt  ks%J d| j d     t  dkrH fdddD }| |}t|}|dd	d
S t  dkrn fdddD }| |}|jd	 dkrlt|||gd	}|S t  d	kr fdddD }| |} t| | | gd	} | S d S )NzJYou can not use the same dimension shordhand twice.         input_format: zKsize of input tensor and input format are different.         tensor shape: z, input_format: r   c                       g | ]}  |qS r   findr   r,   input_formatr   r   r   o       z"convert_to_HWC.<locals>.<listcomp>ZNCHWr   r   r   r   c                    r;   r   r<   r>   r?   r   r   r   u   rA   ZHWCc                    r;   r   r<   r>   r?   r   r   r   |   rA   ZHW)	lensetr"   upperr(   r:   r	   r&   r   )Ztensorr@   indexZtensor_NCHWZ
tensor_CHWZ
tensor_HWCr   r?   r   convert_to_HWCd   s@   



rF   )T)r.   )	numpyr	   Znumpy.typingtypingZnptr   r-   r:   rF   r   r   r   r   <module>   s   
"
!