o
    tBhN                     @   sl  d dl Z d dlZd dl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mZ d dlmZ g dZddgZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z dd  Z!eed!d"d# Z"d$d% Z#eed!d&d' Z$eed!d(d) Z%ej&'d*ej&(d+g d,d-d. Z)d/d0 Z*d1d2 Z+dS )3    N)gammaln)assert_almost_equal)assert_array_equal)adjusted_rand_score)_log_dirichlet_norm)_log_wishart_norm)BayesianGaussianMixture)
RandomData)ConvergenceWarningNotFittedError)ignore_warningsfulltieddiag	sphericaldirichlet_processdirichlet_distributionc                  C   sH   t jd} | d}tt |t t| }t|}t|| d S )Nr      )nprandomRandomStaterandr   sumr   r   )rngweight_concentrationexpected_normpredected_norm r   /var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/mixture/tests/test_bayesian_mixture.pytest_log_dirichlet_norm   s   
r    c            
      C   s   t jd} d\}}t | |d }|t tdd|  }t d}tt	||D ]-\}\}}||d| t d   t 
td|t d|d d t jf   d  ||< q,t|||}	t||	 d S )Nr   )   r         ?r   r!   g      ?g       @)r   r   r   absr   lograngeempty	enumeratezipr   r   arangenewaxisr   r   )
r   n_components
n_featuresdegrees_of_freedomlog_det_precisions_cholr   kdegrees_of_freedom_k	log_det_kr   r   r   r   test_log_wishart_norm*   s.   


r2   c                  C      t jd} d\}}| ||}d}t|| d}td| d}tjt	|d |
| W d    d S 1 s9w   Y  d S )Nr   
   r   bad_covariance_type)covariance_typerandom_statez%Invalid value for 'covariance_type': zE 'covariance_type' should be in ['spherical', 'tied', 'diag', 'full']matchr   r   r   r   r   reescapepytestraises
ValueErrorfit)r   	n_samplesr,   Xr7   bgmmmsgr   r   r   %test_bayesian_mixture_covariance_typeF   s   
"rF   c                  C   r3   )Nr   r4   bad_prior_type)weight_concentration_prior_typer8   z5Invalid value for 'weight_concentration_prior_type': z_ 'weight_concentration_prior_type' should be in ['dirichlet_process', 'dirichlet_distribution']r9   r;   )r   rB   r,   rC   rG   rD   rE   r   r   r   5test_bayesian_mixture_weight_concentration_prior_typeV   s   "rI   c            	      C   s   t jd} d\}}}| ||}d}t|dd}d|dd}tjt|d || W d    n1 s7w   Y  |  }t|| d|}t	||j
 t|| d	|}t	d
| |j
 d S )Nr   )r5   r!   r           )weight_concentration_priorr8   zNThe parameter 'weight_concentration_prior' should be greater than 0., but got .3f.r9   r+   r8   r"   )r   r   r   r   r   r>   r?   r@   rA   r   weight_concentration_prior_)	r   rB   r+   r,   rC   bad_weight_concentration_prior_rD   rE   rK   r   r   r   2test_bayesian_mixture_weights_prior_initialisationh   s.   
rQ   c            
      C   sf  t jd} d\}}}| ||}d}t|| d}d|dd}tjt|d || W d    n1 s7w   Y  |  }t|| d|}t	||j
 t| d	|}t	d
|j
 | |d }	t||	| d}d}tjt|d || W d    n1 sw   Y  | |}	t||	| d|}t	|	|j t|| d|}t	|jdd|j d S )Nr   )r5      r   rJ   )mean_precision_priorr8   zHThe parameter 'mean_precision_prior' should be greater than 0., but got rL   rM   r9   r8   r"      )r+   
mean_priorr8   z/The parameter 'means' should have the shape of rN   axis)r   r   r   r   r   r>   r?   r@   rA   r   mean_precision_prior_mean_prior_mean)
r   rB   r+   r,   rC   bad_mean_precision_prior_rD   rE   rS   rV   r   r   r   /test_bayesian_mixture_mean_prior_initialisation   sP   

r]   c               
   C   s8  t jd} d\}}| ||}|d }t|| d}d|d  d|dd	}tjt|d
 || W d    n1 s=w   Y  |  | d }t|| d|}t	||j
 |}t|| d|}t	||j
 t j|jddd t j|jddd t t t j|jddd |  d}	t| d}dD ]}
|
|_|	|
 |_|| t	|	|
 |j qd}td|| d}d|dd	}tjt|d
 || W d    n1 sw   Y  t t |jt t |jt j|dddt j|ddd d}tdd}dD ]}
|
|_|| t	||
 |j qd S )Nr   r4   r"   )degrees_of_freedom_priorr8   z@The parameter 'degrees_of_freedom_prior' should be greater than rU   z
, but got rL   rM   r9   )biasr5   r!   rR   r   rT   g      r   )r7   covariance_priorr8   zNThe parameter 'spherical covariance_prior' should be greater than 0., but got )rX   ddof)r   r   r   r   r   r>   r?   r@   rA   r   degrees_of_freedom_prior_covTr   
atleast_2dr7   r`   covariance_prior_varr[   )r   rB   r,   rC   bad_degrees_of_freedom_prior_rD   rE   r^    degrees_of_freedom_prior_defaultr`   cov_typebad_covariance_prior_covariance_prior_defaultr   r   r   5test_bayesian_mixture_precisions_prior_initialisation   s   




