o
    /wi:                     @  s  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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 d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ e rd dlmZ d dlmZ d dlm Z  d dlm!Z! d dlm"Z" ee#Z$dZ%G dd deZ&G dd deZ'G dd deZ(G dd deZ)	dLdd d!dMd,d-Z*dNd/d0Z+	 dOdPd4d5Z,dQd;d<Z-			 dRdSd=d>Z.dTdCdDZ/dUdGdHZ0dVdJdKZ1dS )W    )annotations)CallableN)Any)
NamedTuple)
get_logger)_CONSTRAINTS_KEY)Study)StudyDirection)FrozenTrial)
TrialState)_imports)_check_plot_args)_filter_nonfinite)_is_log_scale)_is_numerical)_is_reverse_scale)Contour)go)make_subplots)Scatter)COLOR_SCALEg?c                   @  s>   e Zd ZU ded< ded< ded< ded< ded	< d
ed< dS )	_AxisInfostrnameztuple[float, float]rangeboolis_logis_catzlist[str | int | float]indiceszlist[str | float | None]valuesN__name__
__module____qualname____annotations__ r%   r%   Z/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/optuna/visualization/_contour.pyr   &   s   
 r   c                   @  s2   e Zd ZU ded< ded< ded< g Zded< dS )	_SubContourInfor   xaxisyaxiszdict[tuple[int, int], float]z_valuesz
list[bool]constraintsN)r!   r"   r#   r$   r+   r%   r%   r%   r&   r'   /   s
   
 r'   c                   @  s.   e Zd ZU ded< ded< ded< ded< d	S )
_ContourInfoz	list[str]sorted_paramszlist[list[_SubContourInfo]]sub_plot_infosr   reverse_scaler   target_nameNr    r%   r%   r%   r&   r,   6   s
   
 r,   c                   @  s   e Zd ZU ded< ded< dS )_PlotValues	list[Any]xyNr    r%   r%   r%   r&   r1   =   s   
 r1   Objective Value)targetr0   studyr   paramslist[str] | Noner6   %Callable[[FrozenTrial], float] | Noner0   r   return'go.Figure'c                C  s   t   t| |||}t|S )a  Plot the parameter relationship as contour plot in a study.

    Note that, if a parameter contains missing values, a trial with missing values is not plotted.

    Args:
        study:
            A :class:`~optuna.study.Study` object whose trials are plotted for their target values.
        params:
            Parameter list to visualize. The default is all parameters.
        target:
            A function to specify the value to display. If it is :obj:`None` and ``study`` is being
            used for single-objective optimization, the objective values are plotted.

            .. note::
                Specify this argument if ``study`` is being used for multi-objective optimization.
        target_name:
            Target's name to display on the color bar.

    Returns:
        A :class:`plotly.graph_objects.Figure` object.

    .. note::
        The colormap is reversed when the ``target`` argument isn't :obj:`None` or ``direction``
        of :class:`~optuna.study.Study` is ``minimize``.
    )r   check_get_contour_info_get_contour_plot)r7   r8   r6   r0   infor%   r%   r&   plot_contourB   s   !rA   r@   c                 C  s<  t jdd}| j}| j}| j}| j}t|dkrt jg |dS t|dkr|d }|d }|d d }t|||}	t j|	|d}
|
j	||j
jd |
j||jjd |j
jr\|
j	dd	 |jjrf|
jdd	 |j
jrzd
d |j
jD }|
j	|dd |jjrdd |jjD }|
j|dd |
S tt|t|ddd}
|
| d}t|D ]\}}t|D ]\}}||kr|
jt  |d |d d n4t|| | ||}	|	d }|	d }|j|d |rd}|
j||d |d d |
j||d |d d || | j
}|| | j}|
j	|j|d |d d |
j|j|d |d d |jr2|
j	d|d |d d |jrB|
jd|d |d d |jr[dd |jD }|
j	|d|d |d d |jrtdd |jD }|
j|d|d |d d |dkr|
j||d |d d |t|d kr|
j	||d |d d qq|
S )NzContour Plot)title   )datalayout   r   )
title_textr   category)typec                 S     g | ]}t |qS r%   mathlog10.0pr%   r%   r&   
<listcomp>       z%_get_contour_plot.<locals>.<listcomp>log)r   rI   c                 S  rJ   r%   rK   rN   r%   r%   r&   rQ      rR   T)rowscolsshared_xaxesshared_yaxes)rowcol)	showscaleF)r   rX   rY   )rI   rX   rY   c                 S  rJ   r%   rK   rN   r%   r%   r&   rQ      rR   )r   rI   rX   rY   c                 S  rJ   r%   rK   rN   r%   r%   r&   rQ      rR   )rG   rX   rY   )r   Layoutr-   r.   r/   r0   lenFigure_get_contour_subplotupdate_xaxesr(   r   update_yaxesr)   r   r   r   update_layout	enumerate	add_tracer   update)r@   rE   r-   r.   r/   r0   x_paramy_paramsub_plot_info	sub_plotsfigure	log_rangerZ   x_iy_icontourscatterr(   r)   r%   r%   r&   r?   h   s   -

%r?   r/   r   &tuple['Contour', 'Scatter', 'Scatter']c                 C  s  | j j}| jj}t|dk st|dk rt t t fS t| jdkrAt	d| j j
 d| jj
 d t t t fS tg g }tg g }t| j j| jj| jD ](\}}}	|d ur~|d ur~|	rr|j| |j| qV|j| |j| qVtt|t|ftj}
