o
    mit.                     @   s  d 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
 zddlmZ W n ey9 Z zededZ[ww zddlmZ W n eyS 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%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%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)	telemetry)currentzrError: `metaflow` not installed >> This integration requires metaflow!  To fix, please `pip install -Uqq metaflow`)typedispatchzrError: `fastcore` not installed >> This integration requires fastcore!  To fix, please `pip install -Uqq fastcore`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   a/home/ubuntu/SoloSpeech/.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_track7   s   

 r+   z[Warning: `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   T   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+   e      

 zXWarning: `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   r/   )	Nr6   r0   r    r1   r!   r$   r   r   r   r%   r&   pickledumpr(   r   r   r4   r   r   r   r+      r5   zXWarning: `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)rB   setattrr?   )rJ   keyvalr   r   r   __setattr__   s   
zArtifactProxy.__setattr__c                 C   s2   || j vr|| jvrt| j|| j|< t| j|S rL   )rC   rB   r;   r?   rA   )rJ   rN   r   r   r   __getattr__   s   zArtifactProxy.__getattr__N)__name__
__module____qualname__rK   rP   rQ   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   r/   )	Ngenericotherr    r1   r!   r$   r   r   r7   )	r   r	   othersr   r   r   r   r)   r*   r   r   r   r+      s   
 c              	   O   sL   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printr   r   r	   r   r   r   r   r   	wandb_use   s   
ra   c                 O   s   d S rL   r   r`   r   r   r   ra      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 rL   r   )r<   ar   r   r   	<genexpr>  s    zcoalesce.<locals>.<genexpr>)next)argr   r   r   coalesce  s   rf   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          	   
      sF  t |tjjjst }|jt|jtj	 dtj
 tjjjjd |jt|jtjtjjjjd tj|da}tj|d}d|j_W d    n1 sNw   Y  t| }|j|j |g|R i | |j D ]\}}t|| |d qn|j D ]\}}t|| |d qW d    d S 1 sw   Y  d S )N/)	run_groupsource)run_job_typerl   rh   )r   T)r   r-   r]   r   )
isinstancer   sdkwandb_settingsSettingsrF   rf   rk   r   	flow_namerun_idSourceINITrm   	step_nameinitwb_telemetrycontextfeaturemetaflowr:   configrD   rA   itemsra   rB   r+   )	rJ   ri   r   r   r   telproxyr   r	   )r   funcr-   r]   r   r   wrapperF  sN   


	"z-wandb_log.<locals>.decorator.<locals>.wrapper)r   r-   r]   ri   )
inspectisclassrE   callabler;   hasattrrM   r   rg   _kwargs)r   clsattrr   r   	decoratorr-   r]   ri   )r   r   r   7  s&   



*zwandb_log.<locals>.decoratorNr   )r   r   r-   r]   ri   r   r   r   	wandb_log   s
   Dr   )FNF)NF)NFFFN))__doc__r   r8   	functoolsr   pathlibr   r   wandb.sdk.libr   rx   r{   r   ImportErrore	Exceptionfastcore.allr   pandaspdstr	DataFramer   r+   r_   r2   torch.nnnnModulesklearn.baser6   r:   dictlistrG   intfloatboolra   rf   r   r   r   r   r   <module>   s(   	