
    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 ej                  j                  ej                  ej                  j                   ej"                  ej                  j$                  ej&                  iZ G d d	      Zd
 Zd Zd Zd Zd ZddZy)ztWraps the base_plugin.TBContext to stores additional data shared across API
handlers for the HParams plugin backend.    N)provider)api_pb2)json_format_compat)metadata)json_formatc                       e Zd ZdZd Z	 ddZed        Zd ZddZ	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)Contexta  Wraps the base_plugin.TBContext to stores additional data shared across
    API handlers for the HParams plugin backend.

    Before adding fields to this class, carefully consider whether the
    field truly needs to be accessible to all API handlers or if it
    can be passed separately to the handler constructor. We want to
    avoid this class becoming a magic container of variables that have
    no better place. See http://wiki.c2.com/?MagicContainer
    c                     || _         y)zzInstantiates a context.

        Args:
          tb_context: base_plugin.TBContext. The "base" context we extend.
        N_tb_context)self
tb_contexts     [/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/plugins/hparams/backend_context.py__init__zContext.__init__0   s     &    Nc                     | j                  ||      }|rt        ||       |S | j                  ||||      }|rt        ||       |S | j                  ||||      }	|	r|	S t	        j
                         S )aQ  Returns the experiment proto defining the experiment.

        This method first attempts to find a metadata.EXPERIMENT_TAG tag and
        retrieve the associated proto.

        If no such tag is found, the method will attempt to build a minimal
        experiment proto by scanning for all metadata.SESSION_START_INFO_TAG
        tags (to compute the hparam_infos field of the experiment) and for all
        scalar tags (to compute the metric_infos field of the experiment).

        If no metadata.EXPERIMENT_TAG nor metadata.SESSION_START_INFO_TAG tags
        are found, then will build an experiment proto using the results from
        DataProvider.list_hyperparameters().

        Args:
          experiment_id: String, from `plugin_util.experiment_id`.
          include_metrics: Whether to determine metrics_infos and include them
            in the result.
          hparams_run_to_tag_to_content: The output from an hparams_metadata()
            call. A dict `d` such that `d[run][tag]` is a `bytes` value with the
            summary metadata content for the keyed time series.
          data_provider_hparams: The output from an hparams_from_data_provider()
            call, corresponding to DataProvider.list_hyperparameters().
            A provider.ListHyperpararametersResult.
          hparams_limit: Optional number of hyperparameter metadata to include in the
            result. If unset or zero, all metadata will be included.

        Returns:
          The experiment proto. If no data is found for an experiment proto to
          be built, returns an entirely empty experiment.
        )_find_experiment_tag!_sort_and_reduce_to_hparams_limit_compute_experiment_from_runs&_experiment_from_data_provider_hparamsr   
Experiment)
r   ctxexperiment_idinclude_metricshparams_run_to_tag_to_contentdata_provider_hparamshparams_limit
experimentexperiment_from_runs%experiment_from_data_provider_hparamss
             r   experiment_from_metadataz Context.experiment_from_metadata8   s    P ..)?

 -j-H#AA1N 
  -$m (' 77]O5J 	. 5 2	
 ##%	
r   c                     | j                   S Nr   )r   s    r   r   zContext.tb_context{   s    r   c                     |j                         D ci c]4  \  }}||j                         D ci c]  \  }}||j                   c}}6 c}}}}S c c}}w c c}}}}w r#   )itemsplugin_content)r   data_provider_outputruntag_to_time_seriestagtime_seriess         r   _convert_plugin_metadataz Context._convert_plugin_metadata   sr     .B-G-G-I
 

 *(	 *<*B*B*D&S+ [/// 
 	

s   A
A	A
A
c                     | j                  | j                  j                  j                  ||t        j
                  |            S )a  Reads summary metadata for all hparams time series.

        Args:
          experiment_id: String, from `plugin_util.experiment_id`.
          run_tag_filter: Optional `data.provider.RunTagFilter`, with
            the semantics as in `list_tensors`.

        Returns:
          A dict `d` such that `d[run][tag]` is a `bytes` value with the
          summary metadata content for the keyed time series.
        r   plugin_namerun_tag_filter)r,   r   data_providerlist_tensorsr   PLUGIN_NAMEr   r   r   r0   s       r   hparams_metadatazContext.hparams_metadata   sH     ,,**77+$00-	 8 
 	
