U
    Vc=                     @   sp   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	 G dd de
Zdd	 Zd
d Zdd ZdS )ztWraps the base_plugin.TBContext to stores additional data shared across API
handlers for the HParams plugin backend.    N)api_pb2)metadata)json_formatc                   @   s   e Zd ZdZdddZdd Zedd Zd	d
 ZdddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) 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 truelly 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                 C   s   || _ || _dS )a  Instantiates a context.

        Args:
          tb_context: base_plugin.TBContext. The "base" context we extend.
          max_domain_discrete_len: int. Only used when computing the experiment
            from the session runs. The maximum number of disticnt values a string
            hyperparameter can have for us to populate its 'domain_discrete' field.
            Typically, only tests should specify a value for this parameter.
        N)_tb_context_max_domain_discrete_len)self
tb_contextZmax_domain_discrete_len r   O/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/plugins/hparams/backend_context.py__init__(   s    
zContext.__init__c                 C   s    |  |}|r|S | |||S )a  Returns the experiment protobuffer defining the experiment.

        Accepts a dict containing the plugin contents for all summary tags
        associated with the hparams plugin, as an optimization for callers
        who already have this information available, so that this function
        can minimize its calls to the underlying `DataProvider`.

        This method first attempts to find a metadata.EXPERIMENT_TAG tag and
        retrieve the associated protobuffer. If no such tag is found, the method
        will attempt to build a minimal experiment protobuffer 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).

        Returns:
          The experiment protobuffer. If no tags are found from which an experiment
          protobuffer can be built (possibly, because the event data has not been
          completely loaded yet), returns an entirely empty experiment.
        )_find_experiment_tag_compute_experiment_from_runs)r	   ctxexperiment_idhparams_run_to_tag_to_contentZ
experimentr   r   r   experiment_from_metadata5   s    
  z Context.experiment_from_metadatac                 C   s   | j S N)r   )r	   r   r   r   r
   R   s    zContext.tb_contextc                 C   s   dd |  D S )Nc                 S   s$   i | ]\}}|d d |  D qS )c                 S   s   i | ]\}}||j qS r   )Zplugin_content).0tagZtime_seriesr   r   r   
<dictcomp>X   s    z?Context._convert_plugin_metadata.<locals>.<dictcomp>.<dictcomp>items)r   runZtag_to_time_seriesr   r   r   r   W   s
    z4Context._convert_plugin_metadata.<locals>.<dictcomp>r   )r	   data_provider_outputr   r   r   _convert_plugin_metadataV   s    z Context._convert_plugin_metadataNc                 C   s   |  | jjj||tj|d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_providerZlist_tensorsr   PLUGIN_NAME)r	   r   r   r   r   r   r   hparams_metadata_   s    zContext.hparams_metadatac                 C   s   |  | jjj||tjd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   r   Zlist_scalarsscalar_metadatar    )r	   r   r   r   r   r   scalars_metadatat   s    
zContext.scalars_metadatac                 C   s,   | j jj||tj|dd}dd | D 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_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   r   Z
downsamplec                 S   s$   i | ]\}}|d d |  D qS )c                 S   s   i | ]\}}||d  qS )r   )r   r   datar   r   r   r      s      z8Context.read_last_scalars.<locals>.<dictcomp>.<dictcomp>r   )r   r   Ztag_to_datar   r   r   r      s    z-Context.read_last_scalars.<locals>.<dictcomp>)r   r   Zread_scalarsr"   r    r   )r	   r   r   r   r   r   r   r   read_last_scalars   s    
zContext.read_last_scalarsc                 C   s4   |  D ]&}|tj}|dk	rt|  S qdS )zFinds the experiment associcated with the metadata.EXPERIMENT_TAG
        tag.

        Returns:
          The experiment or None if no such experiment is found.
        N)valuesgetr   ZEXPERIMENT_TAGZparse_experiment_plugin_data)r	   r   tagsZmaybe_contentr   r   r   r      s
    	zContext._find_experiment_tagc                 C   s0   |  |}|r| |||}ng }tj||dS )zComputes a minimal Experiment protocol buffer by scanning the runs.

        Returns an empty Experiment if there are no hparam infos logged.
        )hparam_infosmetric_infos)_compute_hparam_infos_compute_metric_infosr   Z
