o
    i-                    @   s  d dl Z d dlmZ d dlmZ d dlmZmZ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 d dlmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> d d	l?m@Z@ d d
lAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZL d dlMmNZN d dlOmPZP d dlQmRZRmSZSmTZTmUZU d dlVmWZWmXZXmYZYmZZZm[Z[m\Z\ d dl]m^Z^m_Z_m`Z` d dlambZb d dlcmdZdmeZe i de)de*de/de0de-de2de+dedee7ddde;de<dee1d d d!e.d"e,d#ee1d$d d%eed$d d&ed'eiZfi d(ed)ed*eed+d,d-eed.d/d0ed1d2d3 d4e3d5ee3d+d6d7e"d8e>d9ee>d.d/d:e$d;e6d<e8d=e d>ee!d?d@dAee!dBd@i dCe(dDee!dEdBdFdGee dEdHdIee!dEd?dFdJee6dEdHdKee8dEdHdLee$dEdHdMee!dNdBdFdOee dNdHdPee!dNd?dFdQee6dNdHdRee8dNdHdSee$dNdHdTee!dUdBdFdVee dUdHdWee!dUd?dFdXee6dUdHee8dUdHee$dUdHee!dYdBdFee dYdHee!dYd?dFee6dYdHee8dYdHee$dYdHedZ	Zgd[d\ Zhe:ehed]Zii d^ed_e&d`e'daee'd.d/dbe#dcedde9deee9dEdHdfee9dYdHdgee9dNdHdhee9dUdidjdkee9dEdidjdlee9dUdmdjdnee9dEdmdjdoee9dBdpdqedreedEdHeedYdHeedNdHe%e4ee=dsZjek Zlelmej elmeg elmef elmei h dtZnh duZoenpeoZqh dvZrh dwZsh dxZth dyZuh dzZvh d{Zwh d|Zxh d}Zyh d~Zzh dZ{h dZ|h dZ}ddhZ~dd Zdd Zdd Zejdeezdd Zejdee{dd Zdd Zejdeeeleq dd Zdd Zejdeeeleq dd Zejdeeegeq dd Zejdejdd Zd dge
je
jgfd dge
je
jgfd dge
je
jgfd dge
jdgfd dge
jdgfgZejdeej ef ejdedd Zejdeg ejdee
jdd?gg dfe
jdd?gg dfg dd Zejdeg dd Zdd Zdd Zejdejdeeeleq eej dd ZejdejdeeyexB dd Zejdeeydd Zejdeeydd Zejdejde^dd Zejdeexdd Zejdeevdd Zejdeevdd Zejdeevexdd Zdd Zdd Zejdeerdd ZejdeeresB ddÄ Zejdeerddń ZddǄ ZejdeerddɄ Zdd˄ Zejdeeeleefe| dd̈́ Zejdeeeleefe| ddτ Zejdeeeleef e| en ddф Zejdeeeleef e| eq ddӄ ZejdeexewB e| ddՄ Zejdeeye| ddׄ Zddل Zejdeexd4h ddۄ ZejdeeweyB dd݄ Zejdeeejeq dd߄ Zejdegdd Zejded+fed+fe d.fee!dd@d.fe$d.fe5d+fe6d.fe8d.fe:d+fg	ejdeegdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zi eeeege eeege!eege$eeege3eeege6eeege8eeege>eeege"eeege1egee1dd egee1dd ege7eegeEegeeege*eege-eegi e/eege0eegeegeHege.egeBege,ege)ege+eegeCegeIegeDegeFegeGegeJegeKege;eege<eegeLegiZefddZejjdeU eTdejde dd Zejddd gejdeeldd Zdd Zejdeeldd ZdS (      N)partial)	signature)chainpermutationsproduct)config_context)make_multilabel_classification)UndefinedMetricWarning),accuracy_scoreaverage_precision_scorebalanced_accuracy_scorebrier_score_losscohen_kappa_scoreconfusion_matrixcoverage_errord2_absolute_error_scored2_pinball_scored2_tweedie_score	dcg_score	det_curveexplained_variance_scoref1_scorefbeta_scorehamming_loss
hinge_lossjaccard_score%label_ranking_average_precision_scorelabel_ranking_losslog_lossmatthews_corrcoef	max_errormean_absolute_errormean_absolute_percentage_errormean_gamma_deviancemean_pinball_lossmean_poisson_deviancemean_squared_errormean_squared_log_errormean_tweedie_deviancemedian_absolute_errormultilabel_confusion_matrix
ndcg_scoreprecision_recall_curveprecision_scorer2_scorerecall_scoreroc_auc_score	roc_curveroot_mean_squared_errorroot_mean_squared_log_errortop_k_accuracy_scorezero_one_loss)_average_binary_score)additive_chi2_kernelchi2_kernelcosine_distancescosine_similarityeuclidean_distanceslinear_kernelpaired_cosine_distancespaired_euclidean_distancespolynomial_kernel
rbf_kernelsigmoid_kernel)LabelBinarizer)shuffle)_atol_for_type_convert_to_numpy_get_namespace_device_dtype_ids)yield_namespace_device_dtype_combinations)_array_api_for_testsassert_allcloseassert_almost_equalassert_array_equalassert_array_lessignore_warnings)COO_CONTAINERSparse_version
sp_version)type_of_target)_num_samplescheck_random_stater    r!   r&   r'   r$   r)   r"   r   r.   Zvariance_weighted)multioutputr2   r3   mean_normal_deviance)powerr%   r#   mean_compound_poisson_deviancegffffff?r   r   r   r
   r    adjusted_balanced_accuracy_scoreT)adjustedunnormalized_accuracy_scoreF	normalizeunnormalized_confusion_matrixnormalized_confusion_matrixc                  O   s8   t | i |dt | i |jddd d tjf  S )Nfloat   axis)r   astypesumnpZnewaxis)argskwargs rh   p/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/sklearn/metrics/tests/test_common.py<lambda>   s   "rj   (unnormalized_multilabel_confusion_matrix/unnormalized_multilabel_confusion_matrix_sample)Z
samplewiser   r5   unnormalized_zero_one_lossr   r-   r/   r   f2_score   )beta
f0.5_score      ?matthews_corrcoef_scoreweighted_f0.5_scoreweightedaveragerp   weighted_f1_scorerw   weighted_f2_scoreweighted_precision_scoreweighted_recall_scoreweighted_jaccard_scoremicro_f0.5_scoremicromicro_f1_scoremicro_f2_scoremicro_precision_scoremicro_recall_scoremicro_jaccard_scoremacro_f0.5_scoremacromacro_f1_scoremacro_f2_scoremacro_precision_scoresamples)	macro_recall_scoremacro_jaccard_scoresamples_f0.5_scoresamples_f1_scoresamples_f2_scoresamples_precision_scoresamples_recall_scoresamples_jaccard_scorer   c               
   O   sR   t | i |\}}}t|t| }t||tj|tjd|fdtjgdgS )a'  
    The dimensions of precision-recall pairs and the threshold array as
    returned by the precision_recall_curve do not match. See
    func:`sklearn.metrics.precision_recall_curve`

    This prevents implicit conversion of return value triple to an higher
    dimensional np.array of dtype('float64') (it will be of dtype('object)
    instead). This again is needed for assert_array_equal to work correctly.

    As a workaround we pad the threshold array with NaN values to match
    the dimension of precision and recall arrays respectively.
    r   Zconstant)Z	pad_widthmodeZconstant_values)r,   lenre   arraypadrc   float64nan)rf   rg   	precisionZrecallZ
thresholdsZpad_threshholdsrh   rh   ri   (precision_recall_curve_padded_thresholds   s   
r   )r1   r,   r   r   r   r   unnormalized_log_lossr   r   r0   weighted_roc_aucsamples_roc_aucmicro_roc_aucZovr_roc_aucZovr)rw   Zmulti_classZweighted_ovr_roc_aucovo_roc_aucZovoweighted_ovo_roc_aucpartial_roc_auc)Zmax_fprr    weighted_average_precision_score)samples_average_precision_scoremicro_average_precision_scorer   r+   r   r4   >   r   rl   r   r   r+   r   r   r   r   r   r   r   >   r/   r   r   r   rq   r-   rn   r1   r,   r   r0   r   r   r   >   r/   r   rq   r-   rn   r   >   r   r0   r   >   r/   r   r   r   rq   r   r   r-   rn   r   r   r1   r,   r   >"   r   r   r|   rn   r   r1   rk   r   r   r-   r{   r~   r,   r   r^   r   r   r   r   rx   r   r   r}   r   rt   r/   rz   r   r   rl   r]   rq   r   r   >   r4   r
   r5   >   r   r   r   r   r+   r   r   r   r   r   r   r0   r   r   r   r   r   >   r   r|   r   rk   r   r   r{   r~   r   r   r   r   r   rx   rm   r
   r}   r   r   rt   rz   r   r   r   rZ   r5   r   r   r   r   >   r&   r   r2   r"   r$   r.   r)   r'   r   r   r3   r!   >   r2   r   r|   r'   r   r   r   r3   r$   r~   r   r   r   rs   r)   r   rU   rm   r
   r!   r   r   r&   r    rZ   r5   r   r   >   r   r   r"   rW   r   rn   r1   rk   r-   r   r{   r,   r^   r#   r   r   rx   r   r%   r   r}   rt   r/   rz   r   r]   rq   r.   rX   r   r   >   r    r   r)   r   >   r%   rW   r#   r   c                 C   s2   t t|  | d }| |7 } ||7 }| |fS )zMake targets strictly positiver`   )absminy1y2offsetrh   rh   ri   _require_positive_targetsB  s   r   c                 C   sJ   t t|  | d }| tj} |tj}| |7 } ||7 }| |fS )z$Make targets strictly larger than -1gGz?)r   r   rc   re   r   r   rh   rh   ri   _require_log1p_targetsJ  s   r   c                   C   s6   t tB ttB tB ttksJ t t@ t ksJ d S N)SYMMETRIC_METRICSNOT_SYMMETRIC_METRICSsetTHRESHOLDED_METRICS"METRIC_UNDEFINED_BINARY_MULTICLASSALL_METRICSrh   rh   rh   ri   test_symmetry_consistencyT  s   r   namec                 C   s   t d}|jdddd}|jdddd}| tv r t||\}}n| tv r+t||\}}|jdddd}|jdddd}t|  }| tv r\| tv rXt	||||||d|  d d S J d	t	||||||d|  d d S )
