o
    ci.                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlm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 ed
dG dd dZdS )    N)contextmanager)Path)CallableDictListOptionalUnion)Booster)CallbackEnv)
Checkpoint)flatten_dict)	PublicAPIbeta)	stabilityc                   @   s   e Zd ZdZdZdedddfdeeeee e	eef f  dede
d	ed
eee	eeeee f f ge	eef f  f
ddZeefdededefddZde	ee	eef f defddZdedefddZededee fddZdeddfddZdS )RayTrainReportCallbacka  Creates a callback that reports metrics and checkpoints model.

    Args:
        metrics: Metrics to report. If this is a list,
            each item should be a metric key reported by LightGBM,
            and it will be reported to Ray Train/Tune under the same name.
            This can also be a dict of {<key-to-report>: <lightgbm-metric-key>},
            which can be used to rename LightGBM default metrics.
        filename: Customize the saved checkpoint file type by passing
            a filename. Defaults to "model.txt".
        frequency: How often to save checkpoints, in terms of iterations.
            Defaults to 0 (no checkpoints are saved during training).
        checkpoint_at_end: Whether or not to save a checkpoint at the end of training.
        results_postprocessing_fn: An optional Callable that takes in
            the metrics dict that will be reported (after it has been flattened)
            and returns a modified dict.

    Examples
    --------

    Reporting checkpoints and metrics to Ray Tune when running many
    independent xgboost trials (without data parallelism within a trial).

    .. testcode::
        :skipif: True

        import lightgbm

        from ray.train.lightgbm import RayTrainReportCallback

        config = {
            # ...
            "metric": ["binary_logloss", "binary_error"],
        }

        # Report only log loss to Tune after each validation epoch.
        bst = lightgbm.train(
            ...,
            callbacks=[
                RayTrainReportCallback(
                    metrics={"loss": "eval-binary_logloss"}, frequency=1
                )
            ],
        )

    Loading a model from a checkpoint reported by this callback.

    .. testcode::
        :skipif: True

        from ray.train.lightgbm import RayTrainReportCallback

        # Get a `Checkpoint` object that is saved by the callback during training.
        result = trainer.fit()
        booster = RayTrainReportCallback.get_model(result.checkpoint)

    z	model.txtNr   Tmetricsfilename	frequencycheckpoint_at_endresults_postprocessing_fnc                 C   s2   t |tr|g}|| _|| _|| _|| _|| _d S )N)
isinstancestr_metrics	_filename
_frequency_checkpoint_at_end_results_postprocessing_fn)selfr   r   r   r   r    r   V/home/ubuntu/.local/lib/python3.10/site-packages/ray/train/lightgbm/_lightgbm_utils.py__init__M   s   


zRayTrainReportCallback.__init__
checkpointreturnc                 C   s@   |  }tt|| dW  d   S 1 sw   Y  dS )a  Retrieve the model stored in a checkpoint reported by this callback.

        Args:
            checkpoint: The checkpoint object returned by a training run.
                The checkpoint should be saved by an instance of this callback.
            filename: The filename to load the model from, which should match
                the filename used when creating the callback.
        )
model_fileN)as_directoryr	   r   as_posix)clsr!   r   checkpoint_pathr   r   r   	get_model_   s   
$z RayTrainReportCallback.get_model	evals_logc                 C   sd   t |dd}| js|}ni }| jD ]}t| jtr| j| }n|}|| ||< q| jr0| |}|S )N-)	delimiter)r   r   r   dictr   )r   r)   result_dictreport_dictkeymetricr   r   r   _get_report_dictn   s   

z'RayTrainReportCallback._get_report_dictenvc           	      C   s   i }|j D ]8}|dd \}}}t|dkr|d }d}nd }d}||vr)i ||< ||| || < |d ur=||| |d < q|S )Nr         z-mean z-stdv)evaluation_result_listlen)	r   r2   eval_resultentry	data_name	eval_nameresultstdvsuffixr   r   r   _get_eval_result~   s   
z'RayTrainReportCallback._get_eval_resultmodelc                 c   sn    t j  dv r2t }|t|| j	  t
|V  W d    d S 1 s+w   Y  d S d V  d S )N)r   N)raytrainget_contextget_world_ranktempfileTemporaryDirectory
save_modelr   r   r%   r   from_directory)r   r@   temp_checkpoint_dirr   r   r   _get_checkpoint   s   
"
z&RayTrainReportCallback._get_checkpointc           	      C   s   |  |}| |}|j|jd k}|o| j}| jdko%|jd | j dk}|p)|}|rN| j|jd}tj	j
||d W d    d S 1 sGw   Y  d S tj	
| d S )N   r   )r@   )r!   )r?   r1   	iterationend_iterationr   r   rJ   r@   rA   rB   report)	r   r2   r8   r.   on_last_itershould_checkpoint_at_end should_checkpoint_with_frequencyshould_checkpointr!   r   r   r   __call__   s   


"zRayTrainReportCallback.__call__)__name__
__module____qualname____doc__CHECKPOINT_NAMEr   r   r   r   r   intboolr   floatr    classmethodr   r	   r(   listr,   r1   r
   r?   r   rJ   rS   r   r   r   r   r      sF    :(
"r   )rE   
contextlibr   pathlibr   typingr   r   r   r   r   lightgbm.basicr	   lightgbm.callbackr
   	ray.trainrA   r   ray.tune.utilsr   ray.util.annotationsr   r   r   r   r   r   <module>   s    