
    :[gP                        d Z ddlZddlZddl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 ddlZddlmZ ddlmZmZmZmZ g dZeeeeef   f   Zee
e   e
eeef      f   ZeZ G d	 d
e      Zde
e   de
eeeef      fdZ  ed      Z!de!de!fdZ" G d d      Z# G d de      Z$ G d de      Z% G d de      Z& G d de      Z'y)z}Callback library containing training routines.  See :doc:`Callback Functions
</python/callbacks>` for a quick introduction.

    N)ABC)
AnyCallableDictListOptionalSequenceTupleTypeVarUnioncast   )
collective)BoosterDMatrixXGBoostError_parse_eval_str)TrainingCallbackLearningRateSchedulerEarlyStoppingEvaluationMonitorTrainingCheckPointCallbackContainerc                       e Zd ZdZeeeeef   f   ZddZde	de	fdZ
de	de	fdZde	ded	edefd
Zde	ded	edefdZy)r   zCInterface for training callback.

    .. versionadded:: 1.3.0

    returnNc                      y N )selfs    K/var/www/html/bid-api/venv/lib/python3.12/site-packages/xgboost/callback.py__init__zTrainingCallback.__init__5   s        modelc                     |S )zRun before training starts.r   r   r#   s     r    before_trainingz TrainingCallback.before_training8       r"   c                     |S )zRun after training is finished.r   r%   s     r    after_trainingzTrainingCallback.after_training<   r'   r"   epoch	evals_logc                      y)zRun before each iteration.  Returns True when training should stop. See
        :py:meth:`after_iteration` for details.

        Fr   r   r#   r*   r+   s       r    before_iterationz!TrainingCallback.before_iteration@   s    
 r"   c                      y)a  Run after each iteration.  Returns `True` when training should stop.

        Parameters
        ----------

        model :
            Eeither a :py:class:`~xgboost.Booster` object or a CVPack if the cv function
            in xgboost is being used.
        epoch :
            The current training iteration.
        evals_log :
            A dictionary containing the evaluation history:

            .. code-block:: python

                {"data_name": {"metric_name": [0.5, ...]}}

        Fr   r-   s       r    after_iterationz TrainingCallback.after_iterationG   s    & r"   )r   N)__name__
__module____qualname____doc__r   str
_ScoreListEvalsLogr!   _Modelr&   r)   intboolr.   r0   r   r"   r    r   r   ,   s     Cc:o../HV  F v f S X RV V C H QU r"   r   rlistr   c                    i }| d   j                         d   }| D ]  }|j                         }||d   k(  sJ t        |dd       D ]e  \  }}t        |t              s|j	                         }|j                  d      \  }}||f|vrg |||f<   |||f   j                  t        |             g  |}	g }
t        |j                         d       D ]|  \  \  }}}t        j                  |      }t        |	t              s|	j	                         }	t        j                  |      t        j                  |      }}|
j                  |||fg       ~ |
S )z#Aggregate cross-validation results.r   r   N:c                     | d   d   S Nr   r   )xs    r    <lambda>z_aggcv.<locals>.<lambda>n   s    AaDGr"   )key)split	enumerate
isinstancer5   decodeappendfloatsorteditemsnumpyarraymeanstdextend)r;   cvmapidxlinearr
metric_idxitkvmsgresults_namesas_arrrM   rN   s                    r    _aggcvr^   ]   s>    13E
(..
1
Cc!f}}'AB0NJb#&YY[88C=DAqAe+)+z1o&:q/"))%(3 1  CGu{{}2CD	D1Q#s#**,CJJv&		&(9ctS)*+ E Nr"   _ARTscorec                    t        j                         }|dk7  sJ |dk(  r| S t        | t              rt	        d      t        j                  | g      }t        j                  |t         j                  j                        |z  }|d   S )zHelper function for computing customized metric in distributed
    environment.  Not strictly correct as many functions don't use mean value
    as final result.

    r   r   zBxgboost.cv function should not be used in distributed environment.)
r   get_world_sizerE   tuple
ValueErrorrK   rL   	allreduceOpSUM)r`   worldrS   s      r    _allreduce_metricri   {   s     %%'EA::z%P
 	
 ++ug
C


sJMM$5$5
6
>Cq6Mr"   c                      e Zd ZdZ	 	 	 ddee   dee   dededdf
dZ	d	e
de
fd
Zd	e
de
fdZd	e
dededeeeeef         def
dZdeeeeef      eeeeef      f   deddfdZd	e
dededeeeeef         def
dZy)r   zfA special internal callback for invoking a list of other callbacks.

    .. versionadded:: 1.3.0

    N	callbacksmetricoutput_marginis_cvr   c                     t        |      | _        |D ]  }t        |t              rt	        d       d}|t        |      st	        |      || _        t        j                         | _	        || _
        || _        | j                  rd | _        y y )Nz3callback must be an instance of `TrainingCallback`.zmetric must be callable object for monitoring.  For builtin metrics, passing them in training parameter invokes monitor automatically.)setrk   rE   r   	TypeErrorcallablerl   collectionsOrderedDicthistory_output_marginrn   aggregated_cv)r   rk   rl   rm   rn   cbrX   s          r    r!   zCallbackContainer.__init__   s     YBb"23 UVV 