Experiment)r	   r   r   r   r+   r,   r   r   r   r      s    
   z%Context._compute_experiment_from_runsc           
      C   s   t t}| D ]B}tj|kr"qt|tj }|j D ]\}}|| 	| q<qg }| D ]&\}}| 
||}	|	dk	rb|	|	 qb|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 and the number of distinct values is
        small enough.

        Returns:
          A list of api_pb2.HParamInfo messages.
        N)collectionsdefaultdictlistr(   r   SESSION_START_INFO_TAGZ$parse_session_start_info_plugin_datahparamsr   append _compute_hparam_info_from_values)
r	   r   r3   Ztag_to_contentZ
start_infonamevalueresultr(   Zhparam_infor   r   r   r-      s    

zContext._compute_hparam_infosc                 C   s   t j|t jd}tdd |D }|D ]H}t|}|s8q&|jt jkrL||_n|j|kr^t j|_|jt jkr& qpq&|jt jkrdS |jt jkrt|| jkr|j	
| |S )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.
        )r6   typec                 s   s   | ]}t |rt|V  qd S r   )_protobuf_value_type_protobuf_value_to_string)r   vr   r   r   	<genexpr>   s   z;Context._compute_hparam_info_from_values.<locals>.<genexpr>N)r   Z
HParamInfoZDATA_TYPE_UNSETsetr:   r9   DATA_TYPE_STRINGlenr   Zdomain_discreteextend)r	   r6   r(   r8   Zdistinct_valuesr<   Zv_typer   r   r   r5      s,    

z(Context._compute_hparam_info_from_valuesc                 C   s   dd |  |||D S )Nc                 s   s(   | ] \}}t jt j||d dV  qdS ))groupr   )r6   N)r   Z
MetricInfoZ
MetricName)r   r   rB   r   r   r   r=     s   z0Context._compute_metric_infos.<locals>.<genexpr>)_compute_metric_names)r	   r   r   r   r   r   r   r.     s      zContext._compute_metric_infosc                    s   t dd | D }t  }| ||}| D ]J\}}t||}	|	sHq0tj||	  dkrbd | fdd|D  q0t|}
|
	  |
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                 s   s    | ]\}}t j|kr|V  qd S r   )r   r2   )r   r   r*   r   r   r   r=   :  s   
z0Context._compute_metric_names.<locals>.<genexpr>. c                 3   s   | ]}| fV  qd S r   r   )r   r   rB   r   r   r=   L  s     )
r>   r   r#   _find_longest_parent_pathospathrelpathupdater1   sort)r	   r   r   r   Zsession_runsZmetric_names_setZscalars_run_to_tag_to_contentr   r*   sessionZmetric_names_listr   rF   r   rC     s&     
zContext._compute_metric_names)r   )N)__name__
__module____qualname____doc__r   r   propertyr
   r   r!   r#   r'   r   r   r-   r5   r.   rC   r   r   r   r   r      s   


	
!0
r   c                 C   s"   || kr|sdS t j|}q |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)rH   rI   dirname)Zpath_setrI   r   r   r   rG   S  s
    rG   c                 C   s4   |  drtjS |  dr tjS |  dr0tjS dS )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.
    Znumber_valuestring_valueZ
bool_valueN)HasFieldr   ZDATA_TYPE_FLOAT64r?   ZDATA_TYPE_BOOL)r7   r   r   r   r:   o  s    



r:   c                 C   s$   t | }| 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'.
    rT   r$   r%   )r   ZMessageToJsonrU   )r7   Zvalue_in_jsonr   r   r   r;     s    

r;   )rQ   r/   rH   Ztensorboard.plugins.hparamsr   r   Zgoogle.protobufr   Ztensorboard.plugins.scalarr"   objectr   rG   r:   r;   r   r   r   r   <module>   s     8