o
    ôj
<  ã                   @   s²   d dl Z d dlm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 ddlmZ dd	lmZmZ G d
d„ dƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )é    N)ÚMappingé   )Úcheck_consistent_length)Úcheck_matplotlib_support)Ú_get_response_values_binary)Úparse_version)Útype_of_target)Ú_check_pos_label_consistencyÚ_num_samplesc                   @   sp   e Zd ZdZdddœdd„Zeddddœdd	„ƒZedddd
œdd„ƒZedd„ ƒZe	dd„ ƒZ
e	dd„ ƒZdS )Ú"_BinaryClassifierCurveDisplayMixinzØMixin class to be used in Displays requiring a binary classifier.

    The aim of this class is to centralize some validations regarding the estimator and
    the target and gather the response of the estimator.
    N)ÚaxÚnamec                C   sZ   t | jj› d�ƒ dd lm} |d u r| ¡ \}}|d u r't| dt| dd ƒƒ}||j|fS )Nz.plotr   Úestimator_namer   )r   Ú	__class__Ú__name__Úmatplotlib.pyplotÚpyplotÚsubplotsÚgetattrÚfigure)Úselfr   r   ÚpltÚ_© r   új/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/sklearn/utils/_plotting.pyÚ_validate_plot_params   s   z8_BinaryClassifierCurveDisplayMixin._validate_plot_paramsÚauto)Úresponse_methodÚ	pos_labelr   c                C   sB   t | j› d�ƒ |d u r|jjn|}t||||d�\}}|||fS )Nz.from_estimator)r   r   )r   r   r   r   )ÚclsÚ	estimatorÚXÚyr   r   r   Úy_predr   r   r   Ú!_validate_and_get_response_values$   s   
ü
zD_BinaryClassifierCurveDisplayMixin._validate_and_get_response_values)Úsample_weightr   r   c                C   s^   t | j› d�ƒ t|ƒdkrtdt|ƒ› d�ƒ‚t|||ƒ t||ƒ}|d ur)|nd}||fS )Nz.from_predictionsÚbinaryz The target y is not binary. Got ú type of target.Z
Classifier)r   r   r   Ú
ValueErrorr   r	   )r   Úy_truer#   r%   r   r   r   r   r   Ú!_validate_from_predictions_params5   s   ÿ
zD_BinaryClassifierCurveDisplayMixin._validate_from_predictions_paramsc          
   
      s   t | j› d�ƒ ddh}t‡ fdd„|D ƒƒstd|› d�ƒ‚tˆ d d d	 ƒtˆ d d
 d	 ƒ}}t|ƒ|| krKtd|| › dt|ƒ› d�ƒ‚t|ƒdkr[tdt|ƒ› d�ƒ‚t|||ƒ zt||ƒ}W |S  ty }	 z
tt	|	ƒ 
dd¡ƒ‚d }	~	ww )Nz.from_cv_resultsr    Úindicesc                 3   s   � | ]}|ˆ v V  qd S )Nr   ©Ú.0Úkey©Ú
cv_resultsr   r   Ú	<genexpr>U   s   € zV_BinaryClassifierCurveDisplayMixin._validate_from_cv_results_params.<locals>.<genexpr>zB`cv_results` does not contain one of the following required keys: zr. Set explicitly the parameters `return_estimator=True` and `return_indices=True` to the function`cross_validate`.Útrainr   Útestz=`X` does not contain the correct number of samples. Expected z, got Ú.r&   z"The target `y` is not binary. Got r'   r)   r"   )r   r   Úallr(   Úlenr
   r   r   r	   ÚstrÚreplace)