r   c                     | j                  | j                  j                  j                  ||t        j
                              S )a&  Reads summary metadata for all scalar time series.

        Args:
          experiment_id: String, from `plugin_util.experiment_id`.

        Returns:
          A dict `d` such that `d[run][tag]` is a `bytes` value with the
          summary metadata content for the keyed time series.
        )r   r/   )r,   r   r1   list_scalarsscalar_metadatar3   )r   r   r   s      r   scalars_metadatazContext.scalars_metadata   sE     ,,**77++77 8 
 	
r   c                 p    | j                   j                  j                  ||t        j                  |      S )a  Reads the most recent values from scalar time series.

        Args:
          experiment_id: String.
          run_tag_filter: Required `data.provider.RunTagFilter`, with
            the semantics as in `read_last_scalars`.

        Returns:
          A dict `d` such that `d[run][tag]` is a `provider.ScalarDatum`
          value, with keys only for runs and tags that actually had
          data, which may be a subset of what was requested.
        r.   )r   r1   read_last_scalarsr8   r3   r4   s       r   r;   zContext.read_last_scalars   s;     --??''33)	 @ 
 	
r   c                 T    | j                   j                  j                  ||g|      S )zACalls DataProvider.list_hyperparameters() and returns the result.)experiment_idslimit)r   r1   list_hyperparameters)r   r   r   r>   s       r   hparams_from_data_providerz"Context.hparams_from_data_provider   s0    --BBu C 
 	
r   c                 X    | j                   j                  j                  ||g|||      S )zACalls DataProvider.read_hyperparameters() and returns the result.)r=   filterssorthparams_to_include)r   r1   read_hyperparameters)r   r   r   rB   rC   rD   s         r   !session_groups_from_data_providerz)Context.session_groups_from_data_provider   s:     --BB)?1 C 
 	
r   c                     |j                         D ]O  }|j                  t        j                        }|%t        j                  |      }|s|j                  d       |c S  y)zFinds the experiment associated with the metadata.EXPERIMENT_TAG
        tag.

        Returns:
          The experiment or None if no such experiment is found.
        Nmetric_infos)valuesgetr   EXPERIMENT_TAGparse_experiment_plugin_data
