U
    9%eW                     @   s2  d dl Z 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	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!Z-G d"d# d#Z.G d$d% d%Z/d&d' Z0G d(d) d)Z1dJd+d,Z2d-d. Z3ej45d/d0d1gd2d3 Z6ej45d4d5e7fd6e8fd7ej9fd8ej:fd9ej:fd:ej;fd;d<d= fd>d?d= fd@dAd= fdBe<fg
ej45dCej=ej>fej?ej>fej@ejAfejBejAfgdDdE ZCdFdG ZDdHdI ZEdS )K    N)sparse)LinearDiscriminantAnalysis)DecisionTreeClassifier)
TempMemmap_convert_container_delete_folderassert_allcloseassert_allclose_dense_sparseassert_no_warningsassert_raise_messageassert_raisesassert_raises_regexcheck_docstring_parameterscreate_memmap_backed_dataignore_warningsraisesset_random_state)
deprecated)available_ifc                  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   _/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/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)npZ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    
r.   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    r/   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_ValueErrorC   s    z4test_assert_raise_message.<locals>._raise_ValueErrorc                   S   s   d S r0   r   r   r   r   r   	_no_raiseF   s    z,test_assert_raise_message.<locals>._no_raisetestzsomething else)r   r%   r   r   	TypeErrorAttributeError)r2   r3   r   r   r   test_assert_raise_messageB   s6    		r7   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_functionl   s    z.test_ignore_warning.<locals>._warning_functionc                   S   s   t dt t d d S r8   r9   r   r   r   r   _multiple_warning_functiono   s    z7test_ignore_warning.<locals>._multiple_warning_functioncategoryc                      s        d S r0   r   r   r>   r=   r   r   decorator_no_warning   s    z1test_ignore_warning.<locals>.decorator_no_warningc                      s
      d S r0   r   r   r>   r   r   decorator_no_warning_multiple   s    z:test_ignore_warning.<locals>.decorator_no_warning_multiplec                      s
      d S r0   r   r   r=   r   r    decorator_no_deprecation_warning   s    z=test_ignore_warning.<locals>.decorator_no_deprecation_warningc                      s
      d S r0   r   r   rE   r   r   decorator_no_user_warning   s    z6test_ignore_warning.<locals>.decorator_no_user_warningc                      s
      d S r0   r   r   rC   r   r   )decorator_no_deprecation_multiple_warning   s    zFtest_ignore_warning.<locals>.decorator_no_deprecation_multiple_warningc                      s
      d S r0   r   r   rC   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 r0   )r   r   rE   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 Nr?   )r   r<   UserWarningr   rC   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 rK   r   r<   r   rE   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 rK   r   rL   r   rE   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 rK   rN   r   rC   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 rK   rP   r   rC   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 r0   r   r   r   r   r   r4      s    z!test_ignore_warning.<locals>.test)	r
   r   r<   r$   ZwarnsrL   FutureWarningr   r%   )rB   rD   rF   rG   rH   rI   rJ   rM   rO   rQ   rR   rS   Zwarning_classr   Zsilence_warnings_funcr4   r   rA   r   test_ignore_warningi   sr    rU   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 r0   r   )r(   r   r   r   <lambda>       z%TestWarns.test_warn.<locals>.<lambda>r!   )r$   r   r   r
   )selfrW   r   r   r   	test_warn   s    zTestWarns.test_warnN)__name__
