
    1Vh#                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ d
dddddZdZdZd Zej*                  j-                  e        G d dej.                        Zy)zThe TensorBoard Images plugin.    N)wrappers)errors)plugin_util)	http_util)provider)base_plugin)metadataz	image/bmpz	image/gifz
image/jpegz	image/pngzimage/svg+xml)bmpgifjpegpngsvgzapplication/octet-stream
   c                 L    ~| j                  d      s| j                  d      ryy )Ns   <?xml s   <svg r   )
startswith)datafs     W/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/plugins/image/images_plugin.py
detect_svgr   ,   s$    	y!T__X%> &?    c                      e Zd ZdZej
                  Zd Zd Zd Z	d Z
d Zej                  j                  d        Zd Zd	 Zd
 Zd Zd Zej                  j                  d        Zej                  j                  d        Zy)ImagesPluginzImages Plugin for TensorBoard.c                     |j                   xs i j                  | j                  t              | _        |j
                  | _        t        j                  dd      | _	        y)z|Instantiates ImagesPlugin via TensorBoard core.

        Args:
          context: A base_plugin.TBContext instance.
        imager   )	data_kindlatest_known_versionN)
sampling_hintsgetplugin_name_DEFAULT_DOWNSAMPLING_downsample_todata_provider_data_providerr   _MetadataVersionChecker_version_checker)selfcontexts     r   __init__zImagesPlugin.__init__;   sW      '55;@@3
 &33 + C C!"!
r   c                 J    | j                   | j                  | j                  dS )N)z/imagesz/individualImagez/tags)_serve_image_metadata_serve_individual_image_serve_tagsr&   s    r   get_plugin_appszImagesPlugin.get_plugin_appsJ   s'    11 $ < <%%
 	
r   c                      y)NF r-   s    r   	is_activezImagesPlugin.is_activeQ   s    r   c                 .    t        j                  d      S )Nztf-image-dashboard)element_name)r   FrontendMetadatar-   s    r   frontend_metadatazImagesPlugin.frontend_metadataT   s    ++9MNNr   c                    | j                   j                  ||t        j                        }|D ci c]  }|i  }}|j	                         D ]  \  }}|j	                         D ]  \  }}t        j
                  |j                        }	| j                  j                  |	j                  ||      sMt        j                  |j                        }
|j                  |
|j                  dz
  d||   |<     |S c c}w )N)experiment_idr      )displayNamedescriptionsamples)r#   list_blob_sequencesr	   PLUGIN_NAMEitemsparse_plugin_metadataplugin_contentr%   okversionr   markdown_to_safe_htmlr:   display_name
max_length)r&   ctx
experimentmappingrunresulttag_to_contenttag	metadatummdr:   s              r   _index_implzImagesPlugin._index_implW   s   %%99$ ,, : 

 &--c#r'--#*==? 	C"0"6"6"8 Y33I4L4LM,,//

CE)??)) $-#9#9#.(33a7$sC 	  .s   
C8c                    t        j                  |j                        }t        j                  |j                        }|j                  j                  d      }|j                  j                  d      }t        |j                  j                  dd            }	 | j                  |||||      }t        j                  ||d	      S # t        $ r t        j                  |ddd      cY S w xY w)
a  Given a tag and list of runs, serve a list of metadata for images.

        Note that the images themselves are not sent; instead, we respond with URLs
        to the images. The frontend should treat these URLs as opaque and should not
        try to parse information about them or generate them itself, as the format
        may change.

        Args:
          request: A werkzeug.wrappers.Request object.

        Returns:
          A werkzeug.Response application.
        rL   rI   sampler   zInvalid run or tag
text/plain  codeapplication/json)r   r'   environr7   argsr   int_image_response_for_runKeyErrorr   Respond)r&   requestrF   rG   rL   rI   rQ   responses           r   r*   z"ImagesPlugin._serve_image_metadatam   s     !!'//2 ..w?
llu%llu%W\\%%h23	33Zc6H   (4FGG	  	$$-|# 	s   C "C,+C,c           
         | j                   j                  ||t        j                  | j                  t        j                  |g|g            }|j                  |i       j                  |d      }|t        j                  d|d|      |D cg c]V  }t        |j                        dz
  |kD  r9|j                  |j                  | j                  |j                  |dz            dX c}S c c}w )a  Builds a JSON-serializable object with information about images.

        Args:
          run: The name of the run.
          tag: The name of the tag the images all belong to.
          sample: The zero-indexed sample of the image for which to retrieve
            information. For instance, setting `sample` to `2` will fetch
            information about only the third image of each batch. Steps with
            fewer than three images will be omitted from the results.

        Returns:
          A list of dictionaries containing the wall time, step, and URL
          for each image.

        Raises:
          KeyError, NotFoundError: If no image data exists for the given
            parameters.
        )runstags)r7   r   