ClearField)r   r   r   tagsmaybe_contentr   s         r   r   zContext._find_experiment_tag   si     288: 	"D HHX%<%<=M(%BB!
 ' )).9!!	" r   c                     | j                  |      }|r|r| j                  |||      ng }|s|syt        j                  ||      S )zComputes a minimal Experiment protocol buffer by scanning the runs.

        Returns None if there are no hparam infos logged.
        Nhparam_infosrH   )_compute_hparam_infos_compute_metric_infos_from_runsr   r   )r   r   r   r   r   rR   rH   s          r   r   z%Context._compute_experiment_from_runs   s`     112OP
  00]$A  	 L!!%L
 	
r   c                    t        j                  t              }|j                         D ]q  }t        j
                  |vrt	        j                  |t        j
                           }|j                  j                         D ]  \  }}||   j                  |        s g }|j                         D ]+  \  }}| j                  ||      }	|	|j                  |	       - |S )a  Computes a list of api_pb2.HParamInfo from the current run, tag
        info.

        Finds all the SessionStartInfo messages and collects the hparams values
        appearing in each one. For each hparam attempts to deduce a type that fits
        all its values. Finally, sets the 'domain' of the resulting HParamInfo
        to be discrete if the type is string or boolean.

        Returns:
          A list of api_pb2.HParamInfo messages.
        )collectionsdefaultdictlistrI   r   SESSION_START_INFO_TAG$parse_session_start_info_plugin_datahparamsr%   append _compute_hparam_info_from_values)
r   r   r[   tag_to_content
start_infonamevalueresultrI   hparam_infos
             r   rS   zContext._compute_hparam_infos  s     ))$/;BBD 	,N..nD!FFx>>?J  *11779 ,e$$U+,	, #MMO 	+LD&??fMK&k*	+ r   c                    t        j                  |t         j                        }|D ]x  }t        |      }|s|j                  t         j                  k(  r||_        n$|j                  |k7  rt         j
                  |_        |j                  t         j
                  k(  sx n |j                  t         j                  k(  ry|j                  t         j
                  k(  r@t        d |D              }|j                  j                  |       t        |      dkD  |_
        |j                  t         j                  k(  r@t        d |D              }|j                  j                  |       t        |      dkD  |_
        |j                  t         j                  k(  rit        |D cg c]  }|j                   c}      }|rD|d   |j                  _        |d   |j                  _        t        t        |            dkD  |_
        |S c c}w )a2  Builds an HParamInfo message from the hparam name and list of
        values.

        Args:
          name: string. The hparam name.
          values: list of google.protobuf.Value messages. The list of values for the
            hparam.

        Returns:
          An api_pb2.HParamInfo message.
        )r`   typeNc              3   H   K   | ]  }t        |      rt        |        y wr#   )_can_be_converted_to_string_protobuf_value_to_string.0vs     r   	<genexpr>z;Context._compute_hparam_info_from_values.<locals>.<genexpr>H  s%      ).q1 *!,)s    "   c              3   4   K   | ]  }|j                     y wr#   )
bool_valueri   s     r   rl   z;Context._compute_hparam_info_from_values.<locals>.<genexpr>Q  s     &Dq||&Ds   r   )r   
HParamInfoDATA_TYPE_UNSET_protobuf_value_typere   DATA_TYPE_STRINGsetdomain_discreteextendlendiffersDATA_TYPE_BOOLDATA_TYPE_FLOAT64sortednumber_valuedomain_interval	min_value	max_value)	r   r`   rI   rb   rk   v_typedistinct_string_valuesdistinct_bool_valuesdistinct_float_valuess	            r   r]   z(Context._compute_hparam_info_from_values&  s     ##G4K4KL 
	A)!,F{{g555$&%66{{g666
	 ;;'111;;'222%( )) &"
 ""))*@A !781<FN;;'000#&&DV&D#D ""))*>? !56:FN;;'333$*F+KqANN+K$L!$3H3K&&03H3L&&0!$S)>%?!@1!D ,Ls   !Hc                     t        |t              r|}g }n|j                  }|j                  }|D cg c]  }| j	                  |       }}|r| j                  |||      ng }	t        j                  ||	      S c c}w )aI  Returns an experiment protobuffer based on data provider hparams.

        Args:
          experiment_id: String, from `plugin_util.experiment_id`.
          include_metrics: Whether to determine metrics_infos and include them
            in the result.
          data_provider_hparams: The output from an hparams_from_data_provider()
            call, corresponding to DataProvider.list_hyperparameters().
            A provider.ListHyperparametersResult.

        Returns:
          The experiment proto. If there are no hyperparameters in the input,
          returns None.
        rQ   )
isinstancerX   hyperparameterssession_groups_convert_data_provider_hparam6compute_metric_infos_from_data_provider_session_groupsr   r   )
r   r   r   r   r   r   r   	dp_hparamrR   rH   s
             r   r   z.Context._experiment_from_data_provider_hparams_  s    * +T2 4ON 4CCO2AAN -
 ..y9
 
  GG]N  	 !!%L
 	

s   A:c                 "   t        j                  |j                  |j                  |j                        }|j
                  t        j                  j                  k(  rHt         j                  |_
        |j                  \  }}||j                  _        ||j                  _        |S |j
                  t        j!                         v rIt        j#                  |j
                        |_
        |j$                  j'                  |j                         |S )a"  Builds an HParamInfo message from data provider Hyperparameter.

        Args:
          dp_hparam: The provider.Hyperparameter returned by the call to
            provider.DataProvider.list_hyperparameters().

        Returns:
          An HParamInfo to include in the Experiment.
        )r`   display_namery   )r   rq   hyperparameter_namehyperparameter_display_namery   domain_typer   HyperparameterDomainTypeINTERVALr{   re   domainr~   r   r   "_DISCRETE_DOMAIN_TYPE_TO_DATA_TYPEkeysrJ   rv   rw   )r   r   rc   dp_hparam_mindp_hparam_maxs        r   r   z%Context._convert_data_provider_hparam  s     ((..">>%%

   H$E$E$N$NN&88K-6-=-=*]M4AK''14AK''1  ""&H&M&M&OOAEE%% K ''..y/?/?@r   c                 v    t        d |j                         D              }d | j                  |||      D        S )Nc              3   J   K   | ]  \  }}t         j                  |v r|  y wr#   )r   rY   )rj   r(   rN   s      r   rl   z:Context._compute_metric_infos_from_runs.<locals>.<genexpr>  s*      
