
    bcg                     b    d dl Z d dlZd dlZddlmZ ej
                  Zd Zd Z G d de      Z	y)    N   )	_catboostc                     	 ddl m} ddl m}  |d       t        | t              s| g} | D ]
  } ||        y # t        $ r0}d}t	        j
                  |       t        t        |            d }~ww xY w)Nr   )iplot)init_notebook_modeT)	connected(To draw plots you should install plotly.)	plotly.offliner   r   ImportErrorwarningswarnstr
isinstancelist)figsr   r   ewarn_msgfigs         P/var/www/html/bid-api/venv/lib/python3.12/site-packages/catboost/plot_helpers.pytry_plot_offliner   	   sk    "(5T*
 dD!vc
   "=h#a&!!"s   : 	A3+A..A3c                 4   d}	 ddl m fd}t        | t
              r't        t        |       d      5 } ||       d d d        y  ||        y # t        $ r.}t        j                  |       t        t        |            d }~ww xY w# 1 sw Y   y xY w)Nr	   r   )plotc                     | j                  dj                  dddddj                        ddf             D ].  } |d	d
d
      }| j                  dj                  |             0 | j                  d       y )N
z<html>z<head>z<meta charset="utf-8" />z@<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>z<title>{}</title>z</head>z<body>divF)output_type	show_linkinclude_plotlyjsz
{}
z</body>
</html>)writejoinformat)plot_file_streamr   	graph_divr   	plot_nameplotly_plots      r   write_plot_filez'save_plot_file.<locals>.write_plot_file    s    tyy&N&&y1*
   	 C#!!&	I ""8??9#=>  	12    w)	r
   r   r   r   r   r   r   openfspath)	plot_filer$   r   r   r   r&   r"   r%   s    ``    @r   save_plot_filer,      s    9H"6
3( )S!&#S)-=,- *) 		"9  "h#a&!!"2 *)s"   A 	B	B)BBBc                   *    e Zd Zd Zd Zd Zd Zd Zy)OfflineMetricVisualizerc                 L    t        |t              r|g}| j                  |       y N)r   r   
_load_data)self
train_dirss     r   __init__z OfflineMetricVisualizer.__init__<   s    j#&$J
#r'   c                    g i d}t         j                  j                  |d      }t         j                  j                  |      r;	 t	        |d      5 }t        j                  |      }|d   |d<   |d   |d<   d d d        |d   r|d   d   d   nd|d   r|d   d	   |d
S d|d
S # 1 sw Y   /xY w# t        $ r Y =w xY w)N)
iterationsmetazcatboost_training.jsonrr7   r6   	iterationr   iteration_count)passed_iterationstotal_iterationsdata)ospathr    isfiler)   jsonload
ValueError)r2   r@   r>   training_json	json_datatraining_datas         r   _update_data_from_dirz-OfflineMetricVisualizer._update_data_from_dirA   s    

 T+CD77>>-(--$(IIi$8M#0#8DL)6|)DD& . IM\HZl!3B!7!D`aCG<V-> ?
 	
UV
 	
 .-  s*   B2 &B&8B2 &B/+B2 2	B>=B>c                    g }t         j                  j                  t         j                  j                        }|D ]V  }t         j                  j                  |      }|j	                  ||k7  rt         j                  j                  |      nd       X i }t        ||      D cg c]
  \  }}||d }	}}|	D ]?  }
