U
    3d_                     @   s  d dl Z d dlZ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mZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlm Z  d dl!m"Z" d	d
 Z#dd Z$dd Z%dd Z&dd Z'G dd dej(Z)dd Z*dd Z+dd Z,dd Z-dd Z.dd  Z/G d!d" d"Z0G d#d$ d$Z1G d%d& d&Z2G d'd( d(Z3ej45d)ej46d*e2e1 d+e3e1 d+gd,d- Z7G d.d/ d/Z8dSd1d2Z9d3d4 Z:ej46d5d6d7gd8d9 Z;ej46d:ej<ej=ej>ej?gd;d< Z@ej46d=d>eAfd?eBfd@ejCfdAejDfdBejDfdCejEfdDdEdF fdGdHdF fdIdJdF fdKeFfg
ej46dLej>ejGfej?ejGfej<ejHfej=ejHfgdMdN ZIdOdP ZJdQdR ZKdS )T    N)sparse)
deprecated)available_ifif_delegate_has_method)	_test_sum)assert_raisesassert_no_warningsset_random_stateassert_raise_messageignore_warningscheck_docstring_parametersassert_allclose_dense_sparseassert_raises_regex
TempMemmapcreate_memmap_backed_data_delete_folder_convert_containerraisesassert_allclose)DecisionTreeClassifier)LinearDiscriminantAnalysisc                  C   s2   t  } t }t| d t|d |jdks.td S )N   )r   r   r	   Zrandom_stateAssertionError)Zldatree r   D/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/utils/tests/test_testing.pytest_set_random_state$   s
    

r   c               
   C   s   t ddd} d}t| }| |fD ]6}tjt|d t||d  W 5 Q R X t|| q(tjt	dd t| | W 5 Q R X tj
t ddd	 }tt d
}tjtdd t|| W 5 Q R X d S )N	   r   zNot equal to tolerance match   zCan only compare two sparse   r   )offsets)   r!   zArrays are not equal)nparangeZreshaper   
csc_matrixpytestr   r   r   
ValueErrorZdiagsonesZtocsr
csr_matrix)xmsgyXABr   r   r   !test_assert_allclose_dense_sparse-   s    
r1   c                
   C   s2   t td ttdd W 5 Q R X W 5 Q R X d S )NzHello world)r,   )r   r   r   r(   r   r   r   r   test_assert_raises_msg@   s    r2   c                  C   sj   dd } dd }t td| d ttt td| d ttt td| d ttt td| ttt ttfd| d S )Nc                 S   s   t | d S N)r(   )messager   r   r   _raise_ValueErrorG   s    z4test_assert_raise_message.<locals>._raise_ValueErrorc                   S   s   d S r3   r   r   r   r   r   	_no_raiseJ   s    z,test_assert_raise_message.<locals>._no_raisetestzsomething else)r
   r(   r   r   	TypeErrorAttributeError)r5   r6   r   r   r   test_assert_raise_messageF   s6    		r:   c               	      s  dd dd  t t t ttd tt ttd  W 5 Q R X tt t td  W 5 Q R X tt t td  W 5 Q R X t tttfd t fdd} tttfd fdd	}ttdfd