T..$6 
s   !#c              3   x   K   | ]2  \  }}t        j                  t        j                  ||              4 yw)groupr*   r`   N)r   
MetricInfo
MetricNamerj   r*   r   s      r   rl   z:Context._compute_metric_infos_from_runs.<locals>.<genexpr>  s7      
U G$6$6U$LMM
s   8:)ru   r%   _compute_metric_names)r   r   r   r   session_runss        r   rT   z'Context._compute_metric_infos_from_runs  sH      
:@@B
 


"88]L
 	
r   c           
          t        d |D              }| j                  |||      D cg c]0  \  }}t        j                  t        j                  ||            2 c}}S c c}}w )Nc              3   T   K   | ]   }|j                   D ]  }t        |        " y wr#   )sessions#generate_data_provider_session_name)rj   sgss      r   rl   zQContext.compute_metric_infos_from_data_provider_session_groups.<locals>.<genexpr>  s6      
[[
  02
2
s   &(r   r   )ru   r   r   r   r   )r   r   r   r   r   r*   r   s          r   r   z>Context.compute_metric_infos_from_data_provider_session_groups  sk      
$
 
 #88]L
U G$6$6U$LM
 	
 
s   5A"c                 h  
 t               }| j                  ||      }|j                         D ]f  \  }}t        ||      }|t        j
                  j                  ||      

dk(  r|j                  d      sd
|j                  
fd|D               h t        |      }	|	j                          |	S )aT  Computes the list of metric names from all the scalar (run, tag)
        pairs.

        The return value is a list of (tag, group) pairs representing the metric
        names. The list is sorted in Python tuple-order (lexicographical).

        For example, if the scalar (run, tag) pairs are:
        ("exp/session1", "loss")
        ("exp/session2", "loss")
        ("exp/session2/eval", "loss")
        ("exp/session2/validation", "accuracy")
        ("exp/no-session", "loss_2"),
        and the runs corresponding to sessions are "exp/session1", "exp/session2",
        this method will return [("loss", ""), ("loss", "/eval"), ("accuracy",
        "/validation")]

        More precisely, each scalar (run, tag) pair is converted to a (tag, group)
        metric name, where group is the suffix of run formed by removing the
        longest prefix which is a session run. If no session run is a prefix of
        'run', the pair is skipped.

        Returns:
          A python list containing pairs. Each pair is a (tag, group) pair
          representing a metric name used in some session.
        . c              3   &   K   | ]  }|f 
 y wr#    r   s     r   rl   z0Context._compute_metric_names.<locals>.<genexpr>  s     #ASS%L#As   )ru   r9   r%   _find_longest_parent_pathospathrelpathendswithupdaterX   rC   )r   r   r   r   metric_names_setscalars_run_to_tag_to_contentr(   rN   sessionmetric_names_listr   s             @r   r   zContext._compute_metric_names  s    4 5(,(=(=)