|
j                  d      }| j                  |      }|s(||
j                  d      |d||<   A || _	        y c c}}w )Ncurrent)namer@   r@   rK   )r@   rK   content)
r?   r@   abspathcurdirappendbasenamezipgetrH   r>   )r2   r3   namesrN   	train_dirrM   r>   rK   r@   dirsdir_inforL   s               r   r1   z"OfflineMetricVisualizer._load_dataX   s    0#Iggooi0GLLg6G))'2YW $ ?B5*?UV?Utt,?UVH<<'D006G  V,"DJ  	 Ws   &Dc                    	 dd l m} g }| j                  j                         D ]k  \  }}|d   d   d   }|d   dk(  }i }	t        |d         D ]7  \  }
}|d   }|	j                  |i        |d	   D cg c]  }||
f c}|	|   d	<   9 t        |d
         D ]7  \  }
}|d   }|	j                  |i        |d   D cg c]  }||
f c}|	|   d<   9 |d   d   d   }|	j                         D ]  \  }}|j                         }|d   dk(  r|n|d   dz   |z   }|j                  j                  |      |d   d<   d}|j                  d	g       D ]i  \  }}|j                  |j                  |D cg c]  }|d   	 c}|D cg c]
  }||   |    c}|j                   j#                  |      d|             k d}|j                  dg       D ]  \  }}d }|j                  |j                  |D cg c]  }|d   	 c}|D cg c]
  }||   |    c}|j                   j#                  |      d||r|D cg c]
  } ||       c}nd               |j%                  t'        d      t'        |             |j)                  |        n |S # t        $ r.}t        j                  d       t        t        |            d }~ww xY wc c}w c c}w c c}w c c}w c c}w c c}w c c}w )Nr   z1To save plots to files you should install plotly.rL   r>   r7   
parameterslearn_metricsrK   
learn_setstest_metrics	test_setsr6   catboost_info : )textlayouttitlezrgb(160,0,0)r:   )colorlines)xylinemoderK   zrgb(0,160,0)c                     g }| d   d   j                         D ]%  \  }}|j                  |dz   t        |      z          ' dj                  |      S )NrX   r   r^   z<br>)itemsrO   r   r    )r   result
param_nameparam_values       r   generate_params_hoverzGOfflineMetricVisualizer._get_plotly_figs.<locals>.generate_params_hover   sS    !#78q7I7O7O7Q3J"MM*u*<s;?O*OP 8R%{{622r'   )rd   re   rf   rg   rK   	hovertext)ra   )xaxisyaxis)plotly.graph_objs
graph_objsr   r   r   r   r>   ri   	enumerate
setdefaultFigurer`   TitlerR   	add_traceScatterscatterLineupdate_layoutdictrO   )r2   ra   goerrr   r@   dir_datar7   has_parametersmetricsilearn_metricmetric_nameset_nametest_metricr6   subsetsr   figure_titlelearn_graph_colorlearn_set_name
metric_idxr   test_graph_colortest_set_namerm   s                             r   _get_plotly_figsz(OfflineMetricVisualizer._get_plotly_figsp   sK   	(*
 "iioo/ND(I&v.v6D!,/<?N G#,T/-B#C<*62"";3TXYeTf5gTfxmTf5g$\2 $D #,D,@"A;)&1"";3SWXcSd4eSdxh]Sd4e$[1 #B
 "),V4\BJ(/$Wiik*26*:o*MT\]cTdhmmp{{)+l)KHg&$2!29++lB2O.NJMM"**3=>:a1[>:>BLM*Q1^,Z8*MZZ__3D_E$+ #- #  3P $2 18["1M-M:3 MM"**3=>:a1[>:>AKLA1]+J7LZZ__3C_D$*TbZ"PZ#8#;Z"Phl #- #  2N" !!\2[1 " 
 C S )8% 0z E  	(MMMNc#h''	( 6h 5f ?M" ?L #QsA   J 2K:K-K?K0K#K(6K-	K)KKc                 <    t        ||| j                  |             y r0   )r,   r   )r2   ra   file_outputs      r   save_to_filez$OfflineMetricVisualizer.save_to_file   s    {E4+@+@+GHr'   N)__name__
__module____qualname__r4   rH   r1   r   r    r'   r   r.   r.   ;   s    $

.0ENIr'   r.   )
rB   r?   r    r   r*   r   r,   objectr.   r   r'   r   <module>r      s7     	  			 #F}If }Ir'   