tt| j }tt| j }||
|d d df |d d df f< tj|||
d|itdd	d
d|d
}|t|j|jddt|j|jddfS )NrF   r   z,Contour plot will not be displayed because `z` and `z$` cannot co-exist in `trial.params`.rC   rB   Theatmapnoneg?)
r3   r4   zcolorbar
colorscaleconnectgapscontours_coloring	hoverinfoline_smoothingreversescale)is_feasibleF)r(   r   r)   r\   r   r   r   r*   warningswarnr   r1   zipr   r+   r3   appendr4   npfullnanarraylistkeysr   _create_scatter)r@   r/   r0   	x_indices	y_indicesfeasible
infeasiblex_valuey_valuecr*   xyszsrm   r%   r%   r&   r^      sR   

 $r^   r3   r2   r4   rz   r   c                 C  s>   d}|rdnd}|rdnd}t j| |d|d|dd	|d
dS )NGrayblackz#cccccczFeasible TrialzInfeasible Trialg       @)widthcolor)liner   markersF)r3   r4   markermoder   
showlegend)r   r   )r3   r4   rz   
edge_colormarker_colorr   r%   r%   r&   r      s   r   c                 C  s<  t | || t| jdtjfd|d}dd |D }t|dkr(td g }n*|d u r1t|}n!t|dkr<td	 |D ]}||vrKt	d

|q>tt|}t|dkrm|d }|d }	t| |||	|}
|
gg}n$g }t|D ]\}}	|g  |D ]}t| |||	|}
|| |
 q~qst| |}t||||dS )NF)deepcopystates)r6   c                 S  s    h | ]}|j  D ]}|q	qS r%   )r8   r   )rO   tp_namer%   r%   r&   	<setcomp>  s     z$_get_contour_info.<locals>.<setcomp>r   z.Your study does not have any completed trials.rC   z,The length of params must be greater than 1.z*Parameter {} does not exist in your study.rF   )r-   r.   r/   r0   )r   r   
get_trialsr   COMPLETEr\   _loggerwarningsorted
ValueErrorformatset_get_contour_subplot_inforb   r~   r   r,   )r7   r8   r6   r0   trials
all_paramsr-   input_p_namere   rf   rg   r.   ir/   r%   r%   r&   r>      sH   





r>   r   list[FrozenTrial]re   rf   c                 C  s  t ||}t ||}||krt||i dS t|jdk r+td| t||i dS t|jdk rAtd| t||i dS i }t|D ]m\}}	||	jvsU||	jvrVqG|j	| }
|j	| }|
d usfJ |d uslJ |j
|
}|j
|}|d u r|	j}n||	}|d usJ |||f}|d u s|d ur||||f< qG| jtju rt||nt|||||f< qGt|||dd |D dS )N)r(   r)   r*   rF   z,Param {} unique value length is less than 2.c                 S  s   g | ]}t |qS r%   )_satisfy_constraintsrO   r   r%   r%   r&   rQ   g      z-_get_contour_subplot_info.<locals>.<listcomp>)r(   r)   r*   r+   )_get_axis_infor'   r\   r   r   r   r   rb   r8   r   indexvalueget	directionr	   MINIMIZEminmax)r7   r   re   rf   r6   r(   r)   r*   r   trialr   r   rk   rl   r   existingr%   r%   r&   r   2  sJ   




r   r   r
   c                 C  s&   | j t}|d u ptdd |D S )Nc                 S  s   g | ]}|d kqS )g        r%   )rO   r3   r%   r%   r&   rQ   m  r   z(_satisfy_constraints.<locals>.<listcomp>)system_attrsr   r   all)r   r+   r%   r%   r&   r   k  s   r   
param_namec                   s  t |  r fdd| D }n	 fdd| D }tdd |D }tdd |D }t|  r^t|}t|}t|t| t }tdt|| }tdt|| }d}d}nAt |  r~t|}t|}|| t }|| }|| }d}d}n!t	|}t
|d	 }	d |v r|	d	8 }	|	t }| }|	| }d}d}tt	d
d |D }
t
|
dk rt ||f|||
|dS t |  r|
d| |
| t ||f|||
|dS )Nc                   s   g | ]}|j  qS r%   )r8   r   r   r   r%   r&   rQ   s      z"_get_axis_info.<locals>.<listcomp>c                   s*   g | ]} |j v rt|j  nd qS N)r8   r   r   r   r   r%   r&   rQ   u  s    c                 S     g | ]}|d ur|qS r   r%   rO   vr%   r%   r&   rQ   y  r   c                 S  r   r   r%   r   r%   r%   r&   rQ   z  r   
   TFrC   c                 S  r   r   r%   r   r%   r%   r&   rQ     r   rF   )r   r   r   r   r   r   r   )r   r   r   r   floatrL   rM   PADDING_RATIOpowr   r\   r   r   insertr~   )r   r   r   	min_value	max_valuepaddingr   r   unique_valuesspanr   r%   r   r&   r   p  sj   




	
r   r   )
r7   r   r8   r9   r6   r:   r0   r   r;   r<   )r@   r,   r;   r<   )r5   )r@   r'   r/   r   r0   r   r;   ro   )r3   r2   r4   r2   rz   r   r;   r   )NNr5   )
r7   r   r8   r9   r6   r:   r0   r   r;   r,   )r7   r   r   r   re   r   rf   r   r6   r:   r;   r'   )r   r
   r;   r   )r   r   r   r   r;   r   )2
__future__r   collections.abcr   rL   typingr   r   r{   numpyr   optuna.loggingr   optuna.samplers._baser   optuna.studyr   r	   optuna.trialr
   r   $optuna.visualization._plotly_importsr   optuna.visualization._utilsr   r   r   r   r   is_successfulr   r   r   r   r   r!   r   r   r   r'   r,   r1   rA   r?   r^   r   r>   r   r   r   r%   r%   r%   r&   <module>   s^    	
&Q
8
3
9