o
    \iHK                     @   s$  d dl Z d dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
mZmZ d dlmZmZ dd fdd	Zd
d Zdd Zdd Zdd Zdd Zdd Zejddge e dd Zdd Zdd Zejdddgd d! Zd"d# Zd$d% Zejd&ej d'd(d)dd*fej d'ej d'd+d)dd+fej d'd(d,d dd+fej d'ej d'd+d-d dd.fej d'd(d/d dd0fej d'ej d'd+d1d dd2fej d'd(d)d2d2fej d'ej d'd+d)d+d+fej d'd(d3d d2d4fej d'ej d'd+d5d d+d2fg
ejd6d7d8gd9d: Z!d;d< Z"d=d> Z#d?d@ Z$dAdB Z%ejdCd)dDdEgfdFd dGdHgggejdId7d8gdJdK Z&dLdM Z'dNdO Z(ejdPdQdRgejdSg dTdUdV Z)ejdWd)dXd gdYdZ Z*ejdPdQdRgejdWd)d[d gd\d] Z+dS )^    N)make_pipeline)FunctionTransformerStandardScaler)_convert_containerassert_allclose_dense_sparseassert_array_equal)CSC_CONTAINERSCSR_CONTAINERSc                 O      | S N )Xakr   r   /var/www/www-root/data/www/176.119.141.140/sports-predictor/venv/lib/python3.10/site-packages/sklearn/preprocessing/tests/test_function_transformer.py<lambda>       r   c                    s    fdd}|S )Nc                    s&     |   | | | S r   )appendextendupdate)r   argskwargs
args_storefunckwargs_storer   r   _func   s   


z_make_func.<locals>._funcr   )r   r   r   r   r   r   r   
_make_func   s   r   c                  C   s   g } i }t dd}ttt| |||d | |gks'J dj| d|r1J dj|dg | d d < |  tt| ||}t||dd | |gksYJ dj| d|rcJ dj|dd S )N
         z*transform should have returned X unchangedz5Incorrect positional arguments passed to func: {args})r   z3Unexpected keyword arguments passed to func: {args})err_msg)	nparangereshaper   r   r   	transformformatclear)r   r   r   transformedr   r   r   test_delegate_to_func   s<   



r*   c                  C   s0   t dd} ttt j| t |  d S )Nr   r   )r#   r$   r%   r   r   log1pr&   r   r   r   r   test_np_logD   s
   r-   c                  C   sH   t jddddd} tt jtddd}t|| t j| dd d S 	Nr      r   numr      decimalskw_args)r#   linspacer%   r   arounddictr   r&   r   Fr   r   r   test_kw_argN   s   r<   c                  C   sR   t jddddd} tt jtddd}d|jd	< t|| t j| dd d S )
Nr   r/   r   r0   r   r2   r3   r5   r4   	r#   r7   r%   r   r8   r9   r6   r   r&   r:   r   r   r   test_kw_arg_updateW   s   
r>   c                  C   sT   t jddddd} tt jtddd}tdd|_t|| t j| dd d S r.   r=   r:   r   r   r   test_kw_arg_resetb   s   r?   c                  C   sV   t g dd} tt jt jtddd}t||	| t jt | dd d S )Nr/      	      r!   r!   r2   r3   )r   inverse_funcinv_kw_args)
r#   arrayr%   r   sqrtr8   r9   r   inverse_transformr&   r:   r   r   r   test_inverse_transformm   s   rJ   sparse_containerc                 C   s   t jg dt jdd}| d ur| |}tt jt j| d uddd}d}tjt	|d |
| W d    n1 s;w   Y  tt jt j| d uddd}t  tdt	 ||}W d    n1 sgw   Y  t||| d S )	Nr@   dtyperD   T)r   rE   accept_sparsecheck_inversevalidatezThe provided functions are not strictly inverse of each other. If you are sure you want to proceed regardless, set 'check_inverse=False'.matcherror)r#   rG   float64r%   r   rH   r8   pytestwarnsUserWarningfitexpm1r+   warningscatch_warningssimplefilterfit_transformr   rI   )rK   r   transwarning_messageXtr   r   r   test_check_inverse|   s6   
ra   c                  C   s   t jg dt jdd} tt jd ddd}t  tdt	 |
|  W d    n1 s0w   Y  td t jddd}t  tdt	 |
|  W d    d S 1 sYw   Y  d S )Nr@   rL   rD   T)r   rE   rO   rP   rS   )r#   rG   rT   r%   r   rY   rZ   r[   r\   rW   rX   )r   r^   r   r   r   /test_check_inverse_func_or_inverse_not_provided   s   



