
    h=                         S SK r S SKrS SKrS SKJr  S SKrS SKJr  SSK	J
r
   S SKrSqS SKJr  \R                   " \5      r " S	 S
5      rg! \ a    Sq N'f = f)    N)BytesIO)MessageToJson   )
_clean_tagT)ImageFc                      \ rS rSrSS04S jrS r\S 5       r\  SS j5       r\SS	 j5       r\SS
 j5       r	\   SS j5       r
\SS j5       r\  SS j5       r\SS j5       r\SS j5       r\SS j5       r\  S S j5       r\  S!S j5       r\     S"S j5       r\S 5       r\SS j5       r\SS j5       r\ SS j5       rSrg)#CometLogger   disabledTc                 x    S U l         Xl        US   SL a  SU l         g US   SL a  [        SL a  [        S5      eg g )Nr   TFzRComet and/or Python Image Library not installed. Run 'pip install comet-ml pillow')_logging_comet_configcomet_installed	Exception)selfcomet_configs     R/var/www/fran/franai/venv/lib/python3.13/site-packages/tensorboardX/comet_utils.py__init__CometLogger.__init__   sL    )
#t+!DM*%.?e3Kpqq 4L.    c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc                    > U S   nUR                   c  [        (       a  SUl          SUR                  ;  a  [        R                  " 5         [        R
                  " 5       b  [        R                  S5        [        R                  " S0 UR                  D6Ul	        SUl         UR                  R                  SS5        UR                   SL a  T" U 0 UD6$ g ! [         a  n[        R                  U5         S nAN<S nAff = f)	Nr   Fapi_keyzYou have already created a comet                                         experiment manually, which might                                         cause clashesTzCreated fromtensorboardX )r   r   r   comet_mlinitget_global_experimentloggerwarning
Experiment_experiment	log_otherr   )argskwargsr   emethods       r   wrapper+CometLogger._requiresComet.<locals>.wrapper   s    7D}}$ %& (:(:: 557C (7 8 (0':':'PT=O=O'PD$$(DM$$..~~N }}$t.v.. % ! &NN1%%&s   BC 
D !C;;D )	functoolswraps)r'   r(   s   ` r   _requiresCometCometLogger._requiresComet   s%    		 	/ 
!	/( r   c                 b    U R                   R                  5         S[        R                  l        g)zEnds an experiment.N)r"   endr   config
experiment)r   s    r   r/   CometLogger.end6   s"     	%)"r   Nc                 d    US:X  a  [        U5      OUnU R                  R                  XsXEU5        g)a  Logs a general metric (i.e accuracy, f1)..

Args:
    tag: String - Data identifier
    display_name: The title of the plot. If empty string is passed,
      `tag` will be used.
    value: Float/Integer/Boolean/String
    step: Optional. Used as the X axis when plotting on comet.ml
    epoch: Optional. Used as the X axis when plotting on comet.ml
    include_context: Optional. If set to True (the default),
        the current context will be logged along the metric.
 N)r   r"   
log_metric)r   tagdisplay_namevaluestepepochinclude_contextnames           r   r5   CometLogger.log_metric<   s1     #/""4z#,##D$3	5r   c                 <    U R                   R                  XX45        g)a  Logs a key,value dictionary of metrics.

Args:
    dic: key,value dictionary of metrics
    prefix: prefix added to metric name
    step: Optional. Used as the X axis when plotting on comet.ml
    epoch: Optional. Used as the X axis when plotting on comet.ml
N)r"   log_metrics)r   dicprefixr9   r:   s        r   r?   CometLogger.log_metricsO   s     	$$S$>r   c                 <    U R                   R                  XU5        g)zLogs a dictionary (or dictionary-like object) of multiple parameters.

Args:
    parameters: key,value dictionary of parameters
    prefix: prefix added to metric name
    step: Optional. Used as the X axis when plotting on comet.ml
N)r"   log_parameters)r   
parametersrA   r9   s       r   rD   CometLogger.log_parameters[   s     	''
DAr   c           	      B    U R                   R                  XUXEUU5        g)a  Logs the audio Asset determined by audio data.

Args:
audio_data: String or a numpy array - either the file path
    of the file you want to log, or a numpy array given to
    scipy.io.wavfile.write for wav conversion.
sample_rate: Integer - Optional. The sampling rate given to
    scipy.io.wavfile.write for creating the wav file.
file_name: String - Optional. A custom file name to be displayed.
    If not provided, the filename from the audio_data argument
    will be used.
metadata: Some additional data to attach to the the audio asset.
    Must be a JSON-encodable dict.
overwrite: if True will overwrite all existing assets with the same name.
copy_to_tmp: If audio_data is a numpy array, then this flag
    determines if the WAV file is first copied to a temporary
    file before upload. If copy_to_tmp is False, then it is sent
    directly to the cloud.
