U
    ½mœd³$  ã                   @   sR  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"G d2d3„ d3eƒZ#d4d5„ Z$dS )6é    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   ú\/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/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   sv   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	|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   2   s   r1   c                   @   s   e Zd Zddd„ZdS )Ú!EstimatorNoSetOutputWithTransformNc                 C   s   |S r   r   ©Úselfr#   Úyr   r   r   Ú	transform7   s    z+EstimatorNoSetOutputWithTransform.transform)N©r3   r4   r5   r:   r   r   r   r   r6   6   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>D   s     z@EstimatorWithSetOutput.get_feature_names_out.<locals>.<listcomp>r   ©r   r   Úranger@   r   ©r8   Zinput_featuresr   r   r   Úget_feature_names_outC   s    z,EstimatorWithSetOutput.get_feature_names_out)N)N)N©r3   r4   r5   rA   r:   rJ   r   r   r   r   r<   ;   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s¤t‚t| dd� t
d| ƒ}|d	 d
ksÊt‚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<   rA   r   r   r	   r!   )ÚestÚconfigr   r   r   Útest__safe_set_outputG   s    


rR   c                   @   s   e Zd Zddd„ZdS )Ú2EstimatorNoSetOutputWithTransformNoFeatureNamesOutNc                 C   s   |S r   r   r7   r   r   r   r:   c   s    z<EstimatorNoSetOutputWithTransformNoFeatureNamesOut.transform)Nr;   r   r   r   r   rS   b   s   rS   c                  C   s   t ƒ } t| dƒrt‚dS )zEEstimator without get_feature_names_out does not define `set_output`.Ú
set_outputN)rS   Úhasattrr!   )rP   r   r   r   Útest_set_output_mixing   s    rV   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   ÚbadrL   úoutput config must be 'default'r,   N)r   r   r<   r   r   r.   r/   r:   ©r#   rP   Úmsgr   r   r   Útest__safe_set_output_errorm   s    r[   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ƒsˆt‚dS )z Check that the output is pandas.r
   r   r   r   NrL   )r   r   r   r   r<   rA   rT   r!   r:   r   Zndarrayr    )r"   r#   rP   Zest2Z
X_trans_npZ
X_trans_pdr   r   r   Útest_set_output_methody   s    


r\   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   rW   rL   rX   r,   N)	r   r   r<   rA   rT   r   r.   r/   r:   rY   r   r   r   Útest_set_output_method_errorŒ   s    r]   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s†t‚|jdd� td|ƒ}|d dks¬t‚W 5 Q R X |jdd� td|ƒ}|d dksÜt‚dS )	z+Check _get_output_config works as expected.Útransform_outputr:   rN   r
   )r^   rO   rL   N)r   r	   r!   r   r6   r<   rT   )Zglobal_configrQ   rP   r   r   r   Útest__get_output_config˜   s$    




r_   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   r`   ¸   s   r`   ©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.rT   r   r   r   N)r`   rU   r!   r   r   r:   )rP   r#   r   r   r   Útest_get_output_auto_wrap_false½   s    rb   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   rc   Zbad_parameterra   )r   r.   r/   r   )rZ   rc   r   r   r   Ú6test_auto_wrap_output_keys_errors_with_incorrect_inputÆ   s    rd   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)Úclsrh   Úkwargs©Ú	__class__r   r   rg   Ï   s    zAnotherMixin.__init_subclass__)r3   r4   r5   rg   Ú__classcell__r   r   rk   r   re   Î   s   re   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   rI   r   r   r   rJ   Û   s    zTtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.get_feature_names_out)N)N)r3   r4   r5   r:   rJ   r   r   r   r   ÚBothMixinEstimator×   s   
rn   é{   )rh   rT   N)r   re   rh   r!   rU   )rn   rP   r   r   r   Ú"test_set_output_mixin_custom_mixinÔ   s    rp   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   rM   r   é   é   )Zfeat1Zfeat2r   N)
r   r   r    r   r   r   r   r   rH   r?   )r"   rq   r*   Z	X_wrappedZX_npr   r   r   Ú,test__wrap_in_pandas_container_column_errorsã   s    
rt   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   ru   ú   s   ru   c                   @   s   e Zd ZdS )ztest_set_output_mro.<locals>.ANr2   r   r   r   r   ÚAþ   s   rw   c                   @   s   e Zd Zdd„ ZdS )ztest_set_output_mro.<locals>.Bc                 S   s   dS )NÚBr   rv   r   r   r   r:     s    z(test_set_output_mro.<locals>.B.transformNr;   r   r   r   r   rx     s   rx   c                   @   s   e Zd ZdS )ztest_set_output_mro.<locals>.CNr2   r   r   r   r   ÚC  s   ry   N)r   r:   r!   )ru   rw   rx   ry   r   r   r   Útest_set_output_mroô   s
    rz   c                   @   s*   e Zd Zddd„Zd	dd„Zd
dd„ZdS )ÚEstimatorWithSetOutputIndexNc                 C   s   |j d | _| S r=   r>   r7   r   r   r   rA     s    zEstimatorWithSetOutputIndex.fitc                 C   s.   dd l }|j| ¡ dd„ t|jd ƒD ƒd�S )Nr   c                 S   s   g | ]}d |› �‘qS )Úsr   rC   r   r   r   rF     s     z9EstimatorWithSetOutputIndex.transform.<locals>.<listcomp>©r   )r
   r    Zto_numpyrH   r?   )r8   r#   r9   r"   r   r   r   r:     s    z%EstimatorWithSetOutputIndex.transformc                 C   s   t jdd„ t| jƒD ƒtd�S )Nc                 S   s   g | ]}d |› �‘qS rB   r   rC   r   r   r   rF     s     zEEstimatorWithSetOutputIndex.get_feature_names_out.<locals>.<listcomp>r   rG   rI   r   r   r   rJ     s    z1EstimatorWithSetOutputIndex.get_feature_names_out)N)N)NrK   r   r   r   r   r{     s   

r{   c                  C   sb   t  d¡} | jdddgdddggddgd	�}tƒ jdd
�}| |¡ | |¡}t|jddgƒ dS )zZCheck that set_output does not override index.

    Non-regression test for gh-25730.
    r
   r   rM   r   rr   rs   é   r   r}   rL   Ús0Ús1N)	r   r   r    r{   rT   rA   r:   r   r   )r"   r#   rP   ZX_transr   r   r   Ú!test_set_output_pandas_keep_index  s    
"

r�   )%r   Ú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<   rR   rS   rV   r[   r\   r]   r_   r`   rb   rd   re   rp   rt   rz   r{   r�   r   r   r   r   Ú<module>   s<   	 	