o
    $i                     @   s   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
 d dlZd dlZd dlZd dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZmZmZ d d	lmZ d d
lm Z m!Z! e "e#Z$eG dd deZdS )    N)	dataclass)AnyDictListOptionalTupleUnion)Result)
CheckpointCheckpointConfig)$CHECKPOINT_MANAGER_SNAPSHOT_FILENAME)CheckpointManager)StorageContext_exists_at_fs_pathget_fs_and_path)TrainingFailedError)
Deprecated	PublicAPIc                
       s   e Zd ZU ee ed< ee ed< dZeee	ee
eef f   ed< edddeded	ed
 f fddZe	ddeeejf deejj d	d fddZe	ddededee d	d fddZeed	ee
eef  fddZ  ZS )r	   
checkpointerrorNbest_checkpointsalpha)	stabilitymetricmodereturnzray.train.Checkpointc                    s   t  ||S N)superget_best_checkpoint)selfr   r   	__class__ T/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/train/v2/api/result.pyr   !   s   zResult.get_best_checkpointpathstorage_filesystemc           	      C   s   t t||\}}t||std| d|d}tj|tj|}}t	|||d}t|j
|js>tdt dt|t d}| j||dS )	a3  Restore a training result from a previously saved training run path.

        Args:
            path: Path to the run output directory
            storage_filesystem: Optional filesystem to use for accessing the path

        Returns:
            Result object with restored checkpoints and metrics
        zExperiment folder z doesn't exist./)storage_pathexperiment_dir_namer%   z%Failed to restore the Result object: zo doesn't exist in the experiment folder. Make sure that this is an output directory created by a Ray Train run.)storage_contextcheckpoint_config)checkpoint_managerr)   )r   strr   RuntimeErrorrstriposr$   dirnamebasenamer   r%    checkpoint_manager_snapshot_pathr   r   r   _from_checkpoint_manager)	clsr$   r%   fsfs_pathr'   r(   r)   r+   r"   r"   r#   	from_path'   s:   

zResult.from_pathr+   r)   c           	   	   C   sf   |j }|r|j}|j}nd}d}dd |jD }d}|r&tdd |D }t||||j|||jdS )z0Create a Result object from a CheckpointManager.Nc                 S   s   g | ]}|j |jfqS r"   )r   metrics).0rr"   r"   r#   
<listcomp>n   s    
z3Result._from_checkpoint_manager.<locals>.<listcomp>c                 S   s   g | ]\}}|qS r"   r"   )r9   _mr"   r"   r#   r;   v   s    )r8   r   r   r$   r   metrics_dataframe_storage_filesystem)	latest_checkpoint_resultr8   r   best_checkpoint_resultspd	DataFramer	   experiment_fs_pathr%   )	r4   r+   r)   r   r@   latest_metricslatest_checkpointr   r>   r"   r"   r#   r3   _   s*   zResult._from_checkpoint_managerc                 C   s   t d)NzsThe `config` property for a `ray.train.Result` is deprecated, since it is only relevant in the context of Ray Tune.)DeprecationWarning)r   r"   r"   r#   config   s   zResult.configr   )__name__
__module____qualname__r   r
   __annotations__r   r   r   r   r   r,   r   r   r   classmethodr   r/   PathLikepyarrowr5   
FileSystemr7   r   r   r3   propertyr   rH   __classcell__r"   r"   r    r#   r	      sF   
 $
7"&r	   )%loggingr/   dataclassesr   typingr   r   r   r   r   r   pandasrB   rO   rayray.air.resultr	   ResultV1	ray.trainr
   r    ray.train.v2._internal.constantsr   >ray.train.v2._internal.execution.checkpoint.checkpoint_managerr   (ray.train.v2._internal.execution.storager   r   r   ray.train.v2.api.exceptionsr   ray.util.annotationsr   r   	getLoggerrI   loggerr"   r"   r"   r#   <module>   s"     
