o
    ci
                     @   sr   d dl Z d dlmZ d dlmZmZ d dlZd dlmZ d dl	m
Z
 er*d dlmZ e
ddG d	d
 d
eZdS )    N)Path)TYPE_CHECKINGOptional)FrameworkCheckpoint)	PublicAPI)Preprocessorbeta)	stabilityc                
   @   sT   e Zd ZdZdZeddddejded dee	 d	d fd
dZ
d	ejfddZdS )XGBoostCheckpointzHA :py:class:`~ray.train.Checkpoint` with XGBoost-specific functionality.z
model.jsonN)preprocessorpathboosterr   r   r   returnc                C   s\   t |pt }| std| ||| j  | 	| }|r,|
| |S )aV  Create a :py:class:`~ray.train.Checkpoint` that stores an XGBoost
        model.

        Args:
            booster: The XGBoost model to store in the checkpoint.
            preprocessor: A fitted preprocessor to be applied before inference.
            path: The path to the directory where the checkpoint file will be saved.
                This should start as an empty directory, since the *entire*
                directory will be treated as the checkpoint when reported.
                By default, a temporary directory will be created.

        Returns:
            An :py:class:`XGBoostCheckpoint` containing the specified ``Estimator``.

        Examples:

            ... testcode::

                import numpy as np
                import ray
                from ray.train.xgboost import XGBoostCheckpoint
                import xgboost

                train_X = np.array([[1, 2], [3, 4]])
                train_y = np.array([0, 1])

                model = xgboost.XGBClassifier().fit(train_X, train_y)
                checkpoint = XGBoostCheckpoint.from_model(model.get_booster())

        z%`path` must be a directory, but got: )r   tempfilemkdtempis_dir
ValueError
save_modeljoinpathMODEL_FILENAMEas_posixfrom_directoryset_preprocessor)clsr   r   r   checkpoint_path
checkpoint r   X/home/ubuntu/.local/lib/python3.10/site-packages/ray/train/xgboost/xgboost_checkpoint.py
from_model   s   &
zXGBoostCheckpoint.from_modelc                 C   sN   |   }t }|t|| j  |W  d   S 1 s w   Y  dS )z5Retrieve the XGBoost model stored in this checkpoint.N)as_directoryxgboostBooster
load_modelr   r   r   )selfr   r   r   r   r   	get_modelF   s
   
$zXGBoostCheckpoint.get_model)__name__
__module____qualname____doc__r   classmethodr    r!   r   strr   r$   r   r   r   r   r
      s     1r
   )r   pathlibr   typingr   r   r    (ray.train._internal.framework_checkpointr   ray.util.annotationsr   ray.data.preprocessorr   r
   r   r   r   r   <module>   s    