Nr   ro      sizer      z%s is not symmetricerr_msgFz This case is currently unhandled)
rS   randintMETRICS_REQUIRE_POSITIVE_Yr   METRICS_WITH_LOG1P_Yr   r   METRIC_UNDEFINED_BINARYMULTILABELS_METRICSrI   )r   random_statey_truey_predZ
y_true_binZ
y_pred_binmetricrh   rh   ri   test_symmetric_metric`  s.   

r   c           	      C   s   t d}t|  }d}tdD ]1}|jdddd}|jdddd}| tv r+t||\}}|||}|||}t||s?d} nq|rIt|  dd S )	Nr   T   ro   r   r   F seems to be symmetric)	rS   r   ranger   r   r   re   Zallclose
ValueError)	r   r   r   Zalways_symmetric_r   r   ZnominalZswappedrh   rh   ri   test_not_symmetric_metric  s"   

r   c                  C   s   d} d}t |  tjt| dd t | W d    n1 s!w   Y  t| tjt|  dd t|  W d    d S 1 sDw   Y  d S )Nr
   r/   z is not symmetricmatchr   )r   pytestraisesAssertionErrorr   r   )symZnot_symrh   rh   ri   test_symmetry_tests  s   

"r   c                 C   s   t d}|jdddd}|jdddd}| tv r t||\}}n| tv r+t||\}}t||dd\}}t  t|  }t	||||||d|  d W d    d S 1 sVw   Y  d S )Nr   ro   r   r   r    %s is not sample order invariantr   )
rS   r   r   r   r   r   rC   rM   r   rI   )r   r   r   r   y_true_shuffley_pred_shuffler   rh   rh   ri   test_sample_order_invariance  s    "r   c            	      C   s  t d} | jdddd}| jdddd}| j|jd}||jddd }t|||dd\}}}tD ]}t| }t||||||d	| d
 q1t	D ]}t| }t||||||d	| d
 qIt
D ]$}t| }t||||||d	| d
 t||||||d	| d
 qad S )Nr   ro   r   r   r`   Trb   Zkeepdimsr   r   r   )rS   r   uniformshaperd   rC   r   r   rI   THRESHOLDED_MULTILABEL_METRICSMULTIOUTPUT_METRICS)	r   r   r   y_scorer   r   Zy_score_shuffler   r   rh   rh   ri   7test_sample_order_invariance_multilabel_and_multioutput  sF   r   c              	   C   s  t d}|jdddd}|jdddd}| tv r t||\}}n| tv r+t||\}}t|}t|}t|t|}}t	|j
