
    Q-j3                       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 erd dlmZ d dlmZ d dlmZ d dlmZ  e	       5 Zd dlZddd        ej6                         rd dlmZ d dlmZ d dlmZ d dlmZ dZ  ed      	 d#ddd	 	 	 	 	 	 	 	 	 d$d       Z!d%dZ"d&dZ# G d d      Z$	 	 	 	 	 	 d'dZ%	 	 	 	 	 	 d(dZ&d)dZ'	 	 	 	 	 	 	 	 d*d Z(	 	 	 	 	 	 	 	 	 	 	 	 d+d!Z)d,d"Z*y# 1 sw Y   xY w)-    )annotations)TYPE_CHECKINGN)experimental_func)
try_import)	_AxisInfo)_ContourInfo)_get_contour_info)_PlotValues)_SubContourInfo)_imports)Callable)Sequence)Study)FrozenTrial)Axes)Colormap)
ContourSet)pltd   z2.2.0zObjective Value)targettarget_namec               \    t        j                          t        | |||      }t        |      S )a  Plot the parameter relationship as contour plot in a study with Matplotlib.

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

    .. seealso::
        Please refer to :func:`optuna.visualization.plot_contour` for an example.

    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:`matplotlib.axes.Axes` object or a :class:`numpy.ndarray` of
        :class:`matplotlib.axes.Axes` objects.

    .. note::
        The colormap is reversed when the ``target`` argument isn't :obj:`None` or ``direction``
        of :class:`~optuna.study.Study` is ``minimize``.
    )r   checkr	   _get_contour_plot)studyparamsr   r   infos        v/Users/jameslopez/projects/TradingBot25/.venv/lib/python3.12/site-packages/optuna/visualization/matplotlib/_contour.pyplot_contourr   &   s*    L NNUFFK@DT""    c                   | j                   }| j                  }| j                  }| j                  }t	        |      dk  rt        j                         \  }}|S t	        |      }t
        j                  j                  d       |dk(  rzt        j                         \  }}	|	j                  d       t        |      }
t        |d   d   |	|
      }t        |t              r"|j                  |      }|j                  |       |	S t        j                  ||      \  }}	t        |	t         j"                        sJ |j%                  d       t        |      }
g }t'        t	        |            D ]W  }t'        t	        |            D ]>  }|	||f   }t        ||   |   ||
      }t        |t              s.|j)                  |       @ Y |r'|j                  |d   |	      }|j                  |       |	S )N   ggplot   zContour Plotr   )ax)sorted_paramssub_plot_infosreverse_scaler   lenr   subplotsstyleuse	set_title	_set_cmap_generate_contour_subplot
