o
    ni                     @   st   d 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	 ddl
mZ ddlmZ d	ZG d
d deZdS )zPublic API: sweeps.    N)Optional)gql)util)public)Attrs)ipythonzfragment SweepFragment on Sweep {
    id
    name
    method
    state
    description
    displayName
    bestLoss
    config
    createdAt
    updatedAt
    runCount
}
c                       s   e Zd ZdZedZedZd' fdd	Zedd Z	ed	d
 Z
edd Zd(defddZedd Zd'ddZedee fddZedd Zedd Zedd Ze					d)ddZd*d!d"Zdefd#d$Zd%d& Z  ZS )+Sweepa  A set of runs associated with a sweep.

    Examples:
        Instantiate with:
        ```
        api = wandb.Api()
        sweep = api.sweep(path / to / sweep)
        ```

    Attributes:
        runs: (`Runs`) list of runs
        id: (str) sweep id
        project: (str) name of project
        config: (str) dictionary of sweep configuration
        state: (str) the state of the sweep
        expected_run_count: (int) number of expected runs for the sweep
    aT  
    query Sweep($project: String, $entity: String, $name: String!) {
        project(name: $project, entityName: $entity) {
            sweep(sweepName: $name) {
                id
                name
                state
                runCountExpected
                bestLoss
                config
            }
        }
    }
    a3  
    query Sweep($project: String, $entity: String, $name: String!) {
        project(name: $project, entityName: $entity) {
            sweep(sweepName: $name) {
                id
                name
                state
                bestLoss
                config
            }
        }
    }
    Nc                    sD   t  t|pi  || _|| _|| _|| _g | _| j| d d S )N)force)	super__init__dictclient_entityprojectidrunsload)selfr   entityr   sweep_idattrs	__class__ V/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/apis/public/sweeps.pyr   R   s   zSweep.__init__c                 C   s   | j S N)r   r   r   r   r   r   ]   s   zSweep.entityc                 C   s   t d | jS )Nz>Sweep.username is deprecated. please use Sweep.entity instead.)wandbtermwarnr   r   r   r   r   usernamea   s   
zSweep.usernamec                 C   s   t | jd S )Nconfig)r   	load_yaml_attrsr   r   r   r   r    f   s   zSweep.configFr	   c                 C   sN   |s| j s$| | j| j| j| j}|d u rtd| |j | _ |j| _| j S )NzCould not find sweep {})	r"   getr   r   r   r   
ValueErrorformatr   )r   r	   sweepr   r   r   r   j   s   
z
Sweep.loadc                 C   s\   | j dr*| jdr,| jd dd}|dkrdnd}tj|| jd d  S d S d S )Nr    metricgoalminimize+-name)r"   r#   r    r   QueryGeneratorformat_order_key)r   
sort_orderprefixr   r   r   ordert   s   zSweep.orderc                 C   s   |du r| j }ntj|}|du rtd ntd| dd| jigi}ztj	| j
| j| j||ddd W S  tyC   Y dS w )	zRReturn the best run sorted by the metric defined in config or the order passed in.NzVNo order specified and couldn't find metric in sweep config, returning most recent runzSorting runs by {}$andr&      )r1   filtersper_pager   )r1   r   r-   r.   r   r   termlogr%   r   Runsr   r   r   
IndexError)r   r1   r4   r   r   r   best_run}   s.   zSweep.best_runreturnc                 C   s   | j dS )zJReturn the number of expected runs in the sweep or None for infinite runs.runCountExpected)r"   r#   r   r   r   r   expected_run_count   s   zSweep.expected_run_countc                 C   s4   t jt| jt jt| jt jt| jgS r   )urllibparse
quote_plusstrr   r   r   r   r   r   r   path   s   z
Sweep.pathc                 C   s$   | j }|dd | jjd| S )N   sweeps/)rA   insertr   app_urljoin)r   rA   r   r   r   url   s   z	Sweep.urlc                 C   s   | j dp| jS )Nr,   )r    r#   r   r   r   r   r   r,      s   z
Sweep.namec              	   K   s   |du r| j }|||d}|| d}	z	|j||d}	W n ty0   | j}|j||d}	Y nw |	r?|	dr?|	d dsAdS |	d d }
| |||||
d}tj||||ddd|jigid	|_	|S )
z*Execute a query against the cloud backend.N)r   r   r,   )variable_valuesr   r&   )r   
   r2   )r1   r5   r4   )
QUERYupdateexecute	ExceptionLEGACY_QUERYr#   r   r7   r   r   )clsr   r   r   sidr1   querykwargs	variablesresponsesweep_responser&   r   r   r   r#      s@   
	z	Sweep.get  c                 C   sF   | j d }d| d}d}|r|d7 }td}|d|d|d	 S )
z9Generate HTML containing an iframe displaying this sweep.z?jupyter=truezborder:none;width:100%;height:zpx; zdisplay:none;r&   z<iframe src=z style=z
></iframe>)rH   r   toggle_button)r   heighthiddenrH   styler0   r   r   r   to_html   s   

zSweep.to_htmlc                 C   s   |   S r   )r]   r   r   r   r   _repr_html_   s   zSweep._repr_html_c                 C   s   d d| j| jddS )Nz<Sweep {} ({})>rD   statezUnknown State)r%   rG   rA   r"   r#   r   r   r   r   __repr__   s   zSweep.__repr__r   )F)NNNNN)rW   F)__name__
__module____qualname____doc__r   rK   rO   r   propertyr   r   r    boolr   r1   r9   r   intr<   rA   rH   r,   classmethodr#   r]   r@   r^   r`   __classcell__r   r   r   r   r      sJ    









2
r   )rd   r=   typingr   	wandb_gqlr   r   r   
wandb.apisr   wandb.apis.attrsr   wandb.sdk.libr   SWEEP_FRAGMENTr   r   r   r   r   <module>   s    