U
    3dp                     @   s  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
Z
ddl
m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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* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddlm1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZD ddlEmFZF ddlGZGddlHmIZImJZJ ddlmKZK dd  ZLe
jMNd!ejOOd"ePd#d$Q gd%d& ZRd'd( ZSd)d* ZTd+d, ZUe
jMNd-ejVd.fejWd/fejWd.fge
jMNd0ejXejYgd1d2 ZZe
jMNd3ejVd4d5d6fejVd!d5d7fejVd8d5d9fejVd!d/d7fejWd4d5d:fejWd!d5d;fejWd<d5d=fejWd4d>d?fejWd4d@d:fg	e
jMNd0ejXejYgdAdB Z[e
jMNdCd!d<d8ge
jMNd0ejXejYgdDdE Z\dFdG Z]e
jMNdHe^d@ejWggdIfe^d@ejWggdIfe^d@ejVggdJfej^d@ejWgge_dKdLfge
jMNdMd5d.gdNdO Z`edPdQ Zae
jMNd!dRdSgdTdUggej^dRdSgdTdUggdVdKej^dRdSgdTdUggdWdKdXdYgdZd[ggej^dXdYgdZd[ggd\dKgd]d^ Zbe
jMNd_d`dadbdcdddege
jMNdfejcejdgejcfejdejdfdgejdfgdhdi Zedjdk Zfdldm Zgdndo Zhdpdq Zidrds Zjdtdu Zkdvdw Zle
jmdxdydzd{gd|d}d~ Zndd Zodd Zpdd Zqdd Zrdd Zsdd Ztdd Zudd Zvdd Zwe
jMjNde	dexeygdddgddd Zzdd Z{dd Z|dd Z}dd Z~e
jMNdejdejcfejdddgfe
jMNdddd Zdd Zdd ZG dd dZG dd dZdd Ze
jMNdd5d.gdd Ze
jMNd0ejXejYejejejejejejgdd Zdd Zdd Ze
jMNdd$dddgdd Ze
jMNdd@ded$ddedfddejd$ddedÃfddejd$ddedăfd@deefd$ddedŃfd@ded$ddedǃfdded$ddedɃfd$ded$dded̃fdded$ddedσfdded$dded҃fddedddedԃfdded$ddedփfgdd؄ Zde^d@d$gdd4fe^d@d$ge^d@d$gdd4fde^ddgeJdfde^ddgeJd4fej^d@dgejcdKej^d@dgejcdKeJdfej^d@dgejddKej^d@dgejddKeJdfde^ddgeJdfdZe
jMjNdexe exe de
jMNdd5d.gdd Zdedfdedfdedfej^ddgejcdKedfej^ddgejddKedfdZe
jMjNdexe exe ddd Zdd Ze
jMNdej^ejYejgdd Ze
jMNdej^ejYejgdd Ze
jMNdejYejgdd Zdd  Zdd Zdd Ze
jMNddd@dggdd Ze
jMNdd5dxdydzd{gd	d
 Ze
jMNddddddddddg	dd Ze
jMNdddejfddejfddejfddejfdd ejfd!d"ejfd#d$ejfd%d&ejfd'd&ejfd(d)ejfd*d%ejfd+d,ejfd-d.ejfd/d0ejfd1d2ejfd3d4ejfgd5d6 Ze
jMNd7ddd8d9d:d;gd<d= Ze
jMjNd!d@d$dgd>d?d@gd.d5d.gdAdBdCgd>d@id?d$id@diggd'dDdEdFdGgde
jMNd7ddd8dHgdIdJ Ze
jMjNd!d@d?d.dKgd'dDdEdFgddLdM Ze
jMjNdNexed$ed$dd>d?gd@dOgggdPdQdRdSgddTdU ZdVdW ZdXdY Ze
jMjNdZd>d@gd[fd\d>d?ggd]fgd^d_gdd`da ZG dbdc dceFZddde Zdfdg Zdhdi ZdS (j  z$Tests for input validation functions    N)NamedTemporaryFile)product)
itemgetter)importorskip)assert_no_warnings)ignore_warnings)SkipTest)assert_array_equal)assert_allclose_dense_sparse)assert_allclose)_convert_container)as_float_arraycheck_arraycheck_symmetric)	check_X_y)
deprecated)MockDataFrame)parse_version)_NotAnArray)_sparse_random_matrix)ARDRegression)KNeighborsClassifier)RandomForestRegressor)SVR)
make_blobs)_safe_indexing)has_fit_parametercheck_is_fittedcheck_consistent_lengthassert_all_finitecheck_memorycheck_non_negative_num_samplescheck_scalar_check_psd_eigenvalues_check_y_deprecate_positional_args_check_sample_weight_allclose_dense_sparse_num_featuresFLOAT_DTYPES_get_feature_names_check_feature_names_in_check_fit_params)BaseEstimator)NotFittedErrorPositiveSpectrumWarning)
TempMemmapc                  C   s  t jdt jd} | t jdt jd } t| dd}|jt jks@t| t j	} t| dd}t| dd| k	slt|jt j
ks|ttt jt jt jt jt jt jg}|D ]&}| |} t| }|jt jkstq| t} t| dd}|jt j
kstt jdt jd} t| dd| kstt | } t t| dds4ttt d tddd	d
 g}|D ]0}t|dd}t j|d< t | r^tq^d S )N)   
   dtyper3   FcopyT)r2         皙?)Zdensityr   r   )nponesint32aranger   r5   float32AssertionErrorastypeint64float64boolint8int16uint8uint16uint32objectZasfortranarrayZ	isfortransp
csc_matrixtoarrayr   nanisnanany)XZX2Ztested_dtypesr5   ZmatricesMN rU   G/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/utils/tests/test_validation.pytest_as_float_array@   s6    



rW   rR   r3   r8   r3   r8   c                 C   s.   t j| d< t j| d< t| dd}t||  d S )N)r9   r   )      	allow-nanforce_all_finite)r<   rO   r   r
   )rR   ZX_convertedrU   rU   rV   test_as_float_array_nanj   s    

r^   c                  C   sD   t ddd} tt| t jr&tttt| t jr@td S )N   r2      )	r<   r?   reshape
isinstancer   ZmatrixrA   rL   rM   rR   rU   rU   rV   test_np_matrixr   s    rd   c               	   C   s   dd } t ddh}tj|dtjd}d|d d < ttj| fD ]6}||}d|d d < t| |  d|d d < q>W 5 Q R X d S )	Nc                 S   s   t | ddS )NFr6   )r   )xrU   rU   rV   <lambda>}       ztest_memmap.<locals>.<lambda>zsklearn-test)prefix)r3   r3   )shaper5   r   rZ   )r   r<   Zmemmapr@   r   asarrayr	   Zravel)ZasflttmprS   frR   rU   rU   rV   test_memmapz   s    rm   c                  C   s   t d} | | jfD ]V}dD ]L}t|d|d}|jd s<tt|d|d}|jd sXt|r||k	stqqt| } | jd d d | _| jjd rtd S )	N)r3   r9   )TFC)orderr7   C_CONTIGUOUSFF_CONTIGUOUS)	r<   r=   Tr   flagsrA   rL   
csr_matrixdata)rR   Ar7   BrU   rU   rV   test_ordering   s    

rz   zvalue, force_all_finiteFr[   retypec                 C   s@   |t dddt}| |d< t||dd}t|| d S )Nr`   r8   r;   T)r]   accept_sparse)r<   r?   ra   rB   floatr   r
   )valuer]   r{   rR   	X_checkedrU   rU   rV   'test_check_array_force_all_finite_valid   s    r   z.value, input_name, force_all_finite, match_msg TzInput contains infinityzInput X contains infinitysample_weightz%Input sample_weight contains infinityInput contains NaNzInput X contains NaNyzInput y contains NaNz	allow-infz0force_all_finite should be a bool or "allow-nan"rZ   c              	   C   sT   |t dddt j}| |d< tjt|d t|||dd W 5 Q R X d S )Nr`   r8   r;   matchT)
input_namer]   r|   )	r<   r?   ra   rB   rD   pytestraises
ValueErrorr   )r~   r   r]   Z	match_msgr{   rR   rU   rU   rV   (test_check_array_force_all_finiteinvalid   s    r   r   c              	   C   s   |t dddt j}t j|d< t }d|jj d}t	j
td|  dd}t||| d	d
 W 5 Q R X | dkr||jjd kstn||jjd kst| dkrt	j