d t	|j
d t|d}t|d}	t|d}
t|d}t  t|  }|||}t||||d|  d	 t||||d
|  d	 t|||	|d|  d	 t||||d|  d	 t||||d|  d	 t|||	|d|  d	 t||||d|  d	 t|||	|d|  d	 t||||d|  d	 tt ||| W d    n1 sw   Y  tt ||
| W d    n	1 sw   Y  tt ||| W d    n	1 s'w   Y  tt ||
| W d    n	1 sBw   Y  tt ||| W d    n	1 s]w   Y  tt ||
|	 W d    n	1 sxw   Y  | ttB tB vrd| v rtt t||
|sJ W d    n	1 sw   Y  n+tt ||
| W d    n!1 sw   Y  W d    d S W d    d S W d    d S W d    d S 1 sw   Y  d S )Nr   ro   r   r   r`   )r`   )r`   r   z,%s is not representation invariant with listr   z3%s is not representation invariant with np-array-1dz7%s is not representation invariant with np-array-columnz@%s is not representation invariant with mix np-array-1d and listzK%s is not representation invariant with mix np-array-1d and np-array-columnzD%s is not representation invariant with mix list and np-array-columnZroc_auc)rS   r   r   r   r   r   listre   r   rK   ndimreshaperM   r   rI   r   r   r   r   r   r   Zwarnsr	   mathisnan)r   r   r   r   Zy1_listZy2_listZy1_1dZy2_1dZ	y1_columnZ	y2_columnZy1_rowZy2_rowr   measurerh   rh   ri   &test_format_invariance_with_1d_vectors  s   



	


 \ e$r   c                 C   sb  t d}|jdddd}|jdddd}tddg| }tddg| }d}ddg}t x t|  }|||}	|}
| tv rEt|
|d}
|
||}t|	|d	| d	 |
|
d
|
d
}t|	|d	| d	 | tv rt|
|d}
|
||}t|	|d	| d	 |
|
d
|
d
}t|	|d	| d	 W d    d S W d    d S 1 sw   Y  d S )Nr   ro   r   r   eggsspam	pos_label+{0} failed string vs number invariance testr   O2{0} failed string object vs number invariance test)labelsz,{0} failed string vs number  invariance test)rS   r   re   r   rM   CLASSIFICATION_METRICSMETRICS_WITH_POS_LABELr   rK   formatrc   METRICS_WITH_LABELS)r   r   r   r   y1_strZy2_strpos_label_strZ
labels_strr   measure_with_number
metric_strmeasure_with_strmeasure_with_strobjrh   rh   ri   7test_classification_invariance_string_vs_numbers_labelss  sT   


"r   c              	   C   sn  t d}|jdddd}|jdddd}tddg| }d}t  t|  }| tvr^|}| tv r7t||d}|||}|||}	t	||	d
| d	 ||d
|}
t	||
d
| d	 n?tt ||| W d    n1 ssw   Y  tt ||d
| W d    n1 sw   Y  W d    d S W d    d S W d    d S 1 sw   Y  d S )Nr   ro   r   r   r   r   r   r   r   r   r   )rS   r   re   r   rM   r   r   r   r   rK   r   rc   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   rh   rh   ri   4test_thresholded_invariance_string_vs_numbers_labels  sJ   

	"r   r`   r   zy_true, y_scorec                 C   sR   | t kr
