
    ti@                     4    d dl Zd dlmZ ddZd ZddZd Zy)    Nc                     ddl m ddlmc m fd}t        | t              r*| D cg c]
  } ||       }}t        j                  |      S  ||       }|S c c}w )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                    	j                  |       }|j                          t        j                  |j	                         t        j
                        }| j                  j                         \  }}|j                  ||dg      d d d d ddf   }t        j                  |dd      }rj                  |        |S )Ndtype   r         )sourcedestination)FigureCanvasAggdrawnp
frombufferbuffer_rgbauint8canvasget_width_heightreshapemoveaxisclose)
figurer   datawh	image_hwc	image_chwr   pltplt_backend_aggs
          d/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/utils/tensorboard/_utils.pyrender_to_rgbz&figure_to_image.<locals>.render_to_rgb   s     008MM&*<*<*>bhhO}}--/1LL!Q+Aq!A#I6	KK	!C	IIf    )	matplotlib.pyplotpyplotmatplotlib.backends.backend_aggbackendsbackend_agg
isinstancelistr   stack)figuresr   r    r   imagesimager   r   s    `    @@r   figure_to_imager-      sZ     $==	 '4 6=>F-'>>xxg&	 ?s   Ac           
         | j                   \  }}}}}| j                  t        j                  k(  rt        j                  |       dz  } d } || j                   d         skt        d| j                   d   j                         z  | j                   d   z
        }t        j                  | t        j                  |||||f      fd      } d|j                         dz
  dz  z  }| j                   d   |z  }	t        j                  | ||	||||f      } t        j                  | d	      } t        j                  | |||z  |	|z  |f      } | S )
aJ  
    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 spread to a grid, which forms a frame.
    e.g. Video with batchsize 16 will have a 4x4 grid.
    g     o@c                 &    | dk7  xr | | dz
  z  dk(  S )Nr       )nums    r   	is_power2z!_prepare_video.<locals>.is_power28   s    ax4cS1Wo!34r!   r   r	   )shape)axisr0   )r	   r   r   r0      r   )axes)r4   r   r   r   float32int
bit_lengthconcatenatezerosr   	transpose)
Vbtcr   r   r3   len_additionn_rowsn_colss
             r   _prepare_videorE   )   s'    GGMAq!Qww"((JJqME!5
 QWWQZ 1
 5 5 77!''!*DENNArxx|Q1a.HIJQRSALLNQ&1,-FWWQZ6!F


1vvq!Q23A
Q/0A


1q&1*fqj!45AHr!   c           	         t        | t        j                        st        d      | j                  d   dk(  rt        j
                  | | | gd      } | j                  dk7  s| j                  d   dk7  rt        d      | j                  d   }| j                  d   }| j                  d   }t        ||      }t        t        j                  t        |      |z              }t        j                  d||z  ||z  f| j                        }d}t        |      D ]A  }t        |      D ]1  }	||k\  r | |   |d d ||z  |dz   |z  |	|z  |	dz   |z  f<   |dz   }3 C |S )	Nz*plugin error, should pass numpy array herer0   r   r   z0Input should be a 4D numpy array with 3 channelsr   r	   r   )r'   r   ndarrayAssertionErrorr4   r;   ndimminr9   ceilfloatr<   r   range)
IncolsnimgHWnrowsr   iyxs
             r   	make_gridrW   M   sT   a$IJJwwqzQNNAq!9a(vv{aggajAoOPP771:D	
A	
AeEde+,-EXXq!e)QY/qww?F	A5\ u 	ADyBCA$F1a!eq1uk)1q5AEQ;+>>?AA		 Mr!   c                 P   t        t        |            t        |      k7  rt        d|       t        | j                        t        |      k7  rt        d| j                   d|       |j	                         }t        |      dk(  rMdD cg c]  }|j                  |       }}| j                  |      }t        |      }|j                  ddd      S t        |      d	k(  r\d
D cg c]  }|j                  |       }}| j                  |      }|j                  d   dk(  rt        j                  |||gd      }|S t        |      dk(  rJdD cg c]  }|j                  |       }}| j                  |      } t        j                  | | | gd      } | S y c c}w c c}w c c}w )NzNYou 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   NCHWr0   r	   r   r   HWCHW)lensetrH   r4   upperfindr=   rW   r   r;   r)   )tensorinput_formatrA   indextensor_NCHW
tensor_CHW
tensor_HWCs          r   convert_to_HWCrf   i   s   
3|\!22  '.* + 	+
6<<C--  ||n$4\ND E 	E%%'L
<A/56!""1%66&&u-{+
##Aq!,,
<A/45!""1%55%%e,
A!#Z(LaPJ
<A/34!""1%44!!%(6662A6	  7 6 5s   F(FF#)T)   )	numpyr   numpy.typingtypingnptr-   rE   rW   rf   r1   r!   r   <module>rl      s!     D!H8r!   