o
    -wip-                     @   s  d 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 zddlmZ W n ey? Z zededZ[ww zdd	lmZ W n eyY Z zed
edZ[ww z&ddlZe			d(dedejfddZe			d(dedejfddZW n ey   e	d Y nw z,ddlZddlmZ e			d(dedejfddZe			d(dedejfddZW n ey   e	d Y nw z&ddlm Z  e			d(dede fddZe			d(dede fddZW n ey   e	d Y nw G dd dZ!e		d)dedee"e#e$ee%e&e'f fddZe	d(dedefddZe	d(defddZedefdd Z(ededee"e#e$ee%e&e'f fd!d Z(e	d(dedefd"dZed(defd#dZd$d% Z)					d*d&d'Z*dS )+a  W&B Integration for Metaflow.

This integration lets users apply decorators to Metaflow flows and steps to automatically log parameters and artifacts to W&B by type dispatch.

- Decorating a step will enable or disable logging for certain types within that step
- Decorating the flow is equivalent to decorating all steps with a default
- Decorating a step after decorating the flow will overwrite the flow decoration

Examples can be found at wandb/wandb/functional_tests/metaflow
    Nwraps)Path)Union)	telemetry)currentzrError: `metaflow` not installed >> This integration requires metaflow!  To fix, please `pip install -Uqq metaflow`)dispatchzError: `plum-dispatch` not installed >> This integration requires plum-dispatch! To fix, please `pip install -Uqq plum-dispatch`Fnamedatac                 O   H   |r|rdS d S |r"| |  d td|  dt| d d S d S Ndatasets:latestUsing artifact:  ()use_artifactwandbtermlogtyper	   r
   r   runtestingargskwargs r   `/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/integration/metaflow/metaflow.py
_wandb_use(      
 r   c           	      O   s   |r|rdS d S |rFt j| dd}||  dd}|j|dd W d    n1 s,w   Y  || t d|  d	t| d
 d S d S )Nzpd.DataFramedatasetr   z.parquetwbpyarrow)engineLogging artifact: r   r   )r   Artifactnew_file
to_parquetlog_artifactr   r   )	r	   r
   r   r   r   r   r   artifactfr   r   r   wandb_track9   s   

 r,   zR`pandas` not installed >> @wandb_log(datasets=True) may not auto log your dataset!c                 O   r   Nmodelsr   r   r   r   r   r	   r
   r.   r   r   r   r   r   r   r   r   V   r   c           	      O      |r|rdS d S |rEt j| dd}||  dd}t|| W d    n1 s+w   Y  || t d|  dt| d d S d S )	Nz	nn.Modulemodelr!   .pklr"   r%   r   r   )r   r&   r'   torchsaver)   r   r   	r	   r
   r.   r   r   r   r   r*   r+   r   r   r   r,   g      

 zO`pytorch` not installed >> @wandb_log(models=True) may not auto log your model!)BaseEstimatorc                 O   r   r-   r   r/   r   r   r   r      r   c           	      O   r0   )	Nr7   r1   r!   r2   r"   r%   r   r   r   r&   r'   pickledumpr)   r   r   r5   r   r   r   r,      r6   zO`sklearn` not installed >> @wandb_log(models=True) may not auto log your model!c                   @   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 r   )getattr).0pflowr   r   
<dictcomp>   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)rC   setattrr@   )rK   keyvalr   r   r   __setattr__   s   
zArtifactProxy.__setattr__c                 C   s2   || j vr|| jvrt| j|| j|< t| j|S rM   )rD   rC   r<   r@   rB   )rK   rO   r   r   r   __getattr__   s   zArtifactProxy.__getattr__N)__name__
__module____qualname__rL   rQ   rR   r   r   r   r   r;      s    r;   c                 O   s   |rdS | | |i d S )Nscalar)log)r	   r
   r   r   r   r   r   r   r   r,      s   	c                 O   sv   |r|rdS d S |r9t j| dd}| r|| n	| r$|| || t d|  dt| d d S d S )Nr   r    r!   r%   r   r   )	r   r&   is_diradd_diris_fileadd_filer)   r   r   )r	   r
   r   r   r   r   r   r*   r   r   r   r,      s   

 c           	      O   r0   )	Ngenericotherr!   r2   r"   r%   r   r   r8   )	r	   r
   othersr   r   r   r   r*   r+   r   r   r   r,      s   
 c              
   O   sN   zt | |g|R i |W S  tjy&   td|  dt| d Y d S w )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 thisOtherwise you may want to check your internet connection!)r   r   	CommErrortermwarnr   r	   r
   r   r   r   r   r   	wandb_use   s   
rb   c                 O   s   d S rM   r   ra   r   r   r   rb     s   c                 O   r   r   r   r   r   r   r   r     s    c                 O   r   )Nr^   r   r   r   r   r   )r	   r
   r^   r   r   r   r   r   r   r   r     s    c                  G   s   t dd | D d S )Nc                 s   s    | ]	}|d ur|V  qd S rM   r   )r=   ar   r   r   	<genexpr>  s    zcoalesce.<locals>.<genexpr>)next)argr   r   r   coalesce  s   rg   c                    s0   t |  fdd| du rS | S )a  Automatically log parameters and artifacts to W&B by type dispatch.

    This decorator can be applied to a flow, step, or both.
    - Decorating a step will enable or disable logging for certain types within that step
    - Decorating the flow is equivalent to decorating all steps with a default
    - Decorating a step after decorating the flow will overwrite the flow decoration

    Args:
        func: (`Callable`). The method or class being decorated (if decorating a step or flow respectively).
        datasets: (`bool`). If `True`, log datasets.  Datasets can be a `pd.DataFrame` or `pathlib.Path`.  The default value is `False`, so datasets are not logged.
        models: (`bool`). If `True`, log models.  Models can be a `nn.Module` or `sklearn.base.BaseEstimator`.  The default value is `False`, so models are not logged.
        others: (`bool`). If `True`, log anything pickle-able.  The default value is `False`, so files are not logged.
        settings: (`wandb.sdk.wandb_settings.Settings`). Custom settings passed to `wandb.init`.  The default value is `None`, and is the same as passing `wandb.Settings()`.  If `settings.run_group` is `None`, it will be set to `{flow_name}/{run_id}.  If `settings.run_job_type` is `None`, it will be 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settingsc          	   
      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_typeri   )r   T)r   r.   r^   r   )
isinstancer   sdkwandb_settingsSettingsupdate_from_dictrg   rl   r   	flow_namerun_idrm   	step_nameinitwb_telemetrycontextfeaturemetaflowr;   configrG   rE   rB   itemsrb   rC   r,   )	rK   rj   r   r   r   telproxyr	   r
   )r   funcr.   r^   r   r   wrapperH  sH   	
	"z-wandb_log.<locals>.decorator.<locals>.wrapper)r   r.   r^   rj   )
inspectisclassrF   callabler<   hasattrrN   r   rh   _kwargs)r   clsattrr   r   	decoratorr.   r^   rj   )r   r   r   9  s&   



(zwandb_log.<locals>.decoratorNr   )r   r   r.   r^   rj   r   r   r   	wandb_log"  s
   Br   )FNF)NF)NFFFN)+__doc__r   r9   	functoolsr   pathlibr   typingr   r   wandb.sdk.libr   rw   rz   r   ImportErrore	Exceptionplumr   pandaspdstr	DataFramer   r,   r`   r3   torch.nnnnModulesklearn.baser7   r;   dictlistrH   intfloatboolrb   rg   r   r   r   r   r   <module>   s*   	