o
    mi;                     @   sD   d dl Z d dlZd dlmZ d dlmZ d dlZG dd deZdS )    N)
get_digest)RunObserverc                   @   sJ   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dS )WandbObserveru   Log sacred experiment data to W&B.

    Args:
        Accepts all the arguments accepted by wandb.init().

        name — A display name for this run, which shows up in the UI and is editable, doesn't have to be unique
        notes — A multiline string description associated with the run
        config — a dictionary-like object to set as initial config
        project — the name of the project to which this run will belong
        tags — a list of strings to associate with this run as tags
        dir — the path to a directory where artifacts will be written (default: ./wandb)
        entity — the team posting this run (default: your username or your default team)
        job_type — the type of job you are logging, e.g. eval, worker, ps (default: training)
        save_code — save the main python or notebook file to wandb to enable diffing (default: editable from your settings page)
        group — a string by which to group other runs; see Grouping
        reinit — whether to allow multiple calls to wandb.init in the same process (default: False)
        id — A unique ID for this run primarily used for Resuming. It must be globally unique, and if you delete a run you can't reuse the ID. Use the name field for a descriptive, useful name for the run. The ID cannot contain special characters.
        resume — if set to True, the run auto resumes; can also be a unique string for manual resuming; see Resuming (default: False)
        anonymous — can be "allow", "never", or "must". This enables or explicitly disables anonymous logging. (default: never)
        force — whether to force a user to be logged into wandb when running a script (default: False)
        magic — (bool, dict, or str, optional): magic configuration as bool, dict, json string, yaml filename. If set to True will attempt to auto-instrument your script. (default: None)
        sync_tensorboard — A boolean indicating whether or not copy all TensorBoard logs wandb; see Tensorboard (default: False)
        monitor_gym — A boolean indicating whether or not to log videos generated by OpenAI Gym; see Ray Tune (default: False)
        allow_val_change — whether to allow wandb.config values to change, by default we throw an exception if config values are overwritten. (default: False)

    Examples:
        Create sacred experiment::
        from wandb.sacred import WandbObserver
        ex.observers.append(WandbObserver(project='sacred_test',
                                                name='test1'))
        @ex.config
        def cfg():
            C = 1.0
            gamma = 0.7
        @ex.automain
        def run(C, gamma, _run):
            iris = datasets.load_iris()
            per = permutation(iris.target.size)
            iris.data = iris.data[per]
            iris.target = iris.target[per]
            clf = svm.SVC(C, 'rbf', gamma=gamma)
            clf.fit(iris.data[:90],
                    iris.target[:90])
            return clf.score(iris.data[90:],
                                iris.target[90:])
    c                 K   s   t jdi || _i | _d S )N )wandbinitrun	resources)selfkwargsr   r   _/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/integration/sacred/__init__.py__init__:   s   
zWandbObserver.__init__c                 C   s   |  | d S )N)_WandbObserver__update_config)r
   ex_infocommand	host_info
start_timeconfig	meta_info_idr   r   r   started_event>   s   zWandbObserver.started_eventc                 C   s   |rrt |ts
|f}t|D ]e\}}t |tst |tr)td| t|i qt |tr4t| qt |trPtj	d| ddd}|
| | j| qt |tjrdtd| t|i qtjdt| ddd qd S d S )	Nresult_z.pklresulttypez'logging results does not support type 'z' results. Ignoring this result   )
stacklevel)
isinstancetuple	enumeratefloatintr   logdictobjectArtifactadd_filer   log_artifactnumpyndarrayImagewarningswarnr   )r
   	stop_timer   irartifactr   r   r   completed_eventE   s*   



zWandbObserver.completed_eventNc                 C   s4   |d u rd}t j||d}|| | j| d S )Nfiler   )r   r%   r&   r   r'   )r
   namefilenamemetadatacontent_typer0   r   r   r   artifact_event]   s
   
zWandbObserver.artifact_eventc                 C   s$   || j vrt|}|| j |< dS dS )zTODO: Maintain resources list.N)r	   r   )r
   r4   md5r   r   r   resource_eventd   s   
zWandbObserver.resource_eventc                 C   sb   |  D ]*\}}t|d |d D ]\}}t|tjr&t|t|i qt||i qqd S )Nstepsvalues)itemszipr   r(   r)   r   r"   r*   )r
   metrics_by_nameinfometric_name
metric_ptr_stepvaluer   r   r   log_metricsj   s   zWandbObserver.log_metricsc                 C   s.   |  D ]
\}}|| jj|< qg | jjd< d S )Nr	   )r<   r   r   )r
   r   kvr   r   r   __update_configr   s   zWandbObserver.__update_config)NN)__name__
__module____qualname____doc__r   r   r1   r7   r9   rD   r   r   r   r   r   r   
   s    /
r   )r+   r(   sacred.dependenciesr   sacred.observersr   r   r   r   r   r   r   <module>   s    