R 	 hv&6C. 2=2I2I2K+
::!%D r"   r#   c                     | j                   D ][  }|j                  |      }d}| j                  r"t        |j                  t
              r>J |       t        |t              rVJ |        |S )z Function called before training.r#   z'before_training should return the model)rk   r&   rn   rE   cvfoldslistr   r   r#   crX   s       r    r&   z!CallbackContainer.before_training   sd    A%%E%2E;Czz!%--6;;6!%16361   r"   c                     | j                   D ][  }|j                  |      }d}| j                  r"t        |j                  t
              r>J |       t        |t              rVJ |        |S )zFunction called after training.rz   z&after_training should return the model)rk   r)   rn   rE   r{   r|   r   r}   s       r    r)   z CallbackContainer.after_training   sd    A$$5$1E:Czz!%--6;;6!%16361   r"   r*   dtrainevalsc                 H     t         fd j                  D              S )z*Function called before training iteration.c              3   X   K   | ]!  }|j                  j                         # y wr   )r.   ru   .0r~   r*   r#   r   s     r    	<genexpr>z5CallbackContainer.before_iteration.<locals>.<genexpr>   s(      
DRqAueT\\:N   '*)anyrk   )r   r#   r*   r   r   s   ```  r    r.   z"CallbackContainer.before_iteration   s#      
DHNN
 
 	
r"   r`   c           	         |D ]m  }|d   }|d   }| j                   r3t        t        t        t        t        t        f   |      d         }||f}n|}|j                  d      }|d   }	dj                  |dd        }
t        |      }|	| j                  vr!t        j                         | j                  |	<   | j                  |	   }|
|vrt        t        g       ||
<   ||
   }| j                   rPt        t        t        t        t        f      |      j                  t        t        t        t        f   |             :t        t        t           |      j                  t        t        |             p y )Nr   r      -)rn   rH   r   r
   r5   rC   joinri   ru   rs   rt   r6   r   rG   )r   r`   r*   dr[   r\   rN   r@   splited_names	data_namemetric_namedata_historymetric_historys                r    _update_historyz!CallbackContainer._update_history   s?   
 A!DtAzzDsE5'8!91=a@AH JJsOM%a(I((=#45K!!$A,*5*A*A*CY'<<	2L,.,0R,@[))+6NzzT%u-.?FFue|,a0 T%[.188eQH/ r"   c                      j                   rLj                   j                   j                        }t	        |      }| _         j                  |       np|g n|}|D ]   \  }}|j                  d      dk(  rJ d        j                  | j                   j                        }t        |      }	 j                  |	       t         fd j                  D              }
|
S )z)Function called after training iteration.r   z#Dataset name should not contain `-`c              3   X   K   | ]!  }|j                  j                         # y wr   )r0   ru   r   s     r    r   z4CallbackContainer.after_iteration.<locals>.<genexpr>  s%     XA!##E5$,,?r   )rn   evalrl   rv   r^   rw   r   findeval_setr   r   rk   )r   r#   r*   r   r   scoresrZ   r[   r`   metric_scorerets   ```        r    r0   z!CallbackContainer.after_iteration   s     ::ZZt{{D4G4GHFF^F!'D  /-BUE 4yy~+R-RR+ !udkk4CVCVWE*51L  u5XXX
r"   )NTF)r1   r2   r3   r4   r	   r   r   r   r:   r!   r8   r&   r)   r9   r   r   r
   r5   r.   r   rH   r   r0   r   r"   r    r   r      sV    &*"&,-& "& 	&
 & 
&6	V 	 	
F 
v 




 

 	


 U7C<012

 


IT%U
+,d5eU9J3K.LLMI I 
	I<  	
 U7C<012 
r"   r   c                   t     e Zd ZdZdeeegef   ee   f   ddf fdZ	de
dedej                  defd	Z xZS )
r   a  Callback function for scheduling learning rate.

    .. versionadded:: 1.3.0

    Parameters
    ----------

    learning_rates :
        If it's a callable object, then it should accept an integer parameter
        `epoch` and returns the corresponding learning rate.  Otherwise it
        should be a sequence like list or tuple with the same size of boosting
        rounds.

    learning_ratesr   Nc                     t              s;t        t        j                  j                        st        dt                     t              r| _        n
fd| _        t        | %          y )Nz=Invalid learning rates, expecting callable or sequence, got: c                 *    t        t              |    S r   )r   r	   )r*   r   s    r    rA   z0LearningRateScheduler.__init__.<locals>.<lambda>'  s    X~0Nu0Ur"   )
rr   rE   rs   abcr	   rq   typer   superr!   )r   r   	__class__s    `r    r!   zLearningRateScheduler.__init__  sj     '
KOO441
 O'(* 
 N#"0D"UDr"   r#   r*   r+   c                 F    |j                  d| j                  |             y)Nlearning_rateF)	set_paramr   r-   s       r    r0   z%LearningRateScheduler.after_iteration*  s      	)<)<U)CDr"   )r1   r2   r3   r4   r   r   r9   rH   r	   r!   r8   r   r7   r:   r0   __classcell__r   s   @r    r   r   	  sa    #HcUE\$:HUO$KL	"$'4D4M4M	r"   r   c                        e Zd ZdZ	 	 	 	 	 ddedee   dee   dee   dee   ded	df fd