d}ttdfdd}ttd fdd}ttd fdd}t |  t | t | tt |  W 5 Q R X tt |  W 5 Q R X tt |  W 5 Q R X fdd} fdd}fdd}fdd}	 fdd}
 fdd}t | t | t | tt |	  W 5 Q R X tt |
  W 5 Q R X tt |  W 5 Q R X t}d}tjt|d t|}|  W 5 Q R X tjt|d t|d d! }W 5 Q R X d S )"Nc                   S   s   t dt d S Nzdeprecation warningwarningswarnDeprecationWarningr   r   r   r   _warning_functionp   s    z.test_ignore_warning.<locals>._warning_functionc                   S   s   t dt t d d S r;   r<   r   r   r   r   _multiple_warning_functions   s    z7test_ignore_warning.<locals>._multiple_warning_functioncategoryc                      s        d S r3   r   r   rA   r@   r   r   decorator_no_warning   s    z1test_ignore_warning.<locals>.decorator_no_warningc                      s
      d S r3   r   r   rA   r   r   decorator_no_warning_multiple   s    z:test_ignore_warning.<locals>.decorator_no_warning_multiplec                      s
      d S r3   r   r   r@   r   r    decorator_no_deprecation_warning   s    z=test_ignore_warning.<locals>.decorator_no_deprecation_warningc                      s
      d S r3   r   r   rH   r   r   decorator_no_user_warning   s    z6test_ignore_warning.<locals>.decorator_no_user_warningc                      s
      d S r3   r   r   rF   r   r   )decorator_no_deprecation_multiple_warning   s    zFtest_ignore_warning.<locals>.decorator_no_deprecation_multiple_warningc                      s
      d S r3   r   r   rF   r   r   "decorator_no_user_multiple_warning   s    z?test_ignore_warning.<locals>.decorator_no_user_multiple_warningc                	      s   t      W 5 Q R X d S r3   )r   r   rH   r   r   context_manager_no_warning   s    z7test_ignore_warning.<locals>.context_manager_no_warningc                	      s$   t ttfd    W 5 Q R X d S NrB   )r   r?   UserWarningr   rF   r   r   #context_manager_no_warning_multiple   s    z@test_ignore_warning.<locals>.context_manager_no_warning_multiplec                	      s    t td    W 5 Q R X d S rN   r   r?   r   rH   r   r   &context_manager_no_deprecation_warning   s    zCtest_ignore_warning.<locals>.context_manager_no_deprecation_warningc                	      s    t td    W 5 Q R X d S rN   r   rO   r   rH   r   r   context_manager_no_user_warning   s    z<test_ignore_warning.<locals>.context_manager_no_user_warningc                	      s    t td    W 5 Q R X d S rN   rQ   r   rF   r   r   /context_manager_no_deprecation_multiple_warning   s    zLtest_ignore_warning.<locals>.context_manager_no_deprecation_multiple_warningc                	      s    t td    W 5 Q R X d S rN   rS   r   rF   r   r   (context_manager_no_user_multiple_warning   s    zEtest_ignore_warning.<locals>.context_manager_no_user_multiple_warningzA'obj' should be a callable.+you should use 'category=UserWarning'r   c                   S   s   d S r3   r   r   r   r   r   r7      s    z!test_ignore_warning.<locals>.test)	r   r   r?   r'   ZwarnsrO   FutureWarningr   r(   )rE   rG   rI   rJ   rK   rL   rM   rP   rR   rT   rU   rV   Zwarning_classr   Zsilence_warnings_funcr7   r   rD   r   test_ignore_warningm   sr    rX   c                   @   s   e Zd Zdd ZdS )	TestWarnsc              	   C   s@   dd }t t t| W 5 Q R X tdd ddks<td S )Nc                   S   s   t d dS )NZyor   )r=   r>   r   r   r   r   f   s    