|g}|g}tjtdd | || W d    d S 1 s"w   Y  d S )Nzcontains (NaN|infinity)r   )r   r   r   r   )r   r   r   rh   rh   ri   )test_regression_thresholded_inf_nan_input  s   "r   )r`   ro      c                 C   s   t | sd}t | rd}nd}nd}t | r"d}nd}d| d| }tjt|d | || W d   dS 1 sDw   Y  dS )	z{check that classification metrics raise a message mentioning the
    occurrence of non-finite values in the target vectors.r   NaNzinfinity or a value too larger   zInput z
 contains r   N)re   isfiniteallr   anyr   r   r   )r   r   r   Z
input_nameZunexpected_valuer   rh   rh   ri   !test_classification_inf_nan_input  s   "r  c                 C   sT   g dg d}}d}t jt|d | || W d   dS 1 s#w   Y  dS )zocheck that classification metrics raise a message of mixed type data
    with continuous/binary target vectors.)abr  皙?皙?333333?zJClassification metrics can't handle a mix of binary and continuous targetsr   N)r   r   r   )r   r   r   r   rh   rh   ri   +test_classification_binary_continuous_input  s   "r
  c                 C   s\   t |  }| tv rddg}n| tv rddg}nddg}t|ddD ]\}}||g|g q d S )Nr`   ro   gffffffr   repeat)r   r   r   r   )r   r   valuesijrh   rh   ri   check_single_sample  s   

r  c                 C   sL   t |  }tddgddD ]\}}}}|t||ggt||gg qd S )Nr   r`      r  )r   r   re   r   )r   r   r  r  klrh   rh   ri   check_single_sample_multioutput-  s   $r  ignorec                 C      t |  d S r   )r  r   rh   rh   ri   test_single_sample4  s   r  c                 C   r  r   )r  r  rh   rh   ri   test_single_sample_multioutputD  s   r  c                 C   sz   t g dg dg dg}t ddgddgddgg}t|  }tt ||| W d    d S 1 s6w   Y  d S )N)r`   r   r   r`   )r   r`   r`   r`   )r`   r`   r   r`   r   r`   re   r   r   r   r   r   )r   r   r   r   rh   rh   ri   (test_multioutput_number_of_output_differJ  s   "r  c                 C   s   t d}|jdddd}|jdddd}t|  }|||}tdD ]"}||jd }t||d d |f |d d |f |d|  d q!d S )	Nr   ro   r   r   r   r   r`   z'%s is not dimension shuffling invariantr   )rS   r   r   r   Zpermutationr   rI   )r   r   r   r   r   errorr   permrh   rh   ri   =test_multioutput_regression_invariance_to_dimension_shufflingT  s   
 r  z1ignore::sklearn.exceptions.UndefinedMetricWarningcoo_containerc                 C   s  d}d}t d|d|dd\}}t d|d|dd\}}t|dg| gg}t|dg| gg}| |}| |}t|}t|}	dd |D }
d	d |	D }tD ]:}t| }t|trad
|_||_	|||}t
||||d| d t||
||d| d t|||	|d| d qPd S )Nr  2   r`   r   T
n_features	n_classesr   	n_samplesallow_unlabeledc                 S      g | ]}t |qS rh   r   .0r  rh   rh   ri   
<listcomp>      z=test_multilabel_representation_invariance.<locals>.<listcomp>c                 S   r'  rh   r(  r)  rh   rh   ri   r+    r,  tmpzO%s failed representation invariance between dense and sparse indicator formats.r   z\%s failed representation invariance  between dense array and list of list indicator formats.zW%s failed representation invariance  between dense and list of array indicator formats.)r   re   vstackr   r   r   
isinstancer   
__module____name__rI   rJ   )r   r$  r%  r   r   r   Zy1_sparse_indicatorZy2_sparse_indicatorZy1_list_array_indicatorZy2_list_array_indicatorZy1_list_list_indicatorZy2_list_list_indicatorr   r   r   rh   rh   ri   )test_multilabel_representation_invarianceg  sh   

	

	
r2  c              	   C   s   dgdgddggg dg gdgt jg ddggddg}t|  }|D ]}tt ||| W d    n1 s8w   Y  q!d S )Nr`   ro   r   )rh   ro   )r   r`   rh   objectdtyper  )r   Zmultilabel_sequencesr   seqrh   rh   ri   +test_raise_value_error_multilabel_sequences  s   r7  c                 C   s   d}d}t d}|jd||fd}|jd||fd}|j|jd}t|  }| tv r+|n|}|||dd}	|||dd}
td|	 dd	d
 t|	|