Z	de
d	e
fdZdededede
ded	efdZde
dedej                  d	efdZde
d	e
fdZ xZS )r   a)  Callback function for early stopping

    .. versionadded:: 1.3.0

    Parameters
    ----------
    rounds :
        Early stopping rounds.
    metric_name :
        Name of metric that is used for early stopping.
    data_name :
        Name of dataset that is used for early stopping.
    maximize :
        Whether to maximize evaluation metric.  None means auto (discouraged).
    save_best :
        Whether training should return the best model or the last model.
    min_delta :

        .. versionadded:: 1.5.0

        Minimum absolute change in score to be qualified as an improvement.

    Examples
    --------

    .. code-block:: python

        es = xgboost.callback.EarlyStopping(
            rounds=2,
            min_delta=1e-3,
            save_best=True,
            maximize=False,
            data_name="validation_0",
            metric_name="mlogloss",
        )
        clf = xgboost.XGBClassifier(tree_method="hist", device="cuda", callbacks=[es])

        X, y = load_digits(return_X_y=True)
        clf.fit(X, y, eval_set=[(X, y)])
    Nroundsr   r   maximize	save_best	min_deltar   c                     || _         || _        || _        || _        || _        i | _        || _        | j                  dk  rt        d      d| _        i | _	        d| _
        t        | 1          y )Nr   z(min_delta must be greater or equal to 0.)datar   r   r   r   stopping_history
_min_deltard   current_roundsbest_scoresstarting_roundr   r!   )r   r   r   r   r   r   r   r   s          r    r!   zEarlyStopping.__init__]  ss     	&" ;=#??QGHH#$!##$r"   r#   c                 0    |j                         | _        |S r   )num_boosted_roundsr   r%   s     r    r&   zEarlyStopping.before_trainingu  s    #668r"   r`   r[   rl   r*   c                     dt         dt        fddt         dt         dt        f fd}dt         dt         dt        f fd} j                  *d}d	k7  rt	        fd
|D              rd _        nd _         j                  r|}	n|}	 j
                  sd _        i  j
                  |<   t        t        |g       j
                  |   <   i  j                  |<   |g j                  |   <   |j                  t        |      t        |             n |	| j                  |      d         s7 j
                  |      j                  |        xj                  dz  c_        n j
                  |      j                  |        j                  |      j                  |        j
                  |      d   }
