o
    }oi:                     @  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
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 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*	dMdd!d"dNd-d.Z+dOd0d1Z,	!dPdQd5d6Z-dRd<d=Z.			!dSdTd>d?Z/dUdDdEZ0dVdHdIZ1dWdKdLZ2dS )X    )annotations)CallableN)Any)
NamedTuple)optuna_warn)
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&   Q/home/ubuntu/.local/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'   r2   =   s   
 r2   Objective Value)targetr1   studyr	   paramslist[str] | Noner7   %Callable[[FrozenTrial], float] | Noner1   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)r8   r9   r7   r1   infor&   r&   r'   plot_contourB   s   !rB   rA   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   rJ   c                 S  rK   r&   rL   rO   r&   r&   r'   rR      rS   T)rowscolsshared_xaxesshared_yaxes)rowcol)	showscaleF)r   rY   rZ   )rJ   rY   rZ   c                 S  rK   r&   rL   rO   r&   r&   r'   rR      rS   )r   rJ   rY   rZ   c                 S  rK   r&   rL   rO   r&   r&   r'   rR      rS   )rH   rY   rZ   )r   Layoutr.   r/   r0   r1   lenFigure_get_contour_subplotupdate_xaxesr)   r   update_yaxesr*   r   r   r   update_layout	enumerate	add_tracer   update)rA   rF   r.   r/   r0   r1   x_paramy_paramsub_plot_info	sub_plotsfigure	log_ranger[   x_iy_icontourscatterr)   r*   r&   r&   r'   r@   h   s   -

%r@   r0   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r@t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}|	rq|j| |j| qU|j| |j| qUt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 )NrG   r   z,Contour plot will not be displayed because `z` and `z$` cannot co-exist in `trial.params`.rD   rC   Theatmapnoneg?)
r4   r5   zcolorbar
colorscaleconnectgapscontours_coloring	hoverinfoline_smoothingreversescale)is_feasibleF)r)   r   r*   r]   r   r   r   r+   r   r   r2   zipr    r,   r4   appendr5   npfullnanarraylistkeysr   _create_scatter)rA   r0   r1   	x_indices	y_indicesfeasible
infeasiblex_valuey_valuecr+   xyszsrn   r&   r&   r'   r_      sR   

 $r_   r4   r3   r5   r{   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)r4   r5   markermoder   
showlegend)r   r   )r4   r5   r{   
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)r7   c                 S  s    h | ]}|j  D ]}|q	qS r&   )r9   r   )rP   tp_namer&   r&   r'   	<setcomp>  s     z$_get_contour_info.<locals>.<setcomp>r   z.Your study does not have any completed trials.rD   z,The length of params must be greater than 1.z*Parameter {} does not exist in your study.rG   )r.   r/   r0   r1   )r   r   
get_trialsr   COMPLETEr]   _loggerwarningsorted
ValueErrorformatset_get_contour_subplot_inforc   r}   r   r-   )r8   r9   r7   r1   trials
all_paramsr.   input_p_namerf   rg   rh   r/   ir0   r&   r&   r'   r?      sH   





r?   r   list[FrozenTrial]rf   rg   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+   rG   z,Param {} unique value length is less than 2.c                 S  s   g | ]}t |qS r&   )_satisfy_constraintsrP   r   r&   r&   r'   rR   g      z-_get_contour_subplot_info.<locals>.<listcomp>)r)   r*   r+   r,   )_get_axis_infor(   r]   r   r   r   r   rc   r9   r    indexvalueget	directionr
   MINIMIZEminmax)r8   r   rf   rg   r7   r)   r*   r+   r   trialr   r   rl   rm   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&   )rP   r4   r&   r&   r'   rR   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&   )r9   r   r   r   r&   r'   rR   s      z"_get_axis_info.<locals>.<listcomp>c                   s*   g | ]} |j v rt|j  nd qS N)r9   r   r   r   r   r&   r'   rR   u  s    c                 S     g | ]}|d ur|qS r   r&   rP   vr&   r&   r'   rR   y  r   c                 S  r   r   r&   r   r&   r&   r'   rR   z  r   
   TFrD   c                 S  r   r   r&   r   r&   r&   r'   rR     r   rG   )r   r   r   r   r   r    r   )r   r   r   r   floatrM   rN   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   )
r8   r	   r9   r:   r7   r;   r1   r   r<   r=   )rA   r-   r<   r=   )r6   )rA   r(   r0   r   r1   r   r<   rp   )r4   r3   r5   r3   r{   r   r<   r   )NNr6   )
r8   r	   r9   r:   r7   r;   r1   r   r<   r-   )r8   r	   r   r   rf   r   rg   r   r7   r;   r<   r(   )r   r   r<   r   )r   r   r   r   r<   r   )3
__future__r   collections.abcr   rM   typingr   r   numpyr~   optuna._warningsr   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-   r2   rB   r@   r_   r   r?   r   r   r   r&   r&   r&   r'   <module>   s^    	
&Q
8
3
9