
    1VhM                         d Z ddlZddl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  ej                         Z G d d	e
j                         Zd
 Zd Zd Zd Zd Zd Zd Zy)z;Bridge from event multiplexer storage to generic data APIs.    N)errors)summary_pb2)provider)
tb_logging)tensor_utilc                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	dd	Z
dd
ZddZ	 ddddZ	 dddddZ	 ddddZ	 ddddZ	 dddddZd Zd Zd Z	 ddddZ	 dddddZddZy)MultiplexerDataProviderc                      || _         || _        y)a6  Trivial initializer.

        Args:
          multiplexer: A `plugin_event_multiplexer.EventMultiplexer` (note:
            not a boring old `event_multiplexer.EventMultiplexer`).
          logdir: The log directory from which data is being read. Only used
            cosmetically. Should be a `str`.
        N)_multiplexer_logdir)selfmultiplexerlogdirs      b/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/backend/event_processing/data_provider.py__init__z MultiplexerDataProvider.__init__!   s     (    c                      d| j                   z  S )Nz"MultiplexerDataProvider(logdir=%r))r   )r   s    r   __str__zMultiplexerDataProvider.__str__-   s    3dllBBr   c                 P    t        |      j                  dk7  rt        d|      y )NRequestContextz#ctx must be a RequestContext; got: )type__name__	TypeError)r   ctxs     r   _validate_contextz)MultiplexerDataProvider._validate_context0   s(    9!11sLMM 2r   c                 f    t        |t              s!t        dt        dt        |      d|      y )Nzexperiment_id must be z
, but got : )
isinstancestrr   r   )r   experiment_ids     r   _validate_experiment_idz/MultiplexerDataProvider._validate_experiment_id4   s1     --]+]<  .r   c                 r    |t        d      t        |t              ry t        dt        |      d|      )Nz#`downsample` required but not givenz%`downsample` must be an int, but got r   )r   r   intr   )r   
downsamples     r   _validate_downsamplez,MultiplexerDataProvider._validate_downsample>   s=    ABBj#&J-
 	
r   c                 P    |j                   }|||vry|j                  }|||vryy)NFTrunstags)r   run_tag_filterruntagr(   r)   s         r   _test_run_tagz%MultiplexerDataProvider._test_run_tagH   s9    ""4""4r   c                 f    	 | j                   j                  |      S # t        $ r
}Y d }~y d }~ww xY wN)r   FirstEventTimestamp
ValueError)r   run_namees      r   _get_first_event_timestampz2MultiplexerDataProvider._get_first_event_timestampQ   s1    	$$88BB 		s    	00Nc                    | j                  |       | j                  |       t        j                  | j                        S )N)data_location)r   r!   r   ExperimentMetadatar   r   r   r    s      r   experiment_metadataz+MultiplexerDataProvider.experiment_metadataW   s3    s#$$]3**FFr   c                z    | j                  |       | j                  |       | j                  j                         S r/   )r   r!   r   ActivePluginsr8   s      r   list_pluginsz$MultiplexerDataProvider.list_plugins\   s5    s#$$]3   ..00r   c          
          | j                  |       | j                  |       | j                  j                         D cg c])  }t	        j
                  ||| j                  |            + c}S c c}w )N)run_idr2   
start_time)r   r!   r   Runsr   Runr4   )r   r   r    r+   s       r   	list_runsz!MultiplexerDataProvider.list_runsf   sn    s#$$]3 ((--/
  LL::3?
 	
 
s   .A0)r*   c                    | j                  |       | j                  |       | j                  ||t        j                        }| j                  t        j                  |      S r/   )r   r!   _indexr   DATA_CLASS_SCALAR_listr   ScalarTimeSeriesr   r   r    plugin_namer*   indexs         r   list_scalarsz$MultiplexerDataProvider.list_scalarsr   T     	s#$$]3)F)F
 zz(33U;;r   )r$   r*   c                    | j                  |       | j                  |       | j                  |       | j                  ||t        j
                        }| j                  t        ||      S r/   )r   r!   r%   rD   r   rE   _read_convert_scalar_eventr   r   r    rI   r$   r*   rJ   s          r   read_scalarsz$MultiplexerDataProvider.read_scalars|   _     	s#$$]3!!*-)F)F
 zz/