step: Optional. Used to associate the audio asset to a specific step.
N)r"   	log_audio)r   
audio_datasample_rate	file_namemetadata	overwritecopy_to_tmpr9   s           r   rH   CometLogger.log_audiof   s$    0 	"":I#+#'	)r   c                 <    U R                   R                  XU5        g)a  Logs the text. These strings appear on the Text Tab in the Comet UI.

Args:
text: string to be stored
step: Optional. Used to associate the asset to a specific step.
metadata: Some additional data to attach to the the text. Must
    be a JSON-encodable dict.
N)r"   log_text)r   textr9   rL   s       r   rQ   CometLogger.log_text   s     	!!$h7r   c                 B    U R                   R                  " XUXE40 UD6  g)aX  Logs a histogram of values for a 3D chart as an asset for
   this experiment. Calling this method multiple times with the
   same name and incremented steps will add additional histograms
   to the 3D chart on Comet.ml.

Args:
values: a list, tuple, array (any shape) to summarize, or a
    Histogram object
name: str (optional), name of summary
step: Optional. Used as the Z axis when plotting on Comet.ml.
epoch: Optional. Used as the Z axis when plotting on Comet.ml.
metadata: Optional: Used for items like prefix for histogram name.
kwargs: Optional. Additional keyword arguments for histogram.
N)r"   log_histogram_3d)r   valuesr<   r9   r:   rL   r%   s          r   log_histogramCometLogger.log_histogram   s'    " 	))&*/	4,2	4r   c                 t    UR                   S   R                  n[        U5      nXS'   U R                  XQUS9  g)zLog Raw Histogram Data to Comet as an Asset.

Args:
    tag: Name given to the logged asset
    summary: TensorboardX Summary protocol buffer with histogram data
    step: The Global Step for this experiment run. Defaults to None.
r   r<   )datar<   r9   N)r8   histor   log_asset_data)r   r6   summaryr9   histogram_protohistogram_raw_datas         r   log_histogram_rawCometLogger.log_histogram_raw   s?     "--*00*?;%(6"!3DIr   c                 v    U R                   R                  XR                  5       UR                  5       XE5        g)zLog timeseries data.

Args:
name: (str) name of data
x: array of x-axis values
y: array of y-axis values
overwrite: (optional, bool) if True, overwrite previous log
step: (optional, int) the step value
N)r"   	log_curvetolist)r   r<   xyrM   r9   s         r   rc   CometLogger.log_curve   s(     	""4QXXZQr   c                     [        U5      n[        R                  " U5      n[        U5      nU R                  R                  XVUS9  g)zLogs the image. Images are displayed on the Graphics tab on Comet.ml.

Args:
encoded_image_string: Required. An encoded image string
tag: String - Data identifier
step: Optional. Used to associate the image asset to a specific step.
r9   N)r   r   openr   r"   	log_image)r   encoded_image_stringr6   r9   buff	image_pilr<   s          r   log_image_encodedCometLogger.log_image_encoded   s?     +,JJt$	#""9">r   c                 @    U R                   R                  XUXEU5        g)a  Logs the Asset determined by file_data.

Args:
file_data: String or File-like - either the file path of the
    file you want to log, or a file-like asset.
file_name: String - Optional. A custom file name to be displayed.
    If not provided the filename from the file_data argument will be used.
overwrite: if True will overwrite all existing assets with
    the same name.
copy_to_tmp: If file_data is a file-like object, then this flag
    determines if the file is first copied to a temporary file
    before upload. If copy_to_tmp is False, then it is sent
    directly to the cloud.
step: Optional. Used to associate the asset to a specific step.
N)r"   	log_asset)r   	file_datarK   rM   rN   r9   rL   s          r   rr   CometLogger.log_asset   s"    $ 	""9#.h	@r   c                 >    U R                   R                  XX4XV5        g)a:  Logs the data given (str, binary, or JSON).

Args:
data: data to be saved as asset
name: String, optional. A custom file name to be displayed If
    not provided the filename from the temporary saved file
    will be used.
overwrite: Boolean, optional. Default False. If True will
    overwrite all existing assets with the same name.
step: Optional. Used to associate the asset to a specific step.
epoch: Optional. Used to associate the asset to a specific epoch.
metadata: Optional. Some additional data to attach to the
    asset data. Must be a JSON-encodable dict.
