U
    _»{jÍ)  ã                   @   sv  d dl m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 d dlmZmZmZ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#d.d/„ Z$G d0d1„ d1eƒZ%d2d3„ Z&G d4d5„ d5eƒZ'd6d7„ Z(G d8d9„ d9eƒZ)d:d;„ Z*dS )<é    )Ú
namedtupleN)Úassert_array_equal)Ú
csr_matrix)Úconfig_contextÚ
get_config)Ú_get_output_configÚ_safe_set_outputÚ_SetOutputMixinÚ_wrap_in_pandas_containerc                     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   úb/var/www/html/bwave/python/venv/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
    r1   c                   @   s   e Zd ZdS )Ú,EstimatorWithoutSetOutputAndWithoutTransformN©Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r2   5   s   r2   c                   @   s   e Zd Zddd„ZdS )Ú!EstimatorNoSetOutputWithTransformNc                 C   s   |S r   r   ©Úselfr$   Úyr   r   r   Ú	transform:   s    z+EstimatorNoSetOutputWithTransform.transform)N©r4   r5   r6   r;   r   r   r   r   r7   9   s   r7   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_r8   r   r   r   Úfit?   s    zEstimatorWithSetOutput.fitc                 C   s   |S r   r   r8   r   r   r   r;   C   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>G   s     z@EstimatorWithSetOutput.get_feature_names_out.<locals>.<listcomp>r   ©r   r   ÚrangerA   r   ©r9   Zinput_featuresr   r   r   Úget_feature_names_outF   s    z,EstimatorWithSetOutput.get_feature_names_out)N)N)N©r4   r5   r6   rB   r;   rK   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)r2   r   r7   r   r/   r0   r=   rB   r   r   r   r"   )ÚestÚconfigr   r   r   Útest__safe_set_outputJ   s    


rS   c                   @   s   e Zd Zddd„ZdS )Ú2EstimatorNoSetOutputWithTransformNoFeatureNamesOutNc                 C   s   |S r   r   r8   r   r   r   r;   f   s    z<EstimatorNoSetOutputWithTransformNoFeatureNamesOut.transform)Nr<   r   r   r   r   rT   e   s   rT   c                  C   s   t ƒ } t| dƒrt‚dS )zEEstimator without get_feature_names_out does not define `set_output`.Ú
set_outputN)rT   Úhasattrr"   )rQ   r   r   r   Útest_set_output_mixinj   s    rW   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   ÚbadrM   úoutput config must be 'default'r-   N)r   r   r=   r   r   r/   r0   r;   ©r$   rQ   Úmsgr   r   r   Útest__safe_set_output_errorp   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   NrM   )r   r   r   r   r=   rB   rU   r"   r;   r    Zndarrayr!   )r#   r$   rQ   Zest2Z
X_trans_npZ
X_trans_pdr   r   r   Útest_set_output_method|   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   rX   rM   rY   r-   N)	r   r   r=   rB   rU   r   r/   r0   r;   rZ   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;   rO   r   )r_   rP   rM   N)r   r   r"   r   r7   r=   rU   )Zglobal_configrR   rQ   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   r8   r   r   r   r;   ¼   s    z*EstimatorWithSetOutputNoAutoWrap.transform)Nr<   r   r   r   r   ra   »   s   ra   ©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.rU   r   r   r   N)ra   rV   r"   r   r   r;   )rQ   r$   r   r   r   Útest_get_output_auto_wrap_falseÀ   s    rc   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>.BadEstimatorNr3   r   r   r   r   ÚBadEstimatorÍ   s   rd   Zbad_parameterrb   )r   r/   r0   r	   )r[   rd   r   r   r   Ú6test_auto_wrap_output_keys_errors_with_incorrect_inputÉ   s    re   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)Úclsri   Úkwargs©Ú	__class__r   r   rh   Ò   s    zAnotherMixin.__init_subclass__)r4   r5   r6   rh   Ú__classcell__r   r   rl   r   rf   Ñ   s   rf   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   r8   r   r   r   r;   Û   s    zHtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.transformc                 S   s   |S r   r   rJ   r   r   r   rK   Þ   s    zTtest_set_output_mixin_custom_mixin.<locals>.BothMixinEstimator.get_feature_names_out)N)N)r4   r5   r6   r;   rK   r   r   r   r   ÚBothMixinEstimatorÚ   s   
ro   é{   )ri   rU   N)r	   rf   ri   r"   rV   )ro   rQ   r   r   r   Ú"test_set_output_mixin_custom_mixin×   s    rq   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)r0   r   r   r   r   Úget_columnsê   s    zAtest__wrap_in_pandas_container_column_errors.<locals>.get_columnsr   rN   r   é   é   )Zfeat1Zfeat2r   N)
r   r   r!   r
   r   r   r   r   rI   r@   )r#   rr   r+   Z	X_wrappedZX_npr   r   r   Ú,test__wrap_in_pandas_container_column_errorsæ   s    
