o
    wi                      @  s   d dl m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Zd dlmZ d dlZd dlmZ dd	lmZ dd
lmZmZ eejZedZG dd dZG dd dZdS )    )annotationsN)defaultdict)Iterable)Any)Version)Artifact   )internal)	Namespacefor_eachimport_loggerc                   @  sD  e Zd Zdd ZdLddZdLddZdLd	d
ZdMddZdNddZdOddZ	dPddZ
dPddZdLddZdPddZdQddZdRd!d"ZdRd#d$ZdPd%d&ZdPd'd(ZdPd)d*ZdPd+d,ZdPd-d.ZdPd/d0ZdPd1d2ZdPd3d4ZdSd6d7ZdSd8d9ZdSd:d;ZdSd<d=ZdPd>d?ZdPd@dAZdTdCdDZdUdFdGZ e!dVdIdJZ"dKS )W	MlflowRunc                 C  s   || _ || _d S N)runmlflow_client)selfr   r    r   O/home/ubuntu/.local/lib/python3.10/site-packages/wandb/apis/importers/mlflow.py__init__   s   
zMlflowRun.__init__returnstrc                 C  
   | j jjS r   )r   inforun_idr   r   r   r   r         
zMlflowRun.run_idc                 C  r   r   r   r   user_idr   r   r   r   entity    r   zMlflowRun.entityc                 C  s   dS )Nzimported-from-mlflowr   r   r   r   r   project#      zMlflowRun.projectdict[str, Any]c                 C  s2   | j jj}dd | j jj D }i |d|iS )Nc                 S  s    i | ]\}}| d s||qS )zmlflow.)
startswith).0kvr   r   r   
<dictcomp>*   s
    z$MlflowRun.config.<locals>.<dictcomp>imported_mlflow_tags)r   dataparamstagsitems)r   confr*   r   r   r   config&   s
   
zMlflowRun.configdict[str, float]c                 C  r   r   )r   r(   metricsr   r   r   r   summary/   r   zMlflowRun.summaryIterable[dict[str, float]]c                 c  sn    t t}| jjjD ]}| j| jjj|}|D ]}|j	||j
 |j< qq
| D ]\}}d|i|V  q)d S )N_step)r   dictr   r(   r/   r   get_metric_historyr   r   valuestepkeyr+   )r   dr$   metricitemr%   r   r   r   r/   2   s   zMlflowRun.metrics
str | Nonec                 C     d| j jj S )NzExperiment )r   r   experiment_idr   r   r   r   	run_group<      zMlflowRun.run_groupc                 C  r<   )NzUser r   r   r   r   r   job_type@   r?   zMlflowRun.job_typec                 C  s$   t tdk r| jjjd S | jjjS )Nz1.30.0zmlflow.runName)mlflow_versionr   r   r(   r*   r   run_namer   r   r   r   display_nameD   s   
zMlflowRun.display_namec                 C  s   | j jjdS )Nzmlflow.note.content)r   r(   r*   getr   r   r   r   notesI   s   zMlflowRun.noteslist[str] | Nonec                 C     d S r   r   r   r   r   r   r*   L   r    zMlflowRun.tagsIterable[Artifact] | Nonec                 C  sb   t tdk r| jj| jjjdd}n
tjj| jjjd}| 	| 
 }t|d}|| |gS )Nz2.0.0 )r   path)r   zimported-artifacts)rA   r   r   download_artifactsr   r   r   mlflow	artifacts_handle_incompatible_stringsrC   wandbr   add_dir)r   dir_pathartifact_nameartr   r   r   rM   R   s   