| d|  d
 d S )Nro   r   r   r   Tr[   F      0We failed to test correctly the normalize optionr   Failed with )rS   r   normalr   r   r   rL   rI   r   r$  r%  r   r   r   r   metricspredmeasure_normalizedmeasure_not_normalizedrh   rh   ri   +test_normalize_option_binary_classification  s(   
rA  c                 C   s   d}d}t d}|jd||fd}|jd||fd}|j||fd}t|  }| tv r,|n|}|||dd}	|||dd}
td|	 dd	d
 t|	|
| d|  d
 d S )Nr  r   r   r   Tr[   Fr8  r9  r   r:  )rS   r   r   r   r   rL   rI   r<  rh   rh   ri   /test_normalize_option_multiclass_classification  s(   
rB  c                 C   s   d}d}t d}td|dd|d\}}td|dd|d\}}|j|jd}|dg| 7 }|dg| 7 }t|  }| tv r=|n|}	|||	dd}
|||	d	d}td
|
 ddd t|
|| d|  d d S )Nr  d   r   r`   T)r#  r$  r   r&  r%  r   r[   Fr8  r9  r   r:  )rS   r   r   r   r   r   rL   rI   )r   r$  r%  r   r   r   r   r   r=  r>  r?  r@  rh   rh   ri   /test_normalize_option_multilabel_classification  sD   


rD  c                    s  j \}} ||d d}t| fddt|D   ||dd}	t|	     ||dd}
t|
t| tjdtd}t|dkr_ ||dd}t|tj||d	 n ||dd}t|d |r ||d
d}t|t fddt|D  t	
t  ||dd W d    n1 sw   Y  t	
t  ||dd W d    d S 1 sw   Y  d S )Nry   c                    s.   g | ]} d d |f d d |f qS r   rh   r*  r  r   y_pred_binarizey_true_binarizerh   ri   r+  :  s     z$_check_averaging.<locals>.<listcomp>r   r   r   )rb   r5  ru   )weightsr   c                    s   g | ]} | | qS rh   rh   rE  rF  rh   ri   r+  Z  s    unknowngarbage)r   rI   r   Zravelre   meanrd   intrw   r   r   r   )r   r   r   rH  rG  is_multilabelr%  r$  Zlabel_measureZmicro_measureZmacro_measurerI  Zweighted_measureZsample_measurerh   rF  ri   _check_averaging1  sH   
	

"rO  c                 C   sZ   t |d}t|  }| tv rt|||||| d S | tv r)t|||||| d S td)NZ
multilabelz2Metric is not recorded as having an average option)rQ   
startswithr   METRICS_WITH_AVERAGINGrO  "THRESHOLDED_METRICS_WITH_AVERAGINGr   )r   r   rH  r   rG  r   rN  r   rh   rh   ri   check_averagingg  s   rS  c           
      C   sz   d\}}t d}|jd||fd}|jd||fd}|j||fd}t |}||}||}	t| ||||	| d S )N)r!  r   r   r   )rS   r   r   rB   fitZ	transformrS  )
r   r%  r$  r   r   r   r   ZlbrH  rG  rh   rh   ri   test_averaging_multiclassx  s   

rU  c           
      C   sh   d\}}t d|d|dd\}}|d d }|dd  }tdjd|fd}|}|}	t| ||||	| d S )	N)(   r   r`   r   Fr"  r   r   r   )r   rS   r;  rS  )
r   r%  r$  r   yr   r   r   rH  rG  rh   rh   ri   test_averaging_multilabel  s   
rX  c                 C   <   t d}t d}t d}|}|}t| ||||| d S Nr   r   )re   zerosrS  r   r   r   r   rH  rG  rh   rh   ri   $test_averaging_multilabel_all_zeroes     


r^  c                  C   s>   t d} t d}| }|}ddd}t|| |||dd d S )Nr[  r   c                 S   s   t t| ||S r   )r6   r-   )r   r   rw   rh   rh   ri   rj     s    z=test_averaging_binary_multilabel_all_zeroes.<locals>.<lambda>T)rN  )r   )re   r\  rO  )r   r   rH  rG  Zbinary_metricrh   rh   ri   +test_averaging_binary_multilabel_all_zeroes  s   



