o
    rri                     @   s   d 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Zddl	m
Z
 ddlmZ ddlmZ ddlmZ dd
edefddZG dd dZG dd dZdddZdedejfddZdS )zHiPlot support.    )ProcessPoolExecutorN)	OmegaConf)BaseContainer   )XP)get_main   value	precisionc                 C   s&   t | s| S t| d|  d|  S )zCRound value but returned as float, to make display nicer in Hiplot.
   )mathisfiniteround)r	   r
    r   ?/home/ubuntu/.local/lib/python3.10/site-packages/dora/hiplot.pyroundf   s   
r   c                   @   sb   e Zd ZdZdedejeejf fddZ	dedej
ejeejf  fddZd	ejfd
dZdS )HiPlotExplorerzvYou can inherit this class in order to make custom HiPlotExplorer,
    for instance to select a subset of the metrics.xpmetricsc                 C   s   |S )Nr   )selfr   r   r   r   r   process_metrics    s   zHiPlotExplorer.process_metricshistoryc                    s    fdd|D S )Nc                    s   g | ]}  |qS r   )r   ).0mr   r   r   r   
<listcomp>$   s    z2HiPlotExplorer.process_history.<locals>.<listcomp>r   )r   r   r   r   r   r   process_history#   s   zHiPlotExplorer.process_historyexpc                 C   s   dS )zpUse this method to further tune the `hiplot.Experiment` object,
        for instance setting a XY plot.
        Nr   )r   r   r   r   r   postprocess_exp&   s   zHiPlotExplorer.postprocess_expN)__name__
__module____qualname____doc__r   tpDictstrAnyr   Listr   hiplot
Experimentr   r   r   r   r   r      s
    $r   c                   @   s   e Zd ZdZdZdZdS )STYLEzbadge badge-pill badge-primaryz badge badge-pill badge-secondaryzbadge badge-pill badge-darkN)r   r    r!   r   internalparamsr   r   r   r   r*   -   s    r*    c                 C   sT   |d u ri n|}|   D ]\}}t|tr!t|||| d d q|||| < q|S )N.)outprefix)items
isinstancedict_flatten)dctr/   r0   keyr	   r   r   r   r4   3   s   
r4   urireturnc           "   
   C   s|  t  }t }d}d}|jj}|du r|jd }t| D ]M}d|v rH|dd\}}|dkr9|}|du r8|d }n|dkr@|}ntd	| q|jj|jj	 | }	|	
 rd|	 D ]}
||
j qZq|| q|du rpd
}|d | }t|}|dusJ || }td}t||j|}W d   n1 sw   Y  t }|s|S ||d }g }t }|D ]?}||}| D ]\}}|| ||v r|| |kr|| qt| t|  }|D ]}|| q|| q|t| 8 }t||D ]\}}i }| D ]!\}}||vr0|||ddd }|||< tj|j| _ q|D ]0}||vrczt!d| d|i}W n t"yR   d}Y nw |||ddd }|||< q4| D ]$\}}t#|t$rzt%j&|dd}t#|trd'tt(|}|||< qi|j)|d< d}|j*+  |,||j*j-}t }t.|D ]M\}}t/|}||d< |t0|j*j-d k|d< t1|} |2|  tj3|j) d| ||d}!|!j4}|j5|! |  D ]}|| tj6|j| _ qqq|7tj8j92ddgddgt| d |7tj8j:2dgg dt| d tj;|jd _ tj;|jd _ tj;|jd _ |<| |S )a  Loader for hiplot
    Running: python -m hiplot dora.hiplot.load --port=XXXX
    will run an hiplot server. You can provide there a list of sigs or grid names, separated
    by spaces.

    To select metrics or further tune the display, you should inherit from
    `HiPlotExplorer`, very similar in spirit to the grid explorers.

    To specify the explorer, using `explorer=MyExplorer`.
    You can also change the module to look into with `explorer_module=` (default is
    `yourproject.grids._hiplot`).
    Nr   z.grids=r   explorerz._hiplotexplorer_modulezInvalid param zdora.hiplotr.   r   r   zxp.cfg.r   T)resolvez, sigepochlast_)uidfrom_uidvaluesrB   rA   )hideorder)r=   r?   r>   )=r   setdoragrid_packagepackageshlexsplit
ValueErrordir_gridsexistsiterdiraddnamepydoclocater   listmapget_xp_from_sigr(   r)   get_name_partsr1   popkeysappendzipshort_name_partr*   r,   parameters_definition	label_cssevalAttributeErrorr2   r   r   to_containerjoinr%   r=   linkloadr   r   	enumerater3   lenr4   update	DatapointrA   
datapointsr   display_dataDisplaysPARALLEL_PLOTTABLEr+   r   )"r7   mainsigsr;   explorer_name
grids_nametokenr6   r	   grid_folderchildexplorer_qualifiedexplorer_klassr:   poolxpsr   	referencexps_name_partsall_columnsr   partsvalmissingrC   snamerB   r   metric_nameskr   point_valuesflat_metricsdpr   r   r   re   =   s   












re   )r   )Nr-   )r"   concurrent.futuresr   r   rS   rJ   typingr#   r(   	omegaconfr   omegaconf.basecontainerr   r   r   _utilsr   floatintr   r   r*   r4   r%   r&   re   r   r   r   r   <module>   s    