% 7<<> 		BIC/cBGGGOOC1E |CLL$5###AD#AA		B !!12   r   r#   )__name__
__module____qualname____doc__r   r!   propertyr   r,   r5   r9   r;   r@   rF   r   r   rS   r]   r   r   rT   r   r   r   r   r   r	   r	   %   sz    & A
F    

*
$
(


6
,@7r-
^6

+!r   r	   c                     | j                   s| j                  sy| j                   s| j                  S | j                  s| j                   S | j                    d| j                   S )zGenerates a name from a HyperparameterSesssionRun.

    If the HyperparameterSessionRun contains no experiment or run information
    then the name is set to the original experiment_id.
    r   /)r   r(   )r   s    r   r   r     sX       ""{{[[$$$''('++77r   c                 \    || vr'|syt         j                  j                  |      }|| vr'|S )a  Finds the longest "parent-path" of 'path' in 'path_set'.

    This function takes and returns "path-like" strings which are strings
    made of strings separated by os.sep. No file access is performed here, so
    these strings need not correspond to actual files in some file-system..
    This function returns the longest ancestor path
    For example, for path_set=["/foo/bar", "/foo", "/bar/foo"] and
    path="/foo/bar/sub_dir", returns "/foo/bar".

    Args:
      path_set: set of path-like strings -- e.g. a list of strings separated by
        os.sep. No actual disk-access is performed here, so these need not
        correspond to actual files.
      path: a path-like string.

    Returns:
      The element in path_set which is the longest parent directory of 'path'.
    N)r   r   dirname)path_setr   s     r   r   r     s5    * h
wwt$ h
 Kr   c                 D    t        |       syt        j                  |       S )NF)rs   r   is_serializable_valuera   s    r   rg   rg   !  s    &33E::r   c                     | j                  d      rt        j                  S | j                  d      rt        j                  S | j                  d      rt        j                  S y)zReturns the type of the google.protobuf.Value message as an
    api.DataType.

    Returns None if the type of 'value' is not one of the types supported in
    api_pb2.DataType.

    Args:
      value: google.protobuf.Value message.
    r}   string_valuero   N)HasFieldr   r{   rt   rz   r   s    r   rs   rs   '  sO     ~~n%(((~~n%'''~~l#%%%r   c                 \    t        j                  |       }| j                  d      r|dd S |S )zReturns a string representation of given google.protobuf.Value message.

    Args:
      value: google.protobuf.Value message. Assumed to be of type 'number',
        'string' or 'bool'.
    r   rm   rp   )r   MessageToJsonr   )ra   value_in_jsons     r   rh   rh   :  s3      --e4M~~n%Qr""r   c                     |st        | j                        }t        | j                  d       d| }| j                  d       | j                  j	                  |       y)a  Sorts and applies limit to the hparams in the given experiment proto.

    Args:
        experiment: An api_pb2.Experiment proto, which will be modified in place.
        hparams_limit: Optional number of hyperparameter metadata to include in the
            result. If unset or zero, no limit will be applied.

    Returns:
        None. `experiment` proto will be modified in place.
    c                 4    | j                    | j                  fS r#   )ry   r`   )rc   s    r   <lambda>z3_sort_and_reduce_to_hparams_limit.<locals>.<lambda>[  s    [%8%8!8+:J:J K r   )keyNrR   )rx   rR   r|   rM   rw   )r   r   limited_hparam_infoss      r   r   r   H  sa     J334 "K }
 .)""#78r   r#   )r   rV   r   tensorboard.datar   tensorboard.plugins.hparamsr   r   r   google.protobufr   tensorboard.plugins.scalarr8   r   DISCRETE_BOOLrz   DISCRETE_FLOATr{   DISCRETE_STRINGrt   r   r	   r   r   rg   rs   rh   r   r   r   r   <module>r      s   ,  	 & / : 0 ' B %%33W5K5K%%44g6O6O%%55w7O7O& "M! M!`8 8;&9r   