__module____qualname__r[   r   r   r   r   rV      s   rV   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    rd   c                 C   s   | | }|S )zFunction f

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

    Results
    -------
    c : list
        Parameter c
    r   r`   r   r   r   f_bad_sections   s    re   c                 C   s   ||  }|S r_   r   )rb   ra   rc   r   r   r   f_bad_order
  s    rf   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   )ra   rb   dr   r   r   f_too_many_param_docstring  s    rh   c                 C   s   | | }|S )zFunction f

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

    Returns
    -------
    c : list
        Parameter c
    r   r`   r   r   r   	f_missing2  s    ri   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   )ra   rb   rc   rg   er   r   r   f_check_param_definitionC  s    rk   c                   @   s   e Zd Zdd Zdd ZdS )Klassc                 C   s   d S r0   r   rZ   r+   r*   r   r   r   ri   W  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   rm   r   r   r   re   Z  s    zKlass.f_bad_sectionsN)r\   r]   r^   ri   re   r   r   r   r   rl   V  s   rl   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   rZ   r   r   r   __init__m  s    zMockEst.__init__c                 C   s   |S r0   r   rm   r   r   r   fitp  s    zMockEst.fitc                 C   s   |S r0   r   rZ   r+   r   r   r   predicts  s    zMockEst.predictc                 C   s   |S r0   r   rr   r   r   r   predict_probav  s    zMockEst.predict_probac                 C   s   dS )N      ?r   rr   r   r   r   scorey  s    zMockEst.scoreN)r\   r]   r^   rp   rq   rs   rt   rv   r   r   r   r   rn   l  s
   rn   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)rZ   ry   r   r   r   rp   ~  s    zMockMetaEstimator.__init__c                 C   s   t | jdS )Nrs   hasattrry   ro   r   r   r   rX     rY   zMockMetaEstimator.<lambda>c                 C   s   | j |S )zThis is available only if delegate has predict.

        Parameters
        ----------
        y : ndarray
            Parameter y
        )ry   rs   rr   r   r   r   rs     s    	zMockMetaEstimator.predictc                 C   s   t | jdS )Nrv   rz   ro   r   r   r   rX     rY   z%Testing a deprecated delegated methodc                 C   s   dS )zThis is available only if delegate has score.

        Parameters
        ---------
        y : ndarray
            Parameter y
        Nr   rr   r   r   r   rv     s    zMockMetaEstimator.scorec                 C   s   t | jdS )Nrt   rz   ro   r   r   r   rX     rY   c                 C   s   |S )zThis is available only if delegate has predict_proba.

        Parameters
        ---------
        X : ndarray
            Parameter X
        r   rr   r   r   r   rt     s    	zMockMetaEstimator.predict_probaz-Testing deprecated function with wrong paramsc                 C   s   dS )z,Incorrect docstring but should not be testedNr   rm   r   r   r   rq     s    zMockMetaEstimator.fitN)
r\   r]   r^   rp   r   rs   r   rv   rt   rq   r   r   r   r   rw   }  s   




	


rw   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
} tt d
}|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std-|| f qd S ).NZnumpydocz+numpydoc is required to test the docstringsz1.2.0)reasonZ
minversionrb   )ignorezUnknown section Resultsr   zUnknown section Parameterrx   zxsklearn.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'].rt   z&potentially wrong underline length... zParameters z--------- in z.scorez.fitz
"%s"
 not in 
"%s")r$   importorskipr   rd   r   ri   r   RuntimeErrorre   rl   rk   rw   rn   	__class__r\   ziprf   rh   rs   rt   rv   rq   )Z	incorrectZ	mock_metaZmock_meta_namemessagesr)   rW   r   r   r   test_check_docstring_parameters  s    


	
Sr   c                   @   s   e Zd Zdd Zdd ZdS )RegistrationCounterc                 C   s
   d| _ d S )Nr   )nb_callsro   r   r   r   rp   7  s    zRegistrationCounter.__init__c                 C   s    |  j d7  _ |jtkstd S )Nr!   )r   funcr   r   )rZ   Zto_register_funcr   r   r   __call__:  s    zRegistrationCounter.__call__N)r\   r]   r^   rp   r   r   r   r   r   r   6  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	writeabletestingZassert_array_equal)input_arrayZ	mmap_data	mmap_moder   r   r   r   check_memmap?  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_tempmemmapF  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   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   rX     rY   rX   seriesc                   C   s   t djS r   )r$   r   ZSeriesr   r   r   r   rX     rY   indexc                   C   s   t djS r   )r$   r   Indexr   r   r   r   rX     rY   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!   dtype)r   r   r   r   dtypesN)	r   r   r   r"   Z
issubdtypetyper{   r   r   )Zconstructor_name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   r5   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   ru   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   )Fr   r   Zunittestr:   numpyr"   r$   Zscipyr   Zsklearn.discriminant_analysisr   Zsklearn.treer   Zsklearn.utils._testingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Zsklearn.utils.deprecationr   Zsklearn.utils.metaestimatorsr   r   r.   r/   r7   rU   ZTestCaserV   rd   re   rf   rh   ri   rk   rl   rn   rw   r   r   r   r   markZparametrizer   r   r   Zndarrayr'   r#   r   Zint32integerZint64r   Zfloatingr   r   r   r   r   r   r   r   <module>   st   @	'm1 		

%






	F