o
    &ѹg9                    @   s  d 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Zddl	Z
ddlZddlmZ ddlmZmZmZmZmZ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m Z m!Z! ddl"m#Z#m$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/ ddl0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z; ddl<m=Z=m>Z> ddl?m@Z@ ddlAmBZBmCZCmDZDmEZE ddlFmGZG ddlHmIZImJZJ ddlKmLZLmMZMmNZNmOZOmPZPmQZQ ddlRmSZS ddlTmUZUmVZV e ZWdeWjXjY_ZdeWj[jY_ZdZ\G dd deZ]G d d! d!e]Z^G d"d# d#ee^Z_G d$d% d%e_Z`G d&d' d'e`ZaG d(d) d)eeZbG d*d+ d+eZcG d,d- d-e`ZdG d.d/ d/eZed0d1 Zfd2d3 Zgd4d5 Zhd6d7 Zid8d9 Zjd:d; Zkd<d= Zld>d? Zmd@dA ZndBdC ZodDdE ZpdFdG ZqdHdI ZrdJdK ZsejtudLg dMdNdO ZvejtudPeSdQdR ZwdSdT ZxdUdV ZydWdX ZzdYdZ Z{d[d\ Z|ejtud]g d^d_d` Z}dadb Z~dcdd Zdedf Zejtudgddggdhdi Zejtudgddggdjdk Zdldm Zdndo Zejtudpe:e> e/ efe:e> e. efe:e> dqdr fe9g dsdr fgdtdu Zdvdw Zdxdy Zdzd{ Zd|d} Zd~d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd deZdd Zdd Zdd Zdd Zdd ee9de` fdec fgdfe9de` fddec fgdfe9de` fddec fgdfe9de` fdgdfe9ddeb fgdfe9ddeb fgdfe8deb fdeb fgdfe8ddeb fdgdfgg dD Zejtudedd Zdd Zdd Zdd Zdd Zdd Zdd ZejtudgddggddĄ ZejtudeMeLgddǄ ZddɄ Zdd˄ Zdd̈́ Zddτ Zddф Zddӄ ZejtuddeddՃgddׄ Zddل Zddۄ Zeddݍejtudddgdd Zeddݍdd Zdd Zeddݍdd Zejtudg ddd ZG dd deZejtudeeeJddh eddݍdd ZejtudeeeJddh eddݍdd Zejtudg ddd Zeddݍdd Zejtudddggeddݍdd Zeddݍdd Zeddݍdd Zeddݍejtud eCeBgdd ZdS (  z
Test the pipeline module.
    N)mkdtemp)config_context)BaseEstimatorClassifierMixinTransformerMixincloneis_classifieris_regressor)KMeans)	load_iris)PCATruncatedSVD)DummyRegressor)HistGradientBoostingClassifierRandomForestClassifierRandomTreesEmbedding)NotFittedErrorUnsetMetadataPassedError)CountVectorizer)SelectKBest	f_classif)SimpleImputer)LassoLinearRegressionLogisticRegression)accuracy_scorer2_score)train_test_split)LocalOutlierFactor)FeatureUnionPipelinemake_pipeline
make_union)FunctionTransformerStandardScaler)SVC)"ConsumingNoFitTransformTransformerConsumingTransformer	_Registrycheck_recorded_metadata)get_tags)COMPOSITE_METHODSMETHODS)MinimalClassifierMinimalRegressorMinimalTransformerassert_allcloseassert_array_almost_equalassert_array_equal)CSR_CONTAINERS)_check_feature_namescheck_is_fittedF)zthe pizza pizza beer copyrightzthe pizza burger beer copyrightz!the the pizza beer beer copyrightzthe burger beer beer copyrightzthe coke burger coke copyrightzthe coke burger burgerc                   @   s   e Zd ZdZdddZdS )NoFitz*Small class to test parameter dispatching.Nc                 C   s   || _ || _d S Nab)selfr9   r:    r<   _/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/sklearn/tests/test_pipeline.py__init__U      
zNoFit.__init__NN)__name__
__module____qualname____doc__r>   r<   r<   r<   r=   r6   R   s    r6   c                   @   s(   e Zd Zd	ddZd
ddZdd ZdS )NoTransNc                 C      | S r7   r<   r;   Xyr<   r<   r=   fit[      zNoTrans.fitFc                 C   s   | j | jdS )Nr8   r8   )r;   deepr<   r<   r=   
get_params^      zNoTrans.get_paramsc                 K   s   |d | _ | S Nr9   r9   )r;   paramsr<   r<   r=   
set_paramsa   s   
zNoTrans.set_paramsr7   F)rA   rB   rC   rJ   rM   rR   r<   r<   r<   r=   rE   Z   s    

rE   c                   @      e Zd Zdd ZdS )NoInvTransfc                 C      |S r7   r<   r;   rH   r<   r<   r=   	transformg   rK   zNoInvTransf.transformN)rA   rB   rC   rX   r<   r<   r<   r=   rU   f       rU   c                   @      e Zd Zdd Zdd ZdS )Transfc                 C   rV   r7   r<   rW   r<   r<   r=   rX   l   rK   zTransf.transformc                 C   rV   r7   r<   rW   r<   r<   r=   inverse_transformo   rK   zTransf.inverse_transformN)rA   rB   rC   rX   r\   r<   r<   r<   r=   r[   k   s    r[   c                   @      e Zd ZdddZdS )TransfFitParamsNc                 K   s
   || _ | S r7   )
fit_paramsr;   rH   rI   r_   r<   r<   r=   rJ   t      zTransfFitParams.fitr7   rA   rB   rC   rJ   r<   r<   r<   r=   r^   s       r^   c                   @   sV   e Zd ZdddZdd ZdddZd	d
 Zdd Zdd Ze Z	 Z
ZdddZdS )Mult   c                 C   s
   || _ d S r7   mult)r;   rg   r<   r<   r=   r>   z      
zMult.__init__c                 C      dS NTr<   r;   r<   r<   r=   __sklearn_is_fitted__}   rK   zMult.__sklearn_is_fitted__Nc                 C   rF   r7   r<   rG   r<   r<   r=   rJ      rK   zMult.fitc                 C   s   t || j S r7   npasarrayrg   rW   r<   r<   r=   rX         zMult.transformc                 C   s   t || j S r7   rm   rW   r<   r<   r=   r\      rp   zMult.inverse_transformc                 C   s   t || j jddS )Nre   Zaxis)rn   ro   rg   sumrW   r<   r<   r=   predict   s   zMult.predictc                 C   s
   t |S r7   rn   rr   rG   r<   r<   r=   score   rh   z
Mult.score)re   r7   )rA   rB   rC   r>   rl   rJ   rX   r\   rs   predict_probapredict_log_probadecision_functionru   r<   r<   r<   r=   rd   y   s    

rd   c                   @   s>   e Zd ZdZdd ZdddZdd Zdd	d
ZdddZdS )	FitParamTzMock classifierc                 C   s
   d| _ d S )NF
successfulrk   r<   r<   r=   r>      rh   zFitParamT.__init__Fc                 C   s   || _ d| _d S rj   )r{   fitted_r;   rH   rI   should_succeedr<   r<   r=   rJ      r?   zFitParamT.fitc                 C   s   | j S r7   rz   rW   r<   r<   r=   rs      s   zFitParamT.predictc                 C   s   | j |||d | |S )N)r~   )rJ   rs   r}   r<   r<   r=   fit_predict   s   
zFitParamT.fit_predictNc                 C   s   |d ur|| }t |S r7   rt   )r;   rH   rI   sample_weightr<   r<   r=   ru      s   
zFitParamT.scorerS   r@   )	rA   rB   rC   rD   r>   rJ   rs   r   ru   r<   r<   r<   r=   ry      s    

