a
    hz.                     @   s  d Z ddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZ ddlmZmZ dd	lmZ d
Zeddgddgddggd Zeddedddd\ZZdd Zdd Zdd Zdd Zej de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j d.eejg d/d0 Z*d1d2 Z+ej ded3d4 Z,ej ded5d6 Z-d7d8 Z.dS )9z!
Testing for Clustering methods

    N)AffinityPropagationaffinity_propagation)#_equal_similarities_and_preferences)
make_blobs)ConvergenceWarningNotFittedError)euclidean_distances)assert_allcloseassert_array_equal)CSR_CONTAINERS      
   <      g?T)	n_samplesZ
n_featurescenterscluster_stdshufflerandom_statec                 C   sP   t tj|dddd }t|d }t||| d\}}t|}t|ksLJ dS )z.Test consistency of the affinity propagations.FcopyTZsquaredr   
preferencer   N)r   Xastypenpmedianr   len
n_clusters)global_random_seedglobal_dtypeSr   cluster_centers_indiceslabelsn_clusters_ r(   k/var/www/html/assistant/venv/lib/python3.9/site-packages/sklearn/cluster/tests/test_affinity_propagation.pytest_affinity_propagation#   s    
r*   c                  C   s   t tdd } t| d }t|ddd}|| j}t|ddd}|tj}t|| |j}t	|}t
|j|ks|J t|ksJ d	S )
z^Check equality of precomputed affinity matrix to internally computed affinity
    matrix.
    Tr   r   precomputed   r   affinityr   %   r   verboser   N)r   r   r   r   r   fitlabels_r
   Zcluster_centers_indices_r    uniquesizer!   )r$   r   afZlabels_precomputedr&   r%   r'   r(   r(   r)   %test_affinity_propagation_precomputed0   s    
r7   c                  C   s   t tdd } |  }t| d }t|  |r8J t| |ddd t| | t|  |rfJ t|  t	| j
d  t| |ddd t|  | | } t|ddd}|tj}t| |ddd\}}t|| d	S )
z.Check behaviour of not copying the input data.Tr   r   r   )r   r   r   Fr0   J   N)r   r   r   r   r   ZallcloseZdiagonalr   r	   zerosshaper   r2   r3   r
   )r$   Z
S_originalr   r6   r&   _Zlabels_no_copyr(   r(   r)   !test_affinity_propagation_no_copyG   s"    

r<   c                  C   s\   t tdd } d}tjt|d( t| ddddf  W d   n1 sN0    Y  dS )zHCheck the shape of the affinity matrix when using `affinity_propagation.Tr   z1The matrix of similarities must be a square arraymatchNr   )r   r   pytestraises
ValueErrorr   )r$   err_msgr(   r(   r)   (test_affinity_propagation_affinity_shapec   s    rC   csr_containerc                 C   sJ   d}t jt|d$ tdd| d W d    n1 s<0    Y  d S )Nz8Sparse data was passed for X, but dense data is requiredr=   r+   r.   )r   r   )r?   r@   	TypeErrorr   r2   )rD   rB   r(   r(   r)   7test_affinity_propagation_precomputed_with_sparse_inputk   s    rG   c                 C   s<   t d| d}tj|dd}||}||}t|| d S )N	euclideanr.   r   Fr   )r   r   r   fit_predictpredictr
   )r"   r#   r6   ZX_r&   Zlabels2r(   r(   r)   !test_affinity_propagation_predictr   s
    

rL   c                  C   s   t dd} tt | t W d    n1 s40    Y  tttj}t ddd} | 	| tjt
dd | t W d    n1 s0    Y  d S )NrH   rE   r+   9   rI   zexpecting 60 features as inputr=   )r   r?   r@   r   rK   r   r   dotTr2   rA   )r6   r$   r(   r(   r)   'test_affinity_propagation_predict_error{   s    
(
rP   c                 C   s   t jddgddgddgg| d}tdddd}tt || W d    n1 sX0    Y  tt d|j	 t
t g d	|j d S )
Nr   r   ZdtypeR   r   max_iterr   )r   r   r   r   r   )r   arrayr   r?   warnsr   r2   r	   emptycluster_centers_r
   r3   )r#   r   r6   r(   r(   r)   -test_affinity_propagation_fit_non_convergence   s     (r\   c                 C   sD  t jddgddgg| d}t|dd }tjtdd  t|dd	\}}W d    n1 s\0    Y  tddg| tddg| tjtdd  t|d
d	\}}W d    n1 s0    Y  tdg| tddg| t	 2 t
dt t|dd
gdd\}}W d    n1 s0    Y  tdg| tddg| d S )Nr   r   rR   Tr   zmutually equalr=   r   )r   rS   errorir/   r   )r   rX   r   r?   rY   UserWarningr   r
   warningscatch_warningssimplefilter)r#   r   r$   Zcluster_center_indicesr&   r(   r(   r)   3test_affinity_propagation_equal_mutual_similarities   s"    ..

*rb   c                 C   s   t jddgddgddgg| d}tt$ tdddd|}W d    n1 sT0    Y  t ddgd	d	gd
d
gg}tt ||}W d    n1 s0    Y  tt g d| d S )Nr   r   rQ   rR   rS   K   rU   r   r      rW   )	r   rX   r?   rY   r   r   r2   rK   r
   )r#   r   r6   Z
to_predictyr(   r(   r)   1test_affinity_propagation_predict_non_convergence   s     2(rf   c                 C   s   t jg dg dg dg| d}tdddd}d	}tjt|d
 || W d    n1 s`0    Y  tt g d|j d S )N)r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   rR   rH   r   "   )r.   rV   r   zcAffinity propagation did not converge, this model may return degenerate cluster centers and labels.r=   )r   r   r   )	r   rX   r   r?   rY   r   r2   r
   r3   )r#   r   r6   msgr(   r(   r)   8test_affinity_propagation_non_convergence_regressiontest   s    (ri   c                 C   s   t jddgddgddgg| d}t|dd }t|t drBJ t|t ddgrZJ t|t ddgrrJ t jddgddgg| d}t|dd }t|t ddgrJ t|t ddgsJ t|t dsJ d S )Nr   r   rQ   rR   Tr   )r   rX   r   r   )r#   r   r$   r(   r(   r)   'test_equal_similarities_and_preferences   s     rj   c                  C   s   ddgddgddgg} t d| ddd\}}tdddd}|| |j}tddd	d}|| |j}t|| d dksJ d