downsamplerun_tag_filterNzNo image data for run=z, tag=r8   )	wall_timestepquery)r#   read_blob_sequencesr	   r=   r!   r   RunTagFilterr   r   NotFoundErrorlenvaluesrd   re   _data_provider_query)	r&   rF   rG   rI   rL   rQ   
all_imagesimagesdatums	            r   rZ   z$ImagesPlugin._image_response_for_run   s    & ((<<$ ,,**#00se3%H = 

 R(,,S$7>&&693?   
 5<< 1$v- #__

225<<
3KL
 	
 
s   AC1c                 v    |D cg c])  }t        |j                  j                        dz
  |kD  r|+ c}S c c}w )Nr8   )rj   tensor_proto
string_val)r&   tensor_eventsrQ   tensor_events       r   _filter_by_samplezImagesPlugin._filter_by_sample   sE     !.
L--889A= 
 	
 
s   .6c                 N    t         j                  j                  ||||d      }|S )a  Builds a URL for accessing the specified image.

        This should be kept in sync with _serve_image_metadata. Note that the URL is
        *not* guaranteed to always return the same image, since images may be
        unloaded from the reservoir as new images come in.

        Args:
          run: The name of the run.
          tag: The tag.
          sample: The relevant sample index, zero-indexed. See documentation
            on `_image_response_for_run` for more details.
          index: The index of the image. Negative values are OK.

        Returns:
          A string representation of a URL that will load the index-th sampled image
          in the given run with the given tag.
        )rI   rL   rQ   index)urllibparse	urlencode)r&   rI   rL   rQ   rw   query_strings         r   _query_for_individual_imagez(ImagesPlugin._query_for_individual_image   s1    $ ||-- 	
 r   c                 X    t         j                  j                  d|j                  i      S )Nblob_key)rx   ry   rz   r~   )r&   blob_references     r   rl   z!ImagesPlugin._data_provider_query   s"    ||%%z>3J3J&KLLr   c                 <    | j                   j                  ||      S )zReturns the actual image bytes for a given image.

        Args:
          blob_key: As returned by a previous `read_blob_sequences` call.

        Returns:
          A bytestring of the raw image bytes.
        )r~   )r#   	read_blob)r&   rF   r~   s      r   "_get_generic_data_individual_imagez/ImagesPlugin._get_generic_data_individual_image   s      "",,S8,DDr   c                 n   	 t        j                  |j                        }|j                  d   }| j	                  ||      }t        j                  d|      }t        j                  |t              }t        j                  |||      S # t
        t        f$ r t        j                  |ddd      cY S w xY w)zServes an individual image.r~   z"Invalid run, tag, index, or samplerR   rS   rT   N)r   r'   rW   rX   r   r[   
IndexErrorr   r\   imghdrwhat_IMGHDR_TO_MIMETYPEr   _DEFAULT_IMAGE_MIMETYPE)r&   r]   rF   r~   r   
image_typecontent_types          r   r+   z$ImagesPlugin._serve_individual_image   s    
	%%goo6C||J/H::3ID [[t,
*../
   $== *% 	$$4	 	s   A B	 	(B43B4c                     t        j                  |j                        }t        j                  |j                        }| j	                  ||      }t        j                  ||d      S )NrV   )r   r'   rW   r7   rO   r   r\   )r&   r]   rF   rG   rw   s        r   r,   zImagesPlugin._serve_tags   sR    !!'//2 ..w?
  j1  %1CDDr   N)__name__
__module____qualname____doc__r	   r=   r   r(   r.   r1   r5   rO   r   Requestapplicationr*   rZ   ru   r|   rl   r   r+   r,   r0   r   r   r   r   6   s    (&&K

O, !!H "H:'
R
8M	E !!> ">& !!E "Er   r   )r   r   urllib.parserx   werkzeugr   tensorboardr   r   tensorboard.backendr   tensorboard.datar   tensorboard.pluginsr   tensorboard.plugins.imager	   r   r   r    r   testsappendTBPluginr   r0   r   r   <module>r      s}    %     # ) % + .   5     J KE;'' KEr   