ry   c                   @   s   e Zd ZdZdd ZdS )DummyTransfz(Transformer which store the column meansc                 C   s   t j|dd| _t | _| S )Nr   rq   )rn   meanmeans_time
timestamp_rG   r<   r<   r=   rJ      s   
zDummyTransf.fitN)rA   rB   rC   rD   rJ   r<   r<   r<   r=   r      s    r   c                   @   s>   e Zd ZdZdd Zdd ZdddZdd	d
ZdddZdS )DummyEstimatorParamsz,Mock classifier that takes params on predictc                 C   ri   rj   r<   rk   r<   r<   r=   rl      rK   z*DummyEstimatorParams.__sklearn_is_fitted__c                 C   rF   r7   r<   rG   r<   r<   r=   rJ      rK   zDummyEstimatorParams.fitFc                 C   
   || _ | S r7   got_attributer;   rH   r   r<   r<   r=   rs      ra   zDummyEstimatorParams.predictc                 C   r   r7   r   r   r<   r<   r=   rv      ra   z"DummyEstimatorParams.predict_probac                 C   r   r7   r   r   r<   r<   r=   rw      ra   z&DummyEstimatorParams.predict_log_probaNrS   )	rA   rB   rC   rD   rl   rJ   rs   rv   rw   r<   r<   r<   r=   r      s    