td|  dd }t |t |jd  W 5 Q R X ||jjd kstd S )Nr`   r8   r;   
aX   does not accept missing values encoded as NaN natively. For supervised learning, you might want to consider sklearn.ensemble.HistGradientBoostingClassifier and Regressor which accept missing values encoded as NaNs natively. Alternatively, it is possible to preprocess the data, for instance by using an imputer transformer in a pipeline or drop samples with missing values. See https://scikit-learn.org/stable/modules/impute.html You can find a list of all estimators that handle NaN values at the following page: https://scikit-learn.org/stable/modules/impute.html#estimators-that-handle-nan-valueszInput z contains NaNr   T)	estimatorr   r|   rR   r   )r<   r?   ra   rB   rD   rO   r   	__class____name__r   r   r   r   r~   argsrA   fitr=   ri   )r   r{   rw   r   Zextended_msgctxrU   rU   rV   0test_check_array_links_to_imputer_doc_only_for_X   s&    
$r   c               	   C   sz   t jddt jggtdj} t| d dd}| |ks4tt| d dd}| |ksNttjt	dd t| d d	d W 5 Q R X d S )
Nabr4   r[   r5   r]   Fr   r   T)
r<   arrayrO   rK   rt   r   rA   r   r   r   )rR   r   rU   rU   rV   (test_check_array_force_all_finite_object   s    r   z
X, err_msgzInput contains NaN.z5Input contains infinity or a value too large for.*intr4   z#cannot convert float NaN to integerr]   c              	   C   s,   t jt|d t| t|d W 5 Q R X d S )Nr   r   )r   r   r   r   int)rR   err_msgr]   rU   rU   rV   7test_check_array_force_all_finite_object_unsafe_casting  s    r   c               	   C   s  ddgddgg} t | }tt t| W 5 Q R X tdddgdd}|jdksXttjtdd	 tdddgd
d W 5 Q R X tjtdd	 tdd
d W 5 Q R X t	
dddd}tt t| W 5 Q R X t|d
d t	
dddd}|d}|t}|t}||||g}t	jttt	jd ttg}	ddd g}
d
dg}t||	|
|D ] \} }}}t| |||d}|d k	r|j|kstn|j| jkst|dkr|jd st|jd rtn*|dkr|jd st|jd rt|r| |k	s`tnH| j|jkr`|jd | jd kr`|jd | jd kr`| |ks`tq`t |}| }| }|t}|t}|||||g}ddgddgg}dd |	D }t||||D ]\} }}}t| |||d}|d k	r|j|kstn|j| jkst| j|kr6| j|jksJtn|j|d ksJt|r`| |k	stn*| j|jkr| j|jkr| |kstqtddgddgg}t|t	jsttt t|  W 5 Q R X t| d
d t|}t|}t|t	jstd S )NrZ   r8   r2   r`   r   F	ensure_2dz'Expected 2D array, got 1D array insteadr   Tz+Expected 2D array, got scalar array insteadr3      allow_ndrn   rq   )r5   ro   r7   rp   rr   csrcoodokc                 S   s   g | ]}|t k	r|qS rU   )rK   ).0dtrU   rU   rV   
<listcomp>c  s      z$test_check_array.<locals>.<listcomp>r5   r|   r7   )rL   rv   r   r   	TypeErrorr   ndimrA   r   r<   r?   ra   r7   rB   r   r}   r>   r@   rE   rK   r   r5   ru   rM   ZtocooZtodokformatrb   Zndarraytolistr   )rR   X_csrZX_arrayZX_ndimZX_CZX_FZX_intZX_floatZXsdtypesZordersZcopysr5   ro   r7   r   ZX_cscZX_cooZX_dokZaccept_sparsesZnon_object_dtypesr|   ZX_denseZ
X_no_arrayresultrU   rU   rV   test_check_array  s    











   
r   1234US   1   2   3   4ZV1c              	   C   s.   d}t jt|d t| dd W 5 Q R X dS )zmTest that check_array errors when it receives an array of bytes/string
    while a numeric dtype is required.z>dtype='numeric' is not compatible with arrays of bytes/stringsr   numericr4   Nr   r   r   r   )rR   Zexpected_msgrU   rU   rV   test_check_array_numeric_error  s    r   pd_dtypeZInt8ZInt16ZUInt8ZUInt16Float32Float64zdtype, expected_dtyper   c              	   C   s   t d}| dkr t jddd}tdddtjtjgtjtjdd	d
gdddd	dggj}|j|| dddgd}|d d|d< t|d|d}t	|| |j
|kstt|d|d}t	|| |j
|kstd}t jt|d t|dd W 5 Q R X d S )Npandas>   r   r   z1.2Z
minversionrZ   r8   r2   r   r`   rY   r9   r   r   c)r5   columnsr}   r[   )r]   r5   Fr   r   Tr\   )r   r   r<   r   rO   rt   	DataFramerB   r   r   r5   rA   r   r   )r   r5   Zexpected_dtypepdZX_nprR   r   msgrU   rU   rV   "test_check_array_pandas_na_support  s"    
.

