ó
‡ˆ\c           @   s¼  d  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
 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 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 m Z d e e f d „  ƒ  YZ d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d „  Z& d „  Z' d „  Z( d „  Z) d  „  Z* d! „  Z+ d S("   s)   Testing for Spectral Biclustering methodsiÿÿÿÿN(   t
   csr_matrixt   issparse(   t   ParameterGrid(   t   assert_equal(   t   assert_almost_equal(   t   assert_array_equal(   t   assert_array_almost_equal(   t   assert_raises(   t   SkipTest(   t   BaseEstimatort   BiclusterMixin(   t   SpectralCoclustering(   t   SpectralBiclustering(   t   _scale_normalize(   t   _bistochastic_normalize(   t   _log_normalize(   t   consensus_scoret   v_measure_score(   t   make_biclusterst   make_checkerboardt   MockBiclusteringc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   d  S(   N(    (   t   self(    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   __init__   s    c         C   s?   t  j t t t t t g ƒ d t  j t t t t g ƒ d f S(   Ni    (   t   npt   wheret   Truet   False(   R   t   i(    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   get_indices!   s    (   t   __name__t
   __module__R   R   (    (    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyR      s   	c          C   sà   t  j d ƒ j d d ƒ }  t ƒ  } xµ |  t |  ƒ |  j ƒ  f D]˜ } | j d | ƒ } t | ƒ rs | j ƒ  } n  t	 | d d g d d g d	 d
 g g ƒ d | (t | ƒ r½ | j ƒ  } n  t  j
 | d k ƒ s@ t ‚ q@ Wd  S(   Ni   i   i   i    i   i   i   i   i   i   iÿÿÿÿ(   R   t   aranget   reshapeR   R    t   tolistt   get_submatrixR   t   toarrayR   t   allt   AssertionError(   t   datat   modelt   Xt	   submatrix(    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_get_submatrix'   s    	"	c         C   sn   xg t  |  j ƒ D]V } |  j | ƒ \ } } |  j | ƒ \ } } t t | ƒ | ƒ t t | ƒ | ƒ q Wd  S(   N(   t   ranget
   n_clusterst	   get_shapeR   R   t   len(   R'   R   t   mt   nt   i_indt   j_ind(    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   _test_shape_indices8   s
    c          C   sƒ  i d d g d 6d  d g d 6t t g d 6d g d 6d	 g d
 6d g d 6}  d } t d d d d d | ƒ\ } } } | | j ƒ  8} t j | d k  d | ƒ } xÜ | t | ƒ f D]È } x¿ t |  ƒ D]± } t	 d d d | |  } | j
 | ƒ t | j j d ƒ t | j j d d ƒ t j d ƒ ƒ t | j j d d ƒ t j d ƒ ƒ t t | j | | f ƒ d ƒ t | ƒ qÆ Wq³ Wd  S(   Nt
   randomizedt   arpackt
   svd_methodi   t
   n_svd_vecst
   mini_batchs	   k-means++t   initi
   t   n_initi   t   n_jobsi    i   i   t   noiseg      à?t   random_stateR,   t   axis(   i   i   (   i   i   (   t   NoneR   R   R   t   minR   R   R    R   R   t   fitR   t   rows_t   shapeR   t   sumt   onest   columns_R   t   biclusters_R3   (   t
   param_gridR=   t   St   rowst   colst   matt   kwargsR'   (    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_spectral_coclusteringA   s.    

	%%c    	      C   sÍ  t  d d d d d d ƒ\ }  } } i d d g d	 6d
 g d 6d g d 6t g d 6} xq|  t |  ƒ f D]]} xT| j ƒ  D]F\ } } x7| D]/} t d d d d d d d d ƒ } | j t | | f g ƒ   t | ƒ r| j ƒ  j	 d	 ƒ d k rt
 t | j | ƒ qŽ n | j | ƒ t | j j d ƒ t | j j d ƒ t | j j d d ƒ t j d d ƒ ƒ t | j j d d ƒ t j d d ƒ ƒ t t | j | | f ƒ d ƒ t | ƒ qŽ Wq{ Wqh Wd  S(   Ni   i   R<   g      à?R=   i    t   scalet   logt   methodR5   R6   i   R7   R8   R,   R:   R9   s	   k-means++i	   R>   i   (   i   i   (   i	   i   (   i	   i   (   R   R   R    t   itemsR   t
   set_paramst   dictR   t
   get_paramst   getR   t
   ValueErrorRA   R   RB   RC   RF   R   RD   R   t   repeatR   RG   R3   (	   RI   RJ   RK   t   non_default_paramsRL   t
   param_namet   param_valuest   param_valueR'   (    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_spectral_biclustering^   s8    

	'c         C   s«   |  j  d d ƒ } |  j  d d ƒ } t |  ƒ r] t j | ƒ j ƒ  } t j | ƒ j ƒ  } n  t | t j | j ƒ  d ƒ d d ƒt | t j | j ƒ  d ƒ d d ƒd S(   s<   Check that rows sum to one constant, and columns to another.R>   i   i    id   t   decimalN(   RD   R   R   t   asarrayt   squeezeR   t   tilet   mean(   t   scaledt   row_sumt   col_sum(    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   _do_scale_test‡   s    c         C   sE   t  |  ƒ t |  j d d ƒ j ƒ  |  j d d ƒ j ƒ  d d ƒd S(   s5   Check that rows and columns sum to the same constant.R>   i    i   R^   N(   Rf   R   RD   Rb   (   Rc   (    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   _do_bistochastic_test”   s    
c          C   s…   t  j j d ƒ }  |  j d d ƒ } xZ | t | ƒ f D]F } t | ƒ \ } } } t | ƒ t | ƒ r7 t | ƒ s} t ‚ q7 q7 Wd  S(   Ni    id   (	   R   t   randomt   RandomStatet   randR    R   Rf   R   R%   (   t	   generatorR(   RL   Rc   t   _(    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_scale_normalizeœ   s    
c          C   s|   t  j j d ƒ }  |  j d d ƒ } xQ | t | ƒ f D]= } t | ƒ } t | ƒ t | ƒ r7 t | ƒ st t ‚ q7 q7 Wd  S(   Ni    id   (	   R   Rh   Ri   Rj   R    R   Rg   R   R%   (   Rk   R(   RL   Rc   (    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_bistochastic_normalize¦   s    
c          C   sB   t  j j d ƒ }  |  j d d ƒ } t | ƒ d } t | ƒ d  S(   Ni    id   i   (   R   Rh   Ri   Rj   R   Rg   (   Rk   RL   Rc   (    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_log_normalize°   s    c       	   C   s   t  d d ƒ }  t j d d d d d d g d d d d d d g d d d d d d g g ƒ } |  j | d d d	 d ƒ} t | | d  ƒ d  S(
   NR=   i    i   i   i   i   i   t   n_bestR,   (   R   R   t   arrayt   _fit_best_piecewiseR   (   R'   t   vectorst   best(    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_fit_best_piecewise¹   s    c          C   sÓ   t  d d ƒ }  t j d d d g d d d g d d d g d d d g g ƒ } t j d d g d d g d d g g ƒ } xT | t | ƒ f D]@ } |  j | | d d ƒ} t t | d d d d g ƒ d ƒ q‹ Wd  S(	   NR=   i    i   i   i   R,   i   g      ð?(   R   R   Rq   R    t   _project_and_clusterR   R   (   R'   R&   Rs   RL   t   labels(    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_project_and_clusterÂ   s    		c          C   s  t  d ƒ ‚ t d d d d d ƒ}  t d d d d d d ƒ\ } } } |  j | ƒ t t |  j | | f ƒ d	 ƒ t d d d d d d ƒ\ } } } |  j | ƒ t t |  j | | f ƒ d	 ƒ t d d d d d d ƒ\ } } } |  j | ƒ t t |  j | | f ƒ d	 ƒ d  S(   Ns{   This test is failing on the buildbot, but cannot reproduce. Temporarily disabling it until it can be reproduced and  fixed.i   R6   R5   R=   i    i   R<   i   i(   (   i   i   (   i(   i   (   i   i(   (   R   R   R   RA   R   R   RG   (   R'   RI   RJ   RK   (    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_perfect_checkerboardÑ   s"    c          C   sf  t  j d ƒ j d ƒ }  t d d ƒ } t t | j |  ƒ t d d ƒ } t t | j |  ƒ t d d ƒ } t t | j |  ƒ t d d ƒ } t t | j |  ƒ t d d ƒ } t t | j |  ƒ t d	 d
 ƒ } t t | j |  ƒ t d d
 ƒ } t t | j |  ƒ t d	 d d d ƒ } t t | j |  ƒ t ƒ  } t  j d ƒ j d ƒ }  t t | j |  ƒ d  S(   Ni   i   R,   i   t   abcRQ   t   unknownR6   t   n_componentsi    Rp   i   i   (   i   i   (   i   i   i   (   i   Rz   (   i   i   i   (   R   R   R    R   R   RW   RA   (   R&   R'   (    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   test_errorsê   s(    	(,   t   __doc__t   numpyR   t   scipy.sparseR    R   t   sklearn.model_selectionR   t   sklearn.utils.testingR   R   R   R   R   R   t   sklearn.baseR	   R
   t   sklearn.cluster.biclusterR   R   R   R   R   t   sklearn.metricsR   R   t   sklearn.datasetsR   R   R   R*   R3   RN   R]   Rf   Rg   Rm   Rn   Ro   Ru   Rx   Ry   R}   (    (    (    sC   lib/python2.7/site-packages/sklearn/cluster/tests/test_bicluster.pyt   <module>   s>   					)			
	
						