U
    md$                     @   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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<   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st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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:   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<   	 	