zTestWarns.test_warn.<locals>.fc                 S   s   | S r3   r   )r+   r   r   r   <lambda>       z%TestWarns.test_warn.<locals>.<lambda>r#   )r'   r   r   r   )selfrZ   r   r   r   	test_warn   s    zTestWarns.test_warnN)__name__
__module____qualname__r^   r   r   r   r   rY      s   rY   c                 C   s   | | }|S zFunction f

    Parameters
    ----------
    a : int
        Parameter a
    b : float
        Parameter b

    Returns
    -------
    c : list
        Parameter c
    r   abcr   r   r   f_ok   s    rg   c                 C   s   | | }|S )zFunction f

    Parameters
    ----------
    a : int
        Parameter a
    b : float
        Parameter b

    Results
    -------
    c : list
        Parameter c
    r   rc   r   r   r   f_bad_sections   s    rh   c                 C   s   ||  }|S rb   r   )re   rd   rf   r   r   r   f_bad_order  s    ri   c                 C   s   | | }|S )zFunction f

    Parameters
    ----------
    a : int
        Parameter a
    b : int
        Parameter b
    c : int
        Parameter c

    Returns
    -------
    d : list
        Parameter c
    r   )rd   re   dr   r   r   f_too_many_param_docstring!  s    rk   c                 C   s   | | }|S )zFunction f

    Parameters
    ----------
    a : int
        Parameter a

    Returns
    -------
    c : list
        Parameter c
    r   rc   r   r   r   	f_missing6  s    rl   c                 C   s   | | | | S )zFunction f

    Parameters
    ----------
    a: int
        Parameter a
    b:
        Parameter b
    c :
        This is parsed correctly in numpydoc 1.2
    d:int
        Parameter d
    e
        No typespec is allowed without colon
    r   )rd   re   rf   rj   er   r   r   f_check_param_definitionG  s    rn   c                   @   s   e Zd Zdd Zdd ZdS )Klassc                 C   s   d S r3   r   r]   r.   r-   r   r   r   rl   [  s    zKlass.f_missingc                 C   s   dS )zFunction f

        Parameter
        ---------
        a : int
            Parameter a
        b : float
            Parameter b

        Results
        -------
        c : list
            Parameter c
        Nr   rp   r   r   r   rh   ^  s    zKlass.f_bad_sectionsN)r_   r`   ra   rl   rh   r   r   r   r   ro   Z  s   ro   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )MockEstc                 C   s   dS )ZMockEstimatorNr   r]   r   r   r   __init__q  s    zMockEst.__init__c                 C   s   |S r3   r   rp   r   r   r   fitt  s    zMockEst.fitc                 C   s   |S r3   r   r]   r.   r   r   r   predictw  s    zMockEst.predictc                 C   s   |S r3   r   ru   r   r   r   predict_probaz  s    zMockEst.predict_probac                 C   s   dS )N      ?r   ru   r   r   r   score}  s    zMockEst.scoreN)r_   r`   ra   rs   rt   rv   rw   ry   r   r   r   r   rq   p  s
   rq   c                   @   sh   e Zd Zdd Zedd dd Zedd edd	d
 Zedd dd Zeddd Z	dS )MockMetaEstimatorc                 C   s
   || _ dS zMetaEstimator to check if doctest on delegated methods work.

        Parameters
        ---------
        delegate : estimator
            Delegated estimator.
        Ndelegater]   r}   r   r   r   rs     s    zMockMetaEstimator.__init__c                 C   s   t | jdS )Nrv   hasattrr}   rr   r   r   r   r[     r\   zMockMetaEstimator.<lambda>c                 C   s   | j |S zThis is available only if delegate has predict.

        Parameters
        ----------
        y : ndarray
            Parameter y
        r}   rv   ru   r   r   r   rv     s    	zMockMetaEstimator.predictc                 C   s   t | jdS )Nry   r   rr   r   r   r   r[     r\   %Testing a deprecated delegated methodc                 C   s   dS zThis is available only if delegate has score.

        Parameters
        ---------
        y : ndarray
            Parameter y
        Nr   ru   r   r   r   ry     s    zMockMetaEstimator.scorec                 C   s   t | jdS )Nrw   r   rr   r   r   r   r[     r\   c                 C   s   |S zThis is available only if delegate has predict_proba.

        Parameters
        ---------
        X : ndarray
            Parameter X
        r   ru   r   r   r   rw     s    	zMockMetaEstimator.predict_proba-Testing deprecated function with wrong paramsc                 C   s   dS z,Incorrect docstring but should not be testedNr   rp   r   r   r   rt     s    zMockMetaEstimator.fitN)
r_   r`   ra   rs   r   rv   r   ry   rw   rt   r   r   r   r   rz     s   




	


rz   c                   @   sb   e Zd Zdd Zedddd Zeddeddd	 Zeddd
d Zeddd Z	dS )%MockMetaEstimatorDeprecatedDelegationc                 C   s
   || _ dS r{   r|   r~   r   r   r   rs     s    z.MockMetaEstimatorDeprecatedDelegation.__init__r}   r|   c                 C   s   | j |S r   r   ru   r   r   r   rv     s    	z-MockMetaEstimatorDeprecatedDelegation.predictr   c                 C   s   dS r   r   ru   r   r   r   ry     s    z+MockMetaEstimatorDeprecatedDelegation.scorec                 C   s   |S r   r   ru   r   r   r   rw     s    	z3MockMetaEstimatorDeprecatedDelegation.predict_probar   c                 C   s   dS r   r   rp   r   r   r   rt     s    z)MockMetaEstimatorDeprecatedDelegation.fitN)
r_   r`   ra   rs   r   rv   r   ry   rw   rt   r   r   r   r   r     s   


	