isinstancer   colorbar	set_labelnpndarraysuptitlerangeappend)r   r&   r'   r(   r   _r%   n_paramsfigaxscmapcsaxcbcs_listx_iy_is                   r   r   r   Q   s   &&M((N&&M""K
=Q2	=!HIIMM(1}<<>Sn%'&~a'8';S$Gb*%<<#DNN;'( J# <<(3S#rzz***^$' ]+,CS/0c].~c/B3/GTRb*-NN2&	 1 - <<
s<3DNN;'Jr    c                8    | sdnd}t        j                  |      S )NBlues_rBlues)r   get_cmap)r(   r<   s     r   r.   r.   }   s    )9wD<<r    c                  4    e Zd ZddZddZd	dZd
dZddZy)_LabelEncoderc                    g | _         y Nlabelsselfs    r   __init__z_LabelEncoder.__init__   s	    !#r    c                8    t        t        |            | _        | S rI   )sortedsetrK   )rM   rK   s     r   fitz_LabelEncoder.fit   s    S[)r    c                ^    |D cg c]  }| j                   j                  |       c}S c c}w rI   )rK   index)rM   rK   labels      r   	transformz_LabelEncoder.transform   s)    6<=fU!!%(f===s   "*c                    | j                   S rI   rJ   rL   s    r   
get_labelsz_LabelEncoder.get_labels   s    {{r    c                P    t        t        t        | j                                    S rI   )listr6   r)   rK   rL   s    r   get_indicesz_LabelEncoder.get_indices   s    E#dkk*+,,r    N)returnNone)rK   	list[str]r\   z'_LabelEncoder')rK   r^   r\   	list[int])r\   r^   )r\   r_   )__name__
__module____qualname__rN   rR   rV   rX   r[    r    r   rG   rG      s    $>-r    rG   c                    g }g }t        | j                  |j                        D ]-  \  }}|	||j                  |       |j                  |       / ||fS rI   )zipvaluesr7   )xaxisyaxisx_valuesy_valuesx_valuey_values         r   _filter_missing_valuesrm      s\     HHell;7#6OOG$OOG$ < Xr    c                   g }g }| j                   rt               }|j                  t        t	        t
        t        d | j                                           |j                  t        t	        t
        |                  }|j                         }|j                         }nt        t	        d |            }| j                  r\t        j                  t        j                  | j                  d         t        j                  | j                  d         t               }n5t        j"                  | j                  d   | j                  d   t               }|||t        |      fS )Nc                
    | d uS rI   rc   )values    r   <lambda>z&_calculate_axis_data.<locals>.<lambda>   s	    53Dr    c                    t        |       S rI   )float)xs    r   rq   z&_calculate_axis_data.<locals>.<lambda>   s    U1Xr    r   r"   )is_catrG   rR   rZ   mapstrfilterrf   rV   rX   r[   is_logr3   logspacelog10r6   CONTOUR_POINT_NUMlinspace)axisrf   cat_param_labelscat_param_posencreturned_valuescis          r   _calculate_axis_datar      s    
 #%!M{{oSf%DdkkRSTU --Sf-=(>?>>+)s#5v>? {{[[$**Q-0"((4::a=2IK\][[A

17HI_0EEEr    c                T   | j                   }| j                  }| j                  }g }g }g }t        |j                  |j                        D ]y  \  }}|	||j                  |       |j                  |       |j                  j                  |      }	|j                  j                  |      }
|j                  ||	|
f          { t        |      dk(  st        |      dk(  r,t        j                  g       t        g g       t        g g       fS t        ||      \  }}}}t        ||      \  }}}}t        j                  g       }|j                  |j                  k7  rt        |||||      }t        |t               }t        g g       }t        g g       }t        ||| j"                        D ]u  \  }}}|r7|j$                  j                  |       |j&                  j                  |       @|j$                  j                  |       |j&                  j                  |       w |||fS )Nr   )rg   rh   z_valuesre   rf   r7   indicesrT   r)   r3   arrayr
   r   name_create_zmap_interpolate_zmapr|   constraintsrt   y)r   rg   rh   z_values_dictri   rj   r   rk   rl   r@   rA   xir8   transformed_x_valuesyitransformed_y_valueszizmapfeasible
infeasiblecs                        r   _calculate_griddatar      s   JJEJJEMMMHHHell;7#6OOG$OOG$--%%g.C--%%g.COOM3*56 < 8}S]a/xx|[R0+b"2EEE%9&"B1" &:&"B1" XXb\B zzUZZ02FRTVXYt%67 2r"HR$J"#79MtO_O_`!JJg&JJg&LL(LL(  a x##r    c           	     D   |j                          t        | j                  j                        dk  s"t        | j                  j                        dk  ry |j                  | j                  j                  | j                  j                         |j                  | j                  j                  d   | j                  j                  d          |j                  | j                  j                  d   | j                  j                  d          t        | j                  | j                        \  }}t        | j                  |      \  }}}}t        | j                  |      \  }	}
}}| j                  j                  r#|j                  |       |j                  |       n)|j                  | j                  j                   rdnd       | j                  j                  r#|j#                  |       |j%                  |
       n)|j'                  | j                  j                   rdnd       | j                  j                  | j                  j                  k(  ry t)        |       \  }}}d }t        |      dkD  r|j+                  ||	|ddd	
       |j-                  ||	|d|j/                               }t1        |t2              sJ |j5                  |j6                  |j8                  ddddd       |j5                  |j6                  |j8                  ddddd       |S )Nr$   )xlabelylabelr   r"   loglinear   g      ?k)
