U
    3d                      @   s:  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	 d dl
mZ d dl
mZ d dl
mZ d dl
mZ d	d
 Zdd Zdd ZG dd dZG dd dZG dd deZdd ZG dd deZdd Zdd Zdd Zdd  Zd!d" ZG d#d$ d$edd%Zd&d' Zd(d) ZG d*d+ d+Zd,d- Z d.d/ Z!d0d1 Z"dS )2    N)
csr_matrix)assert_array_equal)config_context
get_config)_wrap_in_pandas_container)_safe_set_output)_SetOutputMixin)_get_output_configc                     s   t d} tdddgdddgg}tjdddgtd tddg}t| fd	d
|d}t|| jsltt	|j
  t	|j| dS )z/Check _wrap_in_pandas_container for dense data.pandas   r      f0f1f2Zdtypec                      s    S N r   columnsr   G/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/utils/tests/test_set_output.py<lambda>       z6test__wrap_in_pandas_container_dense.<locals>.<lambda>r   indexN)pytestimportorskipnpasarrayobjectr   
isinstance	DataFrameAssertionErrorr   r   r   )pdXr   Zdense_namedr   r   r   $test__wrap_in_pandas_container_dense   s    
r$   c                  C   st   t d} | jdddgdddggdddgd}tjd	d
dgtd}ddg}t|||d}t|j| t|j	| dS )zACheck that _wrap_in_pandas_container overrides columns and index.r
   r   r   r   abcr   r   r   r   r   
      r   N)
r   r   r    r   r   r   r   r   r   r   )r"   X_dfZnew_columnsZ	new_indexZnew_dfr   r   r   =test__wrap_in_pandas_container_dense_update_columns_and_index   s    
$r+   c               	   C   sV   t dddgdddgg} t| }d}tjt|d t|dddgd	 W 5 Q R X d
S )z*Check errors in _wrap_in_pandas_container.r   r   r   z*Pandas output does not support sparse datamatchr%   r&   r'   r   N)r   r   r   r   raises
ValueErrorr   )r#   ZX_csrr-   r   r   r   /test__wrap_in_pandas_container_error_validation'   s
    r0   c                   @   s   e Zd ZdS ),EstimatorWithoutSetOutputAndWithoutTransformN__name__
__module____qualname__r   r   r   r   r1   0   s   r1   c                   @   s   e Zd ZdddZdS )!EstimatorNoSetOutputWithTransformNc                 C   s   |S r   r   selfr#   yr   r   r   	transform5   s    z+EstimatorNoSetOutputWithTransform.transform)Nr3   r4   r5   r:   r   r   r   r   r6   4   s   r6   c                   @   s*   e Zd ZdddZd	ddZd
ddZdS )EstimatorWithSetOutputNc                 C   s   |j d | _| S )Nr   )shapen_features_in_r7   r   r   r   fit:   s    zEstimatorWithSetOutput.fitc                 C   s   |S r   r   r7   r   r   r   r:   >   s    z EstimatorWithSetOutput.transformc                 C   s   t jdd t| jD tdS )Nc                 S   s   g | ]}d | qS )r#   r   ).0ir   r   r   
<listcomp>B   s     z@EstimatorWithSetOutput.get_feature_names_out.<locals>.<listcomp>r   )r   r   ranger>   r   r8   Zinput_featuresr   r   r   get_feature_names_outA   s    z,EstimatorWithSetOutput.get_feature_names_out)N)N)N)r3   r4   r5   r?   r:   rE   r   r   r   r   r<   9   s   

r<   c               	   C   s   t  } t| dd t } tjtdd t| dd W 5 Q R X t t	dddgg} t| dd t
d| }|d	 dks~tt| d
d t
d| }|d	 d
kstt| dd t
d| }|d	 d
kstdS )z)Check _safe_set_output works as expected.r
   r:   zUnable to configure outputr,   r      r   r:   densedefaultN)r1   r   r6   r   r.   r/   r<   r?   r   r   r	   r!   )estconfigr   r   r   test__safe_set_outputE   s    