r   z,ignore:if_delegate_has_method was deprecated	mock_metar|   c                 C   s  t jdddd tt}|g ks$tttdgd}|g ks>tttdgd}|g ksXtt jtdd tt W 5 Q R X t jtd	d tt	j W 5 Q R X tt
}| jj}|d
ddgkstdddddgddddddgdddddgdddddgdd| d  d!dd"d#d$d#gdd| d% d& d'd(d)gdd| d* d'd(d)gdd| d+ ddddgg}t|tttt	j| j| j| j| jgD ],\}}t|}||ks~td,||f q~d S )-NZnumpydocz+numpydoc is required to test the docstringsz1.2.0)reasonZ
minversionre   )ignorezUnknown section Resultsr   zUnknown section Parameterzxsklearn.utils.tests.test_testing.f_check_param_definition There was no space between the param name and colon ('a: int')ztsklearn.utils.tests.test_testing.f_check_param_definition There was no space between the param name and colon ('b:')zwsklearn.utils.tests.test_testing.f_check_param_definition There was no space between the param name and colon ('d:int')z9In function: sklearn.utils.tests.test_testing.f_bad_orderznThere's a parameter name mismatch in function docstring w.r.t. function signature, at index 0 diff: 'b' != 'a'z
Full diff:z- ['b', 'a']z+ ['a', 'b']zHIn function: sklearn.utils.tests.test_testing.f_too_many_param_docstringz_Parameters in function docstring have more items w.r.t. function signature, first extra item: cz- ['a', 'b']z+ ['a', 'b', 'c']z?          +++++z7In function: sklearn.utils.tests.test_testing.f_missingzaParameters in function docstring have less items w.r.t. function signature, first missing item: bz+ ['a']z=In function: sklearn.utils.tests.test_testing.Klass.f_missingzaParameters in function docstring have less items w.r.t. function signature, first missing item: Xz- ['X', 'y']z+ []zIn function: z!sklearn.utils.tests.test_testing.z.predictznThere's a parameter name mismatch in function docstring w.r.t. function signature, at index 0 diff: 'X' != 'y'z- ['X']z?   ^z+ ['y'].rw   z&potentially wrong underline length... zParameters z--------- in z.scorez.fitz
"%s"
 not in 
"%s")r'   importorskipr   rg   r   rl   r   RuntimeErrorrh   ro   rn   	__class__r_   zipri   rk   rv   rw   ry   rt   )r   Z	incorrectZmock_meta_namemessagesr,   rZ   r   r   r   test_check_docstring_parameters  s    	
			

	
Gr   c                   @   s   e Zd Zdd Zdd ZdS )RegistrationCounterc                 C   s
   d| _ d S )Nr   )nb_callsrr   r   r   r   rs   a  s    zRegistrationCounter.__init__c                 C   s    |  j d7  _ |jtkstd S )Nr#   )r   funcr   r   )r]   Zto_register_funcr   r   r   __call__d  s    zRegistrationCounter.__call__N)r_   r`   ra   rs   r   r   r   r   r   r   `  s   r   rc                 C   s:   t |tjst|dk}|jj|ks(ttj| | d S )Nr   )
isinstancer$   Zmemmapr   flags	writeableZtestingZassert_array_equal)input_arrayZ	mmap_data	mmap_moder   r   r   r   check_memmapi  s    r   c              	   C   s   t  }| td| td}t|}t|| tj	|j
}W 5 Q R X tjdkrdtj|rdt|jdksrtd}t||d"}t|||d tj	|j
}W 5 Q R X tjdkrtj|rt|jdkstd S )Nregisterr   ntr#   r+)r   r    )r   setattratexitr$   r)   r   r   ospathdirnamefilenamenameexistsr   r   )monkeypatchregistration_counterr   dataZtemp_folderr   r   r   r   test_tempmemmapp  s     