r   c            	      C   s  t dg} tt | dggdg W d    n1 sw   Y  d}t dt fg} tjt|d | dggdg W d    n1 sHw   Y  t }t d|fg}|jddtdd d |d	|jd
dksnJ |j	dd |j
dks{J |jd u sJ t| t }tt}t d|fd|fg}|jd |u sJ |jd |u sJ d}t dt fd|fg} tjt|d | dggdg W d    n1 sw   Y  |j	dd |jdksJ t| td}tjt|d |j	dd W d    n	1 sw   Y  t|}|jd |jd usJ |jdd}|jdd}|jd
dD ]}|| q-|jd
dD ]}|| q<|d |d |d |d ||ks`J d S )N)re   re   re   zRLast step of Pipeline should implement fit or be the string 'passthrough'.*NoFit.*clfmatchsvcTrL   )svc__aZsvc__br   F皙?)r   anovaz<All intermediate steps should be transformers.*\bNoTrans\b.*t)Zsvc__Cz]Invalid parameter 'C' for estimator SelectKBest(). Valid parameters are: ['k', 'score_func'].)Zanova__Cr<   )r    pytestraises	TypeErrorrJ   r6   rE   rM   dictrR   r9   r:   reprr%   r   r   named_stepsCreescape
ValueErrorr   pop)	pipelinemsgr   pipefilter1Zpipe2rQ   Zparams2xr<   r<   r=    test_pipeline_invalid_parameters   sj   





r   c                  C   sh   t ddgg} tdt fdt ff}|j| d d ||  |jdd |j| d d ||  d S )Nre      transfr   rI   passthrough)r   )rn   arrayr    r[   ry   rJ   ru   rR   rH   r   r<   r<   r=   test_pipeline_init_tuple  s   
r   c                  C   sl   t j} t j}t }ttdd}td|fd|fg}|| | ||  |	|  |
|  || | d S )Nr   kr   Zlogistic)irisdatatargetr   r   r   r    rJ   rs   rv   rw   ru   )rH   rI   r   r   r   r<   r<   r=   test_pipeline_methods_anova*  s   


r   c                  C   s   t dt fdt fg} | jd d dd | d sJ | jd jd u s%J | jd jd u s/J t	d}t
jt|d | jd d dd W d    d S 1 sOw   Y  d S )Nr   r   T)rH   rI   clf__should_succeedz.fit() got an unexpected keyword argument 'bad'r   )Zclf__bad)r    r[   ry   rJ   rs   r   r9   r:   r   r   r   r   r   )r   r   r<   r<   r=   test_pipeline_fit_params9  s   
"r   c                  C   s   t ddgg} tdt fdt fg}|j| d d || dks$J |j| d ddks/J |j| d d ddks;J |j| t ddgdd	ksKJ d S )
Nre   r   r   r   r      )rI   r   r      )rn   r   r    r[   ry   rJ   ru   r   r<   r<   r=   %test_pipeline_sample_weight_supportedI  s   $r   c                  C   s   t ddgg} tdt fdt fg}|j| d d || dks$J |j| d ddks/J td}t	j
t|d	 |j| t ddgd W d    d S 1 sSw   Y  d S )
Nre   r   r   r   r   r   r   z:score() got an unexpected keyword argument 'sample_weight'r   )rn   r   r    r[   rd   rJ   ru   r   r   r   r   r   )rH   r   r   r<   r<   r=   'test_pipeline_sample_weight_unsupportedT  s   
"r   c                  C   s   t dt fg} td}tjt|d | jdd W d    n1 s%w   Y  tjt|d | jdd W d    n1 sBw   Y  td}tjt|d | jdd W d    d S 1 sew   Y  d S )	NclszInvalid parameter 'fake' for estimator Pipeline(steps=[('cls', LinearRegression())]). Valid parameters are: ['memory', 'steps', 'transform_input', 'verbose'].r   Znope)Zfake)Zfake__estimatorzInvalid parameter 'invalid_param' for estimator LinearRegression(). Valid parameters are: ['copy_X', 'fit_intercept', 'n_jobs', 'positive'].)Zcls__invalid_param)r    r   r   r   r   r   r   rR   )r   	error_msgr<   r<   r=   $test_pipeline_raise_set_params_errora  s    "r   c                  C   st   t j} t j}tddd}tdddd}td|fd|fg}|| | ||  ||  |	|  |
| | d S )	NTr   probabilityrandom_statefullmle
svd_solvern_componentswhitenpcar   )r   r   r   r%   r   r    rJ   rs   rv   rw   ru   )rH   rI   r   r   r   r<   r<   r=   test_pipeline_methods_pca_svm|  s   


r   c                  C   s   t j} tdddd}tdd}td|fd|fg}||  || j| jd fks,J |||  t	|| ||
|  d S )	Nr   r   Tr   )Znoveltyr   lofr   )r   r   r   r   r    rJ   score_samplesshapefit_transformr0   rX   )rH   r   r   r   r<   r<   r=   #test_pipeline_score_samples_pca_lof  s   

 r   c                  C   s   t dgdgg} t ddg}tt }|| | d}d}tjt|d}||  W d    n1 s6w   Y  t	|j
jtsDJ |t|j
jv sNJ d S )Nre   r   z<'LogisticRegression' object has no attribute 'score_samples'z+'Pipeline' has no attribute 'score_samples'r   )rn   r   r!   r   rJ   r   r   AttributeErrorr   
isinstancevalue	__cause__str)rH   rI   r   	inner_msg	outer_msg	exec_infor<   r<   r=   4test_score_samples_on_pipeline_without_score_samples  s   
r   c                  C   s   t j} t j}| jd }tt|}t }tdddd}t	dddd}||fD ]O}t
d|fd	|fg}|| | || }	|	j|fksFJ || }
|
j||fksTJ || }|j||fksbJ || }|j||fkspJ || | q'd S )
Nr   r   
randomizedT)r   r   r   Zovr)r   r   Zdecision_function_shape
preprocessr   )r   r   r   r   lenrn   uniquer$   r   r%   r    rJ   rs   rv   rw   rx   ru   )rH   rI   Z	n_samplesZ	n_classesscalerr   r   Zpreprocessingr   rs   ZprobaZ	log_probarx   r<   r<   r=   'test_pipeline_methods_preprocessing_svm  s(   




r   c                  C   sh   t  } tddd}t  }tddd}| tj}||}td|fd|fg}|tj}t|| d S )Nr   auto)r   Zn_initr   ZKmeans)r$   r
   r   r   r   r   r    r1   )r   kmZscaler_for_pipelineZkm_for_pipelineZscaledZseparate_predr   Zpipeline_predr<   r<   r=   test_fit_predict_on_pipeline  s   
r   c                  C   s   t  } tdd}td| fd|fg}d}d}tjt|d}t|d W d    n1 s-w   Y  t|jj	ts;J |t
|jj	v sEJ d S )	Nr   r   r   r   z)'Pipeline' has no attribute 'fit_predict'z+'PCA' object has no attribute 'fit_predict'r   r   )r$   r   r    r   r   r   getattrr   r   r   r   )r   r   r   r   r   r   r<   r<   r=   0test_fit_predict_on_pipeline_without_fit_predict  s   
r   c                  C   sf   t dt fdt fg} | jd d ddd | jd jd sJ | jd js'J d| jd jvs1J d S )Nr   r   T)rH   rI   Ztransf__should_get_thisr   Zshould_get_thisr~   )r    r^   ry   r   r   r_   r{   r   r<   r<   r=   -test_fit_predict_with_intermediate_fit_params  s   r   method_name)rs   rv   rw   c                 C   sN   t dt fdt fg}|d d  t|| }|d dd |jd js%J d S )Nr   r   T)rH   r   )r    r[   r   rJ   r   r   r   )r   r   methodr<   r<   r=   (test_predict_methods_with_predict_params  s
   
r   csr_containerc                 C   s  t j }||jdd8 }t j}tddd}tdd}td|fd|fg}||| |	|}|j
|j
d d	fks<J t|d d d d
f || t|d d d
f |||  td|fd|fg}| |}|||}t||  t|}	|jd d |	jd d usJ |jdd |||j
|j
d dfksJ tdt fd|fd|fg}|||}|j
|j
d dfksJ d}
tdt fdt fg}tjt|
d || W d    n1 sw   Y  td|fd|ff}||| d S )Nr   rq   r   )r   r   re   r   svdselectr   )Z	select__k   mockr   z>All estimators should implement fit and transform.*\bNoTrans\brX   Zno_transformr   )r   r   copyr   r   r   r   r   rJ   rX   r   r1   r   r2   raveltoarrayr   transformer_listrR   r[   rE   r   r   r   )r   rH   rI   r   r   fsX_transformedZX_spZX_sp_transformedZfs2r   r<   r<   r=   test_feature_union	  s:   


 "  r   c                  C   sh   t  } t }td| fd|fg}|jd | ksJ |jd |ks"J |jj| ks*J |jj|ks2J dS )z6Check the behaviour of `named_transformers` attribute.r   noinvtransfN)r[   rU   r   Znamed_transformersr   r   )r   r   r   r<   r<   r=   %test_feature_union_named_transformers9  s   r   c                  C   sH   t dd} t }t| |}t|j \}}|dksJ || |fks"J d S )Nr   r   )r   r   )r   r[   r"   zipr   )r   r   funamesZtransformersr<   r<   r=   test_make_unionF  s   

r   c                  C   s   t dd} t }t| |dd}|jt| |jksJ d|jks!J td}tjt	|d t| |ddd	d
 W d    d S 1 sCw   Y  d S )Nr   r   r   n_jobszEmake_union() got an unexpected keyword argument 'transformer_weights'r   
   re   )r   r[   Ztransformer_weights)
r   r[   r"   r   r   r   r   r   r   r   )r   r   r   r   r<   r<   r=   test_make_union_kwargsO  s   
"r   c                  C   sz   t j} tddd}td|fg}|| | }|| }|| }t|| t|| ||}||}t|| d S )Nr   r   )r   r   r   )	r   r   r   r    rJ   rX   r   r1   r\   )rH   r   r   X_transX_trans2ZX_trans3ZX_backZX_back2r<   r<   r=   test_pipeline_transform^  s   





r   c                  C   sL   t j} t j}t }td|fg}|| |}|| || }t|| d S )Nr   )	r   r   r   r[   r    r   rJ   rX   r1   )rH   rI   r   r   r   r   r<   r<   r=   test_pipeline_fit_transformq  s   r  z
start, end))r   re   )r   r   )re   r   )re   r   Nre   )re   Nr@   c                 C   s   t dt fdt fdt fgddd}|| | }t|t s J |j|j| | ks,J t|j t|j | | ks@J |jdd}|jdd}|d	= |d	= ||ksXJ d
}t	j
t|d || |d  W d    d S 1 stw   Y  d S )Ntransf1transf2r   Z123T)memoryverboseFr   stepsz*Pipeline slicing only supports a step of 1r   r   )r    r[   ry   r   r  listr   itemsrM   r   r   r   )startendr   Z
pipe_sliceZpipe_paramsZpipe_slice_paramsr   r<   r<   r=   test_pipeline_slice~  s*   "r  c                  C   s   t  } t }td| fd|fg}|d | ksJ |d | ks J |d |ks(J |d |ks0J tt |d  W d    n1 sDw   Y  tt |d  W d    d S 1 s^w   Y  d S )Nr   r   r   r   r   Zfoobar)r[   ry   r    r   r   
IndexErrorKeyError)r   r   r   r<   r<   r=   test_pipeline_index  s   

"r  c                  C   sT  t  } t  }td| fg}|jd | u sJ d|fg|_d|jvs#J |jd |u s,J d|fg|jks6J |jd| fgd d| fg|jksIJ |j|d d|fg|jksYJ |jdgd td}tjt	|d |
dggdg W d    n1 sw   Y  d	}tjt|d |dggdg W d    d S 1 sw   Y  d S )
Nr   mock2r  r   )Zjunkr<   zJLast step of Pipeline should implement fit or be the string 'passthrough'.r   re   z0This 'Pipeline' has no attribute 'fit_transform')r[   r    r   r  rR   r   r   r   r   r   rJ   r   r   )r  r  r   r   r<   r<   r=   test_set_pipeline_steps  s.   "r  c                  C   s   t  } tdd}td| fd|fg}d|jv sJ d|jvs J |jj| u s(J |jj|u s0J td| fd|fg}|jj| usBJ |jj|u sJJ d S )Nr   rf   r   rg   r  values)r[   rd   r    r   r   rg   r  )r   mult2r   r<   r<   r=   test_pipeline_named_steps  s   
r  r   c           	      C   s   t dgg}t dg}tdd}tdd}tdd}td|fd| fd|fd	|fg}||| g d
}dd |jD }||ksDJ d S )Nre   r   rf   r      m2badm3m5)r  r  r  r  c                 S   s   g | ]\}}|qS r<   r<   ).0name_r<   r<   r=   
<listcomp>      z9test_pipeline_correctly_adjusts_steps.<locals>.<listcomp>)rn   r   rd   r    rJ   r  )	r   rH   rI   r  mult3mult5r   Zexpected_namesZactual_namesr<   r<   r=   %test_pipeline_correctly_adjusts_steps  s   


r#  c                    s   t dgg}t dg}tdd tddtdd fdd}| }d}t|gg||| t|g||| t|||gg |j| d	 d
}t|gg||| t|g||| t|||gg |j	dd|j
 | d ddd dd	ksJ |j| d d}t|gg||| t|g||| t|||gg g d}|D ]	}t||| q|j d d
}t|gg||| t|g||| t|||gg | }|j| d d}t|gg|||| t|gg||| t|||gg d}d}	tjt|	d}
t|d W d    n	1 s4w   Y  t|
jjtsCJ |t|
jjv sNJ d
}td fd| fdfg}t|gg||| t|g||| t|||gg d S )Nre   r   rf   r   r  c                      s   t d fdfdfgS )Nr  r  last)r    r<   r  r!  r"  r<   r=   make  s   z0test_set_pipeline_step_passthrough.<locals>.make   r  r   Tr   F)	r  r  r  r$  r  Zm2__multZ
last__multtransform_inputr  r  )rv   rw   rx   rX   ru   )r$     z''str' object has no attribute 'predict'z*This 'Pipeline' has no attribute 'predict'r   rs   r  r  r$  )rn   r   rd   r2   r   rJ   rs   r\   rR   rM   r  r   rX   r   r   r   r   r   r   r   r    )r   rH   rI   r&  r   expZother_methodsr   r   r   r   r<   r%  r=   "test_set_pipeline_step_passthrough  st   



r-  c                  C   s   t td} | j | j | j t t } t| drJ | j | j t d} | jd dks.J t| dr5J | j | j t t t } t| drIJ | j t| drSJ t t t } t| draJ | j t| drkJ d S )Nr  rs   r   r   r   r   r\   )	r!   rd   rs   rX   r\   r[   hasattrr  rU   )r   r<   r<   r=   test_pipeline_ducktypingD  s*   
r0  c                  C   s   t  } t  }t| |}t|tsJ |jd d dksJ |jd d dks(J t| |t }t|ts6J |jd d dksAJ |jd d dksLJ |jd d dksWJ d S )Nr   ztransf-1re   ztransf-2r   Z	fitparamt)r[   r!   r   r    r  ry   )t1t2r   r<   r<   r=   test_make_pipeline`  s   
r3  zpipeline, check_estimator_typec                 C   s   t | jd u S r7   )r*   Zestimator_typeestr<   r<   r=   <lambda>v  s    r6  c                 C   s
   | j d u S r7   )Z_estimator_typer4  r<   r<   r=   r6  x     
 c                 C   s   t |  || s