r   r0   r!   r"   r%   r   Úrequired_keysZ
train_sizeZ	test_sizeÚer   r/   r   Ú _validate_from_cv_results_paramsH   s>   
ÿÿþÿÿÿÿü€þzC_BinaryClassifierCurveDisplayMixin._validate_from_cv_results_paramsc                 C   s`   | dur|dur|› d|› d| d›d�}|S | dur$|› d| d›�}|S |dur,|}|S d}|S )z;Helper to get legend label using `name` and `legend_metric`Nz (z = ú0.2fú)r   )Úcurve_legend_metricÚ
curve_nameÚlegend_metric_nameÚlabelr   r   r   Ú_get_legend_labelv   s   úüÿz4_BinaryClassifierCurveDisplayMixin._get_legend_labelc                    sÖ  ˆ r|rt dƒ‚|rt dt¡ |‰ tˆ tƒr(tˆ ƒ| kr(t d| › dˆ › d�ƒ‚t|tƒr@t|ƒdkr@tˆ tƒs@t d| › d�ƒ‚t|tƒrH|g}t|tƒrWt|ƒdkrW||  }|d	u r`d	g|  n|}tˆ tƒrlˆ g|  ‰ d
dddœ}ˆ d	u rƒ| dkr€|g|  ‰ ni g‰ g }d|v rÈt	 
|d |d |¡}|d d	urº|d d	ur°|d	d… d|d d›d� }n
|d|d d›� }| |gd	g| d   ¡ nt|d |ƒD ]\}	}
| t	 
|	|
|¡¡ qÏ‡ fdd„t|ƒD ƒ}|S )aâ  Get validated line kwargs for each curve.

        Parameters
        ----------
        n_curves : int
            Number of curves.

        name : list of str or None
            Name for labeling legend entries.

        legend_metric : dict
            Dictionary with "mean" and "std" keys, or "metric" key of metric
            values for each curve. If None, "label" will not contain metric values.

        legend_metric_name : str
            Name of the summary value provided in `legend_metrics`.

        curve_kwargs : dict or list of dict or None
            Dictionary with keywords passed to the matplotlib's `plot` function
            to draw the individual curves. If a list is provided, the
            parameters are applied to the curves sequentially. If a single
            dictionary is provided, the same parameters are applied to all
            curves.

        **kwargs : dict
            Deprecated. Keyword arguments to be passed to matplotlib's `plot`.
        z­Cannot provide both `curve_kwargs` and `kwargs`. `**kwargs` is deprecated in 1.7 and will be removed in 1.9. Pass all matplotlib arguments to `curve_kwargs` as a dictionary.z}`**kwargs` is deprecated and will be removed in 1.9. Pass all matplotlib arguments to `curve_kwargs` as a dictionary instead.z>`curve_kwargs` must be None, a dictionary or a list of length z. Got: r4   r   zfTo avoid labeling individual curves that have the same appearance, `curve_kwargs` should be a list of z‹ dictionaries. Alternatively, set `name` to `None` or a single string to label a single legend entry with mean ROC AUC score of all curves.Ng      à?z--Úblue)ÚalphaÚ	linestyleÚcolorÚmeanr   Ústdéÿÿÿÿz +/- r<   r=   Úmetricc                    s"   g | ]\}}t d |iˆ | ƒ‘qS )rA   )Ú_validate_style_kwargs)r-   Zfold_idxrA   ©Úcurve_kwargsr   r   Ú
<listcomp>ö   s    ÿÿzM_BinaryClassifierCurveDisplayMixin._validate_curve_kwargs.<locals>.<listcomp>)r(   ÚwarningsÚwarnÚFutureWarningÚ
isinstanceÚlistr6   r7   r   r   rB   ÚextendÚzipÚappendÚ	enumerate)Zn_curvesr   Zlegend_metricr@   rM   ÚkwargsZdefault_multi_curve_kwargsÚlabelsZlabel_aggregater>   r?   Zcurve_kwargs_r   rL   r   Ú_validate_curve_kwargsƒ   s|   %ÿýÿÿÿÿÿÿÿ


ÿÿÿÿÿ
þz9_BinaryClassifierCurveDisplayMixin._validate_curve_kwargs)r   Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr$   r*   r;   ÚstaticmethodrB   rZ   r   r   r   r   r      s    ÿÿ
-
r   c                 C   sŠ   | dur| S |du r|rdS dS t |ƒr|jn|} |r-|  d¡r'| dd… } nd| › �} n|  d¡r;d| dd… › �} |  dd¡} |  ¡ S )	aÁ  Validate the `score_name` parameter.

    If `score_name` is provided, we just return it as-is.
    If `score_name` is `None`, we use `Score` if `negate_score` is `False` and
    `Negative score` otherwise.
    If `score_name` is a string or a callable, we infer the name. We replace `_` by
    spaces and capitalize the first letter. We remove `neg_` and replace it by
    `"Negative"` if `negate_score` is `False` or just remove it otherwise.
    NzNegative scoreZScoreÚneg_é   z	Negative r   ú )Úcallabler   Ú
startswithr8   Ú
capitalize)Z
score_nameÚscoringZnegate_scorer   r   r   Ú_validate_score_nameý   s   


rg   c                 C   s    t  t  | ¡¡}| ¡ | ¡  S )a   Compute the ratio between the largest and smallest inter-point distances.

    A value larger than 5 typically indicates that the parameter range would
    better be displayed with a log scale while a linear scale would be more
    suitable otherwise.
    )ÚnpÚdiffÚsortÚmaxÚmin)Údatari   r   r   r   Ú_interval_max_min_ratio  s   rn   c                 C   sô   i dd“dd“dd“dd“d	d
