o
    }Th9                    @   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<   \/home/air/segue/gemini/back/venv/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   axis)rn   ro   rg   sumrW   r<   r<   r=   predict   s   zMult.predictc                 C   s
   t |S r7   rn   rs   rG   r<   r<   r=   score   rh   z
Mult.score)re   r7   )rA   rB   rC   r>   rl   rJ   rX   r\   rt   predict_probapredict_log_probadecision_functionrv   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   r{   rW   r<   r<   r=   rt      s   zFitParamT.predictc                 C   s   | j |||d | |S )N)r   )rJ   rt   r~   r<   r<   r=   fit_predict   s   
zFitParamT.fit_predictNc                 C   s   |d ur|| }t |S r7   ru   )r;   rH   rI   sample_weightr<   r<   r=   rv      s   
zFitParamT.scorerS   r@   )	rA   rB   rC   rD   r>   rJ   rt   r   rv   r<   r<   r<   r=   rz      s    

rz   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=   rt      ra   zDummyEstimatorParams.predictc                 C   r   r7   r   r   r<   r<   r=   rw      ra   z"DummyEstimatorParams.predict_probac                 C   r   r7   r   r   r<   r<   r=   rx      ra   z&DummyEstimatorParams.predict_log_probaNrS   )	rA   rB   rC   rD   rl   rJ   rt   rw   rx   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__asvc__br   F皙?)r   anovaz<All intermediate steps should be transformers.*\bNoTrans\b.*t)svc__Cz]Invalid parameter 'C' for estimator SelectKBest(). Valid parameters are: ['k', 'score_func'].)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filter1pipe2rQ   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[   rz   rJ   rv   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   logistic)irisdatatargetr   r   r   r    rJ   rt   rw   rx   rv   )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   )clf__bad)r    r[   rz   rJ   rt   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[   rz   rJ   rv   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   rv   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   nope)fake)fake__estimatorzInvalid parameter 'invalid_param' for estimator LinearRegression(). Valid parameters are: ['copy_X', 'fit_intercept', 'n_jobs', 'positive'].)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   rt   rw   rx   rv   )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   )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   ovr)r   r   decision_function_shape
preprocessr   )r   r   r   r   lenrn   uniquer$   r   r%   r    rJ   rt   rw   rx   ry   rv   )rH   rI   	n_samples	n_classesscalerr   r   preprocessingr   rt   proba	log_probary   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   n_initr   Kmeans)r$   r
   r   r   r   r   r    r1   )r   kmscaler_for_pipelinekm_for_pipelinescaledseparate_predr   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   transf__should_get_thisr   should_get_thisr   )r    r^   rz   r   r   r_   r|   r   r<   r<   r=   -test_fit_predict_with_intermediate_fit_params  s   r   method_name)rt   rw   rx   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   )	select__k   mockr   z>All estimators should implement fit and transform.*\bNoTrans\brX   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_transformedX_spX_sp_transformed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   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names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[   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_trans2X_trans3X_back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   123T)memoryverboseFr   stepsz*Pipeline slicing only supports a step of 1r   r  )r    r[   rz   r   r0  listr   itemsrM   r   r   r   )startendr   
pipe_slicepipe_paramspipe_slice_paramsr   r<   r<   r=   test_pipeline_slice~  s(   "r8  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   foobar)r[   rz   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r0  r	  )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   r0  rR   r   r   r   r   r   rJ   r   r   )r+  r,  r   r   r<   r<   r=   test_set_pipeline_steps  s.   "rA  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   rB  )r   mult2r   r<   r<   r=   test_pipeline_named_steps  s   
rD  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)rF  rG  rH  rI  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   r0  )	r   rH   rI   rC  mult3mult5r   expected_namesactual_namesr<   r<   r=   %test_pipeline_correctly_adjusts_steps  s   