J dS )zCheck that the estimator type returned by the pipeline is correct.

    Non-regression test as part of:
    https://github.com/scikit-learn/scikit-learn/issues/30197
    N)r   )r   Zcheck_estimator_typer<   r<   r=   test_pipeline_estimator_typeo  s   r8  c                  C   s,   t g d} t }| }|  |ksJ dS )zCheck that we propagate properly the tags in a Pipeline.

    Non-regression test as part of:
    https://github.com/scikit-learn/scikit-learn/issues/30197
    r  N)r    r   __sklearn_tags__)Zempty_pipelinebeZexpected_tagsr<   r<   r=   %test_sklearn_tags_with_empty_pipeline  s   
r;  c                  C   sV  t j} t j}tdddd}tdd}td|fd|fgdd	id
}|| | || }td|fd|fgdd	id
}|| |}tdt	 fd|fd|fgdd	id
}|| |}t
|d d d df d	||   t|d d df || |  t
|d d d df d	||   t|d d df || |  |j| jd dfksJ d S )Nr   r   r   r   r   r   re   r   r   r   r   r   r   r      )r   r   r   r   r   r   rJ   rX   r   r[   r1   r2   r   r   )rH   rI   r   r   r   r   ZX_fit_transformedZX_fit_transformed_wo_methodr<   r<   r=   test_feature_union_weights  s.   

$"$"r>  c                  C   s  t } tdtddfdtddfg}tdtddfdtddfgdd}tdtddfdtddfgdd}||  || }|jd t| ksKJ ||  || }|j|jks]J t| |  |	| }t| |  || }t| |  d S )	Nwordswordanalyzercharscharr   r   r   )
JUNK_FOOD_DOCSr   r   rJ   rX   r   r   r2   r   r   )rH   r   Zfs_parallelZfs_parallel2r   ZX_transformed_parallelZX_transformed_parallel2r<   r<   r=   test_feature_union_parallel  s:   





rF  c                  C   s   t dd} t ddd}td|fd| fg}|t | }|D ]}d|v s,d	|v s,J q t|d
ks5J tdt fgdgg}td}t	j
t|d |  W d    d S 1 s^w   Y  d S )Nr@  rA  Zchar_wb)r   r   )rB  Zngram_rangerC  r?  Zchars__Zwords__#   Ztr1re   zDTransformer tr1 (type Transf) does not provide get_feature_names_outr   )r   r   rJ   rE  get_feature_names_outr   r[   r   r   r   r   r   )Z	word_vectZ	char_vectftfeature_namesZfeatr   r<   r<   r=    test_feature_union_feature_names  s    


"rK  c                  C   s   t j} t j}ttddt }|| | tt	 t
|d W d    n1 s*w   Y  ttddtdd}tt	 t
|d W d    n1 sOw   Y  || | t|jt| d S )Nre   r   classes_r   r   )r   r   r   r!   r   r   rJ   r   r   r   r   r   r2   rL  rn   r   )rH   rI   regr   r<   r<   r=   test_classes_property  s   rO  c                  C   s4  t d} t d}t d}dd |_dd | _dd |_td| fd	|fg}tddgg|td
gg tddg|  d|fg|_tdgg|td
gg tdg|  |jd|fgd tdgg|td
gg tdg|  |j|d tdgg|td
gg tdg|  d S )Nr   r   r  c                 S      dgS Nx3r<   input_featuresr<   r<   r=   r6  	      z.test_set_feature_union_steps.<locals>.<lambda>c                 S   rP  Nx2r<   rS  r<   r<   r=   r6  
  rU  c                 S   rP  )NZx5r<   rS  r<   r<   r=   r6    rU  r  r  re   m2__x2m3__x3r  Zm5__x5r   )r   Zmock__x3r  Zmock__x5)	rd   rH  r   r2   rX   rn   ro   r   rR   )r  r!  r"  rI  r<   r<   r=   test_set_feature_union_steps  s$   


 rZ  c                  C   s  t d} t d}dd | _dd |_tdgg}td| fd|fg}tddgg||| tddgg|| td	d
g|  |j	dd tdgg||| tdgg|| td
g|  |j	dd tg g||| tg g|| tg |  |j	|d tdgg||| tdd|fg}tdgg||| tdgg|| td
g|  d S )Nr   r   c                 S   rP  rV  r<   rS  r<   r<   r=   r6  %  rU  z2test_set_feature_union_step_drop.<locals>.<lambda>c                 S   rP  rQ  r<   rS  r<   r<   r=   r6  &  rU  re   r  r  rX  rY  dropr*  r(  )r  r[  )
rd   rH  rn   ro   r   r2   rJ   rX   r   rR   )r  r!  rH   rI  r<   r<   r=    test_set_feature_union_step_drop!  s.   