r`  c                 C   rY  rZ  )re   onesrS  r]  rh   rh   ri   "test_averaging_multilabel_all_ones  r_  rb  c                 C   s,  t jd}|jddt|d}| dkrt|ddn|}|||d d}t||||t jt|ddd	|  d
 ||||d}t	t
 t|| td||| f 1 sUw   Y  |||| d}t||d||| f d
 |t j||ddt j||ddd d}	t||	d|  d
 |dd d }
t |}d|d d d< |dd d }|dd d }||||
d}||||d}t||d||| f d
 | dsdD ]}t|||||| dd|  d
 qdt|t|t|d }tj	t|d |||t ||gd W d    d S 1 sw   Y  d S )Nr   r`   
   r   r4   )r  sample_weight)r   zAFor %s sample_weight=None is not equivalent to sample_weight=onesr   zQUnweighted and weighted scores are unexpectedly almost equal (%s) and (%s) for %szVWeighted scores for array and list sample_weight input are not equal (%s != %s) for %sra   z.Weighting %s is not equal to repeating samplesro   zeZeroing weights does not give the same result as removing the corresponding samples (%s != %s) for %sZunnormalized)ro   r	  z/%s sample_weight is not invariant under scalingzJFound input variables with inconsistent numbers of samples: \[{}, {}, {}\]r   )re   randomRandomStater   r   r   rI   ra  r   r   r   r   tolistr  copyrP  r   rR   Zhstack)r   r   r   r   rngre  Zunweighted_scoreZweighted_scoreZweighted_score_listZrepeat_weighted_scoreZsample_weight_subsetZsample_weight_zeroedZ	y1_subsetZ	y2_subsetZweighted_score_subsetZweighted_score_zeroedZscalingerror_messagerh   rh   ri   check_sample_weight_invariance  s   
	


	$rl  c                 C   sB   d}t d}|j|fd}|j|fd}t|  }t| ||| d S )Nr!  r   r   )rS   random_sampler   rl  )r   r%  r   r   r   r   rh   rh   ri   (test_regression_sample_weight_invariance&  s   rn  c                 C   s   d}t d}|j|fd}|j|fd}t|  }|j|d fd}tjtdd ||||d W d    n1 s:w   Y  |j|d fd|df}tjtd	d ||||d W d    d S 1 sgw   Y  d S )
Nr!  r   r   r`   z'Found input variables with inconsistentr   rd  ro   z)Sample weights must be 1D array or scalar)rS   rm  r   r   r   r   r   )r   r%  r   r   r   r   re  rh   rh   ri   *test_regression_with_invalid_sample_weight7  s   	"ro  c                 C   sr   d}t d}|jdd|fd}|jdd|fd}|j|fd}t|  }| tv r0t| ||| d S t| ||| d S )Nr!  r   ro   r   )rS   r   rm  r   r   rl  )r   r%  r   r   r   r   r   rh   rh   ri   $test_binary_sample_weight_invarianceQ  s   rp  c           	      C   s   d}t d}|jdd|fd}|jdd|fd}|j|dfd}t|  }| tv rCt| }||jdddd }t	| ||| d S t	| ||| d S )Nr!  r   r   r   r   ra   r`   )
rS   r   rm  r   r   re   exprd   r   rl  )	r   r%  r   r   r   r   r   tempZy_score_normrh   rh   ri   (test_multiclass_sample_weight_invarianceh  s   rs  c           	      C   s   t d}tdddddd\}}tdddddd\}}t||g}t||g}|j|jd}||jddd	 }t|  }| tv rIt	| ||| d S t	| ||| d S )
Nr   r`   rc  r!  Fr"  r   Tr   )
rS   r   re   r.  r   r   rd   r   r   rl  )	r   r   r   ZyaZybr   r   r   r   rh   rh   ri   (test_multilabel_sample_weight_invariance  s   	



rt  c                 C   sB   t d}|jdddd}|jdddd}t|  }t| ||| d S )Nr   ro   r  r   )rS   r   r   rl  )r   r   r   r   r   rh   rh   ri   )test_multioutput_sample_weight_invariance  s
   ru  c                  C   s   t g dg dg} t g dg dg}t g d}t g d}t g d}t j|dd\}}tD ]3}||g| |gfD ](\}}	|tvrN|	jd	krNq@t| }
|
||	|d d
}|
||	d d}t|||  q@q6d S )N)r`   r`   r   r   r   r   r`   r`   )r   r`   r`   r   )r   r`   ro   )r   ro   r   )r   r   r`   ro   T)Zreturn_inverser`   )r   rw   ry   )re   r   uniquerQ  r   r   r   rK   )Zy_true_multilabelZy_pred_multilabelZy_true_multiclassZy_pred_multiclassr   r   Zinverse_labelsr   r   r   r   Zscore_labelsscorerh   rh   ri   test_no_averaging_labels  s$   ry  c                 C   s   t d}d\}}|jdd||fd}|jdd||fd}t|  }|||}tt||D ]}|d d |f }	|d d |f }
||
|	}t|| q,d S )Nr   r   r  ro   r   )rS   r   r   r   r   rJ   r   r   r%  r$  r   r   r   rx  r  y_score_permy_true_permcurrent_scorerh   rh   ri   -test_multilabel_label_permutations_invariance  s   

