U
    Ùf
  ã                   @   s~   d dl Zd dlZd dlZzd dlmZ d dlZW n ek
rN   e 	d¡ Y nX ddl
mZ ddl
mZ dd„ Zdd	d
„ZdS )é    Nzmatplotlib could not be loaded!é   )Úlabels)Úcolorsc                 C   sJ   t | ƒ|krB| d t|d ƒd … d | t|d ƒ d d …  S | S d S )Né   z...r   )ÚlenÚint)ÚtextÚmax_len© r
   ú</tmp/pip-target-lpfmz8o1/lib/python/shap/plots/monitoring.pyÚtruncate_text   s    6r   Tc                 C   s  t t|ƒƒ d¡r&|dkr |j}|j}|jd }|dkrPt dd„ t|ƒD ƒ¡}t	j
dd |dd…| f }t t|ƒ¡}g }d}	t|	t|ƒ|	 |	ƒD ]0}
tj |d|
… ||
d… ¡\}}| |¡ q–t |¡}t |¡|	 |	 }|d	|jd  k r
t	j|d
ddd t	j||d|dd…| f tjd t	 d¡ t	jt||  dƒd dd t	 ¡ j d¡ t	 ¡ j d¡ t	 ¡ jd  d¡ t	 ¡ jd  d¡ t	  ¡ }|j! d¡ |j" #¡  $t	 %¡ j& '¡ ¡}|j" (|j)d d ¡ |j*t||  dƒdd |rt	 +¡  dS )aÛ   Create a SHAP monitoring plot.
    
    (Note this function is preliminary and subject to change!!)
    A SHAP monitoring plot is meant to display the behavior of a model
    over time. Often the shap_values given to this plot explain the loss
    of a model, so changes in a feature's impact on the model's loss over
    time can help in monitoring the model's performance.

    Parameters
    ----------
    ind : int
        Index of the feature to plot.

    shap_values : numpy.array
        Matrix of SHAP values (# samples x # features)

    features : numpy.array or pandas.DataFrame
        Matrix of feature values (# samples x # features)

    feature_names : list
        Names of the features (length # features)
    z'pandas.core.frame.DataFrame'>Nr   c                 S   s   g | ]}t d  t|ƒ ‘qS )ZFEATURE)r   Ústr)Ú.0Úir
   r
   r   Ú
<listcomp>4   s     z#monitoring_plot.<locals>.<listcomp>)é
   é   )Zfigsizeé2   gš™™™™™©?Zdashedz#666666gš™™™™™É?)Z	linestyleÚcolorÚalphar   )ÚsÚcZcmapzSample indexé   z
SHAP valueé   )ÚsizeÚbottomÚleftÚrightFÚtopgffffffæ?é   ),r   ÚtypeÚendswithÚcolumnsÚvaluesÚshapeÚnpÚarrayÚrangeÚplZfigureZaranger   ÚscipyÚstatsZ	ttest_indÚappendÚminZargminZaxvlineZscatterr   Zred_blueZxlabelZylabelr   ZgcaZxaxisZset_ticks_positionZyaxisZspinesZset_visibleZcolorbarÚoutlineZaxZget_window_extentZtransformedZgcfZdpi_scale_transÚinvertedZ
set_aspectÚheightZ	set_labelÚshow)ÚindZshap_valuesÚfeaturesZfeature_namesr0   Znum_featuresZysZxsZpvalsÚincr   ÚstatZpvalZmin_pvalZmin_pval_indÚcbZbboxr
   r
   r   Úmonitoring_plot   sB    
"
"
r6   )NT)Únumpyr%   r)   ÚwarningsZmatplotlib.pyplotZpyplotr(   Z
matplotlibÚImportErrorÚwarnÚ r   r   r   r6   r
   r
   r
   r   Ú<module>   s   