r\  c                  C   s  t d} t d}dd | _dd |_tdgg}td| fd|fg}tddgg||| tddgg|| td	d
g|  |j	dd tddgg||| tddgg|| tdd
g|dg |j	dd tddgg||| tddgg|| tddg|dg |j	|d tddgg||| tddgg|| tdd
g|dg tdd|fg}tddgg||| tddgg|| tdd
g|dg t
j}|jd }tdddd}tdd|fg}t||||ddd|f  t|||ddd|f  tg d|g d |j	dd |||}t|t||g ||}t|t||g tg d|g d |j	|d t||||dd| df  t|||dd| df  tg d|g d tdd|fgddid}t|d |||ddd|f  t|d ||ddd|f  tg d|g d dS )z@Check the behaviour of setting a transformer to `"passthrough"`.r   r   c                 S   rP  rV  r<   rS  r<   r<   r=   r6  K  rU  z4test_set_feature_union_passthrough.<locals>.<lambda>c                 S   rP  rQ  r<   rS  r<   r<   r=   r6  L  rU  re   r  r  rX  rY  r   r*  Z
m2__myfeatZmyfeatr(  Z
m3__myfeat)r  r   r   r   r<  r.  r   N)passthrough__f0passthrough__f1passthrough__f2passthrough__f3	pca__pca0	pca__pca1)Zf0f1f2Zf3)r   )r]  r^  r_  r`  pca__f0pca__f1pca__f2pca__f3)r   )Zpassthrough__pca0Zpassthrough__pca1re  rf  rg  rh  r   )rd   rH  rn   ro   r   r2   rJ   rX   r   rR   r   r   r   r   Zhstack)r  r!  rH   rI  columnsr   ZX_ftr<   r<   r=   "test_set_feature_union_passthroughD  s~   


& 

("
*$rj  c                  C   sD   t j} tdddd}td|fdg}||  tg d|  dS )	zDCheck feature_names_out for verbose_feature_names_out=True (default)r   r   r   r<  r   r.  )ra  rb  Zpassthrough__x0Zpassthrough__x1Zpassthrough__x2Zpassthrough__x3Nr   r   r   r   rJ   r2   rH  rH   r   rI  r<   r<   r=   9test_feature_union_passthrough_get_feature_names_out_true  s   
rm  c                  C   sH   t j} tdddd}td|fdgdd}||  tg d	|  d
S )z;Check feature_names_out for verbose_feature_names_out=Falser   r   r   r<  r   r.  FZverbose_feature_names_out)Zpca0Zpca1Zx0x1rW  rR  Nrk  rl  r<   r<   r=   :test_feature_union_passthrough_get_feature_names_out_false  s   
rp  c                  C   s   t d} | jddgddggddgd}tdd	 d
d	 d}tdt fd|fgdd}|| td}t j	t
|d |  W d   dS 1 sLw   Y  dS )zFCheck get_feature_names_out and non-verbose names and colliding names.pandasre   r   r   r9   r:   ri  c                 S   s
   | dg S rO   r<   )rH   r<   r<   r=   r6    r7  zStest_feature_union_passthrough_get_feature_names_out_false_errors.<locals>.<lambda>c                 S   s   t dgS rO   )rn   ro   )r;   r  r<   r<   r=   r6    s    )feature_names_outr1  r2  Frn  zvOutput feature names: ['a'] are not unique. Please set verbose_feature_names_out=True to add prefixes to feature namesr   N)r   importorskip	DataFramer#   r   r$   rJ   r   r   r   r   rH  )pdrH   Zselect_aunionr   r<   r<   r=   Atest_feature_union_passthrough_get_feature_names_out_false_errors  s    


"rx  c                  C   s   t d} | jttdgdd tdD d}tddgdd	}|| td
}t j	t
|d |  W d   dS 1 sAw   Y  dS )zLCheck get_feature_names_out with non-verbose names and >= 5 colliding names.rq  r   c                 S      g | ]}d | qS )fr<   )r  ir<   r<   r=   r        zdtest_feature_union_passthrough_get_feature_names_out_false_errors_overlap_over_5.<locals>.<listcomp>rr  )r1  r   )r2  r   Frn  zOutput feature names: ['f0', 'f1', 'f2', 'f3', 'f4', ...] are not unique. Please set verbose_feature_names_out=True to add prefixes to feature namesr   N)r   rt  ru  r  ranger   rJ   r   r   r   r   rH  )rv  rH   rw  r   r<   r<   r=   Ptest_feature_union_passthrough_get_feature_names_out_false_errors_overlap_over_5  s   
&

"r~  c               
   C   s>  d} d}d}dt dfdt dfg}dt dfdt dfg}td	ftd
ffD ]\}}dt df|t dfg}|| f||f||| ffD ]\}}	tjt|	d |di ||idggdg W d    n1 siw   Y  |di |dt dfgi}
t|
|| tjt|	d |
dggdg W d    n1 sw   Y  tjt|	d |
dggdg W d    n1 sw   Y  |di |dt dfgi}
|
j	di ||i tjt|	d |
dggdg W d    n1 sw   Y  tjt|	d |
dggdg W d    qC1 sw   Y  qCq&d S )Nz3Estimator names must not contain __: got \['a__q'\]z+Names provided are not unique: \['a', 'a'\]z=Estimator names conflict with constructor arguments: \['%s'\]Za__qr   r:   r   r9   r  r   r   re   r<   )
rd   r    r   r   r   r   rJ   setattrr   rR   )Zerror_message_1Zerror_message_2Zerror_message_3Z
bad_steps1Z
bad_steps2r   paramZ
bad_steps3Z	bad_stepsmessager5  r<   r<   r=   test_step_name_validation  s@   
" r  c                  C   sD   t dt dt fgfg} | jdt d | jdt fgdd d S )Nr9   r:   gMbP?)Za__b__alphaZa__br  )Za__stepsZa__b__C)r    r   rR   r   r   )	estimatorr<   r<   r=   test_set_params_nested_pipeline2  s   r  c               	   C   s  t j} t j}t }z0tj|dd}tddd}t }tdt	|fd|fg}td|fd|fg|d}|
| | |
| | |jd j}t|| ||  t|| ||  t|| ||  t|| ||| | t|jd j|jd j t|d	rJ |
| | t|| ||  t|| ||  t|| ||  t|| ||| | t|jd j|jd j ||jd jksJ tddd}	t }
td
|
fd|	fg|d}|
| | t|| ||  t|| ||  t|| ||  t|| ||| | t|jd j|jd
 j ||jd
 jks3J W t| d S t| w )Nr   locationr  Tr   r   r   r   r  r   transf_2)r   r   r   r   joblibMemoryr%   r   r    r   rJ   r   r   r2   rs   rv   rw   ru   r   r/  shutilrmtree)rH   rI   cachedirr  r   r   r   Zcached_pipetsZclf_2r  Zcached_pipe_2r<   r<   r=   test_pipeline_memory8  s^   

r  c                  C   sn   t  } tj| dd}tt t |d}|j|u sJ tt t }|jd u s(J t|dks0J t	|  d S )Nr   r  r  r   )
r   r  r  r!   r   r%   r  r   r  r  )r  r  r   r<   r<   r=   test_make_pipeline_memoryw  s   r  c                   @   s*   e Zd ZdddZdddZdddZdS )	FeatureNameSaverNc                 C   s   t | |dd | S )NT)reset)r4   rG   r<   r<   r=   rJ     s   zFeatureNameSaver.fitc                 C   rV   r7   r<   rG   r<   r<   r=   rX     rK   zFeatureNameSaver.transformc                 C   rV   r7   r<   )r;   rT  r<   r<   r=   rH    rK   z&FeatureNameSaver.get_feature_names_outr7   )rA   rB   rC   rJ   rX   rH  r<   r<   r<   r=   r    s    

r  c                  C   sR   t dt fddt fgd} t }| |j|j t| dd |j	|j	 dS )z5Check pipeline.get_feature_names_out with passthroughr   passr   r   r  Nr   )
r    r  r   r   rJ   r   r   r2   rH  rJ  )r   r   r<   r<   r=   test_features_names_passthrough  s   r  c                  C   sn   t dt fdt fgd} dd tD }| t| t| dd  g d t| dd d	g d dS )
z5Check pipeline.get_feature_names_out with vectorizersZvectr   r  c                 S   s   g | ]}d |v qS )pizzar<   )r  r   r<   r<   r=   r    r   z7test_feature_names_count_vectorizer.<locals>.<listcomp>Nr   )ZbeerZburgerZcoke	copyrightr  ZtheZnonsense_is_ignored)r    r   r   rE  rJ   r2   rH  )r   rI   r<   r<   r=   #test_feature_names_count_vectorizer  s   r  c                  C   sh   t dt fgd} t }| |j|j d}tjt|d | 	  W d   dS 1 s-w   Y  dS )zZCheck that error is raised when a transformer does not define
    `get_feature_names_out`.Znotransr  z&does not provide get_feature_names_outr   N)
r    rE   r   rJ   r   r   r   r   r   rH  )r   r   r   r<   r<   r=   8test_pipeline_feature_names_out_error_without_definition  s   
"r  c                  C   s^   t t } tjtdd | jdgdggddgddgd W d    d S 1 s(w   Y  d S )Nz8Pipeline.fit does not accept the sample_weight parameterr   r   re   r   )r!   r   r   r   r   rJ   )r   r<   r<   r=   test_pipeline_param_error  s   
""r  c                 c   sR    | ]$\\}}}t ||r|d kr t |dr t|jd d ts|||fV  qdS )r   r  r   re   N)r/  r   r  ry   )r  r5  patternr   r<   r<   r=   	<genexpr>  s    
489:
r  r   r   zv\[Pipeline\].*\(step 1 of 2\) Processing transf.* total=.*\n\[Pipeline\].*\(step 2 of 2\) Processing clf.* total=.*\n$)noopNz\[Pipeline\].*\(step 1 of 3\) Processing transf.* total=.*\n\[Pipeline\].*\(step 2 of 3\) Processing noop.* total=.*\n\[Pipeline\].*\(step 3 of 3\) Processing clf.* total=.*\n$)r  r   )r   N)r   Nrg   zw\[Pipeline\].*\(step 1 of 2\) Processing transf.* total=.*\n\[Pipeline\].*\(step 2 of 2\) Processing mult.* total=.*\n$)r   r   mult1r  z\[FeatureUnion\].*\(step 1 of 2\) Processing mult1.* total=.*\n\[FeatureUnion\].*\(step 2 of 2\) Processing mult2.* total=.*\n$)r  r[  )r!  r[  z@\[FeatureUnion\].*\(step 1 of 1\) Processing mult2.* total=.*\n$)rJ   r   r   zest, pattern, methodc                 C   s~   t | |}g dg dg}dgdgg}| jdd ||| | jr'J d| jdd ||| t|| js=J d S )	Nre   r   r   )r   r  r+  r=  r   F)r  zGot output for verbose=FalseT)r   rR   Z
readouterroutr   r   )r5  r   r  ZcapsysfuncrH   rI   r<   r<   r=   test_verbose  s   


r  c                  C   s   ddgddgddgg} g d}t  }t }t||}t|dr!J || | |j|j  kr4dks7J  J t  }t }t||}|| | |j|j  krUdksXJ  J t|dr_J d S 	Nre   r   r   r   r  r+  r   re   r   n_features_in_)r$   r   r!   r/  rJ   r  )rH   rI   ssZgbdtr   r<   r<   r=   test_n_features_in_pipeline  s   
 
 r  c                  C   s   ddgddgddgg} g d}t  }t|}t|drJ || | |j|j  kr0dks3J  J t  }t|}|| | |j|j  krMdksPJ  J d S r  )r$   r"   r/  rJ   r  )rH   rI   r  r   r<   r<   r=    test_n_features_in_feature_union(  s    $r  c                  C   s   G dd dt t} tjtj}}td|  fd|  fg}tt |	|| W d    n1 s2w   Y  tt |
|| W d    n1 sMw   Y  |j	||dd |j
||dd d S )Nc                   @   s    e Zd ZdddZdddZdS )z7test_feature_union_fit_params.<locals>.DummyTransformerNc                 [      |ddikrt | S )Nr9   r   r   r`   r<   r<   r=   rJ   ?     z;test_feature_union_fit_params.<locals>.DummyTransformer.fitc                 S   rV   r7   r<   rG   r<   r<   r=   rX   D  rK   zAtest_feature_union_fit_params.<locals>.DummyTransformer.transformr7   rA   rB   rC   rJ   rX   r<   r<   r<   r=   DummyTransformer>  s    
r  Zdummy0Zdummy1r   rP   )r   r   r   r   r   r   r   r   r   rJ   r   r  rH   rI   r   r<   r<   r=   test_feature_union_fit_params<  s   	r  c                  C   s   G dd dt } tjtj}}td|  fd|  fg}tt |j||dd W d    n1 s3w   Y  |j||dd d S )Nc                   @   r]   )zMtest_feature_union_fit_params_without_fit_transform.<locals>.DummyTransformerNc                 [   r  )Nmetadatare   r  r`   r<   r<   r=   rJ   X  r  zQtest_feature_union_fit_params_without_fit_transform.<locals>.DummyTransformer.fitr7   rb   r<   r<   r<   r=   r  W  rc   r  Znofittransform0Znofittransform1r   )r  re   )	r&   r   r   r   r   r   r   r   r   r  r<   r<   r=   3test_feature_union_fit_params_without_fit_transformS  s   r  c                  C   sp   t j t j } }tjjddg| jddgdt	}tj
| |< tt t }|| || |dks6J d S )Nre   r   r   g?)pg?)r   r   r   r   rn   randomchoicer   Zastypeboolnanr!   r   r   rJ   ru   )rH   rI   maskr   r<   r<   r=   %test_pipeline_missing_values_leniencyk  s
   "
 r  c                  C   s   ddgddgddgg} g d}dt  fg}d	di}d
}t||d}tjt|d || | W d    d S 1 s:w   Y  d S )Nre   r   r   r   r  r+  r  r   transformerzZAttempting to weight transformer "transformer", but it is not present in transformer_list.r   r   )r[   r   r   r   r   rJ   )rH   rI   r   weightsZexpected_msgrw  r<   r<   r=   3test_feature_union_warns_unknown_transformer_weightu  s   "r  c                 C   s    t | t }| jjrJ d S r7   )r!   r%   r9  Z
input_tagspairwise)r   r   r<   r<   r=   test_pipeline_get_tags_none  s   r  	Predictorc                 C   s   t jd}|ddt dgd dgd  }}tdt fd|  fg}||| ||}t	|rLt
|d |||tt||ksJJ d S t||  |||tt||kscJ d S )	Nr      r   r  re      r  Z	predictor)rn   r  RandomStateZrandnr   r    r/   rJ   rs   r   r2   ru   r   Zapproxr   r0   r   r   )r  rngrH   rI   modelZy_predr<   r<   r=   1test_search_cv_using_minimal_compatible_estimator  s   (

$$r  c                  C   sn   G dd dt } td|  fg}tt t| W d    n1 s$w   Y  |tjtj	 t| d S )Nc                   @   rT   )z0test_pipeline_check_if_fitted.<locals>.Estimatorc                 S   
   d| _ | S rj   r|   rG   r<   r<   r=   rJ     ra   z4test_pipeline_check_if_fitted.<locals>.Estimator.fitNrb   r<   r<   r<   r=   	Estimator  rY   r  r   )
r   r    r   r   r   r5   rJ   r   r   r   )r  r   r<   r<   r=   test_pipeline_check_if_fitted  s   
r  c                  C   s   ddgddgddgg} g d}t dt fg}tt t| W d	   n1 s+w   Y  || | t| t d
g}t| t dt fd
g}tt t| W d	   n1 s`w   Y  || | t| d	S )z1Check __sklearn_is_fitted__ is defined correctly.re   r   r   r   r  r+  r  r   Nr  )r   r/   r   r   r   r5   rJ   )rH   rI   rw  r<   r<   r=   "test_feature_union_check_if_fitted  s    