r   c               	   C   s   t d} | jdd| jgdd}d}t jt|d t|dd	d
 W 5 Q R X t|d	d	d
}t|ddtj	g |j
tjks|tt|d	d	tjd}t|ddtj	g |j
tjkstdS )z4Check check_array is correct with pd.NA in a series.r   rZ   r8   ZInt64r4   r   r   TF)r]   r   )r]   r   r5   N)r   r   SeriesZNAr   r   r   r   r<   rO   r5   rD   rA   r@   )r   X_int64r   ZX_outrU   rU   rV   *test_check_array_panadas_na_support_series  s     
   r   c                  C   s  t d} tjdddgdddgdd	d
ggtjd}| |}t|jtjksPtt|t	djtjksht|
dtji}t|jtjtjtjf t|jtjkstt|t	djtjkst|
dtji}t|jtjkstt|t	djtjkst|
dtji}t|jtjkstt|t	djtjks8t|
tj}t|jtjksZtt|t	djtjkstt| d| dddgi}t|jtjkstt|t	djtjkstd S )Nr   rZ   r8   r2   r`   r9   rY      r   	   r4   r   Zcat_col)r   r   r<   r   r@   r   r   r5   rA   r*   rB   float16r	   r   rG   rD   ZCategoricalrC   )r   rR   X_dfZcat_dfrU   rU   rV   %test_check_array_pandas_dtype_casting  s*    
(
r   c                  C   sj   t ddgddgddgddgg} t| }t|}|j| jksBtt|t jd}|jt t jksftd S )Ng?gffffff?g333333?g      ?g?r:   r4   )r<   r   r   r   r5   rA   r@   )arrZmock_dfZchecked_arrrU   rU   rV   "test_check_array_on_mock_dataframe  s    "r   c                  C   sL   dddgdddgddd	gg} t | jjd
ks0tt | ddjjd
ksHtd S )NrZ   r8   r2   r`   r9   rY   r   r   r   iFr   )r   r5   kindrA   rc   rU   rU   rV    test_check_array_dtype_stability  s    r   c            
      C   sj  dddgdddgddd	gg} t j| t jd
}t j| t jd
}t|}t|}tj|t jd
}||g}|||g}|D ]&}tt	|t j
dd}	|	jt j
ksvtqv|D ]t}tt	|t j
t jgdd}	|	jt jkst|	|ksttt	|t j
t jgddgdd}	|	jt jks
t|	|k	stqtt	|t j
t jgddgdd}	|	jt jksHt|	|k	sVt|	jdksftd S )NrZ   r8   r2   r`   r9   rY   r   r   r   r4   T)r5   r|   r   r   r   F)r<   rj   r@   rC   rL   rv   rM   r>   r   r   rD   r5   rA   r   )
Z
X_int_listZ	X_float32r   ZX_csr_float32ZX_csc_float32ZX_csc_int32Zinteger_dataZfloat32_datarR   r   rU   rU   rV   test_check_array_dtype_warning  sX    


     
 

r   c               	   C   s   ddgddgg} t | }t }d}tjt|d t|dd W 5 Q R X d	}tjt|d t||d W 5 Q R X d
}tjt|d t|g d W 5 Q R X tjt|d t|dd W 5 Q R X tjtdd t||gd W 5 Q R X d S )NrZ   r8   r2   r`   zlA sparse matrix was passed, but dense data is required. Use X.toarray\(\) to convert to a dense numpy array.r   Fr|   zjParameter 'accept_sparse' should be a string, boolean or list of strings. You provided 'accept_sparse=.*'.z]When providing 'accept_sparse' as a tuple or list, it must contain at least one string value.rU   r   )rL   rv   r   r   r   r   r   r   )rR   r   Zinvalid_typer   rU   rU   rV   -test_check_array_accept_sparse_type_exception8  s&    
r   c                  C   sP   ddgddgg} t | }t|dd t|dd t|dgd t|dd d S )	NrZ   r8   r2   r`   Tr   r   )r   )rL   rv   r   )rR   r   rU   rU   rV   +test_check_array_accept_sparse_no_exceptionW  s    
r   r   cscr   bsr)paramsc                 c   sH   t jdd| jd}dD ]&}t||rt||t||d q|V  d S )N   r3   )r   )indicesZindptrrowcolrC   )rL   randparamhasattrsetattrgetattrrB   )requestrR   attrrU   rU   rV   X_64bita  s
    
r   c                 C   s   t | ddd d S )NT)accept_large_sparser|   )r   )r   rU   rU   rV   1test_check_array_accept_large_sparse_no_exceptionj  s    r   c              	   C   s0   d}t jt|d t| ddd W 5 Q R X d S )NzQOnly sparse matrices with 32-bit integer indices are accepted. Got int64 indices.r   TF)r|   r   r   )r   r   rU   rU   rV   4test_check_array_accept_large_sparse_raise_exceptiono  s    r   c               	   C   s  d} t jt| d tg g W 5 Q R X d} t jt| d tg dd W 5 Q R X d} t jt| d tddd W 5 Q R X td}td	}d
} t jt| d t||dd W 5 Q R X t jt| d t||ddd W 5 Q R X td}td}d} t jt| d t||dd W 5 Q R X t jt| d t||ddd W 5 Q R X td	dd}td}d} t jt| d t|| W 5 Q R X td}td}t||dd\}}t
|| t
|| d S )NzC0 feature\(s\) \(shape=\(1, 0\)\) while a minimum of 1 is required.r   z@0 sample\(s\) \(shape=\(0,\)\) while a minimum of 1 is required.Fr   zDSingleton array array\(42\) cannot be considered a valid collection.*   )rZ   r3   rZ   zC1 sample\(s\) \(shape=\(1, 10\)\) while a minimum of 2 is required.r8   )ensure_min_samples)r   r   rX   zD2 feature\(s\) \(shape=\(10, 2\)\) while a minimum of 3 is required.r2   )ensure_min_featuresT)r   r   r   r3   zD0 feature\(s\) \(shape=\(10, 0\)\) while a minimum of 1 is required.)r3   r      r   r   )r   r   r   r   r   r<   r=   r   emptyra   r	   )r   rR   r   r   Z	y_checkedrU   rU   rV   2test_check_array_min_samples_and_features_messagesy  sB    







r   c               	   C   s  t dddgdddgg} tjtdd t|  W 5 Q R X dddgdddgg} tjtdd t|  W 5 Q R X d	} tjtdd t|  W 5 Q R X t dddgt dddgg} tjtdd t|  W 5 Q R X t dddgt dddgf} tjtdd t|  W 5 Q R X tt dddgdddgg} tjtdd t|  W 5 Q R X td
dgd
d
gg} tjtdd t|  W 5 Q R X t ddddddg}tjtdd t	| W 5 Q R X d S )N      ?       @      @      @      @      @       @      @      @      @      @      @zComplex data not supportedr   ))r   r   r   )r   r   r   r   )
r<   r   r   r   r   r   r   rL   
coo_matrixr%   )rR   r   rU   rU   rV   #test_check_array_complex_data_error  s0      r   c                  C   s^   t tdrtt tdstt tds*tt t ds:tG dd d} t | dsZtdd S )Nr   c                   @   s   e Zd ZeddddZdS )z@test_has_fit_parameter.<locals>.TestClassWithDeprecatedFitMethodz7Deprecated for the purpose of testing has_fit_parameterNc                 S   s   d S NrU   )selfrR   r   r   rU   rU   rV   r     s    zDtest_has_fit_parameter.<locals>.TestClassWithDeprecatedFitMethod.fit)N)r   
__module____qualname__r   r   rU   rU   rU   rV    TestClassWithDeprecatedFitMethod  s   r   z=has_fit_parameter fails for class with deprecated fit method.)r   r   rA   r   r   )r   rU   rU   rV   test_has_fit_parameter  s     r   c               
   C   s$  t ddgddgg} t d}t ddgddgg}|t|t|t|t|t|t	|d}t
t t| W 5 Q R X | D ]\}}t
t t| W 5 Q R X t
t t|dd W 5 Q R X t|dd}t|r|j|kstt| |  qt||  qd S )	Nr   rZ   r8   )Zdenser   r   r   r   Zlilr   T)Zraise_exceptionF)Zraise_warning)r<   r   r=   rL   
dok_matrixrv   rM   r   
lil_matrix
bsr_matrixr   r   r   r   itemswarnsUserWarningissparser   rA   r	   rN   )Zarr_symZarr_badZarr_asymZtest_arraysZ
arr_formatr   outputrU   rU   rV   test_check_symmetric  s.    
r  c               	   C   sB   G dd dt } tt t|   W 5 Q R X t|    d S )Nc                   @   s   e Zd Zdd Zdd ZdS )z6test_check_is_fitted_with_is_fitted.<locals>.Estimatorc                 [   s
   d| _ | S )NT)
_is_fitted)r   kwargsrU   rU   rV   r     s    z:test_check_is_fitted_with_is_fitted.<locals>.Estimator.fitc                 S   s   t | do| jS )Nr  )r   r  r   rU   rU   rV   __sklearn_is_fitted__  s    zLtest_check_is_fitted_with_is_fitted.<locals>.Estimator.__sklearn_is_fitted__N)r   r   r   r   r  rU   rU   rU   rV   	Estimator  s   r  )r.   r   r   r/   r   r   )r  rU   rU   rV   #test_check_is_fitted_with_is_fitted  s    r  c               	   C   sF  t t tt W 5 Q R X t t td W 5 Q R X t } t }z@t t t|  W 5 Q R X t t t| W 5 Q R X W n  tk
r   dstdY nX d}d}t jt|d t| |d W 5 Q R X d}d	}t jt	|d t||d W 5 Q R X | j
t   |j
t   t| d ks0tt|d ksBtd S )
Nr   Fz&check_is_fitted failed with ValueErrorz!Random message %(name)s, %(name)sz+Random message ARDRegression, ARDRegressionr   )r   z"Another message %(name)s, %(name)szAnother message SVR, SVR)r   r   r   r   r   r   r/   r   rA   AttributeErrorr   r   )ardZsvrr   r   rU   rU   rV   test_check_is_fitted  s2    r  c               	   C   sF  G dd d} d}|  }t jt|d t|ddgd W 5 Q R X t jt|d t|ddgtd W 5 Q R X t jt|d t|ddgtd W 5 Q R X d	|_t jt|d t|ddgd W 5 Q R X t jt|d t|ddgtd W 5 Q R X t|ddgtd d
|_t|ddgd t|ddgtd t|ddgtd d S )Nc                   @   s   e Zd Zdd ZdS )z4test_check_is_fitted_attributes.<locals>.MyEstimatorc                 S   s   | S r   rU   r   rR   r   rU   rU   rV   r   D  s    z8test_check_is_fitted_attributes.<locals>.MyEstimator.fitN)r   r   r   r   rU   rU   rU   rV   MyEstimatorC  s   r  z
not fittedr   a_b_)
attributes)r  Z
all_or_anyr   r   )r   r   r/   r   allrQ   r  r  )r  r   estrU   rU   rV   test_check_is_fitted_attributesB  s&    r  wrapZsinglelisttuple)idsc              	   C   sz   t  }tjtdd t|| dg W 5 Q R X |jt   t|| dg tjtdd t|| dg W 5 Q R X d S )Nzis not fitted yetr   Zcoef_Z	coef_bad_)r   r   r   r/   r   r   r   )r  r  rU   rU   rV   $test_check_is_fitted_with_attributes^  s    r  c                	   C   s.  t dgdgdgdgdg t ddgddgggddgddg t dgdtdgtd	 tjtd
d t ddgdg W 5 Q R X tjtdd t ddgd W 5 Q R X tjtdd t ddgt	  W 5 Q R X tt t ddgtd W 5 Q R X tjtdd t ddgt
  W 5 Q R X d S )NrZ   r8   r2   r`   r9   r   r   )r8   rZ   r8   zinconsistent numbers of samplesr   zgot <\w+ 'int'>zgot <\w+ 'object'>Expected sequence or array-like)r   r<   r   rL   rv   r   r   r   r   rK   r   rU   rU   rU   rV   test_check_consistent_lengthp  s    "r   c                  C   sl   zJdd l } tdddgdddgdd	d
