a
    d=ic                      @   s|   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dZdd	d
Z	dddZ
dd Zeedej ZdS )a"  Summary creation methods for the HParams plugin.

Typical usage for exporting summaries in a hyperparameters-tuning experiment:
1. Create the experiment (once) by calling experiment_pb() and exporting
   the resulting summary into a top-level (empty) run.
2. In each training session in the experiment, call session_start_pb() before
   the session starts, exporting the resulting summary into a uniquely named
   run for the session, say <session_name>.
3. Train the model in the session, exporting each metric as a scalar summary
   in runs of the form <session_name>/<sub_dir>, where <sub_dir> can be empty a
   (in which case the run is just the <session_name>) and depends on the
   metric. The name of such a metric is a (group, tag) pair given by
   (<sub_dir>, tag) where tag is the tag of the scalar summary.
   When calling experiment_pb in step 1, you'll need to pass all the metric
   names used in the experiemnt.
4. When the session completes, call session_end_pb() and export the resulting
   summary into the same session run <session_name>.
    N)api_pb2)metadata)plugin_data_pb2 c                 C   s8   |du rt   }tj|||| |d}ttjtj|dS )a  Creates a summary that defines a hyperparameter-tuning experiment.

    Args:
      hparam_infos: Array of api_pb2.HParamInfo messages. Describes the
          hyperparameters used in the experiment.
      metric_infos: Array of api_pb2.MetricInfo messages. Describes the metrics
          used in the experiment. See the documentation at the top of this file
          for how to populate this.
      user: String. An id for the user running the experiment
      description: String. A description for the experiment. May contain markdown.
      time_created_secs: float. The time the experiment is created in seconds
      since the UNIX epoch. If None uses the current time.

    Returns:
      A summary protobuffer containing the experiment definition.
    N)descriptionusertime_created_secshparam_infosmetric_infos)
experiment)timer   Z
Experiment_summaryr   ZEXPERIMENT_TAGr   HParamsPluginData)r	   r
   r   r   r   r    r   t/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/tensorboard/plugins/hparams/summary.pyexperiment_pb-   s    
r   c                 C   s   |du rt   }tj||||d}|  D ]\}}t|ttfrN||j| _q*t|t	rf||j| _
q*t|tr~||j| _q*t|ttfrt	||j| _
q*td||t|f q*ttjtj|dS )a  Constructs a SessionStartInfo protobuffer.

    Creates a summary that contains a training session metadata information.
    One such summary per training session should be created. Each should have
    a different run.

    Args:
      hparams: A dictionary with string keys. Describes the hyperparameter values
               used in the session, mapping each hyperparameter name to its value.
               Supported value types are  `bool`, `int`, `float`, `str`, `list`,
               `tuple`.
               The type of value must correspond to the type of hyperparameter
               (defined in the corresponding api_pb2.HParamInfo member of the
               Experiment protobuf) as follows:

                +-----------------+---------------------------------+
                |Hyperparameter   | Allowed (Python) value types    |
                |type             |                                 |
                +-----------------+---------------------------------+
                |DATA_TYPE_BOOL   | bool                            |
                |DATA_TYPE_FLOAT64| int, float                      |
                |DATA_TYPE_STRING | str, tuple, list   |
                +-----------------+---------------------------------+

               Tuple and list instances will be converted to their string
               representation.
      model_uri: See the comment for the field with the same name of
                 plugin_data_pb2.SessionStartInfo.
      monitor_url: See the comment for the field with the same name of
                   plugin_data_pb2.SessionStartInfo.
      group_name:  See the comment for the field with the same name of
                   plugin_data_pb2.SessionStartInfo.
      start_time_secs: float. The time to use as the session start time.
                       Represented as seconds since the UNIX epoch. If None uses
                       the current time.
    Returns:
      The summary protobuffer mentioned above.
    N)	model_urimonitor_url
group_namestart_time_secsz2hparams[%s]=%s has type: %s which is not supported)session_start_info)r   r   ZSessionStartInfoitems
isinstancefloatinthparamsZnumber_valuestrZstring_valueboolZ
bool_valuelisttuple	TypeErrortyper   r   ZSESSION_START_INFO_TAGr   )r   r   r   r   r   r   Zhp_nameZhp_valr   r   r   session_start_pbO   s8    )

r"   c                 C   s2   |du rt   }tj| |d}ttjtj|dS )ax  Constructs a SessionEndInfo protobuffer.

    Creates a summary that contains status information for a completed
    training session. Should be exported after the training session is completed.
    One such summary per training session should be created. Each should have
    a different run.

    Args:
      status: A tensorboard.hparams.Status enumeration value denoting the
          status of the session.
      end_time_secs: float. The time to use as the session end time. Represented
          as seconds since the unix epoch. If None uses the current time.

    Returns:
      The summary protobuffer mentioned above.
    N)statusend_time_secs)session_end_info)r   r   ZSessionEndInfor   r   ZSESSION_END_INFO_TAGr   )r#   r$   r%   r   r   r   session_end_pb   s    
r&   c                 C   sD   t jj }t|}| }t jjj|}|j	j
| |td |S )zReturns a summary holding the given HParamsPluginData message.

    Helper function.

    Args:
      tag: string. The tag to use.
      hparams_plugin_data: The HParamsPluginData message to use.
    )tagr   Ztensor)tfcompatv1Summaryr   Zcreate_summary_metadataSerializeToStringZSummaryMetadata
FromStringvalueadd_TF_NULL_TENSOR)r'   Zhparams_plugin_datasummaryZtb_metadataZraw_metadataZtf_metadatar   r   r   r      s    	
r   )r   r   N)r   r   r   N)N)__doc__r   Z
tensorflowr(   Ztensorboard.plugins.hparamsr   r   r   r   r"   r&   r   r!   Zmake_tensor_protor-   ZNULL_TENSORr,   r0   r   r   r   r   <module>   s    
# 
G