r  c                  C   s`   t jt j} }G dd dt}t| t }|| | t j}||}t|dd |D  dS )zRCheck that pipeline passes names through.

    Non-regresion test for #21349.
    c                       s   e Zd Zd fdd	Z  ZS )zYtest_pipeline_get_feature_names_out_passes_names_through.<locals>.AddPrefixStandardScalarNc                    s&   t  j|d}tjdd |D tdS )NrS  c                 S   ry  Z
my_prefix_r<   r  r  r<   r<   r=   r    r|  ztest_pipeline_get_feature_names_out_passes_names_through.<locals>.AddPrefixStandardScalar.get_feature_names_out.<locals>.<listcomp>)Zdtype)superrH  rn   ro   object)r;   rT  r   	__class__r<   r=   rH    s   zotest_pipeline_get_feature_names_out_passes_names_through.<locals>.AddPrefixStandardScalar.get_feature_names_outr7   )rA   rB   rC   rH  __classcell__r<   r<   r  r=   AddPrefixStandardScalar  s    r  c                 S   ry  r  r<   r  r<   r<   r=   r    r|  zLtest_pipeline_get_feature_names_out_passes_names_through.<locals>.<listcomp>N)	r   r   r   r$   r!   rJ   rJ  rH  r2   )rH   rI   r  r   Zinput_namesrs  r<   r<   r=   8test_pipeline_get_feature_names_out_passes_names_through  s   
r  c                  C   sh   t d tddd\} }tt t }|jdd || | |dd  }|d j	}t
|| dS )z.Test pipeline's set_output with feature names.rq  TZas_frameZ
return_X_yrX   Nr   )r   rt  r   r!   r$   r   
set_outputrJ   rH  feature_names_in_r2   )rH   rI   r   r  Zlog_reg_feature_namesr<   r<   r=   $test_pipeline_set_output_integration  s   

r  c                  C   s   t d} tddd\}}t|dd\}}tdt fdt fg}|jdd || |	|}t
|| js9J t|j|  t|j|j d	S )
z'Test feature union with set_output API.rq  Tr  r   rM  scalarr   r  N)r   rt  r   r   r   r$   r   r  rJ   rX   r   ru  r2   ri  rH  index)rv  rH   r  ZX_trainZX_testrw  r   r<   r<   r=   test_feature_union_set_output  s   


r  c                  C   sh   t  } t }td| fd|fddg}|d | u sJ |d |u s"J |d dks*J |d dks2J d	S )
z8Check FeatureUnion.__getitem__ returns expected results.r  r   r  )drop_mer[  r  r   r  r[  N)r$   r   r   )r  r   rw  r<   r<   r=   test_feature_union_getitem  s   r  keyr   c                 C   sX   t dt fdt fg}d}tjt|d ||   W d   dS 1 s%w   Y  dS )z5Raise error when __getitem__ gets a non-string input.r  r   zOnly string keys are supportedr   N)r   r$   r   r   r   r  )r  rw  r   r<   r<   r=    test_feature_union_getitem_error  s
   
"r  c                  C   s   t d tddd\} }t }||  td|fg}t|ds#J t| j|j	 t|j	|j	 tdg}||  t|dsBJ t| j|j	 | 
 }tdg}|| t|dr^J dS )zxEnsure feature union has `.feature_names_in_` attribute if `X` has a
    `columns` attribute.

    Test for #24754.
    rq  Tr  Zscaler  r  N)r   rt  r   r$   rJ   r   r/  r2   ri  r  Zto_numpy)rH   r  r   rw  ZX_arrayr<   r<   r=   $test_feature_union_feature_names_in_   s    





r  c                  C   s$  t jdjdd} tdtddfg}|| } tjt	dd |
  W d    n1 s/w   Y  tjt	d	d |j
| | d
 W d    n1 sMw   Y  tjdd td |
|  W d    n1 smw   Y  tjtdd |j
| d W d    d S 1 sw   Y  d S )Nr   )r   r  )sizer   r   )r   z$Missing required positional argumentr   z$Cannot use both X and Xt. Use X only)rH   XtT)recorderrorzXt was renamed X in version 1.5)r  )rn   r  r  normalr    r   r   r   r   r   r\   warningscatch_warningssimplefilterwarnsFutureWarningr   r<   r<   r=   .test_pipeline_inverse_transform_Xt_deprecationA  s    


"r  T)Zenable_metadata_routingr   rJ   r   c                    s   dd   fdd}dd }t ddgd	d
gg}t ddg}t ddgg}t ddgg}t ddgg}| \}}	}
}}|j|||||d ||	ddg||d ||
ddg ||ddg|d |d ||| d|d	 |d dS )zLTest that with transform_input, data is correctly transformed for each step.c                 S   s   t | dj||dj||dS )z$Get a transformer with requests set.registryr   r  N)r'   set_fit_requestset_transform_request)r  r   r  r<   r<   r=   get_transformer]  s   z6test_transform_input_pipeline.<locals>.get_transformerc               	      sf   t  t  t  t  f\} }}}t | ddd |ddd |ddd |ddddgd}|| |||fS )zGet a pipeline and corresponding registries.

        The pipeline has 4 steps, with different request values set to test different
        cases. One is aliased.
        Tr  Fother_weightsr   r)  N)r(   r!   )
registry_1
registry_2
registry_3
registry_4r   r  r<   r=   get_pipelinee  s   z3test_transform_input_pipeline.<locals>.get_pipelinec                 [   s6   | sJ | D ]}|D ]}t |f||d| q
qdS )zACheck that the right metadata was recorded for the given methods.)r   parentN)r)   )r  methodsr  r  r   r<   r<   r=   check_metadataz  s   z5test_transform_input_pipeline.<locals>.check_metadatare   r   r   r   r   r'  (   d      )r   r  r  rJ   rX   r  r  N)rn   r   rJ   split)r   r  r  rH   rI   r   r  r  r   r  r  r  r  r<   r  r=   test_transform_input_pipelineX  s@   
r   c                  C   s   G dd dt t} G dd dtt}tddgg}tddg}tddgg}tddg}td|  fd	| jd
d
dfgdgd}|j||||d dS )z;Test that the right transformed values are passed to `fit`.c                   @   rZ   )z>test_transform_input_explicit_value_check.<locals>.Transformerc                 S   r  rj   r  rG   r<   r<   r=   rJ     ra   zBtest_transform_input_explicit_value_check.<locals>.Transformer.fitc                 S      |d S r  r<   rW   r<   r<   r=   rX        zHtest_transform_input_explicit_value_check.<locals>.Transformer.transformNr  r<   r<   r<   r=   Transformer      r  c                   @   r]   )z<test_transform_input_explicit_value_check.<locals>.EstimatorNc                 S   sX   t |tddgg t |tddg t |tddgg t |tddg | S )Nre   r   r   r   )r2   rn   r   r;   rH   rI   X_valy_valr<   r<   r=   rJ     s
   z@test_transform_input_explicit_value_check.<locals>.Estimator.fitr@   rb   r<   r<   r<   r=   r    rc   r  r   re   r   r  r  Tr  r  r  r  N)r   r   r   rn   r   r    r  rJ   )r  r  rH   rI   r  r  r   r<   r<   r=   )test_transform_input_explicit_value_check  s   r	  c                  C   st   t ddgddgg} t ddg}d}tjt|d tt dgd	| | W d
   d
S 1 s3w   Y  d
S )z<Make sure the right error is raised if slep6 is not enabled.re   r   r   r   r   z;The `transform_input` parameter can only be set if metadatar   Zblahr  N)rn   r   r   r   r   r!   r   rJ   )rH   rI   r   r<   r<   r=   test_transform_input_no_slep6  s   "r
  c            	      C   s   G dd dt t} G dd dtt}tddgg}tddg}tddgg}tddg}tdd	gg}tddg}td
| fd|  jdddfgdgd}|j||||f||fd dS )zHTest that if metadata is a tuple of arrays, both arrays are transformed.c                   @   r]   )z-test_transform_tuple_input.<locals>.EstimatorNc                 S   s   t |tsJ t |tsJ t|d tddgg t|d tddg t|d tddgg t|d tddg d| _| S )Nr   r   r   re         T)r   tupler2   rn   r   r|   r  r<   r<   r=   rJ     s   z1test_transform_tuple_input.<locals>.Estimator.fitr@   rb   r<   r<   r<   r=   r    rc   r  c                   @   rZ   )z/test_transform_tuple_input.<locals>.Transformerc                 S   r  rj   r  rG   r<   r<   r=   rJ     ra   z3test_transform_tuple_input.<locals>.Transformer.fitc                 S   r  r  r<   rW   r<   r<   r=   rX     r  z9test_transform_tuple_input.<locals>.Transformer.transformNr  r<   r<   r<   r=   r    r  r  re   r   r   r   r  r  r  Tr  r  r  N)r   r   r   rn   r   r    r  rJ   )	r  r  rH   rI   ZX_val0Zy_val0ZX_val1Zy_val1r   r<   r<   r=   test_transform_tuple_input  s   r  )rs   rv   rw   rx   ru   r   rX   r\   c                 C   sf   G dd dt }td| fg}tjtdd t|| dgg W d    d S 1 s,w   Y  d S )Nc                   @   sX   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )z:test_pipeline_warns_not_fitted.<locals>.StatelessEstimatorzStateless estimator that doesn't check if it's fitted.

        Stateless estimators that don't require fit, should properly set the
        `requires_fit` flag and implement a `__sklearn_check_is_fitted__` returning
        `True`.
        c                 S   rF   r7   r<   rG   r<   r<   r=   rJ     rK   z>test_pipeline_warns_not_fitted.<locals>.StatelessEstimator.fitc                 S   rV   r7   r<   rW   r<   r<   r=   rX     rK   zDtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.transformc                 S      t t|S r7   rn   Zonesr   rW   r<   r<   r=   rs     rN   zBtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.predictc                 S   r  r7   r  rW   r<   r<   r=   rv     rN   zHtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.predict_probac                 S   r  r7   rn   zerosr   rW   r<   r<   r=   rw   !  rN   zLtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.predict_log_probac                 S   r  r7   r  rW   r<   r<   r=   rx   $  rN   zLtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.decision_functionc                 S   ri   r  r<   rG   r<   r<   r=   ru   '  rK   z@test_pipeline_warns_not_fitted.<locals>.StatelessEstimator.scorec                 S   r  r7   r  rW   r<   r<   r=   r   *  rN   zHtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.score_samplesc                 S   rV   r7   r<   rW   r<   r<   r=   r\   -  rK   zLtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.inverse_transformN)rA   rB   rC   rD   rJ   rX   rs   rv   rw   rx   ru   r   r\   r<   r<   r<   r=   StatelessEstimator  s    r  r  z)This Pipeline instance is not fitted yet.r   re   )r   r    r   r  r  r   )r   r  r   r<   r<   r=   test_pipeline_warns_not_fitted  s
   #"r  c                   @   sx   e Zd Zdd ZdddZdddZddd	Zdd
dZdddZdddZ	dddZ
dddZdddZdddZdS )SimpleEstimatorc                 C   ri   rj   r<   rk   r<   r<   r=   rl   <  rK   z%SimpleEstimator.__sklearn_is_fitted__Nc                 C   s$   |d usJ ||d usJ || S r7   r<   r;   rH   rI   r   propr<   r<   r=   rJ   ?  s   zSimpleEstimator.fitc                 C   s    |d usJ |d usJ |d S r  r<   r  r<   r<   r=   r   D     zSimpleEstimator.fit_transformc                 C   s&   |d usJ |d usJ t t|S r7   r  r  r<   r<   r=   r   I     zSimpleEstimator.fit_predictc                 C   &   |d usJ |d usJ t t|S r7   r  r;   rH   r   r  r<   r<   r=   rs   N  r  zSimpleEstimator.predictc                 C   r  r7   r  r  r<   r<   r=   rv   S  r  zSimpleEstimator.predict_probac                 C   r  r7   r  r  r<   r<   r=   rw   X  r  z!SimpleEstimator.predict_log_probac                 C   r  r7   r  r  r<   r<   r=   rx   ]  r  z!SimpleEstimator.decision_functionc                 C   s   |d usJ |d usJ dS r  r<   r  r<   r<   r=   ru   b  s   zSimpleEstimator.scorec                 C   s    |d usJ |d usJ |d S r  r<   r  r<   r<   r=   rX   g  r  zSimpleEstimator.transformc                 C   s    |d usJ |d usJ |d S r  r<   r  r<   r<   r=   r\   l  r  z!SimpleEstimator.inverse_transformr@   )rA   rB   rC   rl   rJ   r   r   rs   rv   rw   rx   ru   rX   r\   r<   r<   r<   r=   r  9  s    








r  r  Zpartial_fitc           
   	   C   s  dd }t dggt dg}}dgdd}}}t }||| ddd}||dddd}t jddd	jddd	jddd	}td
|fd|fg}	d| vrW|	j||||d}	zt	|	| |||||d W n t
yy   t	|	| ||||d Y nw t|dd||d t|dd||d dS )z5Test that metadata is routed correctly for pipelines.c                 [   sB   |t v r	t | }n|g}|D ]}t| d| ddi | q| S )zSet requests for a given method.

        If the given method is a composite method, set the same requests for
        all the methods that compose it.
        set__requestNr<   )r+   r   )r5  r   kwargr  r<   r<   r=   set_requestx  s   
z7test_metadata_routing_for_pipeline.<locals>.set_requestre   r9   r:   Tr   r  rJ   r  trsr  )r   r  r  )objr   r  r   r  rX   N)rn   r   r  r'   r  r  Zset_inverse_transform_requestr    rJ   r   r   r)   )
r   r  rH   rI   r   r  r  r5  r!  r   r<   r<   r=   "test_metadata_routing_for_pipelines  sP   



r#  c              	   C   s   dggdg}}dgd}}t  }td|fg}d|  }tjtt|d2 zt|| ||||d W n tyH   t|| |||d Y n	w W d   dS W d   dS 1 s\w   Y  dS )zBTest that metadata is not routed for pipelines when not requested.re   r9   r  zn[sample_weight, prop] are passed but are not explicitly set as requested or not requested for SimpleEstimator.r   r   N)	r  r    r   r   r   r   r   r   r   )r   rH   rI   r   r  r5  r   error_messager<   r<   r=   (test_metadata_routing_error_for_pipeline  s$   "r%  )rx   rX   r\   c                 C   s^   t dt fg}tjtdd t|| dggdgdd W d   dS 1 s(w   Y  dS )zTest that the right error message is raised when metadata is passed while
    not supported when `enable_metadata_routing=False`.r  z1is only supported if enable_metadata_routing=Truer   re   r9   r   N)r    r  r   r   r   r   )r   r   r<   r<   r=   *test_routing_passed_metadata_not_supported  s   "r&  c                  C   s<   t dt fdt fg} | dggdg | dgg dS )zFTest that pipeline works with estimators that have a `__len__` method.r!  r  re   N)r    r   r   rJ   rs   r   r<   r<   r=   %test_pipeline_with_estimator_with_len  s
   r'  	last_stepc                 C   sR   t dt fd| fg}|dggdgdgdgdggdgdgdggks'J dS )zTest that the pipeline works when there is not last step.

    It should just ignore and pass through the data on transform.
    r!  r  re   r   r   N)r    r#   rJ   rX   )r(  r   r<   r<   r=   test_pipeline_with_no_last_step  s   <r)  c                  C   s  t ddgddgddgg} g d}g dd}}td	t fg}d
tj d}tjtt	|d |j
| |||d W d   n1 sFw   Y  td	t jdddfg}d
tj d}tjtt	|d |j
| |||dj| ||d W d   dS 1 sw   Y  dS )zCTest that the right error is raised when metadata is not requested.r   re   r   r   r+  r  re   re   re   r9   sub_transformerzb[sample_weight, metadata] are passed but are not explicitly set as requested or not requested for z.fitr   r  NTz
.transform)rn   r   r   r'   rA   r   r   r   r   r   rJ   r  rX   )rH   rI   r   r  feature_unionr$  r<   r<   r=   )test_feature_union_metadata_routing_error  s<   "r-  c                  C   s   t dt fg} |   dS )zaTest that get_metadata_routing() works regardless of the Child's
    consumption of any metadata.r+  N)r   r'   Zget_metadata_routing)r,  r<   r<   r=   3test_feature_union_get_metadata_routing_without_fit	  s   r.  r  c           	      C   s  t ddgddgddgg}g d}g dd}}td	| t d
jdddjdddfd| t d
jdddjdddfg}||d}|j||fi | |j||fi | |j||fi |j|fi | |j	D ]} | d j
}t|syJ |D ]}td|ddd| q{qldS )z8Test that metadata is routed correctly for FeatureUnion.r   re   r   r   r+  r  r*  r9   Z
sub_trans1r  Tr  Z
sub_trans2rJ   )r"  r   r  Nr<   )rn   r   r   r(   r  r  rJ   r   rX   r   r  r   r)   )	r  rH   rI   r   r  r,  kwargsr  Z	sub_transr<   r<   r=   #test_feature_union_metadata_routing$	  sJ   


"

r0  )rD   	itertoolsr   r  r   r  tempfiler   r  Znumpyrn   r   Zsklearnr   Zsklearn.baser   r   r   r   r   r	   Zsklearn.clusterr
   Zsklearn.datasetsr   Zsklearn.decompositionr   r   Zsklearn.dummyr   Zsklearn.ensembler   r   r   Zsklearn.exceptionsr   r   Zsklearn.feature_extraction.textr   Zsklearn.feature_selectionr   r   Zsklearn.imputer   Zsklearn.linear_modelr   r   r   Zsklearn.metricsr   r   Zsklearn.model_selectionr   Zsklearn.neighborsr   Zsklearn.pipeliner   r    r!   r"   Zsklearn.preprocessingr#   r$   Zsklearn.svmr%   Z%sklearn.tests.metadata_routing_commonr&   r'   r(   r)   Zsklearn.utilsr*   Z sklearn.utils._metadata_requestsr+   r,   Zsklearn.utils._testingr-   r.   r/   r0   r1   r2   Zsklearn.utils.fixesr3   Zsklearn.utils.validationr4   r5   r   r   flagsZ	writeabler   rE  r6   rE   rU   r[   r^   rd   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   markZparametrizer   r   r   r   r   r   r  r  r  r  r  r#  r-  r0  r3  r8  r;  r>  rF  rK  rO  rZ  r\  rj  rm  rp  rx  r~  r  r  r  r  r  r  r  r  r  productZparameter_grid_test_verboser  r  r  r  r  r  r  r  r  r  r  r  r  r  r  slicer  r  r  r   r	  r
  r  r  r  sortedsetr#  r%  r&  r'  r)  r-  r.  r0  r<   r<   r<   r=   <module>   s     


T

/	
!

P
!-#q&?1?





!M
!	
,
-:C

		
(