linewidthscolors)r<   oblack   greyg       @)markerr   s
edgecolors	linewidthz#cccccc)label_outerr)   rg   r   rh   rQ   r   set_xlimr6   set_ylimrm   r   ru   
set_xticksset_xticklabels
set_xscalery   
set_yticksset_yticklabels
set_yscaler   contourcontourfreversedr0   r   scatterrt   r   )r   r%   r<   ri   rj   r   x_cat_param_labelx_cat_param_posr8   r   y_cat_param_labely_cat_param_posr   feasible_plot_valuesinfeasible_plot_valuesr=   s                   r   r/   r/      sz    NN
4::"c$***<*<&=&AFF$**//$**//F:KK

  #TZZ%5%5a%89KK

  #TZZ%5%5a%89/

DJJGHh0DTZZQY0Z-B?A0DTZZQY0Z-B?Azz
o&
,-
tzz00eh?zz
o&
,-
tzz00eh?zz$**//)7J47P4B4	B
2w{


2r2rc#
>[[RRdmmo[>"j)))


 "" "" 	 	
 	

"$$"$$ 	 	
 Ir    c           	     $   t               }t        | ||      D ]u  \  }}}t        t        j                  t        j
                  ||z
                    }	t        t        j                  t        j
                  ||z
                    }
|||	|
f<   w |S rI   )dictre   intr3   argminabs)ri   rj   r   r   r   r   rt   r   zxindexyindexs              r   r   r   ,  sx    " 6Dx841aRYYrvvb1f~./RYYrvvb1f~./!"ff 5
 Kr    c                   g }g }g }t        j                  |dz        }t        |      D ]  }t        |      D ]  }||z  |z   }||f| v r>|j                  d       |j                  |       |j                  |       | ||f   ||<   OdD ]  \  }	}
d||	z   cxk  r|k  sn d||
z   cxk  r|k  s'n *|j                  d       |j                  |       |j                  |       |j                  d       |j                  |       |j                  ||
|z  z   |	z             t        j
                  j                  j                  t        j
                  j                  |||ff      |      }|j                  ||f      S )Nr$   r"   ))r   )r"   r   )r   r   )r   r"   r   r   )
r3   zerosr6   r7   scipysparselinalgspsolve
csc_matrixreshape)r   contour_plot_numa_dataa_rowa_colbrt   r   
grid_indexdxdyr   s               r   r   r   F  sn   " FEE
!1$%A#$'(A--1J1v~a Z(Z( $aV*@FBAF5%55!q2v:XHX:Xa(Z0Z0b)Z0Z"7G2G%G"%LM A ) %$ 	##ELL$;$;VeU^<T$UWXYA99&(89::r    rI   )
r   r   r   zlist[str] | Noner   z%Callable[[FrozenTrial], float] | Noner   rw   r\   'Axes | np.ndarray')r   r   r\   r   )r(   boolr\   
'Colormap')rg   r   rh   r   r\   z+tuple[list[str | float], list[str | float]])r~   r   rf   zSequence[str | float]r\   z:tuple[np.ndarray, list[str], list[int], list[int | float]])r   r   r\   z+tuple[np.ndarray, _PlotValues, _PlotValues])r   r   r%   z'Axes'r<   r   r\   z'ContourSet' | None)ri   Sequence[int | float]rj   r   r   zSequence[float]r   
np.ndarrayr   r   r\   dict[tuple[int, int], float])r   r   r   r   r\   r   )+
__future__r   typingr   numpyr3   optuna._experimentalr   optuna._importsr   optuna.visualization._contourr   r   r	   r
   r   3optuna.visualization.matplotlib._matplotlib_importsr   collections.abcr   r   optuna.studyr   optuna.trialr   _optuna_importsr   is_successfulr   r   r   r   r|   r   r   r.   rG   rm   r   r   r/   r   r   rc   r    r   <module>r      s   "    2 & 3 6 ; 5 9 H (("( \_  8HLNG   7  $'# 59('#'#'# 2	'#
 '# '# '#T)X
- -$		&	0	F
F!F @F<1$h7
7%7-777t##  		
 	 "4);[	 \s   "DD