rS  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   rE  c                      s   t d fdfdfgS )NrF  rH  last)r    r<   rC  rO  rP  r<   r=   make  s   z0test_set_pipeline_step_passthrough.<locals>.make   rH  r  Tr   F)	r0  rF  rH  rT  r.  m2__mult
last__multtransform_inputr/  rF  )rw   rx   ry   rX   rv   )rT     z''str' object has no attribute 'predict'z*This 'Pipeline' has no attribute 'predict'r   rt   rF  rH  rT  )rn   r   rd   r2   r   rJ   rt   r\   rR   rM   r0  r   rX   r   r   r   r   r   r   r   r    )r   rH   rI   rV  r   expother_methodsr  r   r   r   r<   rU  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 )NrE  rt   r   r   r   r   r\   )	r!   rd   rt   rX   r\   r[   hasattrr0  rU   )r   r<   r<   r=   test_pipeline_ducktypingD  s*   
rc  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   	fitparamt)r[   r!   r   r    r0  rz   )t1t2r   r<   r<   r=   test_make_pipeline`  s   
rg  zpipeline, check_estimator_typec                 C   s   t | jd u S r7   )r*   estimator_typeestr<   r<   r=   <lambda>v  s    rk  c                 C   s
   | j d u S r7   )_estimator_typeri  r<   r<   r=   rk  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   check_estimator_typer<   r<   r=   test_pipeline_estimator_typeo  s   ro  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__)empty_pipelinebeexpected_tagsr<   r<   r=   %test_sklearn_tags_with_empty_pipeline  s   
rt  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  X_fit_transformedX_fit_transformed_wo_methodr<   r<   r=   test_feature_union_weights  s.   

$"$"ry  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  fs_parallelfs_parallel2r  X_transformed_parallelX_transformed_parallel2r<   r<   r=   test_feature_union_parallel  s:   





r  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{  r|  char_wb)r   r   )r}  ngram_ranger~  rz  chars__words__#   tr1re   zDTransformer tr1 (type Transf) does not provide get_feature_names_outr   )r   r   rJ   r  get_feature_names_outr   r[   r   r   r   r   r   )	word_vect	char_vectftfeature_namesfeatr   r<   r<   r=    test_feature_union_feature_names  s    


"r  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   r  rn   r   )rH   rI   regr   r<   r<   r=   test_classes_property  s   r  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   rE  c                 S      dgS Nx3r<   input_featuresr<   r<   r=   rk  	      z.test_set_feature_union_steps.<locals>.<lambda>c                 S   r  Nx2r<   r  r<   r<   r=   rk  
  r  c                 S   r  )Nx5r<   r  r<   r<   r=   rk    r  rF  rH  re   m2__x2m3__x3rI  m5__x5r	  )r  mock__x3r?  mock__x5)	rd   r  r   r2   rX   rn   ro   r  rR   )rC  rO  rP  r  r<   r<   r=   test_set_feature_union_steps  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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   r  r  r<   r  r<   r<   r=   rk  %  r  z2test_set_feature_union_step_drop.<locals>.<lambda>c                 S   r  r  r<   r  r<   r<   r=   rk  &  r  re   rF  rH  r  r  dropr\  rX  )rF  r  )
rd   r  rn   ro   r   r2   rJ   rX   r   rR   )rC  rO  rH   r  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   r  r  r<   r  r<   r<   r=   rk  K  r  z4test_set_feature_union_passthrough.<locals>.<lambda>c                 S   r  r  r<   r  r<   r<   r=   rk  L  r  re   rF  rH  r  r  r   r\  
m2__myfeatmyfeatrX  
m3__myfeat)rF  r   r   r   ru  ra  r   N)passthrough__f0passthrough__f1passthrough__f2passthrough__f3	pca__pca0	pca__pca1)f0f1f2f3)r   )r  r  r  r  pca__f0pca__f1pca__f2pca__f3)r   )passthrough__pca0passthrough__pca1r  r  r  r  r   )rd   r  rn   ro   r   r2   rJ   rX   r   rR   r   r   r   r   hstack)rC  rO  rH   r  columnsr   X_ftr<   r<   r=   "test_set_feature_union_passthroughD  s~   


& 

("
*$r  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   ru  r   ra  )r  r  passthrough__x0passthrough__x1passthrough__x2passthrough__x3Nr   r   r   r   rJ   r2   r  rH   r   r  r<   r<   r=   9test_feature_union_passthrough_get_feature_names_out_true  s   
r  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   ru  r   ra  Fverbose_feature_names_out)pca0pca1x0x1r  r  Nr  r  r<   r<   r=   :test_feature_union_passthrough_get_feature_names_out_false  s   
r  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:   r  c                 S   s
   | dg S rO   r<   )rH   r<   r<   r=   rk    rm  zStest_feature_union_passthrough_get_feature_names_out_false_errors.<locals>.<lambda>c                 S   s   t dgS rO   )rn   ro   )r;   rL  r<   r<   r=   rk    s    )feature_names_outre  rf  Fr  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   r  )pdrH   select_aunionr   r<   r<   r=   Atest_feature_union_passthrough_get_feature_names_out_false_errors  s    


"r  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.r  r  c                 S      g | ]}d | qS )fr<   )rJ  ir<   r<   r=   rM        zdtest_feature_union_passthrough_get_feature_names_out_false_errors_overlap_over_5.<locals>.<listcomp>r  )re  r   )rf  r   Fr  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   r  r  r1  ranger   rJ   r   r   r   r   r  )r  rH   r  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    n	1 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'\]a__qr   r:   r   r9   r0  r  r   re   r<   )
rd   r    r   r   r   r   rJ   setattrr   rR   )error_message_1error_message_2error_message_3
bad_steps1
bad_steps2r   param
bad_steps3	bad_stepsmessagerj  r<   r<   r=   test_step_name_validation  sB   
"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?)a__b__alphaa__brE  )a__steps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   rt   rw   rx   rv   r   rb  shutilrmtree)rH   rI   cachedirr.  r   r   r   cached_pipetsclf_2r  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;   r  r<   r<   r=   r    rK   z&FeatureNameSaver.get_feature_names_outr7   )rA   rB   rC   rJ   rX   r  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   r  r  )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 vectorizersvectr   r>  c                 S   s   g | ]}d |v qS )pizzar<   )rJ  r   r<   r<   r=   rM    rN  z7test_feature_names_count_vectorizer.<locals>.<listcomp>Nr  )beerburgercoke	copyrightr  thenonsense_is_ignored)r    r   r   r  rJ   r2   r  )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`.notransr>  z&does not provide get_feature_names_outr   N)
r    rE   r   rJ   r   r   r   r   r   r  )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   r0  r  re   N)rb  r   r0  rz   )rJ  rj  patternr  r<   r<   r=   	<genexpr>  s    
487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   mult1rC  z\[FeatureUnion\].*\(step 1 of 2\) Processing mult1.* total=.*\n\[FeatureUnion\].*\(step 2 of 2\) Processing mult2.* total=.*\n$)r  r  )rO  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  rE  r]  rv  r   F)r/  zGot output for verbose=FalseT)r   rR   
readouterroutr   r   )rj  r  r  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  rE  r]  r   re   r   n_features_in_)r$   r   r!   rb  rJ   r  )rH   rI   ss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"   rb  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#  dummy0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#  nofittransform0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   astypeboolnanr!   r   r   rJ   rv   )rH   rI   maskr   r<   r<   r=   %test_pipeline_missing_values_leniencyk  s
   "
 r3  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  rE  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  weightsexpected_msgr  r<   r<   r=   3test_feature_union_warns_unknown_transformer_weightu  s   "r7  c                 C   s    t | t }| jjrJ d S r7   )r!   r%   rp  
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   rE  re      r4  	predictor)rn   r-  RandomStaterandnr   r    r/   rJ   rt   r   r2   rv   r   approxr   r0   r   r   )r;  rngrH   rI   modely_predr<   r<   r=   1test_search_cv_using_minimal_compatible_estimator  s   (

$$rE  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   rH  r   )
r   r    r   r   r   r5   rJ   r   r   r   )rH  r   r<   r<   r=   test_pipeline_check_if_fitted  s   
rI  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  rE  r]  r  r   Nr  )r   r/   r   r   r   r5   rJ   )rH   rI   r  r<   r<   r=   "test_feature_union_check_if_fitted  s    