CCr   c                   | j                  |       | j                  |       | j                  ||t        j                        }t        j                  t              }|j                         D ]P  \  }}|j                         D ]8  \  }	}
| j                  j                  ||	      }|s%t        |d         ||   |	<   : R |S )N)r   r!   rD   r   rE   collectionsdefaultdictdictitemsr   TensorsrO   )r   r   r    rI   r*   rJ   run_tag_to_last_scalar_datumr+   tags_for_runr,   metadataeventss               r   read_last_scalarsz)MultiplexerDataProvider.read_last_scalars   s     	s#$$]3)F)F
 (3'>'>t'D$!& 	C!-!3!3!5 X**223<-fRj9 15c:	 ,+r   c                    | j                  |       | j                  |       | j                  ||t        j                        }| j                  t        j                  |      S r/   )r   r!   rD   r   DATA_CLASS_TENSORrF   r   TensorTimeSeriesrH   s         r   list_tensorsz$MultiplexerDataProvider.list_tensors   rL   r   c                    | j                  |       | j                  |       | j                  |       | j                  ||t        j
                        }| j                  t        ||      S r/   )r   r!   r%   rD   r   r`   rN   _convert_tensor_eventrP   s          r   read_tensorsz$MultiplexerDataProvider.read_tensors   rR   r   c                 B   |t        j                  dd      }|j                  }|j                  }|rJt	        |      dk(  r<|r:t	        |      dk(  r,|\  }|\  }	 | j
                  j                  ||      }|||ii}	n| j
                  j                         }	i }
|	j                         D ]a  \  }}|||vri }|j                         D ]@  \  }}|||vr|j                  |k7  r|j                  j                  |k7  r7||
|<   |||<   B c |
S # t        $ r i cY S w xY w)a  List time series and metadata matching the given filters.

        This is like `_list`, but doesn't traverse `Tensors(...)` to
        compute metadata that's not always needed.

        Args:
          plugin_name: A string plugin name filter (required).
          run_tag_filter: An `provider.RunTagFilter`, or `None`.
          data_class_filter: A `summary_pb2.DataClass` filter (required).

        Returns:
          A nested dict `d` such that `d[run][tag]` is a
          `SummaryMetadata` proto.
        Nr'      )r   RunTagFilterr(   r)   lenr   SummaryMetadataKeyErrorAllSummaryMetadatarX   
data_classplugin_datarI   )r   rI   r*   data_class_filterr(   r)   r+   r,   r\   all_metadataresulttag_to_metadataresult_for_runs                r   rD   zMultiplexerDataProvider._index   sV    !%224HN"""" CINtD	QFSFS,,<<S#F  #x1L,,??AL$0$6$6$8 	/ CCtON!0!6!6!8 /X#4&&*;;''33{B,s&.s#/		/ +  	s   D DDc           	         i }|j                         D ]  \  }}i }|||<   |j                         D ]  \  }}d}	d}
| j                  j                  ||      D ]=  }|	|	|j                  k  r|j                  }	|
|
|j                  k  s2|j                  }
? | j                  j                  ||      } ||	|
|j                  j                  |j                  |j                        ||<     |S )ac  Helper to list scalar or tensor time series.

        Args:
          construct_time_series: `ScalarTimeSeries` or `TensorTimeSeries`.
          index: The result of `self._index(...)`.

        Returns:
          A list of objects of type given by `construct_time_series`,
          suitable to be returned from `list_scalars` or `list_tensors`.
        N)max_stepmax_wall_timeplugin_contentdescriptiondisplay_name)
rX   r   rY   step	wall_timerj   rn   contentsummary_descriptionry   )r   construct_time_seriesrJ   rq   r+   rr   rs   r,   summary_metadataru   rv   events               r   rF   zMultiplexerDataProvider._list   s    $)KKM 	 CN(F3K)8)>)>)@ %% $!..66sC@ 8E'8ejj+@#(::$,0O(-	8
 $(#4#4#D#DS##N &;%"/#3#?#?#G#G 0 D D!1!>!>'s#	& r   c                    i }|j                         D ]d  \  }}i }|||<   |j                         D ]E  \  }}	| j                  j                  ||      }
|
D cg c]
  } ||       }}t        ||      ||<   G f |S c c}w )a  Helper to read scalar or tensor data from the multiplexer.

        Args:
          convert_event: Takes `plugin_event_accumulator.TensorEvent` to
            either `provider.ScalarDatum` or `provider.TensorDatum`.
          index: The result of `self._index(...)`.
          downsample: Non-negative `int`; how many samples to return per
            time series.

        Returns:
          A dict of dicts of values returned by `convert_event` calls,
          suitable to be returned from `read_scalars` or `read_tensors`.
        )rX   r   rY   _downsample)r   convert_eventrJ   r$   rq   r+   r[   rs   r,   r\   r]   r3   datas                r   rN   zMultiplexerDataProvider._read  s     !& 	DCN(F3K!-!3!3!5 DX**223<289Qa(99&1$
&Cs#D	D  :s   A<c          
         | j                  |       | j                  |       | j                  ||t        j                        }i }|j                         D ]  \  }}i }	|	||<   |j                         D ]  \  }
}d }d }d }| j                  j                  ||
      D ][  }|||j                  k  r|j                  }|||j                  k  r|j                  }t        |j                        }|||kD  sZ|}] t        j                  ||||j                  j                  |j                   |j"                        |	|
<     |S )N)ru   rv   
max_lengthrw   rx   ry   )r   r!   rD   r   DATA_CLASS_BLOB_SEQUENCErX   r   rY   rz   r{   _tensor_sizetensor_protor   BlobSequenceTimeSeriesrn   r|   r}   ry   )r   r   r    rI   r*   rJ   rq   r+   rr   rs   r,   r\   ru   rv   r   r   lengths                    r   list_blob_sequencesz+MultiplexerDataProvider.list_blob_sequences*  sU    	s#$$]3)M)M
 $)KKM 	 CN(F3K!0!6!6!8 X $!
!..66sC@ ,E'8ejj+@#(::$,0O(-)%*<*<=F!)Vj-@%+
, '/&E&E%"/)#+#7#7#?#? ( < <!)!6!6's#	. r   c          
         | j                  |       | j                  |       | j                  |       | j                  ||t        j
                        }i }|j                         D ]  \  }}	i }
|
||<   |	D ]  }| j                  j                  ||      }i }|D ]-  }|j                  |v rt        |||||      ||j                  <   / t        |j                               D cg c]  \  }}|	 }}}t        ||      |
|<     |S c c}}w r/   )r   r!   r%   rD   r   r   rX   r   rY   rz   _convert_blob_sequence_eventsortedr   )r   r   r    rI   r$   r*   rJ   rq   r+   r)   rs   r,   r]   data_by_stepr   rz   datumr   s                     r   read_blob_sequencesz+MultiplexerDataProvider.read_blob_sequencesL  s(    	s#$$]3!!*-)M)M
  	DICN(F3K 
D**223<!# Ezz\1 /K%{Ce0L, 4:,:L:L:N3OP-4PP&1$
&Cs#
D	D  Qs   %D	
c                   | j                  |       t        |      \  }}}}}| j                  j                  ||      }|j                  t
        j                  k7  rt        j                  |      | j                  j                  ||      }	t        fd|	D        d       }
|
st        j                  |d      t        j                  |
j                        }||   S )Nc              3   B   K   | ]  }|j                   k(  s|  y wr/   )rz   ).0r3   rz   s     r   	<genexpr>z4MultiplexerDataProvider.read_blob.<locals>.<genexpr>|  s     IA!&&D.aIs   z: no such step )r   _decode_blob_keyr   rj   rm   r   r   r   NotFoundErrorrY   nextr   make_ndarrayr   )r   r   blob_keyunused_experiment_idrI   r+   r,   rJ   r   tensor_eventsmatching_steptensorrz   s               @r   	read_blobz!MultiplexerDataProvider.read_blobl  s    s# X&	
   ,,<<S#F&&+*N*NN&&x00))11#s;II4P&&$'OPP))-*D*DEe}r   r/   )r   
__module____qualname__r   r   r   r!   r%   r-   r4   r9   r<   rB   rK   rQ   r^   rb   re   rD   rF   rN   r   r   r    r   r   r	   r	       s    
CN
G
1

 <FJ< D D& , ,2 <FJ< D D"/bB2  FJ H  @r   r	   c                     t        j                  | |||||fd      }|j                  d      }t        j                  |      }|j                  d      j                  d      S )a  Generate a blob key: a short, URL-safe string identifying a blob.

    A blob can be located using a set of integer and string fields; here we
    serialize these to allow passing the data through a URL.  Specifically, we
    1) construct a tuple of the arguments in order; 2) represent that as an
    ascii-encoded JSON string (without whitespace); and 3) take the URL-safe
    base64 encoding of that, with no padding.  For example:

        1)  Tuple: ("some_id", "graphs", "train", "graph_def", 2, 0)
        2)   JSON: ["some_id","graphs","train","graph_def",2,0]
        3) base64: WyJzb21lX2lkIiwiZ3JhcGhzIiwidHJhaW4iLCJncmFwaF9kZWYiLDIsMF0K

    Args:
      experiment_id: a string ID identifying an experiment.
      plugin_name: string
      run: string
      tag: string
      step: int
      index: int

    Returns:
      A URL-safe base64-encoded string representing the provided arguments.
    ),:)
separatorsascii=)jsondumpsencodebase64urlsafe_b64encodedecoderstrip)	r    rI   r+   r,   rz   rJ   stringified
bytesifiedencodeds	            r   _encode_blob_keyr     sa    6 **	S#tU;K ##G,J&&z2G>>'"))#..r   c                     t        j                  | dz         }|j                  d      }t        j                  |      \  }}}}}}||||||fS )a'  Decode a blob key produced by `_encode_blob_key` into component fields.

    Args:
      key: a blob key, as generated by `_encode_blob_key`.

    Returns:
      A tuple of `(experiment_id, plugin_name, run, tag, step, index)`, with types
      matching the arguments of `_encode_blob_key`.
    z==r   )r   urlsafe_b64decoder   r   loads)	keydecodedr   r    rI   r+   r,   rz   rJ   s	            r   r   r     sY     &&sTz2G..)K:>**;7]Kc4 ;S$>>r   c                     t        j                  | j                  | j                  t	        j
                  | j                        j                               S )zHelper for `read_scalars`.)rz   r{   value)r   ScalarDatumrz   r{   r   r   r   itemr   s    r   rO   rO     s@    ZZ//&&u'9'9:??A r   c                     t        j                  | j                  | j                  t	        j
                  | j                              S )zHelper for `read_tensors`.)rz   r{   numpy)r   TensorDatumrz   r{   r   r   r   r   s    r   rd   rd     s7    ZZ//&&u'9'9: r   c                      t        j                        }t         fdt        |      D              }t	        j
                  j                  j                  |      S )z!Helper for `read_blob_sequences`.c              3   x   K   | ]1  }t        j                  t        j                  |             3 y wr/   )r   BlobReferencer   rz   )r   idxr   r    rI   r+   r,   s     r   r   z/_convert_blob_sequence_event.<locals>.<genexpr>  sD        	

		
s   7:)r{   rz   values)r   r   tupleranger   BlobSequenceDatumr{   rz   )r    rI   r+   r,   r   	num_blobsr   s   `````  r   r   r     sW    U//0I  # F %%//ZZ r   c                 ^    d}| j                   j                  D ]  }||j                  z  } |S )zCompute the number of elements in a tensor.

    This does not deserialize the full tensor contents.

    Args:
      tensor_proto: A `tensorboard.compat.proto.tensor_pb2.TensorProto`.

    Returns:
      A non-negative `int`.
    rg   )tensor_shapedimsize)r   rq   r   s      r   r   r     s7     F((,, #((Mr   c                 0   |t        |       kD  rt        |       S |dk(  rg S t        j                  d      j	                  t        t        |       dz
        |dz
        }|j                          |t        |       dz
  gz  }|D cg c]  }| |   	 c}S c c}w )av  Downsample `xs` to at most `k` elements.

    If `k` is larger than `xs`, then the contents of `xs` itself will be
    returned. If `k` is smaller than `xs`, the last element of `xs` will
    always be included (unless `k` is `0`) and the preceding elements
    will be selected uniformly at random.

    This differs from `random.sample` in that it returns a subsequence
    (i.e., order is preserved) and that it permits `k > len(xs)`.

    The random number generator will always be `random.Random(0)`, so
    this function is deterministic (within a Python process).

    Args:
      xs: A sequence (`collections.abc.Sequence`).
      k: A non-negative integer.

    Returns:
      A new list whose elements are a subsequence of `xs` of length
      `min(k, len(xs))` and that is guaranteed to include the last
      element of `xs`, uniformly selected among such subsequences.
    r   rg   )ri   listrandomRandomsampler   sort)xskindicesis       r   r   r     s    0 	3r7{BxAv	mmA%%eCGaK&8!a%@GLLNB!}G"#aBqE###s   B)__doc__r   rU   r   r   tensorboardr   tensorboard.compat.protor   tensorboard.datar   tensorboard.utilr   r   
get_loggerloggerDataProviderr	   r   r   rO   rd   r   r   r   r   r   r   <module>r      sq    B      0 % ' (				 `h33 `H!/R?$.&$r   