gg}| j|dddgd}t| W n tk
rf   tdY nX d S )Nr   rZ   r8   r2   r`   r9   rY   r   r   r   r   r   r   r   zPandas not found)r   r<   r   r   r   ImportErrorr   )r   rR   r   rU   rU   rV   "test_check_dataframe_fit_attribute  s    "r#  c               	   C   sp   t dt jg} tt t|  W 5 Q R X tjdd t|  tjdd tt t|  W 5 Q R X d S )Nr   T)Zassume_finiteF)	r<   r   infr   r   r   r   sklearnZ
set_configrc   rU   rU   rV   test_suppress_validation  s    r&  c                  C   sx   t d} t| dddgdd}t|tdddg | ddd	gd
}t|d dd}t|tjddd	gtd d S )Nr   rZ   r8   r2   Fr   r   r   r   category)r5   r   r4   )r   r   r   r	   r<   r   rB   rK   )r   ressrU   rU   rV   test_check_array_series  s    r*  r5   
bool_dtype)rE   booleanc                 C   s   |dkrt ddd}nt d}|jdddgdd	d
g|jdddg|dddddgd}t|| d}|jtjksrttjdddgdd	dgdd
dggt	d}t
|| d S )Nr,  r   1.0r   rZ   r8   r2   r   r:   g @TFr4   )r   r}   rE   r   r}   rE   r!        ?               @      @)r   r   r   r   r5   r<   rD   rA   r   r}   r
   )r5   r+  r   dfr   Zexpected_arrayrU   rU   rV   'test_check_dataframe_mixed_float_dtypes  s"    	 r3  c                  C   s   t d} | ddddgi}t|dd}|jtjks8tt|dgdgdgg | jdddgddd	gd
ddgd}t|dd}|jtjkstt|ddgddgdd	gg dS )z7Check that dataframe with bool return a boolean arrays.r   rE   TFNr4   rZ   r8   r2   )rE   r   r   r!  r   r   )	r   r   r   r5   r<   Zbool_rA   r	   rC   r   r2  r   rU   rU   rV   #test_check_dataframe_with_only_bool  s    r5  c                  C   s`   t ddd} | d| jdddgddi}t|d	d}|jtjksFtt|dgdgdgg d	S )
zFCheck that dataframe with boolean return a float array with dtype=Noner   r-  r   rE   TFr,  r4   N)	r   r   r   r   r5   r<   rD   rA   r	   r4  rU   rU   rV   &test_check_dataframe_with_only_boolean  s
    r6  c                   @   s   e Zd Zdd ZdS )DummyMemoryc                 C   s   |S r   rU   )r   funcrU   rU   rV   cache  s    zDummyMemory.cacheN)r   r   r   r9  rU   rU   rU   rV   r7    s   r7  c                   @   s   e Zd ZdS )WrongDummyMemoryN)r   r   r   rU   rU   rU   rV   r:    s   r:  c               	   C   s   t d} | jdkstt d } | jd ks,tt }t |} | |ksFtd}tjt|d t d W 5 Q R X t }d|}tjt|d t | W 5 Q R X d S )NZcache_directoryzf'memory' should be None, a string or have the same interface as joblib.Memory. Got memory='1' instead.r   rZ   zg'memory' should be None, a string or have the same interface as joblib.Memory. Got memory='{}' instead.)	r    locationrA   r7  r   r   r   r:  r   )Zmemorydummyr   rU   rU   rV   test_check_memory  s$    r=  r7   c              	   C   sZ   t d}t|dd:}t|| d}t |||  ks:t|jd | ksLtW 5 Q R X d S )N)r`   r`   r)Z	mmap_moder6   Z	WRITEABLE)r<   r=   r1   r   Zmay_share_memoryrA   ru   )r7   rR   ZX_memmapr   rU   rU   rV   test_check_array_memmap
  s
    