|j                  t        |
      t        |             d _         j                   j                  k\  ryy)Nvaluer   c                 0    t        | t              r| d   S | S )z+get score if it's cross validation history.r   )rE   rc   )r   s    r    get_sz+EarlyStopping._update_rounds.<locals>.get_s|  s    )%758BUBr"   newbestc                 b    t        j                   |       j                  z
   |            S )z-New score should be greater than the old one.rK   greaterr   r   r   r   r   s     r    r   z.EarlyStopping._update_rounds.<locals>.maximize  s%    ==sdoo!=uT{KKr"   c                 b    t        j                   |      j                  z
   |             S )z,New score should be lesser than the old one.r   r   s     r    minimizez.EarlyStopping._update_rounds.<locals>.minimize  s%    ==tt!>c
KKr"   )
aucaucprprezpre@mapndcgzauc@zaucpr@zmap@zndcg@mapec              3   @   K   | ]  }j                  |        y wr   )
startswith)r   r@   rl   s     r    r   z/EarlyStopping._update_rounds.<locals>.<genexpr>  s     'WFV(9(9!(<FVs   TFr   )
best_scorebest_iterationr   r   )_ScorerH   r:   r   r   r   r   r   r6   r   set_attrr5   rG   r   )r   r`   r[   rl   r#   r*   r   r   maximize_metrics
improve_oprecordr   s   `  `       @r    _update_roundszEarlyStopping._update_roundsy  s   	C 	CE 	C	L& 	L 	L4 	L	L& 	L 	L4 	L ==   C'WFV'W$W $ %==!J!J$$"#D*,D!!$'26zE72KD!!$'/%'DT".3WDT"6*NNc%jUNLE4#3#3D#9&#A"#EF!!$'/66u=1$!!$'/66u=T"6*11%8**408<FNNc&k#e*NM"#D$++-r"   r+   c                     || j                   z  }d}t        |j                               dk  rt        |      | j                  r| j                  }nt        |j                               d   }||vrt        d|       t        |t              st        dt        |             ||   }| j                  r| j                  }nt        |j                               d   }||vrt        d|       ||   d   }| j                  |||||      S )Nz;Must have at least 1 validation dataset for early stopping.r   r   zNo dataset named: z1The name of the dataset should be a string. Got: zNo metric named: )r   lenkeysrd   r   r|   rE   r5   rq   r   r   r   )	r   r#   r*   r+   rX   r   data_logr   r`   s	            r    r0   zEarlyStopping.after_iteration  s     	$$$Ky~~ 1$S/! 99		I Y^^-.r2II%1)=>>)S)CDOCTU  Y' **K x}}/3Kh&0>?? %b)""5)[%OOr"   c                     | j                   s|S 	 |j                  }|j                  }||J |d |dz    }||_        ||_        |S # t        $ r}t        d      |d }~ww xY w)Nr   z4`save_best` is not applicable to the current booster)r   r   r   r   )r   r#   r   r   es        r    r)   zEarlyStopping.after_training  s    ~~L
	"11N))J!-*2HHH.NQ./E#1E )E   	F	s   4A 	A AA )NNNFg        )r1   r2   r3   r4   r9   r   r5   r:   rH   r!   r8   r&   r   r   r   r7   r0   r)   r   r   s   @r    r   r   2  s    'Z &*#'#'$) c] C=	
 4. D>  
0V  ==#&=03=<B=KN=	=~"P"P$'"P4D4M4M"P	"PHF v r"   r   c            
            e Zd ZdZddedededdf fdZded	ed
ede	e   def
dZ
dededej                  defdZdedefdZ xZS )r   aM  Print the evaluation result at each iteration.

    .. versionadded:: 1.3.0

    Parameters
    ----------

    rank :
        Which worker should be used for printing the result.
    period :
        How many epoches between printing.
    show_stdv :
        Used in cv to show standard deviation.  Users should not specify it.
    rankperiod	show_stdvr   Nc                 h    || _         || _        || _        |dkD  sJ d | _        t        |           y r?   )printer_rankr   r   _latestr   r!   )r   r   r   r   r   s       r    r!   zEvaluationMonitor.__init__  s7     "zz&*r"   r   rl   r`   rN   c                 j    |!| j                   rd|dz   |z    d|dd|d}|S d|dz   |z    d|d}|S )N	r   r=   z.5f+)r   )r   r   rl   r`   rN   rX   s         r    _fmt_metriczEvaluationMonitor._fmt_metric  sa     ?t~~tczF*+1U3KqS	BC 
 tczF*+1U3K8C
