o
    e3                     @   s   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ddZG dd deZdS )    N)partial)
components)bfile)
record_pb2)RecordReader)default_data_manager)
decode_tag
encode_tagFc                 C   sL   d| vrdS |r$t | d}| }t }|| d|jd jkr$dS dS )a*  Determine whether it is a VDL log file according to the file name.

    File name of a VDL log file must contain `vdlrecords`.

    Args:
        path: File name to determine.
        check: Check file is valid or not.

    Returns:
        True if the file is a VDL log file, otherwise false.
    Z
vdlrecordsFfilepathZmeta_data_tagr   T)r   Zget_nextr   RecordParseFromStringvaluestag)pathcheck_reader	meta_datarecord r   FD:\Projects\ConvertPro\env\Lib\site-packages\visualdl/reader/reader.pyis_VDLRecord_file   s   

r   c                   @   s   e Zd ZdZd?ddZedd Zejdd Ze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@ddZdd Zdd  ZdAd"d#ZdBd%d&Zd@d'd(Zd)d* ZdCd+d,ZdAd-d.Zed/d0 ZdAd1d2Zd3d4 Zd@d5d6Zd@d7d8ZdAd9d:Zd;d< Z d=d> Z!d$S )D	LogReaderzELog reader to read vdl log, support for frontend api in lib.py.

     c                 C   s   t |tr
|g| _n|| _d| _i | _d| _i | _i | _i | _i | _	t
dd | _|rWt
dd | _| j|d |  }| j|d t }|D ]
}td||f  qJdS |rjt| _| jdd	 i | _d
| _dS dS )z}Instance of LogReader

        Args:
            logdir: The dir include vdl log files, multiple subfolders allowed.
        Nc                   S   s   t dd S )Nc                   S   
   t tS Ncollectionsdefaultdictlistr   r   r   r   <lambda>N       z6LogReader.__init__.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r   r!   N   s    z$LogReader.__init__.<locals>.<lambda>c                   S   r   r   r   r   r   r   r   r!   R   s    )	file_path)remainz(self.get_%s=partial(self.get_data, '%s')Tupdater   )
isinstancestrdirreaderreaderswalks_tags	name2tags	tags2nameZfile_readersr   r   
_log_datas	_log_dataget_file_reader
get_remainread_log_datar   keysexecr   data_managerload_new_dataZ_a_tags_model)selflogdirr#   r$   Zcomponents_namenamer   r   r   __init__8   s8   


zLogReader.__init__c                 C      | j S r   )r9   r:   r   r   r   modela      zLogReader.modelc                 C   s   || _ t|d4}||std|  nt|r|| _ ntd|  W d    d S W d    d S W d    d S 1 s@w   Y  d S )Nrbz:Model path %s should be file path, please check this path.z1Model path %s is invalid, please check this path.)r9   r   ZBFileisfileprintexists)r:   Z
model_pathZbfpr   r   r   r@   e   s$   

"c                 C   r>   r   )r)   r?   r   r   r   r;   t   rA   zLogReader.logdirc                 C   sp   g }d|kr|D ]}| |j|j|j|jg q|S d|kr6|D ]}| |j|j|jj|j|jjg q |S |S )Nscalarscalars)appendidr   	timestampvalue	tag_value)r:   	componentZproto_datasdataitemr   r   r   parsing_from_protox   s   zLogReader.parsing_from_protoc                 C   sH   | j  }i }|D ]}t| j |  }ttdd |}|||< q	|S )Nc                 S   s   t | S r   r	   )xr   r   r   r!      s    z)LogReader._get_log_tags.<locals>.<lambda>)r1   r5   r    map)r:   Zcomponent_keysZlog_tagskeyr-   tagsr   r   r   _get_log_tags   s   

zLogReader._get_log_tagsc                 C   s   || j  v r$|| j |  v r$|| j | |  v r$| j | | | S t|| j| }| j|dd}||_|| _| j|d}| j	|dd| | }| 
||}|| j | | |< |S )NF)r#   r&   )r*   )r$   r&   )r0   r5   r   joinr,   _get_file_readerr)   r*   r3   r4   rP   )r:   rM   runr   r#   r*   r$   rN   r   r   r   get_log_data   s&   zLogReader.get_log_datac                 C   s   |   S r   )rU   r?   r   r   r   get_tags   s   zLogReader.get_tagsc                 C   s   | j | t| S r   )r1   r   )r:   rM   r   r   r   r   get_data   s   zLogReader.get_datac                 C   s  t  }|| |jd }t|j}t| jj	|}|| j
 vr|d}d|kr.d}nQd|kr5d}nJd|kr<d}nCd|krCd}n<d|krJd}n5d	|krQd	}n.d
|krXd
}n'd|krd| | d}nd|krkd}nd|krrd}nd|kryd}ntd| || j
|< | j
| | jj	||fS )zxRegister to self._tags by component type.

        Args:
            record_bin: Binary data from vdl log file.
        r   	one_valuerK   rF   imageZ
embeddingsZaudioZ	histogramZpr_curveZ	roc_curver   textZhparamZhyper_parametersrL   rG   zInvalid value type `%s`.)r   r   r   r   r   r   r   rV   r*   r)   r-   r5   Z
WhichOneofupdate_meta_data	TypeError)r:   Z
record_binr   rK   r   r   Z
value_typerM   r   r   r   parse_from_bin   sB   





zLogReader.parse_from_binc                 C   s:   |j d j}|jr| jj| j|j< |j| j| jj< d S d S )Nr   )r   r   Zdisplay_namer*   r)   r.   r/   )r:   r   metar   r   r   r_      s
   zLogReader.update_meta_datac                 C   s<   i | _ | jD ]}t|D ]\}}}| j ||i qqd S r   )r,   r)   r   walkr&   )r:   r)   rootdirsfilesr   r   r   get_all_walk   s   
