o
    i\8                  	   @   sD  d dl mZ d dlZd dl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	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ e Zeejejd d\ZZ Z!Z"dZ#e!$ Z%de%e#d< dddddZ&e'e&j(e%)e*Z+de+e%dk< dd Z,ej-.de eddd dgej-.dddgdd  Z/d!d" Z0d#d$ Z1d%d& Z2ej-.de eddd dgej-.d'e%e+gd(d) Z3d*d+ Z4ej-.d,e5d-d.d/d0 Z6d1d2 Z7d3d4 Z8d5d6 Z9ej-.d7dd8gd9d: Z:d;d< Z;d=d> Z<d?d@ Z=dAdB Z>dCdD Z?ej-@dEej-.dFg dGdHdI ZAdS )J    )ceilN)assert_array_equal)	load_iris
make_blobs)StackingClassifier)NotFittedError)accuracy_score)train_test_split)KNeighborsClassifier)SelfTrainingClassifier)SVC)SimpleEstimator)DecisionTreeClassifier)random_state2   ABCz-1)r         r   c                  C   s\   t t ddd} tjtdd | tt W d    n1 s w   Y  | jdks,J d S )Nk_besti  )	criterionr   zk_best is larger thanmatchall_labeled)	r   r
   pytestwarnsUserWarningfitX_trainy_train_missing_labelstermination_condition_st r%   j/home/kim/smarthome/.venv/lib/python3.10/site-packages/sklearn/semi_supervised/tests/test_self_training.pytest_warns_k_best&   s
   r'   	estimatorscaleTgammaprobabilityr   selection_crit	thresholdr   c                 C   s<  d}d}t | |||d}|tt |t}|t}t | |||d}|tt |t}|t}	tt	
tj|| t||	 |j|jksLJ tdk}
t|jdk|
 tt|
 |j|
  t	|j|j  krr|ksuJ  J t	|j|j  kr|ksJ  J |jj|jjksJ |jj|jjksJ d S )Ng      ?
   )max_iterr.   r   )r0   r   r.   r   r   )r   r   r    r!   predictX_testpredict_probay_train_missing_stringsr   np	vectorizemappinggetr"   labeled_iter_transduction_maxn_iter_shape)r(   r-   r.   r0   r$   predZprobaZ	st_stringZpred_stringZproba_stringZlabeledr%   r%   r&   test_classification.   s0   	