S )zCheck that different random states lead to different initialisations
    by looking at the center locations after two iterations.
    r   r   i,        ?r   )r   r   r   r   r   )Zconvergence_iterrV   r   L   N)r   r   r2   r[   r   mean)r   r   Zlabels_trueapZcenters0Z	centers76r(   r(   r)   &test_affinity_propagation_random_state   s    


ro   	containerc                 C   s   | t d}t jd}|ddj|dd}d|d t}tdd	}||| ||_	t
 < t
d
t t||t j|jd td W d   n1 s0    Y  dS )z
    Check that having sparse or dense `centers` format should not
    influence the convergence.
    Non-regression test for gh-13334.
    )r   r   *   (   r   Fr   rd   .   )r   r]   r   rR   N)r   r9   randomRandomStateZrandr   intr   r2   r[   r_   r`   ra   r   r
   rK   r:   )rp   r#   r   rngr   re   rn   r(   r(   r)   :test_affinity_propagation_convergence_warning_dense_sparse  s    

rx   c                 C   sX   t jg dg dg dg dg| d}tdddd|}t g d	}t|j| d S )
N)r   r   r   r   )r   r   r   r   )r   r   r   r   rR   r   r+   r   r-   )r   r   r   r   )r   rX   r   r2   r
   r3   )r#   r   Zafpexpectedr(   r(   r)   test_correct_clusters  s    rz   c                 C   s2   t ddd}|t || d}t|d d S )NrH   rq   rI   )r   r   )r   r2   r   rK   r
   )rD   r6   r&   r(   r(   r)   test_sparse_input_for_predict$  s    
r{   c                 C   sD   t ddd}tjd}| |jdddd}||}t|d d S )	NrH   rq   rI   r   r   )   r|   )r5   )r   r   r   r   r   )r   r   rt   ru   randintrJ   r
   )rD   r6   rw   r   r&   r(   r(   r)   !test_sparse_input_for_fit_predict.  s
    
r~   c                  C   s6   t d} tdddd| }t |jdks2J dS )zMake sure we do not assign multiple clusters to equal points.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/pull/20043
    )   r   rH   rk   rq   )r.   Zdampingr   r   N)r   r9   r   r2   allr3   )r   r6   r(   r(   r)   &test_affinity_propagation_equal_points9  s    
r   )/__doc__r_   numpyr   r?   Zsklearn.clusterr   r   Z%sklearn.cluster._affinity_propagationr   Zsklearn.datasetsr   Zsklearn.exceptionsr   r   Zsklearn.metricsr   Zsklearn.utils._testingr	   r
   Zsklearn.utils.fixesr   r!   rX   r   r   r;   r*   r7   r<   rC   markZparametrizerG   rL   rP   r\   rb   rf   ri   rj   ro   rx   rz   r{   r~   r   r(   r(   r(   r)   <module>   sT    

	 

	

