o
    }oi                     @  s   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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 rVd dlmZ eeZdZG dd deZG dd deZdddd"ddZ		d#d$d d!ZdS )%    )annotations)Callable)Sequence)cast)
NamedTupleN)
get_logger)Study)FrozenTrial)
TrialState)_imports)_check_plot_args)_filter_nonfinite)god   c                   @     e Zd ZU ded< ded< dS )_EDFLineInfostr
study_name
np.ndarrayy_valuesN__name__
__module____qualname____annotations__ r   r   M/home/ubuntu/.local/lib/python3.10/site-packages/optuna/visualization/_edf.pyr         
 r   c                   @  r   )_EDFInfozlist[_EDFLineInfo]linesr   x_valuesNr   r   r   r   r   r   !   r   r   Objective Value)targettarget_namestudyStudy | Sequence[Study]r"   %Callable[[FrozenTrial], float] | Noner#   r   return'go.Figure'c          
   	   C  s   t   tjdd|iddid}t| ||}|j}t|dkr&tjg |dS g }|D ]\}}|tj	|j
||dd q*tj||d}	|	jdd	gd
 |	S )a  Plot the objective value EDF (empirical distribution function) of a study.

    Note that only the complete trials are considered when plotting the EDF.

    .. note::

        EDF is useful to analyze and improve search spaces.
        For instance, you can see a practical use case of EDF in the paper
        `Designing Network Design Spaces
        <https://doi.ieeecomputersociety.org/10.1109/CVPR42600.2020.01044>`__.

    .. note::

        The plotted EDF assumes that the value of the objective function is in
        accordance with the uniform distribution over the objective space.

    Args:
        study:
            A target :class:`~optuna.study.Study` object.
            You can pass multiple studies if you want to compare those EDFs.
        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 axis label.

    Returns:
        A :class:`plotly.graph_objects.Figure` object.
    z$Empirical Distribution Function PlottitlezCumulative Probability)r)   xaxisyaxisr   )datalayoutr   )xynamemode   )range)r   checkr   Layout_get_edf_infor   lenFigureappendScatterr    update_yaxes)
r$   r"   r#   r-   info	edf_linestracesr   r   figurer   r   r   plot_edf&   s    'r@   c                   sv  t | tr	| g}nt| }t| | t|dkr'td tg t	g dS  d u r2ddd	}| g }g }|D ]&} t
| jd
tjfd d}t	 fdd|D }|| || j q8tdd |D rvtd tg t	g dS tt|}	tt|}
t|	|
t}g }t||D ] \}}tj|d d tjf |kdd|j }|t||d qt||dS )Nr   zThere are no studies.)r   r    tr	   r'   floatc                 S  s   t t| jS )N)r   rB   value)rA   r   r   r   _targetw   s   z_get_edf_info.<locals>._targetF)deepcopystatesr"   c                   s   g | ]} |qS r   r   ).0trialrG   r   r   
<listcomp>   s    z!_get_edf_info.<locals>.<listcomp>c                 s  s    | ]	}t |d kV  qdS )r   N)r7   )rH   valuesr   r   r   	<genexpr>   s    z _get_edf_info.<locals>.<genexpr>zThere are no complete trials.)axis)r   r   )rA   r	   r'   rB   )
isinstancer   listr   r7   _loggerwarningr   nparrayr   
get_trialsr
   COMPLETEr9   r   allminconcatenatemaxlinspaceNUM_SAMPLES_X_AXISzipsumnewaxissizer   )r$   r"   r#   studiesrD   study_names
all_valuestrialsrK   min_x_valuemax_x_valuer    edf_line_info_listr   r   r   rG   r   r6   e   s<   




&r6   )r$   r%   r"   r&   r#   r   r'   r(   )Nr!   )r$   r%   r"   r&   r#   r   r'   r   )
__future__r   collections.abcr   r   typingr   r   numpyrR   optuna.loggingr   optuna.studyr   optuna.trialr	   r
   $optuna.visualization._plotly_importsr   optuna.visualization._utilsr   r   is_successfulr   r   rP   r[   r   r   r@   r6   r   r   r   r   <module>   s2    A