r"   r#   r*   r+   c           	         |syd| d}t        j                         | j                  k(  r|j                         D ]_  \  }}|j                         D ]G  \  }}d }	t	        |d   t
              r|d   d   }
|d   d   }	n|d   }
|| j                  |||
|	      z  }I a |dz  }|| j                  z  dk(  s| j                  dk(  rt        j                  |       d | _	        y|| _	        y)NF[]r   r   r   
)
r   get_rankr   rJ   rE   rc   r   r   communicator_printr   )r   r#   r*   r+   rX   r   rl   r   logstdvr`   s              r    r0   z!EvaluationMonitor.after_iteration  s     ugQ< D$5$55 ) 1f(.$K,0D!#b'51 #B
"2wqz #B4++D+udKKC )7 !2 4KC#)T[[A-=--c2#   #r"   c                     t        j                         | j                  k(  r+| j                  t        j                  | j                         |S r   )r   r   r   r   r   r%   s     r    r)   z EvaluationMonitor.after_training,  s9     D$5$55$,,:R))$,,7r"   )r   r   F)r1   r2   r3   r4   r9   r:   r!   r5   rH   r   r   r8   r   r7   r0   r)   r   r   s   @r    r   r     s    S c $ SW !$-29A%	$'4D4M4M	6F v r"   r   c                        e Zd ZdZdZ	 	 	 ddeeej                  f   dede	de
dd	f
 fd
ZdedefdZdede
dej                  de	fdZ xZS )r   ak  Checkpointing operation. Users are encouraged to create their own callbacks for
    checkpoint as XGBoost doesn't handle distributed file systems. When checkpointing on
    distributed systems, be sure to know the rank of the worker to avoid multiple
    workers checkpointing to the same place.

    .. versionadded:: 1.3.0

    Since XGBoost 2.1.0, the default format is changed to UBJSON.

    Parameters
    ----------

    directory :
        Output model directory.
    name :
        pattern of output model file.  Models will be saved as name_0.ubj, name_1.ubj,
        name_2.ubj ....
    as_pickle :
        When set to True, all training parameters will be saved in pickle format,
        instead of saving only the model.
    interval :
        Interval of checkpointing.  Checkpointing is slow so setting a larger number can
        reduce performance hit.

    ubjr#   	directoryr[   	as_pickleintervalr   Nc                     t        j                  |      | _        || _        || _        || _        d| _        d| _        t        | %          y r?   )
osfspath_path_name
_as_pickle_iterations_epoch_startr   r!   )r   r   r[   r   r   r   s        r    r!   zTrainingCheckPoint.__init__O  sD     YYy)

##r"   c                 0    |j                         | _        |S r   )r   r   r%   s     r    r&   z"TrainingCheckPoint.before_training^  s    ..0r"   r*   r+   c                 "   | j                   | j                  k(  rt        j                  j	                  | j
                  | j                  dz   t        || j                  z         z   | j                  rdnd| j                   z         }d| _         t        j                         dk(  rI| j                  r,t        |d      5 }t        j                  ||       d d d        n|j!                  |       | xj                   dz  c_         y# 1 sw Y   xY w)NrZ   z.pkl.r   wbr   F)r   r   r   pathr   r   r   r5   r   r   default_formatr   r   openpickledump
save_model)r   r#   r*   r+   r   fds         r    r0   z"TrainingCheckPoint.after_iterationb  s     ;;$***77<<



ut{{*+- "__6Ad6I6I5J2KMD DK""$)??dD)RE2. *) $$T*q *)s   >DD)r#   Fd   )r1   r2   r3   r4   r  r   r5   r   PathLiker:   r9   r!   r8   r&   r   r7   r0   r   r   s   @r    r   r   2  s    4 N
 bkk)*  	
  
V  $'4D4M4M	r"   r   )(r4   rs   r   r  r   r   typingr   r   r   r   r   r	   r
   r   r   r   rK    r   corer   r   r   r   __all__rH   r   r6   r8   r   r5   r^   r_   ri   r   r   r   r   r   r   r"   r    <module>r     s  
  	       A A 
ueE5L))	*4;U5%<%8 99:
	.s .b$s) U3u+<%= > 6 vT d &x xv%, %Rz$ zz@( @FD) Dr"   