
    ӆhN                     p    S SK r S SKJr  S SKrS SKJr  S SKrS SK	r	S r
S rS rS rS rS rS	 rSS
 jrg)    N)wrapc                    Sn[         R                  R                  U 5      (       aN  [         R                  R                  U 5      (       d*  [         R                  " U 5      (       d  Sn[        S5        U$ Sn[        S5        U$ )zwFunction to check if folder at given path exists and is not empty.

Returns True if folder is empty or does not exist.
FTzO'test_images' folder is empty. Please place images to be tested in this folder.zpThere is no 'test_images' folder under current directory. Please create one and place images to be tested there.)ospathexistsisfilelistdirprint)r   emptys     cC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\shap/utils/image.pyis_emptyr      sk    
 E	ww~~dBGGNN4$8$8zz$Ecd L	 ~	
 L    c                 |   [         R                  R                  U 5      (       dG  [         R                  R                  U 5      (       d  [         R                  " U 5        g[        S5        g[         R                  " U 5      (       a6  [         R                  " U 5       H  n[         R                  " X-   5        M     gg)zQFunction to create a new directory with given path or empty if it already exists.z Please give a valid folder path.N)r   r   r   r   makedirsr
   r	   remove)r   files     r   make_dirr      st    77>>$ww~~d##KK45 ::d

4(		$+& ) r   c                     [         R                  R                  5       u  pSn/ SQn[        U5       H>  u  pVXT;   d  M  [        R
                  R                  X S35      n[        Xg5        US-  nM@     g)zLFunction to add sample images from imagenet50 SHAP data in the given folder.   )         ,   .jpgN)shapdatasets
imagenet50	enumerater   r   join
save_image)r   X_counterindexes_listiimagepath_to_images           r   add_sample_imagesr(   .   s`    ==##%DAG#LaLGGLL$/?@Mu,qLG	 !r   c                     [         R                  " U 5      n[         R                  " U[         R                  5      n[        R
                  " U5      R                  S5      $ )zPFunction to load image at given path and return numpy array of RGB float values.float)cv2imreadcvtColorCOLOR_BGR2RGBnparrayastype)r'   r&   s     r   
load_imager2   :   s?    JJ}%ELL 1 12E88E?!!'**r   c                 6    SnU R                  U5      (       a  gg)zFunction to check if a file has valid image extensions and return True if it does.
Note: Azure Cognitive Services only accepts below file formats.
).pngr   z.jpegz.gifz.bmpz.jfifTN)endswith)r'   valid_extensionss     r   check_valid_imager7   A   s%     J.// 0r   c                 b    [         R                  " U 5      S-  n[        R                  " X5        g)zOFunction to save image(RGB values array) at given path (filename and location).g     o@N)r/   r0   pltimsave)r0   r'   r&   s      r   r    r    J   s"     HHUOe#EJJ}$r   c                    [        U 5      nSn[        R                  R                  U 5      u  pEUR                  S5      S   nSnSnUR                  S   UR                  S   :X  a  UR                  S   S:  a  Xw4n	OUR                  S   UR                  S   :  aA  UR                  S   S:  a.  U[        UR                  S   U-  UR                  S   -  5      4n	OcUR                  S   UR                  S   :  aA  UR                  S   S:  a.  [        UR                  S   U-  UR                  S   -  5      U4n	OSnU(       a  [        R                  " UW	S   U	S   4S9n[        R                  R                  XS	-   5      n[        S
UR                  5        [        X#5        [        R                  " U5      R                  S5      nX#4$ )ar  Function to resize given image retaining original aspect ratio and save in given directory 'reshaped_dir'.
Returns numpy array of resized image and path where resized file is saved.

Note
----
Azure COGS CV has size limit of < 4MB and min size of 50x50 for images.
Hence, large image files are being reshaped in code below to increase speed of SHAP explanations and run Azure COGS for image captions.
If image (pixel_size, pixel_size) is greater than 500 for either of the dimensions:
1 - image is resized to have max. 500 pixel size for the dimension > 500
2 - other dimension is resized retaining the original aspect ratio

N.r   i  Tr   F)dsizer4   zReshaped image size:r*   )r2   r   r   splitshapeintr+   resizer   r
   r    r/   r0   r1   )
r'   reshaped_dirr&   reshaped_pathr"   tail	file_name
max_pixelsreshapenew_dims
             r   resize_imagerI   Q   s    }%E MggmmM*GA

3"IJG{{1~Q'EKKNS,@*	Q%++a.	(U[[^c-As5;;q>J#>Q#OPQ	Q%++a.	(U[[^c-Au{{1~
2U[[^CDjQ 

5WQZ(@A\v3EF$ekk25(&&w/r   c                 D   / nU H2  n[        U5      nUR                  UR                  [        5      5        M4     [        R
                  " US9nSn[        [        U5      5       H  n	US-  nXS-   :X  a  [        R
                  " US9nSnUR                  SX(5        [        R                  " XI   5        [        R                  " S5        [        U 5      [        U5      :  d  M  [        R                  " SR                  [        [        X	   5      SS95      5        M     g)	z=Function to display grid of images and their titles/captions.)figsizer   r   off
(   )widthN)r2   appendr1   r@   r9   figurerangelenadd_subplotimshowaxistitler   r   str)
list_of_captionslist_of_imagesmax_columnsrK   masked_imagesfilenamer&   figcolumnr%   s
             r   display_grid_plotr`   |   s     M"8$U\\#./ #
 **W
%CF3}%&!1_$**W-CF;/

=#$ C$66IIdiiS)9)<%=R HIJ 'r   )   )   rb   )r   textwrapr   r+   matplotlib.pyplotpyplotr9   numpyr/   r   r   r   r(   r2   r7   r    rI   r`    r   r   <module>rh      sA    	  
   &' 	+%( VKr   