o
    i+                     @   s  d Z ddl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mZmZ ddlmZ ddlmZ dd	lmZmZ d
d ZeedddjZeg djZejee dkedZee Z ej!ej"edZ#de#e dk < de#e dke dk @ < de#e dk< edddZ$edde$edddeddeddd gZ%dd e%D Z&ej'(de%dd Z)d d! Z*ej'(de&d"d# Z+ej'(de%d$d% Z,ej'(de%d&d' Z-ej'(de&d(d) Z.ej'(de%d*d+ Z/d,d- Z0ej'(de&d.d/ Z1ej'(de%d0d1 Z2ej'(de%d2d3 Z3d4d5 Z4ej'(d6dedie5d7fgd8d9 Z6ej'(de%d:d; Z7d<d= Z8d>d? Z9dS )@z+Testing for Gaussian process classification    N)approx_fprime)ConvergenceWarning)GaussianProcessClassifier)RBFCompoundKernelWhiteKernel)ConstantKernel)MiniSeqKernel)assert_almost_equalassert_array_equalc                 C   s
   t | S )N)npsin)x r   a/home/kim/smarthome/.venv/lib/python3.10/site-packages/sklearn/gaussian_process/tests/test_gpc.pyf   s   
r   
      )       @g      @g      @g      @g      @Zdtypegffffffֿ   gffffff?         ?fixedlength_scalelength_scale_boundsg?)r   )MbP?     @@g{Gz?      Y@c                 C   s   g | ]}|t kr|qS r   )fixed_kernel).0kernelr   r   r   
<listcomp>/   s    r$   r#   c                 C   s<   t | dtt}t|t|td d df dk d S )Nr#   r         ?)r   fitXyr   predictpredict_probar#   gpcr   r   r   test_predict_consistent2   s   *r.   c                  C   s\   g d} t g d}tdd}t|d| |}t|| || d d df dk d S )NAZABBTFTr   Zbaseline_similarity_boundsr%   r   r&   )r   arrayr	   r   r'   r   r*   r+   r(   r)   r#   r-   r   r   r   "test_predict_consistent_structured9   s
   
*r6   c                 C   s4   t | dtt}||jj|| jksJ d S )Nr%   )r   r'   r(   r)   log_marginal_likelihoodkernel_thetar,   r   r   r   test_lml_improvingB   s   r:   c                 C   s0   t | dtt}t||jj| d d S )Nr%      )r   r'   r(   r)   r
   r7   r8   r9   r,   r   r   r   test_lml_precomputedK   s   r<   c                 C   sJ   t | dtt}tj|jjjtj	d}|j
|dd t|jj|d d S )Nr%   r   F)Zclone_kernelr;   )r   r'   r(   r)   r   Zonesr8   r9   shapeZfloat64r7   r
   )r#   r-   Zinput_thetar   r   r   test_lml_without_cloning_kernelT   s   r>   c                 C   sz   t | dtt}||jjd\}}tt	|dk |jj|jj
d d df kB |jj|jj
d d df kB s;J d S )Nr%   T-C6?r   r   )r   r'   r(   r)   r7   r8   r9   r   allabsbounds)r#   r-   lmllml_gradientr   r   r   test_converged_to_local_maximum^   s   rE   c                    sJ   t | dtt  | jd\}}t| j fddd}t||d d S )Nr%   Tc                    s     | dS )NF)r7   )r9   r-   r   r   <lambda>s   s    z#test_lml_gradient.<locals>.<lambda>g|=   )r   r'   r(   r)   r7   r9   r   r
   )r#   rC   rD   Zlml_gradient_approxr   rF   r   test_lml_gradientl   s   rI   c                 C   s   d\}}t j| }|||d d }t |jddt d| jdd dk}tddtd	g| d