r?  c              	   C   s   t ddddgddddgddddgddddgg}| |}t|d | ddgddgg}t|d d|d< | |}tjtdd t|d W 5 Q R X d S )NrZ   r   r   rs   r;   z	Negative r   )r<   r   r!   r   r   r   )r{   rx   rR   rU   rU   rV   test_check_non_negative  s    2

r@  c               	   C   sh   t d} d }d}tjt|d t| | W 5 Q R X d}tjt|d t| |t d W 5 Q R X d S )Nr8   r8   z;estimator requires y to be passed, but the target y is Noner   zGRandomForestRegressor requires y to be passed, but the target y is None)r   )r<   r=   r   r   r   r   r   )rR   r   r   rU   rU   rV    test_check_X_y_informative_error-  s    
rB  c               	   C   s^   G dd d} |  }t |t|ks(tG dd d}tjtdd t |  W 5 Q R X d S )Nc                   @   s   e Zd Zdd Zdd ZdS )zJtest_retrieve_samples_from_non_standard_shape.<locals>.TestNonNumericShapec                 S   s
   d| _ d S N)znot numericri   r
  rU   rU   rV   __init__;  s    zStest_retrieve_samples_from_non_standard_shape.<locals>.TestNonNumericShape.__init__c                 S   s   t dddgS )NrZ   r8   r2   )lenr
  rU   rU   rV   __len__>  s    zRtest_retrieve_samples_from_non_standard_shape.<locals>.TestNonNumericShape.__len__N)r   r   r   rE  rG  rU   rU   rU   rV   TestNonNumericShape:  s   rH  c                   @   s   e Zd Zdd ZdS )zJtest_retrieve_samples_from_non_standard_shape.<locals>.TestNoLenWeirdShapec                 S   s
   d| _ d S rC  rD  r
  rU   rU   rV   rE  F  s    zStest_retrieve_samples_from_non_standard_shape.<locals>.TestNoLenWeirdShape.__init__N)r   r   r   rE  rU   rU   rU   rV   TestNoLenWeirdShapeE  s   rI  r  r   )r"   rF  rA   r   r   r   )rH  rR   rI  rU   rU   rV   -test_retrieve_samples_from_non_standard_shape9  s    rJ  re   r2   g      @r9   c              	   C   sD   t  & t d t| dtjdddd}W 5 Q R X || ks@tdS )zPTest that check_scalar returns no error/warning if valid inputs are
    providederrorZ	test_namer8   r9   Zbothtarget_typemin_valmax_valinclude_boundariesN)warningscatch_warningssimplefilterr#   numbersRealrA   )re   ZscalarrU   rU   rV   test_check_scalar_validM  s    

