o
    i                     @   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   Z/home/kim/smarthome/.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)predictZpredict_probaZpredict_log_probaZ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)Zgetfixturevaluer   fitr   hasattrr   getattrr   r	   r   r   )	estimatorZdatasetrequestr!   Xyfrozenr   r   r   test_frozen_methods*   s   
r,   T)Z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)selfr.   r/   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)r.   Zfitted_)r0   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   )r/   npZoneslen)r0   r)   r3   r   r   r   r"   g   s   z>test_frozen_metadata_routing.<locals>.ConsumesMetadata.predict)NNr   )__name__
__module____qualname__r1   r$   r"   r   r   r   r   ConsumesMetadata\   s    

r9   Tr-   )r3   testZconsumesmetadataFz]Pipeline.predict got unexpected argument(s) {'metadata'}, which are not routed to any object.matchN)r   r   Zset_fit_requestZset_predict_requestr$   r   r"   pytestraises	TypeErrorreescaper   )r   r9   r)   r*   Zpipeliner+   r   r   r   test_frozen_metadata_routingX   s4   
	"rB   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r0   r)   r*   r   r   r   r$      s   z)test_composite_fit.<locals>.Estimator.fitNc                 S      d S r   r   rE   r   r   r   fit_transform      z3test_composite_fit.<locals>.Estimator.fit_transformc                 S   rF   r   r   rE   r   r   r   fit_predict   rH   z1test_composite_fit.<locals>.Estimator.fit_predictr   )r6   r7   r8   r$   rG   rI   r   r   r   r   	Estimator   s    
rJ   Nr    )	r   r$   r   r=   r>   rD   rI   rG   rC   )r   rJ   r)   r*   estr+   r   r   r   test_composite_fit   s   rL   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'   r+   Zclonedr   r   r   test_clone_frozen   s
   rM   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   r=   r>   r   r   r$   )r   r)   r*   r'   r+   r   r   r   test_check_is_fitted   s   
rN   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 r2   )super__sklearn_tags__
input_tagscategorical)r0   tags	__class__r   r   rP      s   
z4test_frozen_tags.<locals>.Estimator.__sklearn_tags__)r6   r7   r8   rP   __classcell__r   r   rT   r   rJ      s    rJ   TFN)r   r   rP   Z
_skip_testrQ   rR   )rJ   r'   r+   Zfrozen_tagsZestimator_tagsr   r   r   test_frozen_tags   s   rW   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 innerr;   r    )Zestimator__CNr'   )r'   )r   r   r=   r>   
ValueErrorZ
set_params
get_paramsr   )rK   r+   Z	other_estr   r   r   test_frozen_params   s   rZ   )5r@   numpyr4   r=   Znumpy.testingr   Zsklearnr   Zsklearn.baser   r   r   r   r   r	   Zsklearn.clusterr
   Zsklearn.composer   Zsklearn.datasetsr   r   Zsklearn.exceptionsr   r   Zsklearn.frozenr   Zsklearn.linear_modelr   r   Zsklearn.neighborsr   Zsklearn.pipeliner   Zsklearn.preprocessingr   r   Zsklearn.utils._testingr   Zsklearn.utils.validationr   Zfixturer   r   markZparametrizer,   rB   rL   rM   rN   rW   rZ   r   r   r   r   <module>   sh    




.	