rJ  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 )Nr  c                 S   r  
my_prefix_r<   rJ  rK  r<   r<   r=   rM    r  ztest_pipeline_get_feature_names_out_passes_names_through.<locals>.AddPrefixStandardScalar.get_feature_names_out.<locals>.<listcomp>)dtype)superr  rn   ro   object)r;   r  r  	__class__r<   r=   r    s   zotest_pipeline_get_feature_names_out_passes_names_through.<locals>.AddPrefixStandardScalar.get_feature_names_outr7   )rA   rB   rC   r  __classcell__r<   r<   rQ  r=   AddPrefixStandardScalar  s    rT  c                 S   r  rK  r<   rM  r<   r<   r=   rM    r  zLtest_pipeline_get_feature_names_out_passes_names_through.<locals>.<listcomp>N)	r   r   r   r$   r!   rJ   r  r  r2   )rH   rI   rT  r   input_namesr  r<   r<   r=   8test_pipeline_get_feature_names_out_passes_names_through  s   
rV  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.r  Tas_frame
return_X_yrX   Nr  )r   r  r   r!   r$   r   
set_outputrJ   r  feature_names_in_r2   )rH   rI   r   r\  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.r  TrW  r   r  scalarr   rZ  N)r   r  r   r   r   r$   r   r[  rJ   rX   r   r  r2   r  r  index)r  rH   rL  X_trainX_testr  r#  r<   r<   r=   test_feature_union_set_output  s   


rc  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   rd  r  N)r$   r   r   )r_  r   r  r<   r<   r=   test_feature_union_getitem  s   re  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;  )rf  r  r   r<   r<   r=    test_feature_union_getitem_error  s
   
"rg  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.
    r  TrW  scaler\  r  N)r   r  r   r$   rJ   r   rb  r2   r  r\  to_numpy)rH   rL  r   r  X_arrayr<   r<   r=   $test_feature_union_feature_names_in_   s    





rk  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  rE  )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)rm  )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    