“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd “d!d"“d#d$d%d&d'd(d)œ¥}|  ¡ D ]\}}||v rY||v rYtd*|› d+|› d,�ƒ‚qB|  ¡ }| ¡ D ]}||v rq|| ||| < qb|| ||< qb|S )-aÃ  Create valid style kwargs by avoiding Matplotlib alias errors.

    Matplotlib raises an error when, for example, 'color' and 'c', or 'linestyle' and
    'ls', are specified together. To avoid this, we automatically keep only the one
    specified by the user and raise an error if the user specifies both.

    Parameters
    ----------
    default_style_kwargs : dict
        The Matplotlib style kwargs used by default in the scikit-learn display.
    user_style_kwargs : dict
        The user-defined Matplotlib style kwargs.

    Returns
    -------
    valid_style_kwargs : dict
        The validated style kwargs taking into account both default and user-defined
        Matplotlib style kwargs.
    ÚlsrE   ÚcrF   ÚecÚ	edgecolorÚfcÚ	facecolorÚlwÚ	linewidthZmecZmarkeredgecolorZmfcaltZmarkerfacecoloraltÚmsÚ
markersizeZmewZmarkeredgewidthZmfcÚmarkerfacecolorÚaaZantialiasedÚdsZ	drawstyleÚfontZfontpropertiesÚfamilyZ
fontfamilyr   ÚfontnameÚsizeÚfontsizeÚstretchZfontstretchZ	fontstyleZfontvariantÚ
fontweightZhorizontalalignmentZverticalalignmentZmultialignment)ÚstyleÚvariantÚweightÚhaÚvaÚmaz	Got both ú and z", which are aliases of one another)ÚitemsÚ	TypeErrorÚcopyÚkeys)Zdefault_style_kwargsZuser_style_kwargsZinvalid_to_valid_kwZinvalid_keyÚ	valid_keyZvalid_style_kwargsr.   r   r   r   rK   #  sl   ÿþýüûúùø	÷
öõôóòñðïéÿ€rK   c                 C   s:   dD ]
}| j |  d¡ qdD ]}| j |  dd¡ qdS )z—Remove the top and right spines of the plot.

    Parameters
    ----------
    ax : matplotlib.axes.Axes
        The axes of the plot to despine.
    )ÚtopÚrightF)ÚbottomÚleftr   r   N)ÚspinesÚset_visibleZ
set_bounds)r   Úsr   r   r   Ú_despineb  s
   ÿr–   c                 C   sb   t |ƒ}|j› d|jd › �}| dkr/|r td|› d|› d�ƒ‚t d|› d|› d�t¡ | S |S )	z/Deprecate `estimator_name` in favour of `name`.r4   é   Ú
deprecatedzSCannot provide both `estimator_name` and `name`. `estimator_name` is deprecated in z and will be removed in z. Use `name` only.z"`estimator_name` is deprecated in z. Use `name` instead.)r   ÚmajorÚminorr(   rO   rP   rQ   )r   r   ÚversionZversion_remover   r   r   Ú_deprecate_estimator_namep  s&   ÿÿÿÿýrœ   c                 C   s    | du rdS t | tƒr| S | gS )z3Convert parameters to a list, leaving `None` as is.N)rR   rS   )Úparamr   r   r   Ú_convert_to_list_leaving_none„  s
   
rž   c                 C   sÊ   i }|  ¡ D ]\}}t|tƒr|||< qi | ¥|¥}tdd„ | ¡ D ƒƒdkrcdd„ | ¡ D ƒ}d d |dd	… ¡|d	 g¡}d
}	d|v rHd}	d dd„ |  ¡ D ƒ¡}
t|› d|› d|	› d|
› �ƒ‚dS )z>Check required and optional parameters are of the same length.c                 S   s   h | ]}t |ƒ’qS r   ©r6   )r-   r�   r   r   r   Ú	<setcomp>•  s    z'_check_param_lengths.<locals>.<setcomp>r   c                 S   s   g | ]}|‘qS r   r   r,   r   r   r   rN   –  s    z(_check_param_lengths.<locals>.<listcomp>r‰   z, NrI   Ú z'name' (or self.name)z (or `plot`)c                 s   s&   � | ]\}}|› d t |ƒ› �V  qdS )z: NrŸ   )r-   r.   Úvaluer   r   r   r1   �  s   € 
ÿz'_check_param_lengths.<locals>.<genexpr>z from `z` initializationz/, should all be lists of the same length. Got: )rŠ   rR   rS   r6   Úvaluesr�   Újoinr(   )ÚrequiredÚoptionalÚ
class_nameZoptional_providedr   r�   Ú
all_paramsZ
param_keysZparams_formattedZor_plotZlengths_formattedr   r   r   Ú_check_param_lengths�  s*   
€ 
ÿÿÿõr©   )rO   Úcollections.abcr   Únumpyrh   r¡   r   Ú_optional_dependenciesr   Ú	_responser   Úfixesr   Ú
multiclassr   Ú
validationr	   r
   r   rg   rn   rK   r–   rœ   rž   r©   r   r   r   r   Ú<module>   s$    n?	