
    ӆhb              	       $   S SK r S SKrS SKrS SKrS SKrS SKJr  SSKJ	r	  SSK
Jr  SSKJrJrJr  SSKJr   S SKrS SKJr  S S	KJr  0 rS
S
S
SSSSSS.rS rS rS r\4S jr\4S jrS rSS jr \" SSSSSS.5      r!S S jr"g! \ a     NFf = f)!    N)LinearSegmentedColormap   )__version__)colors   )methodsmetricsmodels)run_experiments)HTMLz#1E88E5z#ff0d57z#13B755z#999999z#666666z#7C52FF)	tree_shap	deep_shaplinear_shap_corrlinear_shap_indcoefrandomconst_randomkernel_shap_1000_meanrefc                 p   [        [        U 5      R                  R                  S5       H  nUR	                  5       nUR                  S5      (       d  M+  UR                  S5      S   R	                  5       nUR                  S5      (       a$  [        R                  " [        USS 5      5      s  $ Us  $    g)	N
zcolor = =r   zred_blue_circle(   z#000000)	getattrr   __doc__splitstrip
startswithr   red_blue_circlefloat)methodlinevs      gC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\shap/benchmark/plots.pyget_method_colorr%      s    (0066t<zz|??:&&

3"((*A||.//--eAbHo>> =     c                     [        [        U 5      R                  R                  S5       HM  nUR	                  5       nUR                  S5      (       d  M+  UR                  S5      S   R	                  5       s  $    g)Nr   zlinestyle = r   r   solid)r   r   r   r   r   r   )r!   r"   s     r$   get_method_linestyler)     s_    (0066t<zz|??>**::c?1%++-- = r&   c                    [        [        U 5      R                  R                  S5       H  nUR	                  5       nUS-   nSnUR                  U5      (       a0  UR                  U5      (       a  U[        U5      [        U5      *  s  $ US-   nUR                  U5      (       d  M}  [        U[        U5      S  5      s  $    g)Nr   z = ""z =  )	r   r	   r   r   r   r   endswithlenr    )metricattrr"   prefixsuffixs        r$   get_metric_attrr3     s    (0066t<zz| ??6""t}}V'<'<Fs6{l33 ??6""c&km,-- = r&   c                     [        XUS9n[        R                  " 5         / nU Hg  u  nu  pxUu    pn	[        US5      nUS:X  a  U* nOUS:X  a  SU-
  n[        R
                  R                  Xx5      US   -  nUR                  XU45        Mi     [        U5       Hz  u  pn[        [        U
5      R                  R                  S5      S   R                  5       nUS	 S
3U-   n[        R                  " WUS   -  UU[        U
5      S[!        U
5      S9  M|     [        [
        U5      R                  R                  S5      S   R                  5       n[        R"                  " [        US5      5        [        R$                  " [        US5      5        [        [&        U S-   U-   5      R                  R                  S5      S   R                  5       n[        R(                  " US
-   U-   5        [        R*                  " 5       R,                  R/                  S5        [        R*                  " 5       R0                  R/                  S5        [        R*                  " 5       R2                  S   R5                  S5        [        R*                  " 5       R2                  S   R5                  S5        [        R*                  " 5       R7                  5       u  nn[        R8                  " [;        U5      [;        U5      5        [        R<                  " 5       $ )Ndatasetmodelr/   	transformnegate	one_minusr   r   r   r   z6.3f - r   )labelcolor	linewidth	linestylexlabelylabel__bottomleftrightFtop)r   plfigurer3   sklearnr	   aucappendsortedr   r   r   r   r   plotr%   r)   r@   rA   r
   titlegcaxaxisset_ticks_positionyaxisspinesset_visibleget_legend_handles_labelslegendreversedgcf)r6   r7   r/   cmapexperiments
method_arrnamefcountsscores_r!   r8   rJ   method_titler<   metric_titlemodel_titleahandlesalabelss                      r$   
plot_curvere     s]   !'vNKIIKJ#.w1a#FK8	 WF+%ZFoo!!'2WR[@3/0 $/  &j1Vw/77==dCAFLLNt*C </
gbk!"6**62	
  2 7F+3399$?BHHJLIIofh/0IIofh/0&'D.5"89AAGGMaPVVXKHH\E!K/0FFHNN%%h/FFHNN%%f-FFHOOG((/FFHOOE&&u-::<HgIIhx (7"34668Or&   c                 	   [        XUS9n[        R                  " 5         / nU H[  u  nu  pxUu    pn	[        R                  " [        R
                  " US   US   -
  5      5      nUR                  XUS   US   45        M]     [        R                  " S5      nS[        U5      -  S-  nUS-  n[        R                  " XS   S   US	S
SS9  SnSSS.n[        U5       H  u  pn	n[        [        U
5      R                  R                  S5      S   R                  5       nUS S3U-   n[        R                  " XU-  -   UR!                  5       UUS[#        U
5      UR%                  ['        U
5      S 5      S9  US-  nM     [        [(        U5      R                  R                  S5      S   R                  5       n[        R*                  " S5        [        R,                  " S5        [        [.        U S-   U-   5      R                  R                  S5      S   R                  5       n[        R0                  " US-   U-   5        [        R2                  " 5       R4                  R7                  S5        [        R2                  " 5       R8                  R7                  S5        [        R2                  " 5       R:                  S   R=                  S5        [        R2                  " 5       R:                  S   R=                  S5        [        R2                  " 5       R?                  5       u  nn[        R@                  " [        RB                  " / SQ5      X-   S-  -
  / SQ5        [        R2                  " 5       R4                  RE                  [F        RH                  RK                  / SQ5      5        [        R2                  " 5       R4                  RM                  [F        RH                  RO                  / SQ5      5        [        R2                  " 5       RQ                  S SS!9  [        RR                  " SS"S#S$9  [        R2                  " 5       RU                  5       n[        R2                  " 5       RW                  URX                  URZ                  UR\                  S%-  -   UR^                  UR\                  S&-  /5        [        R2                  " 5       Ra                  UUS'S(SS)9  [        Rb                  " 5       $ )*Nr5   r   r            ?皙?g?r   zHuman Consensusblackwhite)r<   r=   	edgecolorz///z...)dasheddottedr   z.2fr;   )r<   rl   r=   hatchzFeatures in the modelzFeature attribution valuerB   rC   rD   rE   FrF   )r   r   r   rg   )r,   r,   r,   r,   )g?gffffff?g333333@)FeverCoughHeadacheminor)whichlengthz#aaaaaa      ?)r=   r>   g333333?gffffff?zupper center)rv   g333333ÿ)locbbox_to_anchorncol)2r   rG   rH   npsumabsrK   aranger.   barrL   r   r   r   r   r   flattenr%   getr)   r	   r@   rA   r
   rN   rO   rP   rQ   rR   rS   rT   rU   xticksarrayset_minor_locator
matplotlibtickerFixedLocatorset_minor_formatterFixedFormattertick_paramsaxhlineget_positionset_positionx0y0heightwidthrV   rX   )r6   r7   r/   rY   rZ   r[   r\   r]   r^   r_   r!   diff_suminds	inc_widthr   iline_style_to_hatchmethods_attrsr`   r<   ra   rb   rc   rd   boxs                            r$   
plot_humanr   D  s   !'vNKIIKJ#.w1a66"&&VAY!6788VAYq	BC $/
 99Q<Ds:&#-IOEFF4Aq!50A\cd	A%*e<.4Z.@*!]w/77==dCAFLLNC.$|3
q= !!#"6*%))*>v*FM	
 	
Q /A 7F+3399$?BHHJLII%&II)*&'D.5"89AAGGMaPVVXKHH\E!K/0FFHNN%%h/FFHNN%%f-FFHOOG((/FFHOOE&&u-::<HgIIbhh|$	(9Q'>>@PQFFHNN$$Z%6%6%C%CO%TUFFHNN&&z'8'8'G'GHf'ghFFHwq1JJq	S1
&&(


!CFFH366366CJJ,<#<ciiVYIYZ[ FFHOOHg>,]^O_668Or&   c           	          S[        [        R                  " [        R                  " X-
  5      5      [        R                  " U 5      R                  5       S-   -  S5      -
  nU$ )zFConverts human agreement differences to numerical scores for coloring.r   rh   )minrz   r{   r|   )human_consensusr   r#   s      r$   _human_score_mapr   |  sK    	Crvvm=>?266/CZC^C^C`cdCdegjkkAHr&   c                   ^^ 0 n0 n[        UU4S jU 5       H  u  u    pxn	u  p[        U	S5      Xi'   X;  a  0 XY'   U(       a?  [        U	S5      nUS:X  a  U* nO)US:X  a  SU-
  nOUS:X  a  [        R                  " U5      * nU
c	  XU	   U'   Mu  U
S:X  a  [	        U6 XY   U'   M  [
        R                  R                  X5      U
S	   -  nXU	   U'   M     [        UR                  5       5      n[        U VVs1 s H  oU   R                  5         H  nUiM     M!     snn5      nS
[        R                  " [        U5      [        U5      45      -  n[        [        U5      5       H0  n[        [        U5      5       H  nX^U      UU      UUU4'   M     M2     [        R                  " US
:H  5      S:X  d   S5       eU(       a;  UUR                  S5      -
  UR                  S5      UR                  S5      -
  S-   -  n[        R                   " UR#                  S5      * 5      nU Vs/ s H  nUU   PM
     nnUUS S 24   nUUU4$ s  snnf s  snf )Nc                 >   > U S   S   T:H  =(       a    U S   S   T:H  $ )Nr   r    )xr6   r7   s    r$   <lambda>make_grid.<locals>.<lambda>  s.    QqT!WPWEWEl\]^_\`ab\cgl\lElr&   
sort_orderr8   r9   r:   r   
negate_loghumanr   iir   zThere are missing data values!g:0yE>)filterr3   rz   log10r   rI   r	   rJ   listkeysonesr.   ranger{   r   maxargsortmean)r^   r6   r7   	normalizer8   
color_valsmetric_sort_orderr_   r!   r/   r]   scoretransform_typerJ   col_keyskr#   row_keysdatar   jr   s    ``                   r$   	make_gridr     s=   J4:;lnt4u0Av 0$3FL$I!#!#J,V[AN);.E	</%(?).vv&)95)AJv&//%%g5CC),vv&) 5v0 JOO%&HG1Q-2D2D2FQQ2FQGHHBGGS]CM:;;D3x=!s8}%A#QK0!=DAJ & " 66$&.!Q&H(HH&txx{"txx{TXXa['@4'GH ::tyy|m$D%)*TTH*a=DXt##% H +s   5&I
(Ired_blue_solid))        ?r   )rh   ?r   ))r   ?r   )rh   r   r   ))r   r   r   )rh   ?r   ))r   r   r   )rh   r   r   )redgreenbluealphac                    Ub  [         R                  " U5        / nU H  nUR                  [        XS95        M     SnSnUS-  nUS-  n[	        U5       GH  u  pt[        X0U5      u  pn
U	 GHP  nSnUR                  S5      (       a  [        XU5        SnOUS	;  a  [        XU5        SnU(       d  MH  [        R                  " 5       n[        R                  " 5       R                  S
S5        [        R                  " USSS9  Ub!  [        R                  " U SU  SU SU S3SS9  [        R                  " 5         UR!                  S5        ["        R$                  " UR'                  5       5      R)                  S5      R+                  SS5      nSU -   S-   U-   S-   U-   nUSU SU S3-  nUSU S3-  nUS-  nGMS     [-        [.        U S-   U-   5      R0                  R3                  S5      S   R5                  5       nUS:X  a{  US-  n[7        U
R8                  S   5       HK  n[-        [:        U	U   5      R0                  R3                  S5      S   R5                  5       nUS U-   S!-   -  nMM     US"-  nUS#-  nUS$-  nUS%U
R8                  S    S&U S'3-  n[7        U
R8                  S   5       GH  nUS(-  n[-        [<        UU   5      R0                  R3                  S5      S   R5                  5       nUS)U-   S*-   -  n[7        U
R8                  S   5       H  nSU -   S-   U-   S-   U	U   -   nUS+U S,3-  nUS-[?        S5      -   S.-   [?        [A        S/ [B        RD                  " S0U
UU4   S1-
  -  5      S S2  5       5      5      -   S3-   [?        S4U
UU4   -  5      -   S5-   [?        S4U
UU4   -  5      -   S6-   -  nUS*-  nM     US"-  nGM     US7U
R8                  S   S-    S83-  nGM     US9-  nUS:-  nUS;[F        -   S<-   -  nUS=-  nUS>U S?:X  a  S@OS-   SA-   -  nUSBU SC:X  a  S@OS-   SD-   -  nUSEU SF:X  a  S@OS-   SG-   -  nUSH-  nUSI-  nUSJ-  nUSK-  nSLn[7        USM-
  5       H  nUS(-  nUSN-  nUU-
  S-
  US-
  -  nUSO[?        [A        SP [B        RD                  " S0US1-
  -  5       5       5      S S2 5      -   S3-   [?        S4U-  5      -   S5-   [?        S4U-  5      -   S6-   -  nUSQ-  nUS"-  nM     USR-  nUSS-  nUb]  [I        UST-   SU5       nURK                  SV5        URK                  U5        URK                  U5        URK                  SW5        S S S 5        g [M        XV-   5      $ ! , (       d  f       g = f)XN)r6   r7   zR<style type='text/css'> .shap_benchmark__select:focus { outline-width: 0 }</style>r,   za<div style='position: fixed; left: 0px; top: 0px; right: 0px; height: 230px; background: #fff;'>
z<div style='position: absolute; bottom: 0px; left: 0px; right: 0px;' align='center'><table style='border-width: 1px; margin-right: 100px'>
Fhuman_T)local_accuracyruntimeconsistency_guaranteesg۶m۶m@gm۶m@png   )formatdpiz/plot_r_   z.pdfpdf)r   r   zutf-8r   plot__rB   z'<div onclick='document.getElementById("z").style.display = "none"' style='display: none; position: fixed; z-index: 10000; left: 0px; right: 0px; top: 0px; bottom: 0px; background: rgba(255,255,255,0.9);' id='z'>z<img width='600' height='500' style='margin-left: auto; margin-right: auto; margin-top: 230px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);' src='data:image/png;base64,z</div>z9<tr><td style='background: #fff; width: 250px'></td></td>r   z<td style='width: 40px; min-width: 40px; background: #fff; text-align: right;'><div style='margin-left: 10px; margin-bottom: -5px; white-space: nowrap; transform: rotate(-45deg); transform-origin: left top 0; width: 1.5em; margin-top: 8em'>z</div></td>z</tr>
z</table></div></div>
zK<table style='border-width: 1px; margin-right: 100px; margin-top: 230px;'>
z3<tr><td style='background: #fff'></td><td colspan='zU' style='background: #fff; font-weight: bold; text-align: center; margin-top: 10px;'>z</td></tr>
z<tr>ze<td style='background: #ffffff; text-align: right; width: 250px' title='shap.LinearExplainer(model)'>z</td>
z&<td onclick='document.getElementById("z").style.display = "block"' style='padding: 0px; padding-left: 0px; padding-right: 0px; border-left: 0px solid #999; width: 42px; min-width: 42px; height: 34px; background-color: #fff'>z<div style='opacity: z; background-color: rgbc              3   >   #    U  H  n[        US -  5      v   M     g7f   Nint.0r#   s     r$   	<genexpr>plot_grids.<locals>.<genexpr>  s     m6lAG6l      ri   r   z
; height:    z1px; margin-left: auto; margin-right: auto; width:z
px'></div>z<tr><td colspan='z%' style='background: #fff'></td></tr>z</table>zs<div style='position: fixed; left: 0px; top: 0px; right: 0px; text-align: left; padding: 20px; text-align: right'>
z<div style='float: left; font-weight: regular; font-size: 24px; color: #000;'>SHAP Benchmark <span style='font-size: 14px; color: #777777;'>vz</span></div>
a/  <select id='shap_benchmark__select' onchange="document.location = '../' + this.value + '/index.html'"dir='rtl' class='shap_benchmark__select' style='font-weight: normal; font-size: 20px; color: #000; padding: 10px; background: #fff; border: 1px solid #fff; -webkit-appearance: none; appearance: none;'>
z<option value='human' r   selectedz)>Agreement with Human Intuition</option>
z<option value='corrgroups60' corrgroups60z'>Correlated Groups 60 Dataset</option>
z$<option value='independentlinear60' independentlinear60z(>Independent Linear 60 Dataset</option>
z
</select>
z</div>
z<table style='border-width: 0px; width: 100px; position: fixed; right: 50px; top: 200px; background: rgba(255, 255, 255, 0.9)'>
za<tr><td style='background: #fff; font-weight: normal; text-align: center'>Higher score</td></tr>
   	   zk<td style='padding: 0px; padding-left: 0px; padding-right: 0px; border-left: 0px solid #999; height: 34px'>z-<div style='opacity: 1; background-color: rgbc              3   >   #    U  H  n[        US -  5      v   M     g7fr   r   r   s     r$   r   r   O  s     Y.XAG.Xr   z</td>z`<tr><td style='background: #fff; font-weight: normal; text-align: center'>Lower score</td></tr>
z	</table>
z/index.htmlwzh<html><body style='margin: 0px; font-size: 16px; font-family: "Myriad Pro", Arial, sans-serif;'><center>z</center></body></html>)'osmkdirextendr   	enumerater   r   r   re   ioBytesIOrG   rX   set_size_inchessavefigcloseseekbase64	b64encodereaddecodereplacer   r
   r   r   r   r   shaper	   r   strtupler   red_blue_no_boundsr   openwriter   )r6   model_namesout_dirr^   r7   r1   outindr   r   r   r/   	save_plotbufdata_uriplot_idrb   r   ra   r   r`   legend_sizevalfs                           r$   
plot_gridsr    s,   
FogCD  bF
C ooC  Z  ZC,
#,Ve#D D
 FI  **762 	VV762 	yjjl((|D

3u#6&JJ'&	5'6($OX]^
!++CHHJ7>>wGOOPTVXY"W,t3e;dBVKDWI  Ny  zA  yB  BD  E  E  f  go  fp  pr  s  s("+ . fgnu&<=EEKKDQRSTZZ\!8NNC4::a=)&w<DDJJ4PQRSYY[ G"##$ * 9C++CaaCDTZZPQ]O  Ti  ju  iv  vB  C  	Ctzz!}%A6MC #7HQK8@@FFtLQOUUWLwC
 4::a=)"W,t3e;dBXa[P@	  JF  G  G+!f/0 %mf6O6OPQUYZ[]^Z^U_beUePf6ghkik6lmmno #	#
 "tAqDz/*+ JJ "tAqDz/*+ ##
 y ! *" 9C7 &: 	"4::a=1#4"55Z[[S -T :C  B  BC 	X
	
	C&   @  @C  G+:	5
6	7C
 ' N2:	<
4	5C
 . $99:r	C
5	6C =C :C   O  OCooCK;?#v||Q"{Q7;%Yf.G.GSSVY.XYYZ][]^_` "s(m B	B
 "s(m 	
 	wy $ nnC<C'M)3/1GG| GGFOGGCLGG-. 0/ FL!! 0/s   AU11
U?)TT)N)#r   r   r   numpyrz   rI   matplotlib.colorsr   r,   r   plotsr   r   r	   r
   rZ   r   r   matplotlib.pyplotpyplotrG   IPython.displayr   ImportErrormetadatabenchmark_color_mapr%   r)   r3   re   r   r   r   r   r  r   r&   r$   <module>r     s     	 	   5   & & (	"$
DN !  )	 :	" -@ #L -@ 5p.$b )MQL+	f"Y  		s   B BB