rL   c                   @   s   e Zd ZdddZdS )2EstimatorNoSetOutputWithTransformNoFeatureNamesOutNc                 C   s   |S r   r   r7   r   r   r   r:   a   s    z<EstimatorNoSetOutputWithTransformNoFeatureNamesOut.transform)Nr;   r   r   r   r   rM   `   s   rM   c                  C   s   t  } t| drtdS )zEEstimator without get_feature_names_out does not define `set_output`.
set_outputN)rM   hasattrr!   )rJ   r   r   r   test_set_output_mixine   s    rP   c               	   C   sX   t dddgdddgg} t }t|dd d}tjt|d ||  W 5 Q R X dS )	z$Check transform with invalid config.r   r   r   badrF   output config must be 'default'r,   N)r   r   r<   r   r   r.   r/   r:   r#   rJ   msgr   r   r   test__safe_set_output_errork   s    rU   c                  C   s   t d} tdddgdddgg}t |}|jdd}||ksHt||}t	|tj
sbt|jdd ||}t	|| jstdS )z Check that the output is pandas.r
   r   r   r   NrF   )r   r   r   r   r<   r?   rN   r!   r:   r   Zndarrayr    )r"   r#   rJ   Zest2Z
X_trans_npZ
X_trans_pdr   r   r   test_set_output_methodw   s    


rV   c               	   C   s^   t dddgdddgg} t | }|jdd d}tjt|d ||  W 5 Q R X dS )	z-Check transform fails with invalid transform.r   r   r   rQ   rF   rR   r,   N)	r   r   r<   r?   rN   r   r.   r/   r:   rS   r   r   r   test_set_output_method_error   s    rW   c               	   C   s   t  d } td}|d | ks"ttdd td}|d dksFtt }td|}|d dksftt }td|}|d dkst|jdd td|}|d dkstW 5 Q R X |jdd td|}|d dkstdS )	z+Check _get_output_config works as expected.transform_outputr:   rH   r
   )rX   rI   rF   N)r   r	   r!   r   r6   r<   rN   )Zglobal_configrK   rJ   r   r   r   test__get_output_config   s$    




rY   c                   @   s   e Zd ZdddZdS ) EstimatorWithSetOutputNoAutoWrapNc                 C   s   |S r   r   r7   r   r   r   r:      s    z*EstimatorWithSetOutputNoAutoWrap.transform)Nr;   r   r   r   r   rZ      s   rZ   Zauto_wrap_output_keysc                  C   sD   t  } t| drttdddgdddgg}|| |ks@tdS )z4Check that auto_wrap_output_keys=None does not wrap.rN   r   r   r   N)rZ   rO   r!   r   r   r:   )rJ   r#   r   r   r   test_get_output_auto_wrap_false   s    r\   c               	   C   s6   d} t jt| d G dd dtdd}W 5 Q R X d S )Nz6auto_wrap_output_keys must be None or a tuple of keys.r,   c                   @   s   e Zd ZdS )zLtest_auto_wrap_output_keys_errors_with_incorrect_input.<locals>.BadEstimatorNr2   r   r   r   r   BadEstimator   s   r]   Zbad_parameterr[   )r   r.   r/   r   )rT   r]   r   r   r   6test_auto_wrap_output_keys_errors_with_incorrect_input   s    r^   c                       s   e Zd Z fddZ  ZS )AnotherMixinc                    s   t  jf | || _d S r   )super__init_subclass__custom_parameter)clsrb   kwargs	__class__r   r   ra      s    zAnotherMixin.__init_subclass__)r3   r4   r5   ra   __classcell__r   r   re   r   r_      s   r_   c                  C   s<   G dd dt tdd} |  }|jdks*tt|ds8tdS )z9Check that multiple init_subclasses passes parameters up.c                   @   s    e Zd ZdddZdddZdS )z>test_set_output_mixin_custom_mixin.<locals>.BothMixinEstimatorNc                 S   s   |S r   r   r7   r   r   r   r:      s    zHtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.transformc                 S   s   |S r   r   rD   r   r   r   rE      s    zTtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.get_feature_names_out)N)N)r3   r4   r5   r:   rE   r   r   r   r   BothMixinEstimator   s   
rh   {   )rb   rN   N)r   r_   rb   r!   rO   )rh   rJ   r   r   r   "test_set_output_mixin_custom_mixin   s    rj   c                  C   s   t d} dd }| dddgdddgd	}t||d
}t|j|j tddgddgddgg}t||d
}t|jt|j	d  dS )zJIf a callable `columns` errors, it has the same semantics as columns=None.r
   c                   S   s   t dd S )NzNo feature names defined)r/   r   r   r   r   get_columns   s    zAtest__wrap_in_pandas_container_column_errors.<locals>.get_columnsr   rG   r         )Zfeat1Zfeat2r   N)
r   r   r    r   r   r   r   r   rC   r=   )r"   rk   r*   Z	X_wrappedZX_npr   r   r   ,test__wrap_in_pandas_container_column_errors   s    
rn   c                  C   sZ   G dd dt } G dd d| }G dd d| }G dd d||}| d	dksVtd	S )
zjCheck that multi-inheritance resolves to the correct class method.

    Non-regression test gh-25293.
    c                   @   s   e Zd Zdd ZdS )z!test_set_output_mro.<locals>.Basec                 S   s   dS )NBaser   r8   r#   r   r   r   r:      s    z+test_set_output_mro.<locals>.Base.transformNr;   r   r   r   r   ro      s   ro   c                   @   s   e Zd ZdS )ztest_set_output_mro.<locals>.ANr2   r   r   r   r   A   s   rq   c                   @   s   e Zd Zdd ZdS )ztest_set_output_mro.<locals>.Bc                 S   s   dS )NBr   rp   r   r   r   r:      s    z(test_set_output_mro.<locals>.B.transformNr;   r   r   r   r   rr      s   rr   c                   @   s   e Zd ZdS )ztest_set_output_mro.<locals>.CNr2   r   r   r   r   C  s   rs   N)r   r:   r!   )ro   rq   rr   rs   r   r   r   test_set_output_mro   s
    rt   )#r   Znumpyr   Zscipy.sparser   Znumpy.testingr   Zsklearn._configr   r   Zsklearn.utils._set_outputr   r   r   r	   r$   r+   r0   r1   r6   r<   rL   rM   rP   rU   rV   rW   rY   rZ   r\   r^   r_   rj   rn   rt   r   r   r   r   <module>   s8   	 	