
    bcgt                         d dl Z d dlZ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 ddlmZ  ej                  e      Zej#                  ej$                          G d de      Z G d	 d
      Zy)    N)display)deepcopy)ListOptionalAnyUnion   )MetricWidgetc                   B     e Zd Z fdZdej
                  ddfdZ xZS )MetricsWidgetc                 6    t         | j                  |           y N)super	__class____init__)selfr   s    Z/var/www/html/bid-api/venv/lib/python3.12/site-packages/catboost/widget/metrics_plotter.pyr   zMetricsWidget.__init__   s    dnnd,.    datareturnNc                 $    t        |      | _        y r   )r   r   )r   r   s     r   update_datazMetricsWidget.update_data   s    TN	r   )__name__
__module____qualname__r   tpDictr   __classcell__)r   s   @r   r   r      s    /# #D #r   r   c                      e Zd ZdZ	 	 ddeeeej                  eef   f      de	eeeej                  eef   f         de	e
   ddfdZddZdefd	Zed
eeeej                  eef   f      deej                  eef      fd       Zedej                  ee
f   d
ej                  eef   dee   fd       Zdede	e   fdZde
ded
ej                  eef   ddfdZy)MetricsPlotterzg
    Context manager that enables widget with learning curves in
    JupyterLab / Jupyter Notebook
    Ntrain_metricstest_metricstotal_iterationsr   c           
         t               | _        g dddddgddgddd| _        d	d| j                  d
| _        ddd| j                  di| _        |xs |}| j                  |      }| j                  |      }t        |      D ci c]  \  }}|d   | c}}| _        t        |      D ci c]  \  }}|d   | c}}| _        | j                  d   j                  ||d       d	| _
        || _        ||| j                  d   d<   |xs d	| j                  d<   t        j                         | _        yc c}}w c c}}w )a0  
        Constructor that defines metrics to be plotted and total iterations count.

        Parameters
        ----------
        train_metrics : list of str or list of dict
            List of train metrics to be tracked.
            Each item in list can be either string with metric name or dict
            with the following format:
            {
                "name": "{metric_name}",
                "best_value": "Max|Min|Undefined",
            }

        test_metrics : list of str or list of dict, optional (default=None)
            List of test metrics to be tracked.
            Has the same format as train_metrics. Equals to train_metrics, if not defined

        total_iterations: int, optional (default=None)
            Total number of iterations, allows for remaining time estimation.
        Train 
experimentNlearntest)launch_mode
parametersnameiteration_count
learn_setslearn_metrics	test_setsr"   )
iterationsmetar   )passed_iterationsr#   r   	test_path)pathr,   contentr,   r2   )r/   r"   r-   r#   )r   _widget_values_content_dataconstruct_metrics_meta	enumerate_train_metrics_positions_test_metrics_positionsupdater3   r#   time_start_time)r   r!   r"   r#   train_metrics_metatest_metrics_metaposr2   s           r   r   zMetricsPlotter.__init__   s   2 % & $#'&i!%$X $	
 "# $LL
 #$==

 $4}6:6Q6QR_6`595P5PQ]5^ 099K/L)N/L)#tDL#/L)N% 099J/K(M/K)#tDL#/K(M$ 	V##/-%
 	
 "# 0'6FDLL !23,<,A()99;%)N(Ms   D7.D=c                 0    t        | j                         | S r   )r   r7   )r   s    r   	__enter__zMetricsPlotter.__enter__o   s    r   c                 ^    |t         k(  r$t        j                  d| j                   d       yy )Nz$Learning was stopped manually after z epochsT)KeyboardInterruptloggerinfor3   )r   exc_type	exc_value	tracebacks       r   __exit__zMetricsPlotter.__exit__s   s3    ((KK6t7M7M6NgVX )r   metricsc                     g }| D ]\  }t        |t              r|d}}n1t        |t              rd|v rd|v sJ d       |d   |d   }}nJ d       |j                  ||d       ^ |S )N	Undefinedr,   
best_valuezKWrong metrics definition format: should have `name` and `best_value` fieldszREach metric should be defined as str or asdict with `name` and `best_value` fields)rR   r,   )
isinstancestrdictappend)rO   r2   itemr,   rR   s        r   r;   z%MetricsPlotter.construct_metrics_metay   s    (*D$$#'jD$'~,$*> 555> $(<l1Cj? ? ?uKKz4@A  r   metrics_positionsc                 X   dgt        |       z  }t        |j                               t        | j                               k(  s0J ddj                  t	        | j                                             |j                         D ]$  \  }}t        |t              sJ d       ||| |   <   & |S )Ng        z?Not all metrics were passed while logging, expected following: z, z'Type of metric {metric} should be float)lensetkeysjoinlistitemsrS   float)rX   rO   arraymetricvalues        r   construct_metrics_arrayz&MetricsPlotter.construct_metrics_array   s     !TC(9$:: 7<<>"c*;*@*@*B&CC 	F))D):)?)?)A$BCDF	FC %]]_MFEeU+V-VV+/4E#F+, - r   time_from_startc                 r    | j                   y | j                   | j                  z
  }|| j                  z  |z  S r   )r#   r3   )r   re   remaining_iterationss      r   estimate_remaining_timez&MetricsPlotter.estimate_remaining_time   s?      ($($9$9D<R<R$R!7!77:NNNr   epochtrainc                 ^   |dz   | _         | j                   | j                  d<   t        | j                  d   | j                         }|| j                  d<   || j                  d   d<   t	        | j                  d         ||dz   fv sJ d       t        j
                         | j                  z
  }t	        | j                  d         |dz   k(  }t	        | j                  d         |k(  rK| j                  d   j                  g g ||d       | j                  |      }||| j                  d   d
   d<   |rdnd}| j                  |r| j                  n| j                  |      }	| j                  d   d
   j                  ||	i       |r&| j                  j                  | j                         y	y	)ac  
        Save metrics at specific training epoch.

        Parameters
        ----------
        epoch : int
            Current epoch

        train : bool
            Flag that indicates whether metrics are calculated on train or test data

        metrics: dict
            Values for each of metrics defined in `__init__` method of this class
        r	   r3   r#   r2   r-   r1   zBData for epochs should be passed successively (wrong epoch number))r(   r)   	iterationpassed_timeNremaining_timer(   r)   )r3   r9   maxr8   rZ   r@   rA   rV   rh   rd   r=   r>   r?   r7   r   r:   )
r   ri   rj   rO   r#   re   should_redrawro   keyrc   s
             r   logzMetricsPlotter.log   s     "'-1-C-C)*t}}-?@$BXBXY,<()2BV./4<<-.5%!)2DD 	QP	QD "&t/?/?!?!$,,|"<=Jt||L)*e3LL&--".	/  "99/JN)CQ\*2./?@#7!99-2D))8T8TV]_ 	\"2&--sEl;LL$$TZZ0 r   )NN)r   r    )r   r   r   __doc__r   r   rT   r   r   r   intr   rF   r   rN   staticmethodr;   r`   rd   rh   boolrs    r   r   r    r       su    PT37N'd5bggc3h6G1G+H&I N''U3S8I3I-J(KLN'#+C=N'<@N'`#  U3S8I3I-J(K PTUWU\U\]`be]eUfPg    277383D )+e)<AEe Ou O% O11 11T 11BGGCJ4G 11D 11r   r    )r@   loggingtypingr   IPython.displayr   copyr   r   r   r   r   ipythonwidgetr
   	getLoggerr   rI   setLevelDEBUGr   r    rx   r   r   <module>r      sW       #  - - '			8	$  #L #w1 w1r   