r  c                 C   s   t d}d\}}|jdd||fd}|j|jd}||jddd }d||ddkdf< d||ddkdf< t|  }|||}tt||D ].}|d d |f }	|d d |f }
||
|	}|tkrqt	
|sjJ |d	kspJ qHt|| qHd S )
Nr   rz  ro   r   r`   Tr   r  g    .A)rS   r   r   r   rd   r   r   r   r"   re   r   rJ   r{  rh   rh   ri   ?test_thresholded_multilabel_multioutput_permutations_invariance  s$   

r  c                 C   s   d\}}t d}|||}t| }||jdddd }|jd||d}t|  }|||}tt	||D ]*}	tj
|td}
t||
t|	< |d d |
f }t|	|}|||}t|| q8d S )N)rC  r   r   r   ra   r`   r   r4  )rS   Zrandre   rq  rd   r   r   r   r   r   r\  rM  Zaranger   ZtakerJ   )r   r%  r$  r   r   rr  r   r   rx  r  Zinverse_permr|  r}  r~  rh   rh   ri   .test_thresholded_metric_permutation_invariance  s    

r  metric_namec                 C   s   t jd}t jdgd dgd  td}|jdd|jd}d	}tjt	|d
 t
|  || W d    d S 1 s:w   Y  d S )N*   r   r   r   ro   r4  r   r   z7Labels in y_true and y_pred should be of the same type.r   )re   rf  rg  r   r3  r   r   r   r   	TypeErrorr   )r  rj  r   r   r   rh   rh   ri   "test_metrics_consistent_type_error  s   "r  zmetric, y_pred_thresholddtype_y_strc           
      C   s   t jd}t jdgd dgd  |d}|jdd|jd}|s+t jddg|d| }d	}d
}t| jd j}|dkr=|n|}	t	j
t|	d | || W d    d S 1 sWw   Y  d S )Nr  r   r   r   ro   r4  r   r   zy_true takes value in {'eggs', 'spam'} and pos_label is not specified: either make y_true take value in {0, 1} or {-1, 1} or pass pos_label explicitzHpos_label=1 is not a valid label. It should be one of \['eggs', 'spam'\]r   r`   r   )re   rf  rg  r   r   r   r   
parametersdefaultr   r   r   )
r   Zy_pred_thresholdr  rj  r   r   Zerr_msg_pos_label_NoneZerr_msg_pos_label_1Zpos_label_defaultr   rh   rh   ri    test_metrics_pos_label_error_str$  s   "r  c              
   K   s  t ||}|j||d}|j||d}	| ||fi |}
|dd ur.|j|d |d|d< |d}t|tjrB|j||d|d< zt| t|	 d}W n tttfy^   d}Y nw |r| ||	fi |}t	||
t
|d | ||	fi |}t	||
t
|d | ||fi |}t	||
t
|d tdd! | ||	fi |}t	t||||
t
|d W d    d S 1 sw   Y  d S )N)devicere  rT   TF)Zatol)Zarray_api_dispatch)rH   Zasarraygetr/  re   ndarrayr  RuntimeErrorr   rI   rD   r   rE   )r   array_namespacer  
dtype_namea_npb_npmetric_kwargsZxpZa_xpZb_xpZ	metric_nprT   Znumpy_as_array_worksZ	metric_xpZmetric_xp_mixed_1Zmetric_xp_mixed_2rh   rh   ri   check_array_api_metricM  sZ   




"r  c              	   C   s^   t g d}t g d}t| |||||d d t jg d|d}t| ||||||d d S )Nrv  )r   r`   r   r`   r  r  re          r         @      ?r4  re   r   r  r   r  r  r  	y_true_np	y_pred_npre  rh   rh   ri   ,check_array_api_binary_classification_metric  s*   

r  c           
      C   s   t g d}t g d}ddd}t| |d}|D ])}t| |||f||d d| t jg d|d	}	t| |||f|||	d| qd S )
N)r   r`   ro   r   )r   r`   r   ro   r   r   ru   r  rr   g?rv   r   paramsr  r  r4  re   r   (_get_metric_kwargs_for_array_api_testingr  
r   r  r  r  r  r  Zadditional_paramsmetric_kwargs_combinationsr  re  rh   rh   ri   0check_array_api_multiclass_classification_metric  sH   r  c           
      C   s   t jddgddgddgg|d}t jddgddgddgg|d}ddd}t| |d}|D ])}t| |||f||d d| t jg d	|d}	t| |||f|||	d| q-d S )
Nr`   r   r4  r  r  rv   r  r  )r  r  r  r  r  rh   rh   ri   0check_array_api_multilabel_classification_metric  sH     r  c           	      C   s   t | tr	| jjn| j}|dkrttdk rtd tj	g d|d}tj	g d|d}i }t
| j}d|v r<d |d< t| |||f||d| d|v ritj	g d	|d|d< t| |||f||d| d S d S )
Nr%   z1.14.0zJmean_poisson_deviance's dependency `xlogy` is available as of scipy 1.14.0)r  r  r  g      @r4  )rr   rr   ro   ro   re  r  r  )r  r        ?rr   )r/  r   funcr1  rP   rO   r   skipre   r   r   r  r  )	r   r  r  r  	func_namer  r  r  Zmetric_paramsrh   rh   ri   !check_array_api_regression_metric  sL   