rV  zJx, target_name, target_type, min_val, max_val, include_boundaries, err_msgZ
test_name1r`   Zneitherz1test_name1 must be an instance of float, not int.z6test_name1 must be an instance of float, not NoneType.z4test_name1 must be an instance of int, not NoneType.z9test_name1 must be an instance of {float, bool}, not int.Z
test_name2ztest_name2 == 1, must be > 2.Z
test_name3ztest_name3 == 5, must be < 4.Z
test_name4rightztest_name4 == 2, must be > 2.Z
test_name5leftztest_name5 == 4, must be < 4.Z
test_name6zbad parameter valuezyUnknown value for `include_boundaries`: 'bad parameter value'. Possible values are: ('left', 'right', 'both', 'neither').Z
test_name7zT`include_boundaries`='left' without specifying explicitly `min_val` is inconsistent.Z
test_name8zU`include_boundaries`='right' without specifying explicitly `max_val` is inconsistent.c              	   C   sZ   t t}t| |||||d W 5 Q R X t|jt|ks@tt|jt|ksVtdS )zLTest that check_scalar returns the right error if a wrong input is
    givenrL  N)r   r   	Exceptionr#   strr~   rA   type)re   Ztarget_namerM  rN  rO  rP  r   Zraised_errorrU   rU   rV   test_check_scalar_invalid^  s    vr\  r  )r9   y        -C6
?zJThere are imaginary parts in eigenvalues \(1e\-05 of the maximum real part)r9   g-C6
gưz?There are negative eigenvalues \(1e\-06 of the maximum positiveg|۽z?There are negative eigenvalues \(1e\-10 of the maximum positive)r9   g-=z=the largest eigenvalue is more than 1e\+12 times the smallest)ZnominalZnominal_np_arrayZinsignificant_imagzinsignificant negzinsignificant neg float32zinsignificant neg float64zinsignificant posz(lambdas, expected_lambdas, w_type, w_msgenable_warningsc              	   C   sr   |sd }|d kr>t   t dt t| |d}W 5 Q R X n&tj||d t| |d}W 5 Q R X t|| d S )NrK  )r]  r   )rQ  rR  rS  r0   r$   r   r  r   )lambdasZexpected_lambdasZw_typeZw_msgr]  Zlambdas_fixedrU   rU   rV    test_check_psd_eigenvalues_valid   s    
  r_  )r9   y              @z/There are significant imaginary parts in eigenv)rs   z,All eigenvalues are negative \(maximum is -1)r9   rs   z*There are significant negative eigenvaluesga2U0*3?ggh㈵>g|)Zsignificant_imagzall negativezsignificant negzsignificant neg float32zsignificant neg float64zlambdas, err_type, err_msgc              	   C   s&   t j||d t|  W 5 Q R X d S )Nr   )r   r   r$   )r^  Zerr_typer   rU   rU   rV   "test_check_psd_eigenvalues_invalid;  s    	ra  c               	   C   s  t dd d d } | jd r"tt| t dd} | jd sBttd t dd} t| t d tdt dd} t| dt d  tjtd	d
 tt dt dd W 5 Q R X d}tjt|d
 tt dt dd W 5 Q R X t d}t jdt j	d} t| |} | j
t j	ks(tt jdtd}td ||j
d} | j
t jksXtt d}t t|} d| d< d}tjt|d
 t| |dd W 5 Q R X d S )Nr3   r8   rp   )r9   rZ   rc   )r9   r8   r9   r0  z)Sample weights must be 1D array or scalarr   )r8   r`   rA  z/sample_weight.shape == \(4,\), expected \(2,\)!r`   r4   irs   z1Negative values in data passed to `sample_weight`T)Zonly_non_negative)r<   r=   ru   rA   r'   r   r   r   r   r@   r5   r   rD   r"   )r   r   rR   r   rU   rU   rV   test_check_sample_weightH  s4    ""


rb  rN   c                 C   s6   t ddd}| || | }}t||s2td S )Nr   r2   r<   r?   ra   r(   rA   rN   basere   r   rU   rU   rV   !test_allclose_dense_sparse_equalst  s    rf  c                 C   s:   t ddd}| || |d  }}t||r6td S )Nr   r2   rZ   rc  rd  rU   rU   rV   %test_allclose_dense_sparse_not_equals{  s    rg  c              	   C   sJ   t ddd}| |d }d}tjt|d t|| W 5 Q R X d S )Nr   r2   rZ   zFCan only compare two sparse matrices, not a sparse matrix and an arrayr   )r<   r?   ra   r   r   r   r(   )rN   re   r   r   rU   rU   rV    test_allclose_dense_sparse_raise  s
    rh  c               	   C   s   t ddddd} tjtdd | ddd W 5 Q R X tjtd	d | dddd
 W 5 Q R X t ddddddd}tjtdd |dd W 5 Q R X t ddddd}tjtdd |dd W 5 Q R X d S )NrZ   r   dc                S   s   d S r   rU   r   r   r   rj  rU   rU   rV   f1  s    z=test_deprecate_positional_args_warns_for_function.<locals>.f1Pass c=3 as keyword argsr   r8   r2   Pass c=3, d=4 as keyword argsr`   )r   r   rj  c                S   s   d S r   rU   rk  rU   rU   rV   f2  s    z=test_deprecate_positional_args_warns_for_function.<locals>.f2zPass b=2 as keyword argsc                S   s   d S r   rU   rk  rU   rU   rV   f3  s    z=test_deprecate_positional_args_warns_for_function.<locals>.f3)rZ   r&   r   r  FutureWarning)rl  ro  rp  rU   rU   rV   1test_deprecate_positional_args_warns_for_function  s    rs  c               	   C   s:   t dddd } tjtdd | dd W 5 Q R X d S )	N1.1)versionc                S   s   d S r   rU   )r   r   rU   rU   rV   rl    s    zEtest_deprecate_positional_args_warns_for_function_version.<locals>.f1z,From version 1.1 passing these as positionalr   rZ   r8   rq  )rl  rU   rU   rV   9test_deprecate_positional_args_warns_for_function_version  s    
 rv  c               	   C   s   G dd d} t jtdd | ddd W 5 Q R X t jtdd | dddd	 W 5 Q R X G d
