U
    3d(,                     @   sn  d Z ddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ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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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*d/e*d/gd0d1 Z+d2d3 Z,d4d5 Z-d6d7 Z.dS )8z!
Testing for Clustering methods

    N)
csr_matrix)ConvergenceWarningNotFittedError)assert_array_equalassert_allclose)AffinityPropagation)#_equal_similarities_and_preferences)affinity_propagation)
make_blobs)euclidean_distances      
   <      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sLtdS )z.Test consistency of the affinity propagations.FcopyTZsquaredr   
preferencer   N)	r   Xastypenpmedianr	   len
n_clustersAssertionError)global_random_seedglobal_dtypeSr   cluster_centers_indiceslabelsn_clusters_ r)   S/tmp/pip-unpacked-wheel-zrfo1fqw/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|tt|kst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%   r   afZlabels_precomputedr'   r&   r(   r)   r)   r*   %test_affinity_propagation_precomputed2   s      
r8   c                  C   s   t tdd } |  }t| d }t|  |r8tt| |ddd t	| | t|  |rft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   Fr1   J   N)r   r   r   r   r   ZallcloseZdiagonalr"   r	   r   zerosshaper   r3   r4   r   )r%   Z
S_originalr   r7   r'   _Zlabels_no_copyr)   r)   r*   !test_affinity_propagation_no_copyI   s(    
   
r=   c               	   C   sH   t tdd } d}tjt|d t| ddddf  W 5 Q R X 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_shapee   s    rD   c               	   C   s6   d} t jt| d tddtd W 5 Q R X d S )Nz6A sparse matrix was passed, but dense data is requiredr>   r,   r/   )r   r   )r@   rA   	TypeErrorr   r3   r   )rC   r)   r)   r*   7test_affinity_propagation_precomputed_with_sparse_inputm   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$   r7   ZX_r'   Zlabels2r)   r)   r*   !test_affinity_propagation_predicts   s
    

rL   c               	   C   sv   t dd} tt | t W 5 Q R X tttj}t ddd} | 	| tjt
dd | t W 5 Q R X d S )NrH   rE   r,   9   rI   zexpecting 60 features as inputr>   )r   r@   rA   r   rK   r   r   dotTr3   rB   )r7   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 5 Q R X tt d|j	 t
t d	d	d	g|j d S )
Nr   r   ZdtypeR   r   max_iterr   )r   r   r   )r   arrayr   r@   warnsr   r3   r   emptycluster_centers_r   r4   )r$   r   r7   r)   r)   r*   -test_affinity_propagation_fit_non_convergence   s     r[   c              	   C   s  t jddgddgg| d}t|dd }tjtdd t|dd	\}}W 5 Q R X tddg| tddg| tjtdd t|d
d	\}}W 5 Q R X tdg| tddg| t	 ( t
dt t|dd
gdd\}}W 5 Q R X tdg| tddg| d S )Nr   r   rR   Tr   zmutually equalr>   r   )r   rS   errorir0   r   )r   rW   r   r@   rX   UserWarningr	   r   warningscatch_warningssimplefilter)r$   r   r%   Zcluster_center_indicesr'   r)   r)   r*   3test_affinity_propagation_equal_mutual_similarities   s&    
  ra   c              	   C   s   t jddgddgddgg| d}tt tdddd|}W 5 Q R X t ddgd	d	gd
d
gg}tt ||}W 5 Q R X tt dddg| d S )Nr   r   rQ   rR   rS   K   rU   r   r      r   )	r   rW   r@   rX   r   r   r3   rK   r   )r$   r   r7   Z
to_predictyr)   r)   r*   1test_affinity_propagation_predict_non_convergence   s     re   c              	   C   s   t jddddddgddddddgddddddgg| d}tdddd}d}tjt|d	 || W 5 Q R X tt dddg|j d S )
Nr   r   rR   rH   r   "   )r/   rV   r   zcAffinity propagation did not converge, this model may return degenerate cluster centers and labels.r>   )	r   rW   r   r@   rX   r   r3   r   r4   )r$   r   r7   msgr)   r)   r*   8test_affinity_propagation_non_convergence_regressiontest   s    , rh   c                 C   s   t jddgddgddgg| d}t|dd }t|t drBtt|t ddgrZtt|t ddgrrtt jddgddgg| d}t|dd }t|t ddgrtt|t ddgstt|t dstd S )Nr   r   rQ   rR   Tr   )r   rW   r   r   r"   )r$   r   r%   r)   r)   r*   'test_equal_similarities_and_preferences   s     ri   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std
S )zCheck that different random states lead to different initialisations
    by looking at the center locations after two iterations.
    r   r   i,  g      ?r   )r   r   r   r   r   )Zconvergence_iterrV   r   L   N)r
   r   r3   rZ   r   Zmeanr"   )r   r   Zlabels_trueapZcenters0Z	centers76r)   r)   r*   &test_affinity_propagation_random_state   s       


rl   r   )r   r   c              	   C   s   t jd}|ddj|dd}d|d t}tdd}||| | |_t	
 2 t	d	t t||t j|jd
 td W 5 Q R X dS )z
    Check that having sparse or dense `centers` format should not
    influence the convergence.
    Non-regression test for gh-13334.
    *   (   r   Fr   rc   .   )r   r\   r   rR   N)r   randomRandomStateZrandr   intr   r3   rZ   r^   r_   r`   r   r   rK   r:   r;   )r   r$   rngr   rd   rk   r)   r)   r*   :test_affinity_propagation_convergence_warning_dense_sparse  s    

rt   c                 C   sl   t jddddgddddgddddgddddgg| d}tdddd|}t ddddg}t|j| d S )Nr   r   rR   r,   r.   r   )r   rW   r   r3   r   r4   )r$   r   Zafpexpectedr)   r)   r*   test_correct_clusters  s    * rv   c                  C   s2   t ddd} | t | td}t|d d S )NrH   rm   rI   )r   r   )r   r3   r   rK   r   r   )r7   r'   r)   r)   r*   test_sparse_input_for_predict$  s    
rw   c                  C   sD   t ddd} tjd}t|jdddd}| |}t|d d S )	NrH   rm   rI   r   r   )   rx   )r6   )r   r   r   r   r   )r   r   rp   rq   r   randintrJ   r   )r7   rs   r   r'   r)   r)   r*   !test_sparse_input_for_fit_predict-  s
    
rz   )/__doc__Znumpyr   r@   r^   Zscipy.sparser   Zsklearn.exceptionsr   r   Zsklearn.utils._testingr   r   Zsklearn.clusterr   Z%sklearn.cluster._affinity_propagationr   r	   Zsklearn.datasetsr
   Zsklearn.metricsr   r!   rW   r   r   r<   r+   r8   r=   rD   rG   rL   rP   r[   ra   re   rh   ri   rl   markZparametrizer:   rt   rv   rw   rz   r)   r)   r)   r*   <module>   sN    
	  
	