ru   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   ©r9   r$   r   r   r   r;   þ   s    z+test_set_output_mro.<locals>.Base.transformNr<   r   r   r   r   rv   ý   s   rv   c                   @   s   e Zd ZdS )ztest_set_output_mro.<locals>.ANr3   r   r   r   r   ÚA  s   rx   c                   @   s   e Zd Zdd„ ZdS )ztest_set_output_mro.<locals>.Bc                 S   s   dS )NÚBr   rw   r   r   r   r;     s    z(test_set_output_mro.<locals>.B.transformNr<   r   r   r   r   ry     s   ry   c                   @   s   e Zd ZdS )ztest_set_output_mro.<locals>.CNr3   r   r   r   r   ÚC  s   rz   N)r	   r;   r"   )rv   rx   ry   rz   r   r   r   Útest_set_output_mro÷   s
    r{   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?   r8   r   r   r   rB     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   rD   r   r   r   rG     s     z9EstimatorWithSetOutputIndex.transform.<locals>.<listcomp>©r   )r   r!   Zto_numpyrI   r@   )r9   r$   r:   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 rC   r   rD   r   r   r   rG     s     zEEstimatorWithSetOutputIndex.get_feature_names_out.<locals>.<listcomp>r   rH   rJ   r   r   r   rK     s    z1EstimatorWithSetOutputIndex.get_feature_names_out)N)N)NrL   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   rN   r   rs   rt   é   r   r~   rM   Ús0Ús1N)	r   r   r!   r|   rU   rB   r;   r   r   )r#   r$   rQ   ÚX_transr   r   r   Ú!test_set_output_pandas_keep_index  s    
"

rƒ   c                   @   s   e Zd Zdd„ Zddd„ZdS )ÚEstimatorReturnTuplec                 C   s
   || _ d S r   ©ÚOutputTuple)r9   r†   r   r   r   Ú__init__-  s    zEstimatorReturnTuple.__init__Nc                 C   s   |   |d| ¡S )NrN   r…   r8   r   r   r   r;   0  s    zEstimatorReturnTuple.transform)N)r4   r5   r6   r‡   r;   r   r   r   r   r„   ,  s   r„   c                  C   s^   t ddƒ} t dddgg¡}t| d�}| |¡}t|| ƒs>t‚t|j|ƒ t|j	d| ƒ dS )z+Check that namedtuples are kept by default.ÚOutputzX, Yr   rN   r   r…   N)
r   r   r   r„   r;   r    r"   r   r$   ÚY)rˆ   r$   rQ   r‚   r   r   r   Útest_set_output_named_tuple_out4  s    


rŠ   c                   @   s*   e Zd Zddd„Zd	dd„Zd
dd„ZdS )ÚEstimatorWithListInputNc                 C   s    t |tƒst‚t|d ƒ| _| S )Nr   )r    Úlistr"   ÚlenrA   r8   r   r   r   rB   A  s    zEstimatorWithListInput.fitc                 C   s   |S r   r   r8   r   r   r   r;   F  s    z EstimatorWithListInput.transformc                 C   s   t jdd„ t| jƒD ƒtd�S )Nc                 S   s   g | ]}d |› �‘qS rC   r   rD   r   r   r   rG   J  s     z@EstimatorWithListInput.get_feature_names_out.<locals>.<listcomp>r   rH   rJ   r   r   r   rK   I  s    z,EstimatorWithListInput.get_feature_names_out)N)N)NrL   r   r   r   r   r‹   @  s   

r‹   c                  C   sl   t  d¡} ddddgdddd	gg}tƒ }|jdd
� | |¡ |¡}t|| jƒsTt‚t	|j
ddddgƒ dS )zJCheck set_output for list input.

    Non-regression test for #27037.
    r   r   r   rN   r   rs   rt   r   é   rM   ZX0ZX1ZX2ZX3N)r   r   r‹   rU   rB   r;   r    r!   r"   r   r   )r#   r$   rQ   ZX_outr   r   r   Útest_set_output_list_inputM  s    
r�   )+Úcollectionsr   Únumpyr   r   Znumpy.testingr   Zscipy.sparser   Zsklearn._configr   r   Zsklearn.utils._set_outputr   r   r	   r
   r%   r,   r1   r2   r7   r=   rS   rT   rW   r\   r]   r^   r`   ra   rc   re   rf   rq   ru   r{   r|   rƒ   r„   rŠ   r‹   r�   r   r   r   r   Ú<module>   s@   	 	