U
    3d                     @   sL  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 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ejdddg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-dd.gd/d0 Z$d1d2 Z%dS )3z,
Testing for mean shift clustering methods

    N)sparse)assert_array_equal)assert_allclose)	MeanShift)
mean_shift)estimate_bandwidth)get_bin_seeds)
make_blobs)v_measure_score      
   i,     皙?T   	n_samples
n_featurescenterscluster_stdshufflerandom_statec                  C   s*   t tdd} d|   kr dks&n td S )N   )r   g?g      ?)r   XAssertionError	bandwidth r   I/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/cluster/tests/test_mean_shift.pytest_estimate_bandwidth#   s    r    c                 C   sB   t tj| ddddd}|jtjks(t|tjdddks>td S )	NFcopyr   g333333?)r   Zquantile        gh㈵>)abs)r   r   astypedtyper   pytestapprox)global_dtyper   r   r   r   test_estimate_bandwidth_1sample)   s      r*   z5bandwidth, cluster_all, expected, first_cluster_label)333333?Tr   r   )r+   F   r   c                 C   s   t j| dd}t||d}||j}t|}t|}	|	|ksDt|d |ksTt|j	j
| ksdtt||d\}
}t|}t|}||kst|d |kst|
j
| kstd S )NFr!   )r   cluster_allr   )r-   )r   r%   r   fitlabels_npuniquelenr   cluster_centers_r&   r   )r)   r   r-   expectedZfirst_cluster_labelX_with_global_dtypemslabelsZlabels_uniqueZn_clusters_Zcluster_centersZlabels_mean_shiftZlabels_mean_shift_uniqueZn_clusters_mean_shiftr   r   r   test_mean_shift4   s"    
 

r8   c               	   C   s4   t d} d}tjt|d t|  W 5 Q R X d S )N)  r9   z7A sparse matrix was passed, but dense data is required.match)r   Z
lil_matrixr'   raises	TypeErrorr   )r   msgr   r   r   *test_estimate_bandwidth_with_sparse_matrixO   s    
r?   c                 C   s   t ddgddgddggd }tdd|dddd	\}}|j| d
d}tdd}|| t }|| t|j|j |jj|jjkst	t
|j|j d S )Nr   r   r   2   r   r   Tr   r   Fr!   )Zn_jobs)r0   arrayr	   r%   r   r.   r   r3   r&   r   r   r/   )r)   r   r   _Zms1Zms2r   r   r   test_parallelW   s"     
	


rC   c                 C   s:   t dd}tj| dd}||}||}t|| d S )Nr+   r   Fr!   )r   r   r%   Zfit_predictZpredictr   )r)   r6   r5   r7   Zlabels2r   r   r   test_meanshift_predicto   s
    


rD   c               	   C   sD   t dddgddggd} d}tjt|d | t W 5 Q R X d S )N皙?ii)r   Zseedsz!No point was within bandwidth=0.1r:   )r   r'   r<   
ValueErrorr.   r   )r6   r>   r   r   r   test_meanshift_all_orphansx   s    rG   c                  C   s&   t  } t| drtt| dr"td S )Nr3   r/   )r   hasattrr   )r6   r   r   r   test_unfitted   s    rI   c                 C   s   t jddgddgddgddgddgddgg| d	}tdd
|}t jddgddgddgddgddgddgg| d	}tdd
|}t|jddddddg t|jddddddg d S )Nr   r   r   r,      r         r&   r   )r0   rA   r   r.   r   r/   )r)   r   c1c2r   r   r   test_cluster_intensity_tie   s    22rP   c              	   C   s.  t jddgddgddgddgddgddgg| d	}d
ddh}t|dd}tdd |D }t||dksptd
dh}t|dd}tdd |D }t||dksttjdd t|dd}W 5 Q R X t	|| t
ddddgddggddd\}}|j| dd}t|d}t|ddgddgg d S )N      ?gffffff?g?r+          @g @g?r#   rM   )rQ   rQ   )rR   rQ   )r#   r#   r   c                 s   s   | ]}t |V  qd S Ntuple.0pr   r   r   	<genexpr>   s     z!test_bin_seeds.<locals>.<genexpr>r   r   c                 s   s   | ]}t |V  qd S rS   rT   rV   r   r   r   rY      s     T)recordg{Gz?d   rE   )r   r   r   r   r   Fr!   )r0   rA   r   setr2   symmetric_differencer   warningscatch_warningsr   r	   r%   r   )r)   r   Zground_truthZ	test_binsZtest_resultrB   r   r   r   test_bin_seeds   s2    &



r`   max_iterr[   c                 C   sr   t t| d\}}t| dt}|j}|j|jks6tt|t|ksJtt	||D ]\}}t
||sTtqTd S )N)ra   )r   r   r   r.   r3   Zn_iter_ra   r   r2   zipr0   Zallclose)ra   Z	clusters1rB   r6   Z	clusters2rN   rO   r   r   r   test_max_iter   s    rc   c              
   C   s   t jddddddddg| ddd}t|}|dks:tt||d|ksNttdd d	|}td
d|}t ddddddddg}t|j	|t
dkstt|j	|t
dkstt|j|j d S )Nr   r   r   rM   r   r   )Zbin_sizeT)bin_seedingr   F)rd   )r0   rA   Zreshaper   r   r   r   r.   r
   r/   r'   r(   r   r3   )r)   r   r   Z
ms_binningZms_nobinningZexpected_labelsr   r   r   test_mean_shift_zero_bandwidth   s    &re   )&__doc__Znumpyr0   r^   r'   Zscipyr   Zsklearn.utils._testingr   r   Zsklearn.clusterr   r   r   r   Zsklearn.datasetsr	   Zsklearn.metricsr
   Z
n_clustersrA   r   r   rB   r    r*   markZparametrizer8   r?   rC   rD   rG   rI   rP   r`   rc   re   r   r   r   r   <module>   sN    


	

*