"rb   c                  C   s@   t d} | tjdd}t }||}t|dsJ d S )Npandasd   r   loc)	rU   importorskip	DataFramer#   randomrandnr   r]   hasattr)pdX_dftransformer
X_df_transr   r   r   test_function_transformer_frame   s
   

ro   X_typerG   seriesc                    s   dddddddd   D d	g d
}t| dgd}fdd} fdd}t||ddd}d}tjt|d || W d   dS 1 sPw   Y  dS )zKCheck that `FunctionTransformer.check_inverse` raises error on mixed dtype.r/   r!   r2   fivesix)onetwothreer       c                 S   s   i | ]\}}||qS r   r   ).0keyvaluer   r   r   
<dictcomp>       zJtest_function_transformer_raise_error_with_mixed_dtype.<locals>.<dictcomp>object)rt   ru   rv   rt   rt   r    rw   rz   columns_namerM   c                    s$   t j fddt jD tdS )Nc                    s   g | ]} |  qS r   r   rx   i)r   mappingr   r   
<listcomp>   s    zXtest_function_transformer_raise_error_with_mixed_dtype.<locals>.func.<locals>.<listcomp>rL   )r#   rG   rangesizer}   r,   )r   r,   r   r      s   $zDtest_function_transformer_raise_error_with_mixed_dtype.<locals>.funcc                    s    t fdd| D  dgdS )Nc                    s   g | ]} | qS r   r   )rx   x)inverse_mappingr   r   r          z`test_function_transformer_raise_error_with_mixed_dtype.<locals>.inverse_func.<locals>.<listcomp>rz   r~   )r   r,   )rp   rM   r   r   r   rE      s   zLtest_function_transformer_raise_error_with_mixed_dtype.<locals>.inverse_funcFT)r   rE   rP   rO   L'check_inverse' is only supported when all the elements in `X` is numerical.rQ   N)itemsr   r   rU   raises
ValueErrorrX   )rp   datar   rE   rm   msgr   )rp   rM   r   r   r   6test_function_transformer_raise_error_with_mixed_dtype   s   "r   c                  C   sT   t d} | g dg dd}tdd dd dd	}||}t||d
  dS )z8Check support for dataframes with only numerical values.rc   r/   r!   r2   )rA   r    rw   r   bc                 S   s   | d S Nr!   r   r   r   r   r   r          z`test_function_transformer_support_all_nummerical_dataframes_check_inverse_True.<locals>.<lambda>c                 S   s   | d S r   r   r   r   r   r   r      r   Tr   rE   rO   r!   N)rU   rf   rg   r   r]   r   )rk   dfrm   df_outr   r   r   Ntest_function_transformer_support_all_nummerical_dataframes_check_inverse_True   s   

r   c                  C   sz   t d} tdd dd dd}| g dg dd}d	}t jt|d
 || W d   dS 1 s6w   Y  dS )zYCheck error is raised when check_inverse=True.

    Non-regresion test for gh-25261.
    rc   c                 S   r
   r   r   r   r   r   r   r      r   zQtest_function_transformer_with_dataframe_and_check_inverse_True.<locals>.<lambda>Tr   r   r   r   cr   r   rQ   N)rU   rf   r   rg   r   r   rX   )rk   rm   df_mixedr   r   r   r   ?test_function_transformer_with_dataframe_and_check_inverse_True   s   
"r   z.X, feature_names_out, input_features, expectedrd   r2   
one-to-one)x0x1x2r   c                 C      dS )Nr   r   rm   input_featuresr   r   r   r     r   c                 C   r   )Nr   der   r   r   r   r   r     r   r   c                 C      t |d S )N)r   tupler   r   r   r   r         )r   r   r   r   c                 C   r   N)r   r   r   r   r   r   r      r   r   c                 C   r   )N)r   r   r   r   r   r   r   5  r   )r   r   r   r   c                 C   r   r   r   r   r   r   r   r   <  r   rP   TFc                 C   sj   t | trtd}|| } t||d}||  ||}t |tj	s'J |j
tks.J t|| d S )Nrc   feature_names_outrP   )
isinstancer9   rU   rf   rg   r   rX   get_feature_names_outr#   ndarrayrM   r}   r   )r   r   r   expectedrP   rk   rm   namesr   r   r   /test_function_transformer_get_feature_names_out   s   
O



r   c                  C   sZ   t ddd} tjdd}| | | d}t|tjsJ |jt	ks&J t
|d d S )Nr   Fr   rd   r!   r   )r   r#   rh   randr]   r   r   r   rM   r}   r   )rm   r   r   r   r   r   Btest_function_transformer_get_feature_names_out_without_validationT  s   

r   c                  C   s^   t  } tjdd}| | d}tjt|d |   W d    d S 1 s(w   Y  d S )Nrd   r!   zCThis 'FunctionTransformer' has no attribute 'get_feature_names_out'rQ   )	r   r#   rh   r   r]   rU   r   AttributeErrorr   )rm   r   r   r   r   r   3test_function_transformer_feature_names_out_is_None_  s   

"r   c                  C   s   dd } dd }t | |tdddd}td	}|tjd
tjd
d}|| |	 }t
|tjs9J |jtks@J t|d d S )Nc                 S   s    t j| t jt| |gddS )Nr/   axis)r#   concatenaterh   r   len)r   nr   r   r   add_n_random_featuresj  s    zYtest_function_transformer_feature_names_out_uses_estimator.<locals>.add_n_random_featuresc                 S   s$   | j d }t|dd t|D  S )Nr   c                 S   s   g | ]}d | qS )rndr   r   r   r   r   r   o  r|   zitest_function_transformer_feature_names_out_uses_estimator.<locals>.feature_names_out.<locals>.<listcomp>)r6   listr   )rm   r   r   r   r   r   r   m  s   
zUtest_function_transformer_feature_names_out_uses_estimator.<locals>.feature_names_outr2   )r   T)r   r   r6   rP   rc   rd   r   )r   r   rnd0rnd1rnd2)r   r9   rU   rf   rg   r#   rh   r   r]   r   r   r   rM   r}   r   )r   r   rm   rk   r   r   r   r   r   :test_function_transformer_feature_names_out_uses_estimatori  s   
 
r   c                  C   sz   dd } dd }t ddgddgddgg}t| |d	d
}||}|j|jd ks,J || |j|jd ks;J dS )zSTest that function transformer does not reset estimator in
    `inverse_transform`.c                 S   s&   t | jd df}t j| |fddS )Nr   r/   r   )r#   onesshaper   )r   X_oner   r   r   add_constant_feature  s   zHtest_function_transformer_validate_inverse.<locals>.add_constant_featurec                 S   s   | d d d df S )Nr   r,   r   r   r   inverse_add_constant  s   zHtest_function_transformer_validate_inverse.<locals>.inverse_add_constantr/   r!   r2   rA   T)r   rE   rP   N)r#   rG   r   r]   n_features_in_r   rI   )r   r   r   r^   X_transr   r   r   *test_function_transformer_validate_inverse  s   

r   zfeature_names_out, expectedpetcolorc                 C      dd |D S )Nc                 S      g | ]}| d qS )_outr   )rx   r   r   r   r   r     r|   <lambda>.<locals>.<listcomp>r   )estr   r   r   r   r         pet_out	color_outin_pipelinec           	         s   t d}|ddgddgd} fdd}t| d	}|r#t|}||}t||js0J | }t|tj	s<J |j
tksCJ t|| d
S )zHCheck that get_feature_names_out works with DataFrames with string data.rc   dogcatredgreen)r   r   c                    s0    dkr| S  d | j }| jtt| j |dS )Nr   columns)r   renamer9   zip)r   namer   r   r   r     s   zCtest_get_feature_names_out_dataframe_with_string_data.<locals>.funcr   r   N)rU   rf   rg   r   r   r]   r   r   r#   r   rM   r}   r   )	r   r   r   rk   r   r   rm   r   r   r   r   r   5test_get_feature_names_out_dataframe_with_string_data  s   

r   c               	   C   s  t d} | g dg dd}ttjdd}t  tdt	 |j
dd W d	   n1 s3w   Y  ||}t|| jsEJ t|jd
dg tdd }|j
dd t  tdt	 ||}W d	   n1 ssw   Y  t|| jsJ t|jd
dg tdd }dD ]-}|j
|d d| d| d}t jt	|d || W d	   n1 sw   Y  q|j
dd t  tdt	 || W d	   d	S 1 sw   Y  d	S )z5Check behavior of set_output with different settings.rc   r   r      rd   r   r   r   rS   r&   Nr   r   c                 S   s   d|  S r   r   r   r   r   r   r     r   z&test_set_output_func.<locals>.<lambda>c                 S   s
   t | S r   )r#   asarrayr   r   r   r   r     s   
 )rc   polarsz'When `set_output` is configured to be 'z'.*z DataFrame.*rQ   default)rU   rf   rg   r   r#   logrZ   r[   r\   rW   
set_outputr]   r   r   r   rV   )rk   r   ftr   ft_npr&   r   r   r   r   test_set_output_func  s@   




"r   c                  C   s   t d} dd }tttj|dt }| jddgddgd	d
ggddgd}||}|	 
 ddgks7J t|tjs?J dS )zCheck that we have a consistence between the feature names out of
    `FunctionTransformer` and the feature names in of the next step in the pipeline.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/27695
    rc   c                 S   r   )Nc                 S      g | ]}|d  qS __logr   rx   r   r   r   r   r     r   zStest_consistence_column_name_between_steps.<locals>.with_suffix.<locals>.<listcomp>r   _r   r   r   r   with_suffix     z?test_consistence_column_name_between_steps.<locals>.with_suffixr   r/   r!   r2   rA   r    rw   r   r   r   a__logb__logN)rU   rf   r   r   r#   r+   r   rg   r]   r   tolistr   r   )rk   r   pipeliner   r   r   r   r   *test_consistence_column_name_between_steps  s   
$
r   dataframe_librc   r   transform_output)r   rc   r   c                 C   s   t | }|dkrt | |g dg dd}dd }t|dj|d}||}tt|t| |	 }t
|j|d	|jksHJ | |d	|jksTJ d	S )
z8Check that we overwrite the column names when we should.numpyr   r   r   c                 S   r   )Nc                 S   r   r   r   r   r   r   r   r     r   zYtest_function_transformer_overwrite_column_names.<locals>.with_suffix.<locals>.<listcomp>r   r   r   r   r   r     r   zEtest_function_transformer_overwrite_column_names.<locals>.with_suffixr   r   N)rU   rf   rg   r   r   r]   r   r#   r   r   r   r   r   )r   r   libr   r   rm   r   feature_namesr   r   r   0test_function_transformer_overwrite_column_names  s   



r  r   c                 C   r   )Nc                 S   r   _logr   r   r   r   r   r     r|   r   r   r   r   r   r   r     r   c                 C   sn   t d}|g dg dd}t| d}||}tt|t| | }t	|j
t	|ks5J dS )zCheck the same as `test_function_transformer_overwrite_column_names`
    but for the specific case of pandas where column names can be numerical.rc   r   r   )r   r/   r   N)rU   rf   rg   r   r]   r   r#   r   r   r   r   )r   rk   r   rm   r   r  r   r   r   :test_function_transformer_overwrite_column_names_numerical  s   


r  c                 C   r   )Nc                 S   r   r  r   r   r   r   r   r   /  r|   r   r   r   r   r   r   r   /  r   c                    s~   t  }|g dg dd} fdd}t||d}d}t jt|d ||j W d	   d	S 1 s8w   Y  d	S )
zCheck that we raise an error when `func` returns a dataframe with new
    column names that become inconsistent with `get_feature_names_out`.r   r   r   c                    s&    dkr| j ddidS |  ddiS )Nrc   r   r   r   )r   )r   r   r   r   r   :  s   zAtest_function_transformer_error_column_inconsistent.<locals>.funcr   z:The output generated by `func` have different column namesrQ   N)rU   rf   rg   r   r   r   r]   r   )r   r   r   r   r   rm   r"   r   r  r   3test_function_transformer_error_column_inconsistent,  s   

"r  ),rZ   r   r#   rU   sklearn.pipeliner   sklearn.preprocessingr   r   sklearn.utils._testingr   r   r   sklearn.utils.fixesr   r	   r   r*   r-   r<   r>   r?   rJ   markparametrizera   rb   ro   r   r   r   rh   r   r   r   r   r   r   r   r   r   r  r  r  r   r   r   r   <module>   s    
*
	
#
									K

.


