o
    $iw                     @   s   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 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 G dd dZeddG dd deZdS )    N)abstractmethod)contextmanager)Path)CallableDictListOptionalUnion)Booster)CallbackEnv)
Checkpoint)flatten_dict)	PublicAPIc                   @   s$  e 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ededefddZedefddZdeddfddZdS ) RayReportCallbackz	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   _/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/train/lightgbm/_lightgbm_utils.py__init__   s   


zRayReportCallback.__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.
        Returns:
            The model loaded from the checkpoint.
        )
model_fileN)as_directoryr
   r   as_posix)clsr!   r   checkpoint_pathr   r   r   	get_model%   s   
$zRayReportCallback.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_dict6   s   

z"RayReportCallback._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_resultF   s   
z"RayReportCallback._get_eval_resultmodelc                 C      t )z_Get checkpoint from model.

        This method needs to be implemented by subclasses.
        NotImplementedError)r   r@   r   r   r   _get_checkpointW      z!RayReportCallback._get_checkpointr.   c                 C   rA   )zSave checkpoint and report metrics corresonding to this checkpoint.

        This method needs to be implemented by subclasses.
        rB   )r   r.   r@   r   r   r   _save_and_report_checkpoint_   rE   z-RayReportCallback._save_and_report_checkpointc                 C   rA   )zTReport Metrics.

        This method needs to be implemented by subclasses.
        rB   r   r.   r   r   r   _report_metricsg   rE   z!RayReportCallback._report_metricsc                 C   sx   |  |}| |}|j|jd k}|o| j}| jdko%|jd | j dk}|p)|}|r5| ||j d S | | d S )N   r   )	r?   r1   	iterationend_iterationr   r   rF   r@   rH   )r   r2   r8   r.   on_last_itershould_checkpoint_at_end should_checkpoint_with_frequencyshould_checkpointr   r   r   __call__o   s   


zRayReportCallback.__call__)__name__
__module____qualname__CHECKPOINT_NAMEr   r	   r   r   r   intboolr   floatr    classmethodr   r
   r(   listr,   r1   r   r?   r   rD   rF   rH   rP   r   r   r   r   r      sL    (
"r   beta)	stabilityc                   @   sJ   e Zd ZdZededee fddZde	defddZ
de	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 LightGBM 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)

    r@   r"   c                 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   rD      s   
"
z&RayTrainReportCallback._get_checkpointr.   c                 C   sB   | j |d}tjj||d W d    d S 1 sw   Y  d S )N)r@   )r!   )rD   r]   r^   report)r   r.   r@   r!   r   r   r   rF      s   "z2RayTrainReportCallback._save_and_report_checkpointc                 C   s   t j| d S r   )r]   r^   rf   rG   r   r   r   rH      s   z&RayTrainReportCallback._report_metricsN)rQ   rR   rS   __doc__r   r
   r   r   rD   r   rF   rH   r   r   r   r   r\      s    :r\   )ra   abcr   
contextlibr   pathlibr   typingr   r   r   r   r	   lightgbm.basicr
   lightgbm.callbackr   	ray.trainr]   r   ray.tune.utilsr   ray.util.annotationsr   r   r\   r   r   r   r   <module>   s    s