g| d }t j }t	dD ]#}t
||| d||}	|	|	jj}
|
|t t jj ksdJ |
}qCd S )N)   r   r   r   )ZaxisrH   r   r   r   r   )r?   r    r      )r#   n_restarts_optimizerZrandom_state)r   randomRandomStateZrandnr   sumCr   infranger   r'   r7   r8   r9   ZfinfoZfloat32eps)global_random_seedZ	n_samplesZ
n_featuresrngr(   r)   r#   Zlast_lmlrL   gprC   r   r   r   test_random_startsy   s(   ,
rW   c                    sF    fdd}t | |d}|tt ||jj|| jks!J d S )Nc           	         s   t j }|| |dd}}tdD ].}t |t d|d d df t d|d d df }| |dd}||k rA||}}q||fS )NF)Zeval_gradientr   r   r   )r   rM   rN   rR   Z
atleast_1duniformmaximumminimum)	Zobj_funcZinitial_thetarB   rU   Z	theta_optZfunc_min_r9   r   rT   r   r   	optimizer   s   
2
z(test_custom_optimizer.<locals>.optimizer)r#   r^   )r   r'   r(   y_mcr7   r8   r9   )r#   rT   r^   r-   r   r]   r   test_custom_optimizer   s   
r`   c                 C   sP   t | d}|tt |t}t|dd |t}t	t
|d| d S )Nr%   r   )r   r'   r(   r_   r+   X2r
   rO   r*   r   r   Zargmax)r#   r-   y_probZy_predr   r   r   test_multi_class   s   


rc   c                 C   sP   t | d}|tt t | dd}|tt |t}|t}t|| d S )Nr%   r   )r#   Zn_jobs)r   r'   r(   r_   r+   ra   r
   )r#   r-   Zgpc_2rb   Zy_prob_2r   r   r   test_multi_class_n_jobs   s   


rd   c            	      C   s   t ddgd} t| d}d}tjt|d |tt W d    n1 s&w   Y  tddgdt dd	gd }t|d}t	j
d
dG}t	d |tt t|dksXJ t|d jtsbJ |d jjd dksnJ t|d jtsxJ |d jjd dksJ W d    n1 sw   Y  ttd}t ddgddgd}t|d}t	j
d
dH}t	d ||t t|dksJ t|d jtsJ |d jjd dksJ t|d jtsJ |d jjd dksJ W d    d S 1 sw   Y  d S )Ngh㈵>r   )r   r%   zThe optimal value found for dimension 0 of parameter length_scale is close to the specified upper bound 0.001. Increasing the bound and calling fit again may find a better value.match)Znoise_level_boundsr   g     j@T)recordalwaysr   r   zThe optimal value found for dimension 0 of parameter k1__noise_level is close to the specified upper bound 0.001. Increasing the bound and calling fit again may find a better value.r   zThe optimal value found for dimension 0 of parameter k2__length_scale is close to the specified lower bound 1000.0. Decreasing the bound and calling fit again may find a better value.r   r   g      $@r    r   zThe optimal value found for dimension 0 of parameter length_scale is close to the specified upper bound 100.0. Increasing the bound and calling fit again may find a better value.zThe optimal value found for dimension 1 of parameter length_scale is close to the specified upper bound 100.0. Increasing the bound and calling fit again may find a better value.)r   r   pytestZwarnsr   r'   r(   r)   r   warningscatch_warningssimplefilterlen
issubclasscategorymessageargsr   Ztile)	r#   r-   Zwarning_messageZ
kernel_sumZgpc_sumrg   ZX_tileZkernel_dimsZgpc_dimsr   r   r   test_warning_bounds   s@   




"rr   zparams, error_type, err_msgz!kernel cannot be a CompoundKernelc                 C   sN   t di | }tj||d |tt W d   dS 1 s w   Y  dS )z0Check that expected error are raised during fit.re   Nr   )r   ri   raisesr'   r(   r)   )paramsZ
error_typeerr_msgr-   r   r   r   test_gpc_fit_error  s   "rv   c                 C   sT   t | d}|tt |t\}}|jtjd fksJ |jtjd fks(J dS )>Checks that the latent mean and variance have the right shape.r%   r   N)r   r'   r(   r)   latent_mean_and_variancer=   )r#   r-   Zlatent_meanZlatent_variancer   r   r   'test_gpc_latent_mean_and_variance_shape   s
   
ry   c                  C   sV   t t d} | tt tjtdd | t W d   dS 1 s$w   Y  dS )rw   r%   zdReturning the mean and variance of the latent function f is only supported for binary classificationre   N)	r   r   r'   r(   r_   ri   rs   
ValueErrorrx   rF   r   r   r   Atest_gpc_latent_mean_and_variance_complain_on_more_than_2_classes,  s   "r{   c                  C   s@   g d} t g d}tdd}t|d| |}||  d S )Nr/   r2   r   r3   r%   )r   r4   r	   r   r'   rx   r5   r   r   r   9test_latent_mean_and_variance_works_on_structured_kernels:  s
   
r|   ):__doc__rj   numpyr   ri   Zscipy.optimizer   Zsklearn.exceptionsr   Zsklearn.gaussian_processr   Z sklearn.gaussian_process.kernelsr   r   r   r   rP   Z4sklearn.gaussian_process.tests._mini_sequence_kernelr	   Zsklearn.utils._testingr
   r   r   Z
atleast_2dZlinspaceTr(   ra   r4   Zravelintr)   ZfXemptyr=   r_   r!   ZkernelsZnon_fixed_kernelsmarkZparametrizer.   r6   r:   r<   r>   rE   rI   rW   r`   rc   rd   rr   rz   rv   ry   r{   r|   r   r   r   r   <module>   sz    

	


	




I



