o
    xi$                  
   @  s  d dl mZ d dlZd dlZd dlmZ d dlmZ d dlZd dl	m
Z ddlmZ zd dlmZ W n eyC Z zed	edZ[ww zdd
lmZ W n ejye Z ze  dZW Y dZ[ndZ[ww zddlmZ W n ejy Z ze  dZW Y dZ[ndZ[ww zddlmZ W n ejy Z ze  dZW Y dZ[ndZ[ww G dd dZ	d2d3ddZ	d2d4ddZ	d2d5ddZ					d6d7d$d%Z					d6d8d&d'Z	d2d4d(d)Z	d2d5d*d+Zd,d- Z 					d9d:d0d1Z!dS );    )annotationsNwraps)Path)	telemetry   )errors)currentzqError: `metaflow` not installed >> This integration requires metaflow! To fix, please `pip install -Uqq metaflow`)data_pandas)data_pytorch)data_sklearnc                   @  s$   e Zd Zdd Zdd Zdd ZdS )ArtifactProxyc              	     s4   | j  i i tt  fddtjD d d S )Nc                   s   i | ]}|t  |qS  )getattr).0pflowr   W/home/ubuntu/.local/lib/python3.10/site-packages/wandb/integration/metaflow/metaflow.py
<dictcomp>2   s    z*ArtifactProxy.__init__.<locals>.<dictcomp>)r   inputsoutputsbaseparams)__dict__updatesetdirr	   parameter_names)selfr   r   r   r   __init__*   s   
zArtifactProxy.__init__c                 C  s   || j |< t| j||S N)r   setattrr   )r   keyvalr   r   r   __setattr__6   s   
zArtifactProxy.__setattr__c                 C  s2   || j vr|| jvrt| j|| j|< t| j|S r!   )r   r   r   r   r   )r   r#   r   r   r   __getattr__:   s   zArtifactProxy.__getattr__N)__name__
__module____qualname__r    r%   r&   r   r   r   r   r   )   s    r   Fnamestrdata,dict | list | set | str | int | float | booltestingboolreturn
str | Nonec                 C  s   |rdS | | |i d S )Nscalar)logr*   r,   runr.   r   r   r   _track_scalar@   s   r6   r   c                 C  sf   |rdS t j| dd}| r|| n	| r|| || t d|  dt| d d S )Nr   datasettypeLogging artifact:  ())	wandbArtifactis_diradd_diris_fileadd_filelog_artifacttermlogr9   )r*   r,   r5   r.   artifactr   r   r   _track_pathM   s   

