o
    ?Hh                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	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mZ d d	lmZ d d
l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( ej)dd Z*ej)dd Z+ej,-de dfe dfe!e$ e dfe!e$ e dfe$ dfe dfe dfee$ d gfe# dgfdfgej,-dg ddd Z.edddd Z/dd Z0d d! Z1d"d# Z2d$d% Z3d&d' Z4dS )(    N)assert_array_equal)config_context)BaseEstimatorcloneis_classifieris_clustereris_outlier_detectoris_regressor)KMeans)make_column_transformer)make_classificationmake_regression)NotFittedErrorUnsetMetadataPassedError)FrozenEstimator)LinearRegressionLogisticRegression)LocalOutlierFactor)make_pipeline)RobustScalerStandardScaler)set_random_state)check_is_fittedc                   C      t  S N)r    r   r   `/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/sklearn/frozen/tests/test_frozen.pyregression_dataset       r   c                   C   r   r   )r   r   r   r   r   classification_dataset%   r   r   zestimator, dataset   method)predictpredict_probapredict_log_probadecision_function	transformc                 C   s   | |\}}t|  | || t| }|dggdg t| |r2tt| ||t||| t| t|ks<J t| t|ksFJ t	| t	|ksPJ t
| t
|ksZJ dS )zTest that frozen.fit doesn't do anything, and that all other methods are
    exposed by the frozen estimator and return the same values as the estimator.
    r    N)getfixturevaluer   fitr   hasattrr   getattrr   r	   r   r   )	estimatordatasetrequestr!   Xyfrozenr   r   r   test_frozen_methods*   s   
r1   T)enable_metadata_routingc                 C   s  G dd dt }| \}}t|dddjddjdd}|j||dd t|}|j|dd |j|dd |d jdd tjt	t
d	d
 |j|dd W d   n1 sZw   Y  |d jdd tt |j|dd W d   dS 1 sw   Y  dS )z8Test that metadata routing works with frozen estimators.c                   @   s*   e Zd ZdddZd	ddZd	ddZdS )
z6test_frozen_metadata_routing.<locals>.ConsumesMetadataNc                 S   s   || _ || _d S r   on_fit
on_predict)selfr4   r5   r   r   r   __init__]   s   
z?test_frozen_metadata_routing.<locals>.ConsumesMetadata.__init__c                 S   s   | j r	|d us	J d| _| S NT)r4   fitted_)r6   r.   r/   metadatar   r   r   r(   a   s   z:test_frozen_metadata_routing.<locals>.ConsumesMetadata.fitc                 S   s    | j r	|d us	J tt|S r   )r5   nponeslen)r6   r.   r:   r   r   r   r"   g   s   z>test_frozen_metadata_routing.<locals>.ConsumesMetadata.predict)NNr   )__name__
__module____qualname__r7   r(   r"   r   r   r   r   ConsumesMetadata\   s    

rA   Tr3   )r:   testconsumesmetadataFz]Pipeline.predict got unexpected argument(s) {'metadata'}, which are not routed to any object.matchN)r   r   set_fit_requestset_predict_requestr(   r   r"   pytestraises	TypeErrorreescaper   )r   rA   r.   r/   pipeliner0   r   r   r   test_frozen_metadata_routingX   s4   
	"rN   c                 C   s   G dd dt }| \}}| ||}t|}tt ||| W d   n1 s-w   Y  tt ||| W d   n1 sHw   Y  |jdksTJ dS )zATest that calling fit_transform and fit_predict doesn't call fit.c                   @   s(   e Zd Zdd ZdddZdddZdS )	z%test_composite_fit.<locals>.Estimatorc                 S   s0   z
|  j d7  _ W | S  ty   d| _ Y | S w )Nr    )_fit_counterAttributeErrorr6   r.   r/   r   r   r   r(      s   z)test_composite_fit.<locals>.Estimator.fitNc                 S      d S r   r   rQ   r   r   r   fit_transform      z3test_composite_fit.<locals>.Estimator.fit_transformc                 S   rR   r   r   rQ   r   r   r   fit_predict   rT   z1test_composite_fit.<locals>.Estimator.fit_predictr   )r>   r?   r@   r(   rS   rU   r   r   r   r   	Estimator   s    
rV   Nr    )	r   r(   r   rH   rI   rP   rU   rS   rO   )r   rV   r.   r/   estr0   r   r   r   test_composite_fit   s   rX   c                 C   s8   | \}}t  ||}t|}t|}|j|u sJ dS )z<Test that cloning a frozen estimator keeps the frozen state.N)r   r(   r   r   r+   )r   r.   r/   r+   r0   clonedr   r   r   test_clone_frozen   s
   rZ   c                 C   sj   | \}}t  }t|}tt t| W d   n1 sw   Y  t  ||}t|}t| dS )z5Test that check_is_fitted works on frozen estimators.N)r   r   rH   rI   r   r   r(   )r   r.   r/   r+   r0   r   r   r   test_check_is_fitted   s   
r[   c                  C   sn   G dd dt } |  }t|}| }| }|jdu sJ |jdu s%J |jjdu s-J |jjdu s5J dS )zjTest that frozen estimators have the same tags as the original estimator
    except for the skip_test tag.c                       s   e Zd Z fddZ  ZS )z#test_frozen_tags.<locals>.Estimatorc                    s   t   }d|j_|S r8   )super__sklearn_tags__
input_tagscategorical)r6   tags	__class__r   r   r]      s   
z4test_frozen_tags.<locals>.Estimator.__sklearn_tags__)r>   r?   r@   r]   __classcell__r   r   ra   r   rV      s    rV   TFN)r   r   r]   
_skip_testr^   r_   )rV   r+   r0   frozen_tagsestimator_tagsr   r   r   test_frozen_tags   s   rg   c                  C   s   t  } t| }tjtdd |jdd W d   n1 sw   Y  | d| iks.J t }|j|d | d|iksAJ dS )z?Test that FrozenEstimator only exposes the estimator parameter.z&You cannot set parameters of the innerrD   r    )estimator__CNr+   )r+   )r   r   rH   rI   
ValueError
set_params
get_paramsr   )rW   r0   	other_estr   r   r   test_frozen_params   s   rm   )5rK   numpyr;   rH   numpy.testingr   sklearnr   sklearn.baser   r   r   r   r   r	   sklearn.clusterr
   sklearn.composer   sklearn.datasetsr   r   sklearn.exceptionsr   r   sklearn.frozenr   sklearn.linear_modelr   r   sklearn.neighborsr   sklearn.pipeliner   sklearn.preprocessingr   r   sklearn.utils._testingr   sklearn.utils.validationr   fixturer   r   markparametrizer1   rN   rX   rZ   r[   rg   rm   r   r   r   r   <module>   sh    




.	