r   alignedFTc           	   	   C   s&  t  }| td| td}t||d}t|| |jdksBtt|d|d\}}t|| |t	j
|jkstt|jdkstd}t|||d	}t||| |jdkst||d |d g}t||d}t||D ]\}}t|| q|jd
ksttjtdd t|dgdd W 5 Q R X d S )Nr   r   )r   r#   T)Zreturn_folderr   r    r   r   r      z`When creating aligned memmap-backed arrays, input must be a single array or a sequence of arraysr   znot-an-array)r   r   r   r$   r)   r   r   r   r   r   r   r   r   r   r'   r   r(   )	r   r   r   r   r   folderr   Z
input_listZmmap_data_listr   r   r   test_create_memmap_backed_data  s8    

  

r   dtypec                 C   sd   t d| }|jd st|jd s,tt|}t|ddd}t|}|tj|ddks`td	S )
z2Test memory mapped array on contiguous memoryview.
   ZC_CONTIGUOUSZALIGNEDr   Tr   gdy=)relN)	r$   r%   Zastyper   r   r   r   r'   Zapprox)r   r+   Z
sum_originZx_mmapZsum_mmapr   r   r   test_memmap_on_contiguous_data  s    r   z constructor_name, container_typelisttuplearrayr   Z
sparse_csrZ
sparse_csc	dataframec                   C   s   t djS NZpandas)r'   r   Z	DataFramer   r   r   r   r[     r\   r[   seriesc                   C   s   t djS r   )r'   r   ZSeriesr   r   r   r   r[     r\   indexc                   C   s   t djS r   )r'   r   ZIndexr   r   r   r   r[     r\   slicezdtype, superdtypec                 C   s   | dkr| }ddg}t || |d}t||s2t| dkrTtt|d |stn6t|drn|j|kstnt|dr|jd |kstdS )	z\Check that we convert the container to the right type of array with the
    right data type.)r   r   r   r   r#   r   )r   r   r   r   dtypesN)	r   r   r   r$   Z
issubdtypetyper   r   r   )Zconstructor_nameZcontainer_typer   Z
superdtype	containerZcontainer_convertedr   r   r   test_convert_container  s     

r   c               
   C   s  t t t W 5 Q R X t tdd} tdW 5 Q R X | jsDtt tddgd} tdW 5 Q R X | jsrttj tdd" t t} tdW 5 Q R X W 5 Q R X | jrttj tdd$ t tdd} t W 5 Q R X W 5 Q R X | jrttj tdd& t tdd} tdW 5 Q R X W 5 Q R X | jr:ttj td	d& t td
d} tdW 5 Q R X W 5 Q R X | jrttj tdd( t td
dd} tdW 5 Q R X W 5 Q R X | jrttj tdd t t} W 5 Q R X W 5 Q R X | jrtt td
dd} W 5 Q R X | jr(tt ttf t W 5 Q R X t ttf t W 5 Q R X t t t ttf W 5 Q R X W 5 Q R X d S )Nzhow are your   zhello how are youznot this onezthis will be raisedzthe failure message)err_msgzthis is ignoredz,should contain one of the following patternsZhellozBad message)r   r   zDid not raiseT)r   Zmay_pass)r   r8   Zraised_and_matchedr   r'   r(   )cmr   r   r   test_raises  sZ    





 

r   c                	   C   s   t tjdgtjdd tt t tjdgtjdd W 5 Q R X t tjdgtjdd tt t tjdgtjdd W 5 Q R X tt t tjdgtjdd W 5 Q R X t tjdgtjddd	d
 d S )Ng㈵ ?r   rx   gX ?gc^  ?g55  ?gh㈵>g        gh㈵>)Zatol)r   r$   r   float32r'   r   r   float64r   r   r   r   "test_float32_aware_assert_allclose;  s    """r   )r   )Lr=   Zunittestr   r   Znumpyr$   Zscipyr   r'   Zsklearn.utils.deprecationr   Zsklearn.utils.metaestimatorsr   r   Z%sklearn.utils._readonly_array_wrapperr   Zsklearn.utils._testingr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   Zsklearn.treer   Zsklearn.discriminant_analysisr   r   r1   r2   r:   rX   ZTestCaserY   rg   rh   ri   rk   rl   rn   ro   rq   rz   r   markfilterwarningsZparametrizer   r   r   r   r   r   r   Zint32Zint64r   r   r   Zndarrayr*   r&   r   integerZfloatingr   r   r   r   r   r   r   <module>   s   @	'm11


u	

%







	F