rm   c                  C   sl   t jd} d\}}t| d}| ||}d}tjt|d || W d    d S 1 s/w   Y  d S )Nr   r4   rT   z8This BayesianGaussianMixture instance is not fitted yet.r9   )	r   r   r   r   r   r>   r?   r@   score)r   rB   r,   rD   rC   rE   r   r   r   %test_bayesian_mixture_check_is_fitted  s   
"ro   c            	   	   C   s   t jd} d\}}| ||}tdd| d|}|jt |j }t||j	 tt |j	d tdd| d|}|jd |jd  }|jd | }|jd | t 
dt |d d	 f }|t | }t||j	 tt |j	d d S )
Nr   r4   r   rR   )rH   r+   r8   r"   r   rU   )r   r   r   r   r   rA   weight_concentration_r   r   weights_hstackcumprod)	r   rB   r,   rC   rD   expected_weightsdpgmmweight_dirichlet_sumtmpr   r   r   test_bayesian_mixture_weights  s@   ry   )categoryc            
      C   s   t jd} t| dd}|j}tD ]<}tD ]7}|j| }t|d| |dd| dd}t j	 }t
d	D ]}|}	||j}||	ksAJ |jrF nq1|jsLJ qqd S )
Nr      scaler   TrU   MbP?)rH   r+   r7   
warm_startmax_iterr8   toliX  )r   r   r   r	   r+   
PRIOR_TYPECOVARIANCE_TYPErC   r   inftyr%   rA   lower_bound_
converged_)
r   	rand_datar+   
prior_type
covar_typerC   rD   current_lower_bound_prev_lower_boundr   r   r   test_monotonic_likelihood5  s4   
	r   c            
   	   C   s  t jd} t| dd}|jd }|j}tD ]}t|d| ddddd}|| |	|t jd |j
|jd d t jt jf  }t|d| d	dddd}|| |	|t jd |j
|j }t|t |d t|d| d
dddd}|| |	|t jd |j
|jd d t jf  }t|t dd |D  t|d| ddddd}|| |	|t jd |j
|j }	t|	t |d qd S )Nr      r|   r   r   rU   Hz>)rH   r+   r7   r   r8   r   r   r   c                 S   s   g | ]}t |qS r   )r   r   ).0rc   r   r   r   
<listcomp>  s    z+test_compare_covar_type.<locals>.<listcomp>r   )r   r   r   r	   rC   r+   r   r   _check_initial_parameters_initialize_parameterscovariances_degrees_of_freedom_r*   r   r[   array)
r   r   rC   r+   r   rD   full_covariancestied_covariancediag_covariancesspherical_covariancesr   r   r   test_compare_covar_typeV  sp   




r   c                  C   s   t jd} t| dd}d|j d}}t|d| ddd}tD ]\}||_||j	|  |dkrIt
|j|jD ]\}}tt ||t | q7q|d	kr\tt |j|jt | q|d
krot|j|j t ||f qt|j|j t | qd S )Nr   r   r|   r   d   r~   r+   r   r8   r   	reg_covarr   r   r   )r   r   r   r	   r+   r   r   r7   rA   rC   r(   r   precisions_r   doteyeones)r   r   r+   r,   rD   r   covar	precisionr   r   r   test_check_covariance_precision  s4   

r   c               
   C   s   t jd} t| dd}d|j }tD ]?}tD ]:}|j| }t||ddddd	|}t||ddddd	|d }t
|j|jd  t
|j|j t
|j|j qqd S )Nr   r   r|   r   r~   )rH   r+   r   r8   r   r   )r   r   r   r	   r+   r   r   rC   r   rA   r   means_rr   r   )r   r   r+   r   r   rC   bgmm1bgmm2r   r   r   test_invariant_translation  s>   


	r   zignore:.*did not converge.*zseed, max_iter, tol))r   r   r   )rU   r   皙?)rR   ,  r   )   r   r   c                 C   s   t j| }t|ddd}d|j }tD ]*}t||||dd}||_t	|}|j
| }	||	|	}
||	}t|
| qd S )N2   r   )rB   r}   r   r   r   )r   r   r   r	   r+   r   r   r7   copydeepcopyrC   rA   predictfit_predictr   )seedr   r   r   r   r+   r   r   r   rC   Y_pred1Y_pred2r   r   r   !test_bayesian_mixture_fit_predict  s$   



r   c                  C   sD   t jddd} tdddd}|| }|| }t|| d S )Nr   r   r!   r5   )r+   n_initr8   )r   r   r   randnr   r   r   r   )rC   gmy_pred1y_pred2r   r   r   (test_bayesian_mixture_fit_predict_n_init  s
   

r   c            
   
   C   s   t jd} t| }tD ]W}tD ]R}|j| }|j}t|j	| ||d}d}t
jt|d || W d    n1 s<w   Y  || ||}||jdd}	t||	 t||dksbJ qqd S )Nr   )r+   r8   rH   r7   z{This BayesianGaussianMixture instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.r9   rU   rW   gffffff?)r   r   r   r	   r   r   rC   Yr   r+   r>   r?   r   r   rA   predict_probaargmaxr   r   )
r   r   r   r   rC   r   rD   rE   Y_predY_pred_probar   r   r   +test_bayesian_mixture_predict_predict_proba  s0   
	


r   ),r   r<   numpyr   scipy.specialr   r>   sklearn.utils._testingr   r   sklearn.metrics.clusterr   !sklearn.mixture._bayesian_mixturer   r   sklearn.mixturer   +sklearn.mixture.tests.test_gaussian_mixturer	   sklearn.exceptionsr
   r   r   r   r   r    r2   rF   rI   rQ   r]   rm   ro   ry   r   r   r   r   markfilterwarningsparametrizer   r   r   r   r   r   r   <module>   sP   1M%
 H
#

 		