r  c                 C   s   t jg dg dg|d}t jg dg dg|d}t| |||||d d t jddg|d}t| ||||||d t| |||||t jg d	|dd
 t| |||||dd
 d S )N)r`   r   ro   )r`   ro   ro   r4  )r`   r  r  )r`   r`   r`   r  r  r  )r  r	  ffffff?)r  r  rT   Z
raw_valuesr  r  rh   rh   ri   -check_array_api_regression_metric_multioutput$  sN   



r  c                 C   s   t jg dg dg|d}t jg dg dg|d}i }dt| jv r9d|d< t| |||f||d| d	|d< t| |||f||d| d S )
Nr  )皙?rr   333333?r4  )r  r	  r  )rr   r  r  Zdense_outputFr  T)re   r   r   r  r  )r   r  r  r  ZX_npZY_npr  rh   rh   ri   check_array_api_metric_pairwiseU  s8   	
r  g      r  c                 c   s,    |   D ]\}}|D ]}||fV  qqd S r   )items)Zmetric_checkersr   Zcheckerscheckerrh   rh   ri   !yield_metric_checker_combinations  s   r  z#array_namespace, device, dtype_name)Zidszmetric, check_funcc                 C   s   || ||| d S r   rh   )r   r  r  r  Z
check_funcrh   rh   ri   test_array_api_compliance  s   r  df_lib_nameZpandasZpolarsc                 C   s|   t |}|g d}|g d}t|  }z|| | }W n ty3   t |  d Y nw t|||| d S )N)r  r  r   r  )r  r  r  r  z can not deal with 1d inputs)r   ZimportorskipZSeriesr   Zto_numpyr   r  rI   )r  r  Zdf_libr   r   r   Zexpected_metricrh   rh   ri   test_metrics_dataframe_series  s   
r  c           	      C   sb   i g}|  D ]'\}}|t| jvrqg }|D ]}|D ]}| }|||< || qq|}q|S )zHelper function to enable specifying a variety of additional params and
    their corresponding values, so that they can be passed to a metric function
    when testing for array api compliance.)r  r   r  ri  append)	r   r  r  paramr  Znew_combinationsrg   valueZ
new_kwargsrh   rh   ri   r    s   r  c                 C   s   t jd}|jdddd}|jdddd}| tv r!t||\}}| tv r5|jdddd}|jdddd}t|  }|||}t|t	t j
tfsIJ t|t jt jfrTJ t|trmtdd |D sotdd |D sqJ d	S d	S d	S )
zEnsure that the returned values of all metrics are consistent.

    It can either be a float, a numpy array, or a tuple of floats or numpy arrays.
    It should not be a numpy float64 or float32.
    r   ro   r   r   r[  c                 s   s    | ]}t |tV  qd S r   )r/  r_   r*  vrh   rh   ri   	<genexpr>*	  s    z2test_returned_value_consistency.<locals>.<genexpr>c                 s   s    | ]	}t |tjV  qd S r   )r/  re   r  r  rh   rh   ri   r  *	  s    
N)re   rf  rg  r   r   r   r   r   r/  r_   r  tupler   Zfloat32r  )r   rj  r   r   r   rx  rh   rh   ri   test_returned_value_consistency	  s$   

r  )r   	functoolsr   inspectr   	itertoolsr   r   r   numpyre   r   Zsklearn._configr   Zsklearn.datasetsr   Zsklearn.exceptionsr	   Zsklearn.metricsr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   Zsklearn.metrics._baser6   Zsklearn.metrics.pairwiser7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   Zsklearn.preprocessingrB   Zsklearn.utilsrC   Zsklearn.utils._array_apirD   rE   rF   rG   Zsklearn.utils._testingrH   rI   rJ   rK   rL   rM   Zsklearn.utils.fixesrN   rO   rP   Zsklearn.utils.multiclassrQ   Zsklearn.utils.validationrR   rS   ZREGRESSION_METRICSr   r   ZCURVE_METRICSr   dictr   updater   ZMETRIC_UNDEFINED_MULTICLASSunionr   rQ  rR  r   r   ZMETRICS_WITH_NORMALIZE_OPTIONr   r   r   r   r   ZMETRICS_WITHOUT_SAMPLE_WEIGHTr   r   r   r   r   markZparametrizesortedr   r   r   r   r   r   r   r   r   infr   Zinvalids_nan_infr  r   r  r
  r  r  filterwarningsr  r  r  r  r2  r7  rA  rB  intersectionrD  rO  rS  rU  rX  r^  r`  rb  rl  rn  ro  rp  rs  rt  ru  ry  r  r  r  r  strr3  r  r  r  r  r  r  r  r  Zarray_api_metric_checkersr  r  r  r  r  rh   rh   rh   ri   <module>   sX   .4 #	
	
 !"#$%&'()*+,
-




9 	

"




&"$$	

!

-
~
5
+		




	

K



/6





a



	
	


	

!

@((*1#(-./0459=AEILMNOPQUVWXYZ[\`h