rF   c                 C  s~   |rdS t j| dd}||  dd}t|| W d    n1 s%w   Y  || t d|  dt| d d S )	Ngenericotherr8   z.pklwbr:   r;   r<   )r=   r>   new_filepickledumprC   rD   r9   )r*   r,   r5   r.   rE   fr   r   r   _track_generic`   s   
rN   datasetsmodelsothersr5   wandb.Run | Nonec              	   C  s   t rt |r|rt | |||S tr"t|r"|r"t| |||S tr3t|r3|r3t| |||S t	|t
rA|rAt| |||S t	|tttttttfrTt| |||S |r]t| |||S dS )z6Track data as wandb artifacts based on type and flags.N)r
   is_dataframetrack_dataframer   is_nn_moduletrack_nn_moduler   is_estimatortrack_estimator
isinstancer   rF   dictlistr   r+   intfloatr/   r6   rN   r*   r,   rO   rP   rQ   r5   r.   r   r   r   wandb_trackq   s   r_   c              
   C  s   t |tttttttfrdS zOtr t	|r |r t
| ||W S tr1t|r1|r1t| ||W S trBt|rB|rBt| ||W S t |trQ|rQt| |||W S |r[t| |||W S W dS  tjyv   td|  dt| d Y dS w )z1Use wandb artifacts based on data type and flags.NzThis artifact (z, z) does not exist in the wandb datastore! If you created an instance inline (e.g. sklearn.ensemble.RandomForestClassifier), then you can safely ignore this. Otherwise you may want to check your internet connection!)rY   rZ   r[   r   r+   r\   r]   r/   r
   rS   use_dataframer   rU   use_nn_moduler   rW   use_estimatorr   	_use_path_use_genericr=   	CommErrortermwarnr9   r^   r   r   r   	wandb_use   s(   rg   c                 C  8   |rdS | |  d td|  dt| d d S )NrO   :latestUsing artifact: r;   r<   use_artifactr=   rD   r9   r4   r   r   r   rc      
   rc   c                 C  rh   )NrQ   ri   rj   r;   r<   rk   r4   r   r   r   rd      rm   rd   c                  G  s   t dd | D d S )Nc                 s  s    | ]	}|d ur|V  qd S r!   r   )r   ar   r   r   	<genexpr>   s    zcoalesce.<locals>.<genexpr>)next)argr   r   r   coalesce   s   rr   settingswandb.Settings | Nonec                  s0   t |  fdd| du rS | S )a  Automatically log parameters and artifacts to W&B.

    This decorator can be applied to a flow, step, or both:

    - Decorating a step enables or disables logging within that step
    - Decorating a flow is equivalent to decorating all steps
    - Decorating a step after decorating its flow overwrites the flow decoration

    Args:
        func: The step method or flow class to decorate.
        datasets: Whether to log `pd.DataFrame` and `pathlib.Path`
            types. Defaults to False.
        models: Whether to log `nn.Module` and `sklearn.base.BaseEstimator`
            types. Defaults to False.
        others: If `True`, log anything pickle-able. Defaults to False.
        settings: Custom settings to pass to `wandb.init`.
            If `run_group` is `None`, it is set to `{flow_name}/{run_id}`.
            If `run_job_type` is `None`, it is set to `{run_job_type}/{step_name}`.
    c                   s   t  r& }|jD ]}tt||r#t|ds#t||t|| q
|S t dr- S t d fdd
} |_d|_	|S )N
_base_funcrs   c          	   
     s*  t |tjjjst }|t|jtj	 dtj
 t|jtjd tj|da}tj|d}d|j_W d    n1 s@w   Y  t| }|j|j |g|R i | |j D ]\}}t|| |d q`|j D ]\}}t|| |d qtW d    d S 1 sw   Y  d S )N/)	run_grouprun_job_typerv   )r5   T)rO   rP   rQ   r5   )rY   r=   sdkwandb_settingsSettingsupdate_from_dictrr   rx   r	   	flow_namerun_idry   	step_nameinitwb_telemetrycontextfeaturemetaflowr   configr   r   r   itemsrg   r   r_   )	r   rs   argskwargsr5   telproxyr*   r,   )rO   funcrP   rQ   r   r   wrapper  sH   	
	"z-wandb_log.<locals>.decorator.<locals>.wrapper)rO   rP   rQ   rs   )
inspectisclassr   callabler   hasattrr"   r   ru   _kwargs)r   clsattrr   rO   	decoratorrP   rQ   rs   )r   r   r     s$   


(zwandb_log.<locals>.decoratorNr   )r   rO   rP   rQ   rs   r   r   r   	wandb_log   s
   Ar   )F)r*   r+   r,   r-   r.   r/   r0   r1   )r*   r+   r,   r   r.   r/   r0   r1   )r*   r+   r.   r/   r0   r1   )FFFNF)r*   r+   rO   r/   rP   r/   rQ   r/   r5   rR   r.   r/   r0   r1   )r*   r+   rO   r/   rP   r/   rQ   r/   r.   r/   r0   r1   )NFFFN)rO   r/   rP   r/   rQ   r/   rs   rt   )"
__future__r   r   rK   	functoolsr   pathlibr   r=   wandb.sdk.libr   r    r   r   r	   ImportErrore	Exceptionr
   MissingDependencyErrorwarnr   r   r   r6   rF   rN   r_   rg   rc   rd   rr   r   r   r   r   r   <module>   s    )3