B
    !`                 @   s   d Z ddlmZ ddlmZ ddlm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 ddlmZ ddlmZ ddlmZ dZdd ZG dd dejZdddZdddZdd ZdS )z/Experiment Data Access API for tensorboard.dev.    )absolute_import)division)print_functionN)base_experiment)auth)util)server_info)export_service_pb2)export_service_pb2_grpc)server_info_pb2)	grpc_utilzhttps://tensorboard.devc              C   s.   yddl } W n tk
r(   tdY nX | S )zCImport pandas, guarded by a user-friendly error message on failure.r   NzThe get_scalars() feature requires the pandas package, which does not seem to be available in your Python environment. You can install it with command:

  pip install pandas
)pandasImportError)r    r   `/home/dcms/DCMS/lib/python3.7/site-packages/tensorboard/data/experimental/experiment_from_dev.pyimport_pandas(   s    r   c                   s4   e Zd ZdZd
 fdd	ZdddZdd	 Z  ZS )ExperimentFromDevzBImplementation of BaseExperiment, specialized for tensorboard.dev.Nc                s$   t t|   || _t|d| _dS )a  Constructor of ExperimentFromDev.

        Args:
          experiment_id: String ID of the experiment on tensorboard.dev (e.g.,
            "AdYd1TgeTlaLWXx6I8JUbA").
          api_endpoint: Optional override value for API endpoint. Used for
            development only.
        )api_endpointN)superr   __init___experiment_idget_api_client_api_client)selfexperiment_idr   )	__class__r   r   r   9   s    	zExperimentFromDev.__init__Fc             C   s(  t  }|d k	rtd|d k	r&tdt }| j|_t }t|j	| | j
j|t d}g }	g }
g }g }g }xx|D ]p}t|jj}|	|jg|  |
|jg|  |t|jj |dd |jjD  |t|jj qzW |	|
||d}|r
||d< ||}|r$| |}|S )Nz=runs_filter support for get_scalars() is not implemented yet.z=tags_filter support for get_scalars() is not implemented yet.)metadatac             S   s   g | ]}|  d  qS )g    eA)ZToNanoseconds).0tr   r   r   
<listcomp>p   s    z1ExperimentFromDev.get_scalars.<locals>.<listcomp>)runtagstepvalue	wall_time)r   NotImplementedErrorr	   ZStreamExperimentDataRequestr   r   timer   Zset_timestampZread_timestampr   ZStreamExperimentDatar   Zversion_metadatalenZpointsvaluesextendZrun_nameZtag_nameliststeps
wall_timesZ	DataFrame_pivot_dataframe)r   Zruns_filterZtags_filterZpivotZinclude_wall_timer   requestZ	read_timestreamrunstagsr+   r,   r(   responseZ
num_valuesdata	dataframer   r   r   get_scalarsF   sH    



zExperimentFromDev.get_scalarsc             C   s   t | j}|jd|jkr&ddgndddgddd}t | j}||krXtd| }d |j_d	d
 |jj	D |j_	|S )Nr$   r#   r    r"   r!   F)r(   indexcolumnsZdropnazpivoted DataFrame contains missing value(s). This is likely due to two timeseries having different sets of steps in your experiment. You can avoid this error by calling `get_scalars()` with `pivot=False` to disable the DataFrame pivoting.c             S   s   g | ]}d qS )Nr   )r   namer   r   r   r      s    z6ExperimentFromDev._pivot_dataframe.<locals>.<listcomp>)
npZcount_nonzeroZisnullr(   Zpivot_tabler7   
ValueErrorZreset_indexr8   names)r   r4   Znum_missing_0Znum_missing_1r   r   r   r-      s    
z"ExperimentFromDev._pivot_dataframe)N)NNFF)__name__
__module____qualname____doc__r   r5   r-   __classcell__r   r   )r   r   r   6   s      
6r   c             C   sV   t | d}t| t }t  }|r<t|t|}t	|j
j|}t|S )N)r   )_get_server_info_handle_server_infogrpcZssl_channel_credentialsr   ZCredentialsStoreZread_credentialsZcomposite_channel_credentialsZid_token_call_credentialsZsecure_channelZ
api_serverZendpointr
   ZTensorBoardExporterServiceStub)r   r   Zchannel_credscredentialsZchannelr   r   r   r      s    
r   c             C   s$   dg}| rt t| |S t t|S )NZscalars)server_info_libZcreate_server_infoDEFAULT_ORIGINZfetch_server_info)r   Zpluginsr   r   r   rA      s
    
rA   c             C   sN   | j }|jtjkr0tjd|j  tj  n|jtj	krJt
d|j d S )NzWarning [from server]: %s
zError [from server]: %s)ZcompatibilityZverdictr   ZVERDICT_WARNsysstderrwritedetailsflushZVERDICT_ERRORr:   )infocompatr   r   r   rB      s    rB   )N)N)r?   
__future__r   r   r   rG   r&   rC   numpyr9   Ztensorboard.data.experimentalr   Ztensorboard.uploaderr   r   r   rE   Ztensorboard.uploader.protor	   r
   r   Ztensorboard.utilr   rF   r   ZBaseExperimentr   r   rA   rB   r   r   r   r   <module>   s*   k