N)r"   r\   )r   rZ   r<   rM   r9   rL   r:   s          r   r\   CometLogger.log_asset_data   s    " 	''I(0	9r   c
                    Sn
Uc  gUbI  UR                  5       R                  5       R                  5       nUR                  SS n
U
S   S:X  a  U
SS n
[	        U5      [
        :X  a  [        R                  " U5      nO,UR                  5       R                  5       R                  5       nU R                  R                  XUXUUXxU	5
        g)aZ  Log a multi-dimensional dataset and metadata for viewing
   with Comet's Embedding Projector (experimental).

Args:
vectors: the tensors to visualize in 3D
labels: labels for each tensor
image_data: (optional) list of arrays or Images
image_preprocess_function: (optional) if image_data is an array,
    apply this function to each element first
image_transparent_color: a (red, green, blue) tuple
image_background_color_function: a function that takes an
    index, and returns a (red, green, blue) color tuple
title: (optional) name of tensor
template_filename: (optional) name of template JSON file
Nr   r   )
cpudetachnumpyshapetypelistnparrayr"   log_embedding)r   vectorslabels
image_dataimage_preprocess_functionimage_transparent_colorimage_background_color_functiontitletemplate_filenamegroup
image_sizes              r   r   CometLogger.log_embedding   s    * 
>!#)00288:J#))!"-J!}!'^
<4XXf%FZZ\((*002F&&w
'1'>'F',',	.r   c                     0 nXS'   UR                  5       US'   UR                  5       US'   UR                  5       US'   XXS'   XxS'   SUS'   [        R                  " U5      nU R                  XUS	9  g
)as  Logs a mesh as an asset

Args:
tag: Data identifier
vertices: List of the 3D coordinates of vertices.
colors: Colors for each vertex
faces: Indices of vertices within each triangle.
config_dict: Dictionary with ThreeJS classes names and configuration.
step: step value to record
walltime: Optional override default walltime (time.time())
    seconds after epoch of event
r6   verticescolorsfacesconfig_dictwalltimemesh
asset_typeri   N)rd   jsondumpsr\   )	r   r6   r   r   r   r   r9   r   	mesh_jsons	            r   log_meshCometLogger.log_mesh  s}     	% ( 1	*$mmo	("\\^	'#.-  (*"(	,JJy)	I6r   c                 0    UnX%S'   U R                  XQUS9  g)zLogs a histogram as an asset.

Args:
tag: Data identifier
asset_type: List of the 3D coordinates of vertices.
step: step value to record
r   ri   N)r\   )r   r6   r   r9   r%   	file_jsons         r   log_raw_figureCometLogger.log_raw_figure8  s$     	",,I6r   c           	         UR                   S   R                  nUR                  R                   Vs/ s H  ofR                  PM     nn[
        R                  " UR                  [
        R                  S9R                  U5      n[        U5       V	s/ s H  n	SU-  U	-  PM     n
n	[        S [
        R                  " XR                  S   5      5      u  ppnnUUUUUUU
US.nU R                  UXS9  gs  snf s  sn	f )Logs a Precision-Recall Curve Data as an asset.

Args:
tag: An identifier for the PR curve
summary: TensorboardX Summary protocol buffer.
step: step value to record
r   )dtype      ?c                 >    U R                  5       R                  5       $ Nflattenrd   re   s    r   <lambda>)CometLogger.log_pr_data.<locals>.<lambda>S      !))+:L:L:Nr   )TPFPTNFN	precisionrecall
thresholdsr<   r<   r9   N)r8   tensortensor_shapedimsizer~   fromiter	float_valfloat32reshaperangemapvsplitr{   r\   )r   r6   r]   num_thresholdsr9   tensor_protodr{   rV   ir   tpfptnfnr   r   pr_datas                     r   log_pr_dataCometLogger.log_pr_dataE  s     }}Q'..!-!:!:!>!>?!>A!>?\332::FNNuU8=n8MN8M1cN*Q.8M
N,/0NPRPYPYZ`bnbnopbqPr,s)	6 "$	
 	G#9# @ Os   C5C:c           
          [        U5       Vs/ s H  nSU-  U-  PM     nn[        S UUUUUU/5      u  pnnpgUUUUUUUU	US.	nU R                  UXS9  gs  snf )r   r   c                 >    U R                  5       R                  5       $ r   r   r   s    r   r   -CometLogger.log_pr_raw_data.<locals>.<lambda>o  r   r   )	r   r   r   r   r   r   r   weightsr<   r   N)r   r   r\   )r   r6   true_positive_countsfalse_positive_countstrue_negative_countsfalse_negative_countsr   r   r   r   r9   r   r   r   r   r   r   r   s                     r   log_pr_raw_dataCometLogger.log_pr_raw_datab  s     9>n8MN8M1cN*Q.8M
N,/0N ! !Q -)B	 "$

 	G#9+ Os   A)r   r"   r   )NNT)NNN)NN)NNNFTN)NNNNr   )FN)NFTNN)NFNNN)NNNNzComet EmbeddingNN)__name__
__module____qualname____firstlineno__r   r,   r/   r5   r?   rD   rH   rQ   rW   r`   rc   ro   rr   r\   r   r   r   r   r   __static_attributes__r   r   r   r	   r	      s   %/$6 r0 * *
 DH#'5 5$ 	? 	? B B @D>B) )6 	8 	8 @D#4 4( J J 
R 
R ? ? =B8<@ @( DH,09 9& 8<NRBS(, 	%. %.N 7 70 
7 
7 : :8  7; :  :r   r	   )r*   r   loggingior   rz   r~   google.protobuf.json_formatr   r]   r   r   r   PILr   ImportError	getLoggerr   r   r	   r   r   r   <module>r      s^         5 O 
		8	$o: o:  Os   A AA