d d}t jtdd |ddd W 5 Q R X t jtdd |dddd	 W 5 Q R X d S )Nc                   @   s    e Zd ZedddddZdS )z:test_deprecate_positional_args_warns_for_class.<locals>.A1rZ   ri  c                S   s   d S r   rU   r   r   r   r   rj  rU   rU   rV   rE    s    zCtest_deprecate_positional_args_warns_for_class.<locals>.A1.__init__Nr   r   r   r&   rE  rU   rU   rU   rV   A1  s   ry  rm  r   rZ   r8   r2   rn  r`   c                   @   s"   e Zd ZeddddddZdS )z:test_deprecate_positional_args_warns_for_class.<locals>.A2rZ   ri  c                S   s   d S r   rU   rw  rU   rU   rV   rE    s    zCtest_deprecate_positional_args_warns_for_class.<locals>.A2.__init__N)rZ   rZ   rx  rU   rU   rU   rV   A2  s   rz  )r   r  rr  )ry  rz  rU   rU   rV   .test_deprecate_positional_args_warns_for_class  s    r{  r   c              	   C   s   t jdd}ddddgt ddddgtddddgjtddddgddd d}t||| }| d k	rn| ntt	|j
d }dD ]}|| || kstq|d	 t|d	 |kstt|d
 t|d
 | t|d t|d | d S )Nr`   r8   rZ   r2   Zxxx)r  r   
sparse-col
sparse-row
scalar-int
scalar-strNoner   )r}  r~  r  r  r  r   r|  )r<   randomZrandnr   rL   rM   rt   r-   r  rangeri   rA   r   r	   r
   )r   rR   Z
fit_paramsr   Zindices_keyrU   rU   rV   test_check_fit_params  s&    
	 r  	sp_formatc                 C   sd   t d}tdd}|jj|}t|| d}| dkr:d} t|sHt	|j
| ksVt	t|| d S )Nr   r3   r2   r   Tr   )r   r   r   r   sparseZfrom_spmatrixr   rL   r  rA   r   r
   )r  r   Zsp_matZsdfr   rU   rU   rV   "test_check_sparse_pandas_sp_format  s    

r  zntype1, ntype2)
longdoubler   )r   r@   )r@   double)rG   r>   )r>   long)byterI   )ushortrJ   )rJ   uint64)rH   rF   c              	   C   s   t d}||jjdddg| dd|jjdddg|ddd}t|jtdk rd}t jt|d t	|d	d
gd W 5 Q R X nt	|d	d
gd dS )zcheck that we raise an error with dataframe having
    sparse extension arrays with unsupported mixed dtype
    and pandas version below 1.1. pandas versions 1.1 and
    above fixed this issue so no error will be raised.r   r   rZ   r5   Z
fill_valueZcol1Zcol2rt  z3Pandas DataFrame with mixed sparse extension arraysr   r   r   r   N)
r   r   r   arraysSparseArrayr   __version__r   r   r   )ntype1ntype2r   r2  r   rU   rU   rV    test_check_pandas_sparse_invalid  s    
r  z ntype1, ntype2, expected_subtypeZ	longfloatr  r   Zhalfr@   r  rD   rF   r  shortrG   Zintcr>   Zintpr  r   rC   Zlonglongint_ZubyterH   rI   r  ZuintcrJ   Zuintr  ZuintpZ	ulonglongc                 C   sh   t d}||jjdddg| dd|jjdddg|ddd}t|ddgd}t|j|sdt	d S )	Nr   r   rZ   r  r  r   r   r   )
r   r   r   r  r  r   r<   Z
issubdtyper5   rA   )r  r  Zexpected_subtyper   r2  r   rU   rU   rV   test_check_pandas_sparse_valid  s    
r  constructor_namer   Z	dataframeZ
sparse_csrZ
sparse_cscc                 C   s2   dddgdddgg}t || }t|dks.tdS )z$Check _num_features for array-likes.rZ   r8   r2   r`   r9   rY   N)r   r)   rA   )r  rR   rU   rU   rV   test_num_features>  s    
r  r   r   r   r.  g333333@g      @rZ  rE   r}   dictseriesc              	   C   s   t | |} |dkrd}n|dkr&d}n|}d| }t| drH|d7 }n.t| d tr`|d	7 }nt| d trv|d
7 }tjtt	|d t
|  W 5 Q R X d S )Nr   znumpy.ndarrayr  zpandas.core.series.Series5Unable to find the number of features from X of type ri   z with shape (3,)r   z" where the samples are of type strz# where the samples are of type dictr   )r   r   rb   rZ  r  r   r   r   reescaper)   )rR   r  Zexpected_type_namemessagerU   rU   rV   &test_num_features_errors_1d_containersI  s     