zMlflowRun.artifactsc                 C  rG   r   r   r   r   r   r   used_artifactsc   r    zMlflowRun.used_artifactsc                 C  rG   r   r   r   r   r   r   
os_versionf       zMlflowRun.os_versionc                 C  rG   r   r   r   r   r   r   python_versionh   rV   zMlflowRun.python_versionc                 C  rG   r   r   r   r   r   r   cuda_versionj   rV   zMlflowRun.cuda_versionc                 C  rG   r   r   r   r   r   r   programl   rV   zMlflowRun.programc                 C  rG   r   r   r   r   r   r   hostn   rV   zMlflowRun.hostc                 C  rG   r   r   r   r   r   r   usernamep   rV   zMlflowRun.usernamec                 C  rG   r   r   r   r   r   r   
executabler   rV   zMlflowRun.executablec                 C  rG   r   r   r   r   r   r   	gpus_usedt   rV   zMlflowRun.gpus_used
int | Nonec                 C  rG   r   r   r   r   r   r   	cpus_usedv   r    zMlflowRun.cpus_usedc                 C  rG   r   r   r   r   r   r   memory_usedy   rV   zMlflowRun.memory_usedc                 C  s0   | j jjd ur| j jjd n|  }||   S Ni  )r   r   end_time
start_time)r   rb   r   r   r   runtime{   s
   zMlflowRun.runtimec                 C  s   | j jjd S ra   )r   r   rc   r   r   r   r   rc         zMlflowRun.start_timec                 C  rG   r   r   r   r   r   r   	code_path   rV   zMlflowRun.code_pathc                 C  rG   r   r   r   r   r   r   cli_version   rV   zMlflowRun.cli_version Iterable[tuple[str, str]] | Nonec                 C  rG   r   r   r   r   r   r   files   rV   zMlflowRun.filesIterable[str] | Nonec                 C  rG   r   r   r   r   r   r   logs   rV   zMlflowRun.logssc                 C  s   d}d}t ||| S )Nz[^a-zA-Z0-9_\-\.]__)resub)rl   valid_charsreplacementr   r   r   rN      s   z&MlflowRun._handle_incompatible_stringsN)r   r   )r   r!   )r   r.   )r   r1   )r   r;   )r   rF   )r   rH   )r   r^   )r   rh   )r   rj   )rl   r   r   r   )#__name__
__module____qualname__r   r   r   r   r-   r0   r/   r>   r@   rC   rE   r*   rM   rT   rU   rW   rX   rY   rZ   r[   r\   r]   r_   r`   rd   rc   rf   rg   ri   rk   staticmethodrN   r   r   r   r   r      sB    




	
























r   c                   @  sb   e Zd Z	d)ddd*ddZdd Zddd+ddZddddd,d!d"Zddddd#d-d'd(ZdS ).MlflowImporterN)custom_api_kwargsdst_base_urlr   dst_api_keymlflow_tracking_urimlflow_registry_urir;   rw   dict[str, Any] | Noner   Nonec                C  sj   || _ || _|d u rddi}tjd|d|id|| _|| _t| j |r,t| tj	
|| _d S )NtimeoutiX  base_url)api_key	overridesr   )rx   ry   rO   Apidst_apirz   rL   set_tracking_uriset_registry_uritrackingMlflowClientr   )r   rx   ry   rz   r{   rw   r   r   r   r      s   	
zMlflowImporter.__init__c                 C  s   d| j  dS )Nz<MlflowImporter src=>)rz   r   r   r   r   __repr__   re   zMlflowImporter.__repr__)limitr   r^   Iterable[MlflowRun]c                #  sN    t tdk rj  nj   fdd}t| |}|E d H  d S )Nz1.28.0c                  3  s2     D ]} j | jD ]	}t|j V  qqd S r   )r   search_runsr=   r   )expr   experimentsr   r   r   _runs   s   z*MlflowImporter.collect_runs.<locals>._runs)rA   r   r   list_experimentssearch_experiments	itertoolsislice)r   r   r   runsr   r   r   collect_runs   s   
zMlflowImporter.collect_runsT)rM   	namespacer-   r   r   rM   boolr   Namespace | Noner-   !internal.SendManagerConfig | Nonec             	   C  s   |d u rt | | }|d u rtjdddddddd}| j| jddd}t| j	 tj
||j||d |rWt| }td| tj
|||j|tjddd d S d S )	NT)metadatari   mediacodehistoryr0   terminal_outputallow)r   r   resumeresumed)r   settings_overrider-   z!Importing history artifacts, run=)log_artifacts)
extra_artsr   r   r-   )r
   r   r   r	   SendManagerConfigry   rx   rL   r   rz   send_runsend_manager_overrideslistrM   loggerdebug)r   r   rM   r   r-   r   artsr   r   r   _import_run   sF   

zMlflowImporter._import_run)rM   r   parallelmax_workersr   r   r   c                  s$    fdd}t ||||d d S )Nc                   s   j |  d d S )N)r   rM   )r   )r   rM   r   r   r   r   _import_run_wrapped   s   z7MlflowImporter.import_runs.<locals>._import_run_wrapped)r   r   )r   )r   r   rM   r   r   r   r   r   r   r   import_runs   s   	zMlflowImporter.import_runsr   )rx   r   ry   r   rz   r   r{   r;   rw   r|   r   r}   )r   r^   r   r   )
r   r   rM   r   r   r   r-   r   r   r}   )r   r   rM   r   r   r   r   r   r   r^   r   r}   )rr   rs   rt   r   r   r   r   r   r   r   r   r   rv      s     5rv   )
__future__r   r   loggingrn   collectionsr   collections.abcr   typingr   rL   packaging.versionr   rO   r   	internalsr	   internals.utilr
   r   __version__rA   	getLoggerr   r   rv   r   r   r   r   <module>   s"    

~