&&r?   c                  C   s   t tddddd d} tt}d|dd < tjd }t|d d }| t| | j	|ks0J t
| jdkdks<J td|D ]}t
| j|kdksOJ qAt
| j|k|d d ks`J | jdksgJ d S )	Nr   Zn_neighborsr   r/   )r   r   r0   r   r   r   )r   r
   r5   copyy_trainr=   r   r   r    r<   sumr9   ranger"   )r$   y_train_only_one_label	n_samplesn_expected_iterir%   r%   r&   test_k_best\   s"   

 rI   c                  C   s   t ddd} | ttd  ttd   t| }|tt | t|t}}t	
||r1J t| tt}t|tt}||ksGJ d S )Nr)   Tr+   r,   )r   r   r    n_labeled_samplesrB   r   r!   r1   r2   r5   Zarray_equalr   y_test)r(   r$   Zpred1Zpred2Zscore_supervisedZscore_self_trainingr%   r%   r&   test_sanity_classifications   s   rM   c                  C   s<   t t dd d} | tt | jdk sJ | jdksJ d S )Ng?)r.   r0   r/   r   )r   r
   r   r    r!   r<   r"   r#   r%   r%   r&   test_none_iter   s   rN   yc                 C   s\   t | dd}|t| | td t |d t }t|t|t |jdks,J d S )Nr   r0   r0   )r   r   r    rK   r   r1   r2   r"   )r(   rO   Zclf1Zclf2r%   r%   r&   test_zero_iterations   s
   rQ   c                  C   sX   t  } | tt t| }tjtdd |t W d    d S 1 s%w   Y  d S )Nz6This SelfTrainingClassifier instance is not fitted yetr   )	r
   r   r    rB   r   r   raisesr   r1   Zknnr$   r%   r%   r&   test_prefitted_throws_error   s   "rT   r0   r      c                 C   sd   t t | d}|tt t|j|jdk }|tksJ t	|j|j
  kr-| ks0J  J d S )NrP   r   )r   r
   r   r    r!   lenr9   rK   r5   r;   r<   )r0   r$   Zamount_iter_0r%   r%   r&   test_labeled_iter   s
   *rW   c                  C   s   t  } | tt t| }tjtdd |tt W d    n1 s%w   Y  t| 	t
|	t
 t|jdks?J |jdksFJ d S )Nzy contains no unlabeled samplesr   r   r   )r
   r   r    rB   r   r   r   r   r   r1   r2   r5   allr9   r"   rS   r%   r%   r&   test_no_unlabeled   s   rY   c                  C   s\   t ddd} t| }dgdgdgdgg}g d}||| |jdks%J |jdks,J d S )	Nr)   TrJ   r   r   g      ?)r   r   r   r   Z	no_change)r   r   r   r<   r"   )svcr$   ZX_train_easyZy_train_easyr%   r%   r&   test_early_stopping   s   r[   c                  C   sp   t t } tdddd\}}g d}t||}tjtdd | || W d    d S 1 s1w   Y  d S )N   r   g?)rF   r   Zcluster_std)onetwothreeZdtyper   )	r   r
   r   r5   Ztaker   rR   
ValueErrorr   )clfXrO   Zlabels_multiclassZ	y_stringsr%   r%   r&   test_strings_dtype   s   
"rc   verboseFc                 C   sJ   t t |d}|tt |  }|rd|jv sJ d S d|jvs#J d S )N)rd   	iteration)r   r
   r   r    r!   
readouterrout)capsysrd   ra   capturedr%   r%   r&   test_verbose   s   rj   c                 C   s   t tdddddd d}tt}d|dd < tjd }t|d d }|t| | 	 }d	}t
d|D ]}||d|jv sBJ q5|||d d |jv sRJ d S )
Nr   r@   r   r/   T)r   r   rd   r0   r   r   z)End of iteration {}, added {} new labels.)r   r
   r5   rA   rB   r=   r   r   r    rf   rD   formatrg   )rh   r$   rE   rF   rG   ri   msgrH   r%   r%   r&   test_verbose_k_best   s"   

"rm   c            	      C   s   t dddd} t| dddd}td	k}|tt | |jd	k@ }| t| t|  | t|  }tj|dd
}t|  t	|dd   }tt
|  }| D ]}||v s]J qUd S )Nr)   Tr   r*   r   r   r/   )r   r0   r   r   )Zaxisi)r   r   r!   r   r    r:   r3   r5   r;   Zargsortwheretolist)	rZ   r$   Z	has_labelZ	got_labelr>   Z	max_probaZmost_confident_svcZadded_by_strowr%   r%   r&   test_k_best_selects_best  s   rq   c                  C   s   t dtddfdtddfgtdddd} t| dsJ t| d}|tt |t t dtd	dfdtd	dfgtd	ddd} t| drJJ t| d}t	
t |tt W d    d S 1 sfw   Y  d S )
NZsvc_1T)r,   Zsvc_2r   )Z
estimatorsZfinal_estimatorZcvr3   r(   F)r   r   hasattrr   r   r    r!   r3   r2   r   rR   AttributeError)r(   ra   r%   r%   r&   test_estimator_meta_estimator  s.   	

	
"ru   c                  C   s   t ddd} t| }tjtdd |tt W d   n1 s"w   Y  tt d}d}d	}tjt|d}|tt	t W d   n1 sLw   Y  t
|jjtsZJ |t|jjv sdJ dS )
a>  Check that we raise the proper AttributeErrors when the `estimator`
    does not implement the `predict_proba` method, which is called from within
    `fit`, or `decision_function`, which is decorated with `available_if`.

    Non-regression test for:
    https://github.com/scikit-learn/scikit-learn/issues/28108
    Fr)   )r,   r+   z has no attribute 'predict_proba'r   Nrr   zBThis 'SelfTrainingClassifier' has no attribute 'decision_function'zD'DecisionTreeClassifier' object has no attribute 'decision_function')r   r   r   rR   rt   r   r    r!   r   decision_function
isinstancevalue	__cause__str)r(   Zself_trainingZ	outer_msgZ	inner_msgZ	exec_infor%   r%   r&   ,test_self_training_estimator_attribute_error@  s   r{   c                  C   s   d} t jt| d tt dtt W d    n1 sw   Y  d}t jt	|d t tt W d    n1 s>w   Y  d}t jt	|d tt t dtt W d    d S 1 sdw   Y  d S )Nz?`base_estimator` has been deprecated in 1.6 and will be removedr   )base_estimatorz4You must pass an estimator to SelfTrainingClassifierz;You must pass only one estimator to SelfTrainingClassifier.)r|   r(   )
r   r   FutureWarningr   r   r   r    r!   rR   r`   )Zwarn_msg	error_msgr%   r%   r&   'test_deprecation_warning_base_estimator^  s"   "r   z2ignore:y contains no unlabeled samples:UserWarningmethod)rv   Zpredict_log_probar3   r1   c                 C   s   t t d}tjtdd |jdgdggddgdgdd W d   n1 s(w   Y  t t d}tjtdd d|_t|| dggdgdd W d   dS 1 sVw   Y  dS )	zTest that the right error message is raised when metadata is passed while
    not supported when `enable_metadata_routing=False`.rr   z1is only supported if enable_metadata_routing=Truer   r   a)Zsample_weightpropNT)r   r   r   rR   r`   r   Zfitted_params_getattr)r   Zestr%   r%   r&   *test_routing_passed_metadata_not_supportedt  s   ""r   )Bmathr   numpyr5   r   Znumpy.testingr   Zsklearn.datasetsr   r   Zsklearn.ensembler   Zsklearn.exceptionsr   Zsklearn.metricsr   Zsklearn.model_selectionr	   Zsklearn.neighborsr
   Zsklearn.semi_supervisedr   Zsklearn.svmr   Zsklearn.tests.test_pipeliner   Zsklearn.treer   Zirisdatatargetr    r2   rB   rL   rK   rA   r!   r7   r6   r8   Zastypeobjectr4   r'   markZparametrizer?   rI   rM   rN   rQ   rT   rD   rW   rY   r[   rc   rj   rm   rq   ru   r{   r   filterwarningsr   r%   r%   r%   r&   <module>   sx    
)


$