r  r1  c              	   C   s6   dt | j }tjt|d t|  W 5 Q R X d S )Nr  r   )r[  r   r   r   r   r)   )rR   r   rU   rU   rV    test_num_features_errors_scalarsj  s    r  namesrj  zlist-intr  defaultZ
MultiIndexc              	   C   sb   t d}|jddgddgddgg| d}t  tdt t|} W 5 Q R X | d	ks^td	S )
zGet feature names with pandas dataframes without warning.

    Column names with consistent dtypes will not warn, such as int or MultiIndex.
    r   rZ   r8   r`   r9   rY   r!  rK  N)	r   r   r   rQ  rR  rS  rr  r+   rA   )r  r   rR   rU   rU   rV   2test_get_feature_names_pandas_with_ints_no_warningq  s    

 
r  c                  C   sP   t d} dd tdD }| jdddgddd	gg|d
}t|}t|| dS )z)Get feature names with pandas dataframes.r   c                 S   s   g | ]}d | qS )Zcol_rU   )r   r   rU   rU   rV   r     s     z1test_get_feature_names_pandas.<locals>.<listcomp>r2   rZ   r8   r`   r9   rY   r!  N)r   r   r  r   r+   r	   )r   r   rR   Zfeature_namesrU   rU   rV   test_get_feature_names_pandas  s
    
r  c                  C   s2   t dddgdddgg} t| }|dks.tdS )z/Get feature names return None for numpy arrays.rZ   r8   r2   r`   r9   rY   N)r<   r   r+   rA   )rR   r  rU   rU   rV   test_get_feature_names_numpy  s    r  znames, dtypesz['int', 'str']Zpizzaz['list', 'str']zint-strzlist-strc              	   C   sb   t d}|jddgddgddgg| d}td| d	}t jt|d
 t|} W 5 Q R X dS )zAGet feature names errors when the feature names have mixed dtypesr   rZ   r8   r`   r9   rY   r!  z]Feature names are only supported if all input features have string names, but your input has a.   as feature name / column name types. If you want feature names to be stored and validated, you must convert them all to strings, by using X.columns = X.columns.astype(str) for example. Otherwise you can remove feature / column names from your input data, or convert them all to a non-string data type.r   N)r   r   r   r  r  r   r   r+   )r  r   r   rR   r   rU   rU   rV   %test_get_feature_names_invalid_dtypes  s    

 
r  c                   @   s(   e Zd ZdddZdd Zd	ddZdS )
PassthroughTransformerNc                 C   s   | j |dd | S )NT)reset)Z_validate_datar  rU   rU   rV   r     s    zPassthroughTransformer.fitc                 C   s   |S r   rU   )r   rR   rU   rU   rV   	transform  s    z PassthroughTransformer.transformc                 C   s
   t | |S r   )r,   )r   Zinput_featuresrU   rU   rV   get_feature_names_out  s    z,PassthroughTransformer.get_feature_names_out)N)N)r   r   r   r   r  r  rU   rU   rU   rV   r    s   
r  c               	   C   s   t dddgg} t | }| }t|dddg ddg}tjtdd	 || W 5 Q R X |`	tjtd
d	 |  W 5 Q R X dS )z4Check behavior of check_feature_names_in for arrays.r/  r.  r0  Zx0x1x2Zx10z*input_features should have length equal tor   z Unable to generate feature namesN)
r<   r   r  r   r  r	   r   r   r   Zn_features_in_)rR   r  r  Zincorrect_len_namesrU   rU   rV   test_check_feature_names_in  s    r  c               	   C   s|   t d} dddg}| jdddgg|d}t |}| }t|dddg t jtd	d
 |dddg W 5 Q R X dS )z?Check behavior of check_feature_names_in for pandas dataframes.r   r   r   r   r/  r.  r0  r!  zinput_features is not equal tor   r  r  Zx3N)	r   r   r   r  r   r  r	   r   r   )r   r  r2  r  rU   rU   rV   "test_check_feature_names_in_pandas  s    

r  c                  C   sJ   t d} t| ddgdd}tddg}|j|jks<tt|| dS )zRegression test for gh-25145r   TFr   N)r   r   r   r<   r   r5   rA   r	   )r   r(  expectedrU   rU   rV   #test_boolean_series_remains_boolean  s
    r  )__doc__rT  rQ  r  tempfiler   	itertoolsr   operatorr   r   r   Znumpyr<   Zscipy.sparser  rL   Zsklearn.utils._testingr   r   r   r	   r
   r   r   Zsklearn.utilsr   r   r   r   r   Zsklearn.utils._mockingr   Zsklearn.utils.fixesr   Zsklearn.utils.estimator_checksr   Zsklearn.random_projectionr   Zsklearn.linear_modelr   Zsklearn.neighborsr   Zsklearn.ensembler   Zsklearn.svmr   Zsklearn.datasetsr   r   Zsklearn.utils.validationr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   Zsklearn.baser.   r%  Zsklearn.exceptionsr/   r0   r1   rW   markZparametrizer  r   Ztocsrr^   rd   rm   rz   r$  rO   rj   rv   r   r   r   r   r   rK   r   r   r   r@   rD   r   r   r   r   r   r   r   r   Zfixturer   r   r   r   r   r   r  r  r  r  r  r  r  r   r#  r&  r*  r3  r5  r6  r7  r:  r=  r?  rM   r   r   r  r   Z
dia_matrixr@  rB  rJ  rV  r}   r   rU  IntegralrE   r   r   r\  Z_psd_cases_validvalueskeysr_  Z_psd_cases_invalidra  rb  rf  rg  rh  rs  rv  r{  r  r  r  ZfloatingintegerZunsignedintegerr  r  r  r  r  r  r  r  r  r  r  r  r  rU   rU   rU   rV   <module>   s:  P*$
 (	
j

 
%	,


:*#$  
 











q





,


	
,*