"rv  T)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(  )r'   set_fit_requestset_transform_request)ry  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.
        Trz  Fother_weightsr   r[  )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)   )ry  methodsr(  r  r  r<   r<   r=   check_metadataz  s   z5test_transform_input_pipeline.<locals>.check_metadatare   r   r   r  r   rW  (   d      )r   r~  r(  rJ   rX   rz  rL  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   rF  rj   rG  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=   rH    rc   rH  r   re   r   r4  r  Tr  r  r  r  N)r   r   r   rn   r   r    r{  rJ   )r  rH  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   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=   rH    rc   rH  c                   @   rZ   )z/test_transform_tuple_input.<locals>.Transformerc                 S   rF  rj   rG  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  r4  r  Tr  r  r  N)r   r   r   rn   r   r    r{  rJ   )	rH  r  rH   rI   X_val0y_val0X_val1y_val1r   r<   r<   r=   test_transform_tuple_input  s   r  )rt   rw   rx   ry   rv   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   onesr   rW   r<   r<   r=   rt     rN   zBtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.predictc                 S   r  r7   r  rW   r<   r<   r=   rw     rN   zHtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.predict_probac                 S   r  r7   rn   zerosr   rW   r<   r<   r=   rx   !  rN   zLtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.predict_log_probac                 S   r  r7   r  rW   r<   r<   r=   ry   $  rN   zLtest_pipeline_warns_not_fitted.<locals>.StatelessEstimator.decision_functionc                 S   ri   r*  r<   rG   r<   r<   r=   rv   '  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   rt   rw   rx   ry   rv   r   r\   r<   r<   r<   r=   StatelessEstimator  s    r  r  z)This Pipeline instance is not fitted yet.r   re   )r   r    r   rt  ru  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=   rt   N  r  zSimpleEstimator.predictc                 C   r  r7   r  r  r<   r<   r=   rw   S  r  zSimpleEstimator.predict_probac                 C   r  r7   r  r  r<   r<   r=   rx   X  r  z!SimpleEstimator.predict_log_probac                 C   r  r7   r  r  r<   r<   r=   ry   ]  r  z!SimpleEstimator.decision_functionc                 C   s   |d usJ |d usJ dS r*  r<   r  r<   r<   r=   rv   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   rt   rw   rx   ry   rv   rX   r\   r<   r<   r<   r=   r  9  s    








r  r  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   )rj  r  kwargr  r<   r<   r=   set_requestx  s   
z7test_metadata_routing_for_pipeline.<locals>.set_requestre   r9   r:   Tr   r  rJ   rz  trsr  )r   r  r(  )objr  r  r   r(  rX   N)rn   r   r  r'   r{  r|  set_inverse_transform_requestr    rJ   r   r   r)   )
r  r  rH   rI   r   r  r(  rj  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  rj  r   error_messager<   r<   r=   (test_metadata_routing_error_for_pipeline  s$   "r  )ry   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   rt   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   rz  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'   get_metadata_routing)r  r<   r<   r=   3test_feature_union_get_metadata_routing_without_fit	  s   r  r4  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   
sub_trans1rx  Trz  
sub_trans2rJ   )r  r  r  Nr<   )rn   r   r   r(   r{  r|  rJ   r   rX   r  ry  r   r)   )	r4  rH   rI   r   r(  r  kwargsry  	sub_transr<   r<   r=   #test_feature_union_metadata_routing$	  sJ   


"

r  )rD   	itertoolsr   r  r   rq  tempfiler   r  numpyrn   r   sklearnr   sklearn.baser   r   r   r   r   r	   sklearn.clusterr
   sklearn.datasetsr   sklearn.decompositionr   r   sklearn.dummyr   sklearn.ensembler   r   r   sklearn.exceptionsr   r   sklearn.feature_extraction.textr   sklearn.feature_selectionr   r   sklearn.imputer   sklearn.linear_modelr   r   r   sklearn.metricsr   r   sklearn.model_selectionr   sklearn.neighborsr   sklearn.pipeliner   r    r!   r"   sklearn.preprocessingr#   r$   sklearn.svmr%   %sklearn.tests.metadata_routing_commonr&   r'   r(   r)   sklearn.utilsr*    sklearn.utils._metadata_requestsr+   r,   sklearn.utils._testingr-   r.   r/   r0   r1   r2   sklearn.utils.fixesr3   sklearn.utils.validationr4   r5   r   r   flags	writeabler   r  r6   rE   rU   r[   r^   rd   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   markparametrizer  r  r  r  r"  r(  r)  r8  r<  rA  rD  rS  r`  rc  rg  ro  rt  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  productparameter_grid_test_verboser  r  r  r'  r+  r3  r7  r:  rE  rI  rJ  rV  r^  rc  re  slicerg  rk  rv  r  r  r  r  r  r  sortedsetr  r  r  r  r  r  r  r  r<   r<   r<   r=   <module>   s     


T

/	
!

P
!-#q&?1?





!M
!	
,
-:C

		
(

