a
    ij                     @  s  U d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZ e ( ddlZddlZddlZW d   n1 s0    Y  eZdZded	< d
dddZdddddddddd
dddZddddddZddddddddd Zd!d!d"d#d$d%ZdS )&zWUtils for colab/jupyter.

Usage:

```python
from etils.ecolab import array_as_img
```

    )annotationsN)AnyOptionalTuple)enp)	_internal)
   r   zTuple[int, int]_MIN_IMG_SHAPENone)returnc                  O  s   t jj| i |S )z$Alias for `IPython.display.display`.)IPythondisplay)Zobjskwargs r   W/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/ecolab/array_as_img.pyshow1   s    r      )d      video_min_num_framesheightshow_images_kwargsshow_videos_kwargsintzNone | int | tuple[int, int]zOptional[dict[str, Any]])r   r   r   r   r   c                 C  s   t  }|du rdS |pi }|p"i }tjt| |||d}td |jjd }ztj	j
}W n tyj   Y n0 t|jdd}||| ztj	j}	W n ty   Y n0 ||	j| ztj	j}
W n ty   Y n0 ||
j| |tj	jj| dS )a[  If called, 2d/3d imgage arrays will be plotted as images in colab/jupyter.

  Usage:

  >>> ecolab.auto_plot_array()
  >>> np.zeros((28, 28, 3))  # Displayed as image

  Args:
    video_min_num_frames: Video `(num_frames, h, w, c)` with less than this
      number of frames will be displayed as individual images
    height: `(min, max)` image height in pixels. Images smaller/larger will be
      reshaped. `None` to disable. If a single number, assume `min == max`.
    show_images_kwargs: Kwargs forwarded to `mediapy.show_images`
    show_videos_kwargs: Kwargs forwarded to `mediapy.show_videos`
  Nr   z?Display big np/tf/jax arrays as image for nicer IPython displayz	text/htmlr   )shape)r   get_ipython	functoolspartial_array_repr_htmlprintdisplay_formatter
formattersr   lazyjnpImportErrortypeZzerosZfor_typetfZTensortorchnpZndarray)r   r   r   r   ipythonZarray_repr_html_fn	formatterr$   Zjax_array_clsr'   r(   r   r   r   auto_plot_array6   s>    	r,   Arrayr   zOptional[str])arrayr   r   c                 K  s4   zt | fi |W S  ty.   t   Y n0 dS )zCReturns the HTML `<img/>` repr, or `None` if array is not an image.N)_array_repr_html_inner	Exception	traceback	print_exc)r.   r   r   r   r   r      s
    r   zdict[str, Any])imgr   r   r   r   r   c                C  s  t j| sdS t j| s(t j| r0|  } | j}t|}|dkrP|}d}nH|dkrn|dd }|d }n*|dkr|dd }|d }|d }	ndS d|v rdS t|t	rdS |dvrdS |
 }|
 }|r&t|tr|}
|}n|\}
}~|d }t||
}t||}|d	| |d	| |dk rHtj| fd
di|}n`|	|k rjtj| fd
di|}n>|dkrxdS t|	d d}|d| tj| fd
di|}|S )zCDisplay the normalized img, or `None` if the input is not an image.N            r   >   r5   r6   r8   r   Zreturn_htmlT   g      9@fps)r   r#   Zis_arrayZis_tfZis_torchnumpyr   len_smaller_thanr	   copy
isinstancer   maxmin
setdefaultmediaZ
show_imageZshow_imagesZ
show_video)r3   r   r   r   r   r   ndimZ	img_shapeZnum_channelZ
num_framesZ
min_height
max_heightZtarget_heightoutr:   r   r   r   r/      sh    









r/   ztuple[int, ...]bool)r   	min_shaper   c                 C  s   t dd t| |D S )zFReturns True if one of the dim of `shape` is smaller than `min_shape`.c                 s  s   | ]\}}||k V  qd S )Nr   ).0dimZmin_dimr   r   r   	<genexpr>       z _smaller_than.<locals>.<genexpr>)anyzip)r   rH   r   r   r   r=      s    r=   )__doc__
__future__r   r   r1   typingr   r   r   Zetilsr   Z	etils.epyr   Zcheck_missing_depsr   IPython.displayZmediapyrC   r-   r	   __annotations__r   r,   r   r/   r=   r   r   r   r   <module>   s*   

&KQ