zLogReader.get_all_walkFc                 C   sv   | j du s	|du r8|   i }| j  D ] \}}dd |D }|jdd t|dkr4|||d i q|| _ | j S )a   Get logs.

        Every dir(means `run` in vdl) has only one log(meads `actual log file`).

        Returns:
            walks: A dict like {"exp1": "vdlrecords.1587375595.log",
                                "exp2": "vdlrecords.1587375685.log"}
        NTc                 S   s   g | ]}t |r|qS r   )r   ).0r   r   r   r   
<listcomp>   s    z"LogReader.logs.<locals>.<listcomp>)reverser   )r,   rg   itemssortlenr&   )r:   r&   Z
walks_temprX   rT   Z	tags_tempr   r   r   logs   s   	zLogReader.logsc                 C   sn   | j du r	|   | j |d|krtd|d | t||}|| j vr.| || | j| | _	| j	S )zGet log reader for every vdl log file.

        Get instance of class RecordReader base on BFile. Note that each
        `log` may contain multi `tag`.

        Args:
            dir: Dir name of log.
            log: Vdl log file name.
        NzCan't find file %s./)
r,   rn   getFileNotFoundErrorr   rV   r+   r5   register_readerr*   )r:   r)   logr   r   r   r   get_log_reader   s   

zLogReader.get_log_readerc                 C   s   |  |dS )zGet file reader for specified vdl log file.

        Get instance of class RecordReader base on BFile.

        Args:
            file_path: Vdl log file path.
        T)rW   )r:   r#   r   r   r   r2   	  s   zLogReader.get_file_readerTc                 C   s.   |r|  | | j| | _| jS t|d}|S )Nr   )rr   r+   r*   r   )r:   r#   r&   r*   r   r   r   rW     s   

zLogReader._get_file_readerNc                 C   s@   |r|t | j vrt||d}||_|| j|< d S d S 	 d S )N)r   r)   )r    r+   r5   r   r)   )r:   r   r)   r&   r*   r   r   r   rr     s   zLogReader.register_readerc                 C   s:   |  | | j D ]\}}t||}| || q
dS )zxRegister all readers for all vdl log files.

        Args:
            update: Need update if `update` is True.
        N)rn   r,   rk   r   rV   rr   )r:   r&   r)   r   r   r   r   r   register_readers%  s
   
zLogReader.register_readersc                 C   sV   | j  D ]#}|| _| j }|D ]}| |\}}}}| j|| jj|| qqdS )zAdd remain data to data_manager.

        Add remain data to data manager according its component type and tag
        one by one.
        N)r+   r   r*   r3   ra   r7   Zadd_itemr)   )r:   r*   r$   rO   rM   r)   r   r   r   r   r   
add_remain0  s   
zLogReader.add_remainc                 C   s4   | j du r|du rtd|du r| j  S | S )z,Get all remain data by self.reader.
        NzPlease specify log path!)r*   RuntimeErrorr3   )r:   r*   r   r   r   r3   ?  s   
zLogReader.get_remainc           	      C   sJ   t dd }|D ]}| |\}}}}|| | | q	|r#|| _|S )znParse data from log file without sampling.

        Args:
            remain: Raw data from log file.
        c                   S   r   r   r   r   r   r   r   r!   M  r"   z)LogReader.read_log_data.<locals>.<lambda>)r   r   ra   rH   r1   )	r:   r$   r&   r1   rO   rM   r)   r   r   r   r   r   r4   G  s   zLogReader.read_log_datac                 C   r>   r   )r1   r?   r   r   r   log_dataV  rA   zLogReader.log_datac                 C   s   | j |d t| j S )Nr%   )rn   r    r,   r5   r:   r&   r   r   r   runsZ  s   zLogReader.runsc                 C   s   | j d u r	|   | j S r   )r-   rv   r?   r   r   r   rT   ^  s   
zLogReader.tagsc                 C   s6   | j du rt S |du r| j|d t| j }|S )z)Get components type used by vdl.
        NTr%   )r;   setr8   r-   r   )r:   r&   Zcomponents_setr   r   r   r   c  s   
zLogReader.componentsc                 C   sP   t  }| js|S |du r| j|d t | j D ]}|dkr q|| q|S )z1Get component tabs used by vdl frontend.
        Tr%   r   )r{   r;   r8   r-   r   add)r:   r&   component_tabsrM   r   r   r   r}   n  s   zLogReader.component_tabsc                 C   s&   | j dur| j|d |   dS dS )z~Load remain data.

        Make sure all readers for every vdl log file are registered, load all
        remain data.
        Nr%   )r;   ru   rv   ry   r   r   r   r8   |  s   
zLogReader.load_new_datac                 C   s   | S r   r   r?   r   r   r   	__enter__     zLogReader.__enter__c                 C   s   d S r   r   )r:   exc_typeexc_valexc_tbr   r   r   __exit__  r   zLogReader.__exit__)r   r   F)T)NTr   )"__name__
__module____qualname____doc__r=   propertyr@   setterr;   rP   rU   rY   rZ   r[   ra   r_   rg   rn   rt   r2   rW   rr   ru   rv   r3   r4   rx   rz   rT   r   r}   r8   r~   r   r   r   r   r   r   3   sF    
)



+



	
	







r   r   )r   	functoolsr   Zvisualdl.componentr   Zvisualdl.ior   Zvisualdl.protor   Zvisualdl.reader.record_readerr   Zvisualdl.server.data_managerr   Zvisualdl.utils.string_utilr   r
   r   objectr   r   r   r   r   <module>   s   
