U
    -e)                     @   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   d/var/www/html/Darija-Ai-Train/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
    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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)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st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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;   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@   	 	