
    Bh                        S 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
rSSKr SSKJr  S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JrJr  SS/rS rS r S r!S r" " S S5      r#SSSSSSSSSSSSSSSSSSS.S jr$ " S S5      r%SSSSSSSSS.S jr& " S  S!\5      r'SSSSSS"SSSSSSSSS#S$S%SS&.S' jr(g! \ a    Sr Nf = f)(z(Functions to visualize matrices of data.    N)LineCollection)gridspec)	hierarchyFT   )cm)Grid)get_colormap)despineaxis_ticklabels_overlaprelative_luminanceto_utf8_draw_figureheatmap
clustermapc                     [        U [        R                  5      (       a)  SR                  [	        [
        U R                  5      5      $ U R                  $ )z6Convert a pandas index or multiindex to an axis label.-)
isinstancepd
MultiIndexjoinmapr   namesname)indexs    aC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\seaborn/matrix.py_index_to_labelr      s7    %''xxGU[[122zz    c           	          [        U [        R                  5      (       a:  U R                   Vs/ s H"  nSR	                  [        [        U5      5      PM$     sn$ U R                  $ s  snf )z5Convert a pandas index or multiindex into ticklabels.r   )r   r   r   valuesr   r   r   )r   is     r   _index_to_ticklabelsr!   '   sK    %''38<<@<aWa)<@@|| As   )A&c           
          [         R                  R                  n U" U S   5        [        [	        X5      5      $ ! [
         a.    U  Vs/ s H  n[        [	        X5      5      PM     Os  snf sns $ f = f)zAConvert either a list of colors or nested lists of colors to RGB.r   )mplcolorsto_rgblistr   
ValueError)r$   r%   
color_lists      r   _convert_colorsr)   /   sc    ZZFHvayC'(( H@FG*S,-GGHs   ; A3	A('	A32A3c                 p   Uc%  [         R                  " U R                  [        5      n[	        U[         R
                  5      (       aU  UR                  U R                  :w  a  [        S5      e[        R                  " UU R                  U R                  [        S9nO[	        U[        R                  5      (       aa  UR                  R                  U R                  5      (       d7  UR                  R                  U R                  5      (       a  Sn[        U5      eU[        R                  " U 5      -  nU$ )zEnsure that data and mask are compatible and add missing values.

Values will be plotted for cells where ``mask`` is ``False``.

``data`` is expected to be a DataFrame; ``mask`` can be an array or
a DataFrame.

z&Mask must have the same shape as data.)r   columnsdtypez2Mask must have the same index and columns as data.)npzerosshapeboolr   ndarrayr'   r   	DataFramer   r+   equalsisnull)datamaskerrs      r   _matrix_maskr8   <   s     |xx

D)$

##::#EFF||D"&**$(LL"&(
 
D",,	'	'zz  ,,||""4<<00FCS/!
 "))D/!DKr   c                   B    \ rS rSrSr SS jrS rS rS rS r	S	 r
S
rg)_HeatMappera   z?Draw a heatmap plot of a matrix with nice labels and colormaps.Nc                    [        U[        R                  5      (       a  UR                  nO,[        R
                  " U5      n[        R                  " U5      n[        X5      n[        R                  R                  [        R
                  " U5      U5      nSn[        U[        5      (       a  Un[        UR                  5      nO"USL a  [        UR                  5      nOUSL a  / nSn[        U[        5      (       a  Un[        UR                  5      nO"USL a  [        UR                  5      nOUSL a  / n[        U5      (       d  / U l        / U l        O\[        U[         5      (       a(  US:X  a"  SU l        [        UR                  5      U l        OU R#                  UU5      u  U l        U l        [        U5      (       d  / U l        / U l        O\[        U[         5      (       a(  US:X  a"  SU l        [        UR                  5      U l        OU R#                  UU5      u  U l        U l        [)        UR                  5      n[)        UR                  5      nUb  UOSU l        Ub  UOSU l        U R/                  XUXEU5        Ub  USL a  SnSnOW[        U[0        5      (       a  UnO=[        R
                  " U5      nUR2                  UR2                  :w  a  Sn[5        U5      eSnXl        Xl        Xpl        UU l        Xl        U	c  0 OU	RA                  5       U l!        Xl"        Uc  0 U l#        gURA                  5       U l#        g)zInitialize the plotting object.r   TFautoN (`data` and `annot` must have same shape.)$r   r   r2   r   r-   asarrayr8   mamasked_whereintr!   r+   r   lenxticksxticklabelsstr_skip_ticksyticksyticklabelsr   xlabelylabel_determine_cmap_paramsr0   r/   r'   r5   	plot_dataannot
annot_datafmtcopy	annot_kwscbarcbar_kws)selfr5   vminvmaxcmapcenterrobustrO   rQ   rS   rT   rU   rF   rJ   r6   rN   
xtickevery
ytickeveryrK   rL   rP   r7   s                         r   __init___HeatMapper.__init__d   s    dBLL))I

4(I<<	*D D'EE&&rzz$'7C	 
k3''$J.t||<KD .t||<KE!K
k3''$J.tzz:KD .tzz:KE!K;DK!DS))kV.C DK3DLLAD,0,<,<[=G-I)DK) ;DK!DS))kV.C DK3DJJ?D,0,<,<[=G-I)DK) !. , & 2f & 2f 	##IT$(&	: =EUNEJ%&&&
ZZ.
##y6DC$S/)E 	"
$(0inn6F	&.HMMOr   c                    UR                  [        5      R                  [        R                  5      nUc5  U(       a  [        R
                  " US5      nO[        R                  " U5      nUc5  U(       a  [        R
                  " US5      nO[        R                  " U5      nX#sU l        U l	        Uc/  Uc  [        R                  U l        O|[        R                  U l        Of[        U[        5      (       a  [!        U5      U l        O@[        U["        5      (       a%  [$        R&                  R)                  U5      U l        OX@l        UGb  U R                  [        R*                  R-                  [        R                  /5      5      S   nU R                  [        R.                  * 5      n	U R                  [        R.                  5      n
XR                  S5      :g  nXR                  U R                  R0                  S-
  5      :g  n[3        X5-
  XR-
  5      n[$        R&                  R5                  X]-
  X]-   5      nU" X#/5      u  nn[        R6                  " UUS5      n[$        R&                  R)                  U R                  U5      5      U l        U R                  R9                  U5        U(       a  U R                  R;                  U	5        U(       a  U R                  R=                  U
5        ggg)z@Use some heuristics to set good defaults for colorbar and range.N   b   r   r      )astypefloatfilledr-   nannanpercentilenanminnanmaxrW   rX   r   rocketrY   icefirer   rG   r	   r&   r#   r$   ListedColormaprA   masked_invalidinfNmax	Normalizelinspaceset_bad	set_underset_over)rV   rN   rW   rX   rY   rZ   r[   	calc_databadunderover	under_setover_setvrangenormlizecmincmaxccs                     r   rM   "_HeatMapper._determine_cmap_params   s   
 $$U+22266:	<''	15yy+<''	26yy+#	49 <~II	JJ	c""$T*DId##

11$7DII 
 ))BEE00"&&:;A>C IIrvvg&E99RVV$D1-Iyyq99H6Fzz++FOV_MH!4,/JD$T4-B

11$))B-@DIIIc"		##E*		""4( - r   c                    UR                  5         U R                  R                  u  p4[        R                  " [        R
                  " U5      S-   [        R
                  " U5      S-   5      u  pV[        UR                  UR                  UR                  5       R                  UR                  5       U R                  R                  5       H  u  pxpnU	[        R                  R                  Ld  M&  [        U
5      nUS:  a  SOSnSU R                  -   S-   R                  U5      n[        USSS9nUR!                  U R"                  5        UR$                  " XxU40 UD6  M     g	)
z/Add textual labels with the value in each cell.      ?g&1?z.15wz{:}rZ   )colorhavaN)update_scalarmappablerP   r/   r-   meshgridarangezipflat	get_arrayget_facecolorsrA   maskedr   rQ   formatdictupdaterS   text)rV   axmeshheightwidthxposyposxymr   vallum
text_color
annotationtext_kwargss                   r   _annotate_heatmap_HeatMapper._annotate_heatmap   s   ""$--[[5!1B!6		&8IB8NO
#&tyy$))'+~~'7'<'<d>Q>Q>S'+';';$=A!C $(/&)DjUc
"TXXo3;;C@
"XN""4>>2j8K8$=r   c                     [        U5      nUS:X  a  / / pXA4$ US:X  a  [        R                  " U5      S-   UpXA4$ SX2pvn[        R                  " XVU5      S-   nXXg2   nXA4$ )z3Return ticks and labels at evenly spaced intervals.r   r   r   )rD   r-   r   )rV   labels	tickeverynticksstartendsteps           r   rH   _HeatMapper._skip_ticks	  s~    K>6 } !^IIaL2-v6
 }  !!EIIe$/"4E#N+F}r   c                    UR                   R                  R                  5       nUR                  5       R	                  U5      nUR
                  UR                  /U   nUR                  UR                  /U   nUR                  S/5      u  nUR                  R                  5       n[        XhS-  -  5      n	U	S:  a  / / 4$ [        U5      U	-  S-   n
U
S:X  a  SOU
n
U R                  X*5      u  pX4$ )z5Determine ticks and ticklabels that minimize overlap.r   H   r   )figuredpi_scale_transinvertedget_window_extenttransformedr   r   xaxisyaxis	set_tickslabel1get_sizerC   rD   rH   )rV   r   r   axis	transformbboxsizetickfontsize	max_ticks
tick_everyr   s               r   _auto_ticks_HeatMapper._auto_ticks  s    II--668	##%11)<

DKK(."((#D)s#;;'')B/0	q=r6M[I-1
$/Qz
((<}r   c                    [        USSS9  UR                  S5      c8  UR                  SU R                  5        UR                  SU R                  5        UR
                  " U R                  4SU R                  0UD6nUR                  SU R                  R                  S	   4SU R                  R                  S   4S
9  UR                  5         U R                  (       au  UR                  R                  " XBU40 U R                  D6nUR                   R#                  S5        UR                  SS5      (       a  UR$                  R'                  S5        [)        U R*                  [,        5      (       a/  U R*                  S:X  a  U R/                  XR0                  S5      u  pgOU R*                  U R0                  pv[)        U R2                  [,        5      (       a/  U R2                  S:X  a  U R/                  XR4                  S	5      u  pOU R2                  U R4                  pUR                  XhS9  UR7                  U5      n
UR9                  U	SS9n[:        R<                  " USS9  [?        UR                  5        [A        U
5      (       a  [:        R<                  " U
SS9  [A        U5      (       a  [:        R<                  " USS9  UR                  U RB                  U RD                  S9  U RF                  (       a  U RI                  X5        gg)z&Draw the heatmap on the provided Axes.T)r   leftbottomnormNrW   rX   rY   r   r   )xlimylim
rasterizedFr=   )rE   rI   verticalrotationrZ   )r   
horizontal)rK   rL   )%r
   get
setdefaultrW   rX   
pcolormeshrN   rY   setr5   r/   invert_yaxisrT   r   colorbarrU   outlineset_linewidthsolidsset_rasterizedr   rE   rG   r   rF   rI   rJ   set_xticklabelsset_yticklabelspltsetpr   r   rK   rL   rO   r   )rV   r   caxkwsr   cbrE   rF   rI   rJ   xtlytls               r   plot_HeatMapper.plot&  sT    	2D. 776?"NN6499-NN6499- }}T^^C$))CsC 	Q		*+1diiooa6H2IJ 	 99##DrCT]]CBJJ$$Q' ww|U++		((. dkk3''DKK6,A"&"2"227G7G"KFK"&++t/?/?Kdkk3''DKK6,A"&"2"227G7G"KFK"&++t/?/?K
f,  -  z B" 	RYY"3''HHS:."3''HHS<0 	dkk$++6 ::""2, r   )rO   rP   rS   rT   rU   rY   r5   rQ   rN   rX   rW   rK   rF   rE   rL   rJ   rI   )TTN)__name__
__module____qualname____firstlineno____doc__r^   rM   r   rH   r   r   __static_attributes__ r   r   r:   r:   a   s,    I ;?ZDx7)r9  :-r   r:   z.2gwhiter=   )rW   rX   rY   rZ   r[   rO   rQ   rS   
linewidths	linecolorrT   rU   cbar_axsquarerF   rJ   r6   r   c                    [        XX#XEXgXXUU5      nU	US'   U
US'   Uc  [        R                  " 5       nU(       a  UR                  S5        UR	                  UUU5        U$ )a  Plot rectangular data as a color-encoded matrix.

This is an Axes-level function and will draw the heatmap into the
currently-active Axes if none is provided to the ``ax`` argument.  Part of
this Axes space will be taken and used to plot a colormap, unless ``cbar``
is False or a separate Axes is provided to ``cbar_ax``.

Parameters
----------
data : rectangular dataset
    2D dataset that can be coerced into an ndarray. If a Pandas DataFrame
    is provided, the index/column information will be used to label the
    columns and rows.
vmin, vmax : floats, optional
    Values to anchor the colormap, otherwise they are inferred from the
    data and other keyword arguments.
cmap : matplotlib colormap name or object, or list of colors, optional
    The mapping from data values to color space. If not provided, the
    default will depend on whether ``center`` is set.
center : float, optional
    The value at which to center the colormap when plotting divergent data.
    Using this parameter will change the default ``cmap`` if none is
    specified.
robust : bool, optional
    If True and ``vmin`` or ``vmax`` are absent, the colormap range is
    computed with robust quantiles instead of the extreme values.
annot : bool or rectangular dataset, optional
    If True, write the data value in each cell. If an array-like with the
    same shape as ``data``, then use this to annotate the heatmap instead
    of the data. Note that DataFrames will match on position, not index.
fmt : str, optional
    String formatting code to use when adding annotations.
annot_kws : dict of key, value mappings, optional
    Keyword arguments for :meth:`matplotlib.axes.Axes.text` when ``annot``
    is True.
linewidths : float, optional
    Width of the lines that will divide each cell.
linecolor : color, optional
    Color of the lines that will divide each cell.
cbar : bool, optional
    Whether to draw a colorbar.
cbar_kws : dict of key, value mappings, optional
    Keyword arguments for :meth:`matplotlib.figure.Figure.colorbar`.
cbar_ax : matplotlib Axes, optional
    Axes in which to draw the colorbar, otherwise take space from the
    main Axes.
square : bool, optional
    If True, set the Axes aspect to "equal" so each cell will be
    square-shaped.
xticklabels, yticklabels : "auto", bool, list-like, or int, optional
    If True, plot the column names of the dataframe. If False, don't plot
    the column names. If list-like, plot these alternate labels as the
    xticklabels. If an integer, use the column names but plot only every
    n label. If "auto", try to densely plot non-overlapping labels.
mask : bool array or DataFrame, optional
    If passed, data will not be shown in cells where ``mask`` is True.
    Cells with missing values are automatically masked.
ax : matplotlib Axes, optional
    Axes in which to draw the plot, otherwise use the currently-active
    Axes.
kwargs : other keyword arguments
    All other keyword arguments are passed to
    :meth:`matplotlib.axes.Axes.pcolormesh`.

Returns
-------
ax : matplotlib Axes
    Axes object with the heatmap.

See Also
--------
clustermap : Plot a matrix using hierarchical clustering to arrange the
             rows and columns.

Examples
--------

.. include:: ../docstrings/heatmap.rst

r   	edgecolorequal)r:   r   gca
set_aspectr   )r5   rW   rX   rY   rZ   r[   rO   rQ   rS   r   r   rT   rU   r   r   rF   rJ   r6   r   kwargsplotters                        r   r   r   c  sm    v $d&%#8%t-G
 &F<#F; 
zWWY
gLLWf%Ir   c                   V    \ rS rSrSrS rS rS r\S 5       r	S r
\S 5       rS	 rS
rg)_DendrogramPlotteri  zAObject for drawing tree of similarities between data rows/columnsc                    XPl         U R                   S:X  a  UR                  n[        U[        R                  5      (       a  UR
                  nO,[        R                  " U5      n[        R                  " U5      nXl        Xl	        U R                  R                  U l
        X0l        X@l        XPl         X`l        Xpl        Uc  U R                  U l        OX l        U R#                  5       U l        S[        R&                  " U R                  R                  S   5      -  S-   n	U R                  (       a  [)        U R                  R*                  5      n
U R,                   Vs/ s H  oU   PM	     n
nU R                  (       aF  / U l        Xl        / U l        Xl        [7        U R                  R*                  5      U l        SU l        OsXl        / U l        Xl        / U l        SU l        [7        U R                  R*                  5      U l        O-/ / sU l        U l        / / sU l        U l        Su  U l        U l        U R$                  S   U l        U R$                  S	   U l        gs  snf )
zPlot a dendrogram of the relationships between the columns of data

Parameters
----------
data : pandas.DataFrame
    Rectangular data
r   N
   r      r>   )r>   r>   dcoordicoord) r   Tr   r   r2   r   r-   r@   arrayr5   r/   metricmethodlabelrotatecalculated_linkagelinkagecalculate_dendrogram
dendrogramr   r!   r   reordered_indrE   rI   rF   rJ   r   rL   rK   dependent_coordindependent_coord)rV   r5   r  r  r  r   r  r  r   r   
ticklabelsr    s               r   r^   _DendrogramPlotter.__init__  s    	99>66DdBLL))KKEJJt$E<<&D
	YY__
	
?22DL"L335 RYYtyyq122Q6::-diioo>J151C1CD1CAQ-1CJD{{ ##% #- -diioo> # #- #%  -diioo>')2$DK13R.Dd.'-$DK#x8!%!:- Es   !I$c                 n    [         R                  " U R                  U R                  U R                  S9nU$ )Nr  r  )r   r  r   r  r  )rV   r  s     r   _calculate_linkage_scipy+_DendrogramPlotter._calculate_linkage_scipy  s*    ##DJJt{{+/;;8r   c                 F   SS K nSnU R                  S:H  =(       a    U R                  U;   nU(       d  U R                  S:X  a/  UR                  U R                  U R                  U R                  S9$ UR                  U R                  U R                  U R                  S9nU$ )Nr   )centroidmedianward	euclideansingler  )fastclusterr  r  linkage_vectorr   r  )rV   r  euclidean_methodsr  r  s        r   _calculate_linkage_fastcluster1_DendrogramPlotter._calculate_linkage_fastcluster  s     ;KK;. 4;;4	x/--djj59[[59[[ . B B "))$**T[[15 * >GNr   c                      U R                  5       $ ! [         aN    [        R                  " U R                  5      S:  a  Sn[
        R                  " U5         U R                  5       $ f = f)Ni'  zYClustering large matrix with scipy. Installing `fastcluster` may give better performance.)r  ImportErrorr-   prodr/   warningswarnr  )rV   msgs     r   r  %_DendrogramPlotter.calculated_linkage'  s^    	#6688 	#wwtzz"e+Dc",,..	#s    AA*)A*c                 `    [         R                  " U R                  S[        R                  * S9$ )a  Calculates a dendrogram based on the linkage matrix

Made a separate function, not a property because don't want to
recalculate the dendrogram every time it is accessed.

Returns
-------
dendrogram : dict
    Dendrogram dictionary as returned by scipy.cluster.hierarchy
    .dendrogram. The important key-value pairing is
    "reordered_ind" which indicates the re-ordering of the matrix
T)no_plotcolor_threshold)r   r  r  r-   ro   rV   s    r   r  '_DendrogramPlotter.calculate_dendrogram4  s(     ##DLL$57VVG= 	=r   c                      U R                   S   $ )z2Indices of the matrix, reordered by the dendrogramleaves)r  r'  s    r   r	   _DendrogramPlotter.reordered_indD  s     x((r   c                    Uc  0 OUR                  5       nUR                  SS5        UR                  SUR                  SS5      5        U R                  (       a1  U R                  S:X  a!  [        U R                  U R                  5      nO [        U R                  U R                  5      n[        U VVs/ s H  u  pE[        [        XE5      5      PM     snn40 UD6nUR                  U5        [        U R                  5      n[        [        [        U R                  5      5      nU R                  (       af  UR                  R!                  S5        UR#                  SUS-  5        UR%                  SUS	-  5        UR'                  5         UR)                  5         O*UR%                  SUS-  5        UR#                  SUS	-  5        [+        US
S
S9  UR-                  U R.                  U R0                  U R2                  U R4                  S9  UR7                  U R8                  5      n	UR;                  U R<                  SS9n
[?        UR@                  5        [        U
5      S:  a%  [C        U
5      (       a  [D        RF                  " U
SS9  [        U	5      S:  a%  [C        U	5      (       a  [D        RF                  " U	SS9  U $ s  snnf )zPlots a dendrogram of the similarities between data on the axes

Parameters
----------
ax : matplotlib.axes.Axes
    Axes object upon which the dendrogram is plotted

r   r   r$   r   )皙?r-  r-  r   rightr   g?Tr   r   r   )rE   rI   rK   rL   r   r   r   )$rR   r   popr  r   r   r
  r  r   r&   add_collectionrD   r	  rq   r   r   set_ticks_positionset_ylimset_xliminvert_xaxisr   r
   r   rE   rI   rK   rL   r   rF   r   rJ   r   r   r   r   r   )rV   r   tree_kwscoordsr   r   linesnumber_of_leavesmax_dependent_coordr   r   s              r   r   _DendrogramPlotter.plotI  s2    ")2x}}L"-Hhll7L&IJ;;499>--t/E/EFF//1E1EFFFCFDASYFC +!)+ 	% t112!#c4+?+?"@A;;HH''0 KK+b01KK.56OOOO KK+b01KK.562d.
dkk$++kk$++ 	 	7  !1!12  !1!1J G 	RYYs8a<3C88HHS<0s8a<3C88HHS:.K  Ds   6 K
)r   r   r5   r  r
  r  r  r  r  r  r  r/   rK   rF   rE   rL   rJ   rI   N)r   r   r   r   r   r^   r  r  propertyr  r  r	  r   r   r   r   r   r   r     sG    K=;~
" 
/ 
/=  ) )6r   r   r  average)r  r   r  r  r  r  r6  r   c          
          [         (       a  [        S5      e[        XUXEX6S9n	Uc  [        R                  " 5       nU	R                  XS9$ )aS  Draw a tree diagram of relationships within a matrix

Parameters
----------
data : pandas.DataFrame
    Rectangular data
linkage : numpy.array, optional
    Linkage matrix
axis : int, optional
    Which axis to use to calculate linkage. 0 is rows, 1 is columns.
label : bool, optional
    If True, label the dendrogram at leaves with column or row names
metric : str, optional
    Distance metric. Anything valid for scipy.spatial.distance.pdist
method : str, optional
    Linkage method to use. Anything valid for
    scipy.cluster.hierarchy.linkage
rotate : bool, optional
    When plotting the matrix, whether to rotate it 90 degrees
    counter-clockwise, so the leaves face right
tree_kws : dict, optional
    Keyword arguments for the ``matplotlib.collections.LineCollection``
    that is used for plotting the lines of the dendrogram tree.
ax : matplotlib axis, optional
    Axis to plot on, otherwise uses current axis

Returns
-------
dendrogramplotter : _DendrogramPlotter
    A Dendrogram plotter object.

Notes
-----
Access the reordered dendrogram indices with
dendrogramplotter.reordered_ind

z)dendrogram requires scipy to be installed)r  r   r  r  r  r  )r   r6  )	_no_scipyRuntimeErrorr   r   r   r   )
r5   r  r   r  r  r  r  r6  r   r   s
             r   r  r    sM    T yFGG T(.',=G 
zWWY<<2<11r   c                       \ rS rSr   SS jrS r  SS jr\SS j5       r\SS j5       r	S r
\SS	 j5       rS
 rS rS rS rSrg)ClusterGridi  Nc                 h   [         (       a  [        S5      e[        U[        R                  5      (       a  Xl        O[        R                  " U5      U l        U R                  U R
                  X#U5      U l        [        U R                  U5      U l	        [        R                  " US9U l        U R                  XSS9u  U l        U l        U R                  XSS9u  U l        U l         U	u  p U
u  pU R'                  U R                  UU5      nU R'                  U R                   UU5      nU R                   c  SOSnU R                  c  SOSn[(        R*                  " UUUUS	9U l        U R                  R/                  U R,                  S
   5      U l        U R                  R/                  U R,                  S   5      U l        U R0                  R5                  5         U R2                  R5                  5         SU l        SU l        U R                  b-  U R                  R/                  U R,                  S   5      U l        U R                   b-  U R                  R/                  U R,                  S   5      U l        U R                  R/                  U R,                  S   5      U l        Uc  S=U l        U l        O>U R                  R/                  U R,                  S   5      U l        U R<                  U l        Xl         SU l!        SU l"        g! [$         a    U	=p GN/f = f! [$         a    U
=p GN>f = f)z=Grid object for organizing clustered heatmap input on to axesz*ClusterGrid requires scipy to be available)figsizer   r   r   Nra      )width_ratiosheight_ratios)r   )r   rI  )rI  r   )r   rI  )rI  rI  )r   r   )#r?  r@  r   r   r2   r5   format_datadata2dr8   r6   r   r   _figure_preprocess_colors
row_colorsrow_color_labels
col_colorscol_color_labels	TypeError
dim_ratiosr   GridSpecgsadd_subplotax_row_dendrogramax_col_dendrogramset_axis_offax_row_colorsax_col_colors
ax_heatmapax_cbarr   cbar_posdendrogram_rowdendrogram_col)rV   r5   	pivot_kwsz_scorestandard_scalerD  rN  rP  r6   dendrogram_ratiocolors_ratior^  row_dendrogram_ratiocol_dendrogram_ratiorow_colors_ratiocol_colors_ratiorG  rH  nrowsncolss                       r   r^   ClusterGrid.__init__  s    9KLLdBLL))IT*DI&&tyy)'57 !d3	zz'2 ##D1#= 	/. ##D1#= 	/.	K9I6 	?1=. t';'79 (<(8: __,!__,!##E51=2?A "&!9!9$''%.!I!%!9!9$''%.!I++-++-!!??&!%!9!9" D??&!%!9!9" D ,,224776?C&**DL48  <<33DGGDMBDL||DH ""a  	K:JJ #7	K
  	?2>>/	?s$   )L .L  LL L10L1c                    SnUGb]  [        U[        R                  [        R                  45      (       Ga"  [	        US5      (       d  US:X  d  [	        US5      (       d$  US:X  a  U(       a  SOSnU SU S	3n[        U5      eUS:X  a  UR                  UR                  5      nOUR                  UR                  5      nUR                  [        5      R                  S
5      n[        U[        R                  5      (       a,  [        UR                  5      nUR                  R                  nO*UR                  c  S/nOUR                  /nUR                  n[!        U5      nX$4$ )zAPreprocess {row/col}_colors to extract labels and convert colors.Nr   r   r+   r   colrowz<_colors indices can't be matched with data indices. Provide z?_colors as a non-indexed datatype, e.g. by using `.to_numpy()``r   r>   )r   r   r2   SerieshasattrrR  reindexr   r+   rd   objectfillnar&   r   r   r   r)   )rV   r5   r$   r   r   	axis_namer"  s          r   rM  ClusterGrid._preprocess_colors  s.   &2<<";<<  g..419i00TQY)-5I'[ )//8k :DDC $C.( 19#^^DJJ7F#^^DLL9F  v.55g> fbll33!&..1F#XX__F{{*"$"(++#]]F$V,F~r   c                     Ub  UR                   " S0 UD6nOUnUb  Ub  [        S5      eUb  U R                  XS5      nUb  U R                  XT5      nU$ )z,Extract variables from data or use directly.z:Cannot perform both z-scoring and standard-scaling on datar   )pivotr'   rb  rc  )rV   r5   ra  rb  rc  rK  s         r   rJ  ClusterGrid.format_data0  sn    
  ZZ,),FF>#=LN N \\&2F%((@Fr   c                     US:X  a  U nOU R                   nX"R                  5       -
  UR                  5       -  nUS:X  a  U$ UR                   $ )an  Standarize the mean and variance of the data axis

Parameters
----------
data2d : pandas.DataFrame
    Data to normalize
axis : int
    Which axis to normalize across. If 0, normalize across rows, if 1,
    normalize across columns.

Returns
-------
normalized : pandas.DataFrame
    Noramlized data with a mean of 0 and variance of 1 across the
    specified axis.
r   )r   meanstd)rK  r   z_scoreds      r   rb  ClusterGrid.z_scoreD  sH    $ 19HxxH}}.(,,.@19O::r   c                     US:X  a  U nOU R                   nUR                  5       nX#-
  UR                  5       UR                  5       -
  -  nUS:X  a  U$ UR                   $ )ay  Divide the data by the difference between the max and min

Parameters
----------
data2d : pandas.DataFrame
    Data to normalize
axis : int
    Which axis to normalize across. If 0, normalize across rows, if 1,
    normalize across columns.

Returns
-------
standardized : pandas.DataFrame
    Noramlized data with a mean of 0 and variance of 1 across the
    specified axis.

r   )r   minrq   )rK  r   standardizedsubtracts       r   rc  ClusterGrid.standard_scaleb  sg    ( 19!L!88L##%$/!1!1!335 19>>!r   c                     U/nUb0  [         R                  " U5      S:  a  [        U5      nOSnXEU-  /-  nUR                  S[	        U5      -
  5        U$ )z8Get the proportions of the figure taken up by each axes.ra   r   )r-   ndimrD   appendsum)rV   r$   rd  re  ratiosn_colorss         r   rS  ClusterGrid.dim_ratios  sY    "#wwv"v;,.//F 	a#f+o&r   c                 j  ^  [         R                  R                  U S   5        S[        U 5      snmU /n 0 n[        R                  " UT4[        5      n[        U 5       H9  u  pg[        U5       H%  u  pUR                  U	[        U5      5      n
XXh4'   M'     M;     USS2U4   nUS:X  a  UR                  n[         R                  R                  [        U5      5      nX[4$ ! [         aE    [        U 5      [        U S   5      snm[        U4S jU SS  5       5      (       d  [	        S5      e Nf = f)a[  Turns a list of colors into a numpy matrix and matplotlib colormap

These arguments can now be plotted using heatmap(matrix, cmap)
and the provided colors will be plotted.

Parameters
----------
colors : list of matplotlib colors
    Colors to label the rows or columns of a dataframe.
ind : list of ints
    Ordering of the rows or columns, to reorder the original colors
    by the clustered dendrogram order
axis : int
    Which axis this is labeling

Returns
-------
matrix : numpy.array
    A numpy array of integer values, where each indexes into the cmap
cmap : matplotlib.colors.ListedColormap

r   r   c              3   @   >#    U  H  n[        U5      T:H  v   M     g 7f)N)rD   ).0cr   s     r   	<genexpr><ClusterGrid.color_list_to_matrix_and_cmap.<locals>.<genexpr>  s     7Jqs1v{Js   Nz/Multiple side color vectors must have same size)r#   r$   r%   rD   r'   allr-   r.   rC   	enumerater   r   rm   r&   )r$   indr   r   unique_colorsmatrixr    innerjr   idxrY   r   s               @r   color_list_to_matrix_and_cmap)ClusterGrid.color_list_to_matrix_and_cmap  s%   0
	JJfQi( c&kDAqXF 1a&#&!&)HA%e,#..uc-6HI"qt - * 319XXFzz((m)<=|1  	Tv;F1IDAq7F12J777 !RSS 8	Ts   "C# #AD21D2c                    U(       a*  [        U R                  X4SSU R                  SUUS9	U l        O6U R                  R	                  / 5        U R                  R                  / 5        U(       a)  [        U R                  X4SSU R                  UUS9U l        O6U R                  R	                  / 5        U R                  R                  / 5        [        U R                  SSS9  [        U R                  SSS9  g )NFr   T)r  r  r  r   r   r  r  r6  r   )r  r  r  r   r   r  r6  r/  )	r  rK  rW  r_  
set_xticks
set_yticksrX  r`  r
   )rV   row_clustercol_clusterr  r  row_linkagecol_linkager6  s           r   plot_dendrogramsClusterGrid.plot_dendrograms  s     ",FQ))$!#D ""--b1""--b1",F411;!#D ""--b1""--b14))$TB4))$TBr   c           	      Z   UR                  5       nUR                  SS5        UR                  SS5        UR                  SS5        UR                  SS5        UR                  SS5        UR                  SS5        UR                  SS5        UR                  S	S5        UR                  S
S5        U R                  b  U R                  U R                  USS9u  pEU R                  b  U R                  nOSn[        U4USU R                  USS.UD6  USLa-  [        R                  " U R                  R                  5       SS9  O[        U R                  SSS9  U R                  b  U R                  U R                  USS9u  pEU R                  b  U R                  nOSn[        U4USU R                  SUS.UD6  USLaR  U R                  R                  R                  5         [        R                  " U R                  R!                  5       SS9  gg[        U R                  SSS9  g)zPlots color labels between the dendrogram and the heatmap

Parameters
----------
heatmap_kws : dict
    Keyword arguments heatmap

rY   Nr   rZ   rO   rW   rX   r[   rF   rJ   r   rE  F)rY   rT   r   rF   rJ   Z   r   T)r   r   r   )rR   r0  rN  r  rO  r   rZ  r   r   get_xticklabelsr
   rP  rQ  r[  r   
tick_rightget_yticklabels)rV   xindyindr   r  rY   rO  rQ  s           r   plot_colorsClusterGrid.plot_colors  s    hhj$$t$t$ ??&==A > /LF $$0#'#8#8 #( F LEd6H6H 0eLGJL  u,++;;=KD&&T$? ??&==A > /LF $$0#'#8#8 #( F LEd6H6H %3CLGJL  u,""((335++;;=J - D&&T$?r   c                    U R                   R                  X24   U l         U R                  R                  X24   U l        UR                  SS5      n [        R
                  " U5      U   nUR                  SS5      n [        R
                  " U5      U   nUR                  SS 5      nUb  USL a  Ov[        U[        5      (       a  U R                   nOR[        R
                  " U5      nUR                  U R                   R                  :w  a  Sn	[        U	5      eX   S S 2U4   nUnUR                  SU R                  S L5        [        U R                   4U R                  U R                  XR                  XVUS.UD6  U R                  R!                  5       nU(       d  S OUS	   R#                  5       n
U R                  R$                  R'                  S
5        U R                  R$                  R)                  S
5        U
b.  U R                  R!                  5       n[*        R,                  " XjS9  [/        SSS9nU R                  c  U R0                  R2                  " S0 UD6  g U R                  R5                  5         U R0                  R2                  " S0 UD6  U R                  R7                  5         U R                  R9                  U R:                  5        g ! [        [        4 a     GNf = f! [        [        4 a     GNnf = f)NrF   r=   rJ   rO   Fr?   rT   )r   r   rU   r6   rF   rJ   rO   r   r.  r   {Gz?)h_padw_padr   )rK  ilocr6   r0  r-   r@   rR  
IndexErrorr   r0   r/   r'   r   r]  r   r\  r  get_rotationr   r2  set_label_positionr   r   r   rL  tight_layoutrY  set_axis_onset_positionr^  )rV   colorbar_kwsr  r  r   r   r   rO   rP   r7   ytl_rottight_paramss               r   plot_matrixClusterGrid.plot_matrix$  sz   kk&&tz2IINN4:.	 ggmV,	**S/$'C ggmV,	**S/$'C
 &=EUN%&&![[
ZZ.
##t{{'8'88DC$S/)'-ag6
E 	vt||478 	F%II	FAD	F oo--/!$s1v':':'<009009//113CHHS+#S1<<LL%%55
 LL%%'LL%%55LL$$&LL%%dmm4_ :& 		
 :& 		s$   K >K2 K/.K/2LLc	           
      V   U	R                  SS5      (       a)  Sn
[        R                  " U
5        U	R                  S5        Uc  0 OUnU R	                  XEXXgUS9   U R
                  R                  n U R                  R                  nU R                  " X40 U	D6  U R                  " X;U40 U	D6  U $ ! [         a0    [        R                  " U R                  R                  S   5      n Nyf = f! [         a0    [        R                  " U R                  R                  S   5      n Nf = f)Nr   Fz%``square=True`` ignored in clustermap)r  r  r6  r   r   )r   r   r!  r0  r  r`  r	  AttributeErrorr-   r   rK  r/   r_  r  r  )rV   r  r  r  r  r  r  r  r6  r   r"  r  r  s                r   r   ClusterGrid.plot]  s   
 778U##9CMM#GGH)1r|k*5'/ 	 	1	3&&44D	3&&44D 	+s+T9S9  	399T[[..q12D	3  	399T[[..q12D	3s$   B1 2C. 17C+*C+.7D('D()rL  r]  r[  rX  r\  rZ  rW  r   r^  rQ  rP  r5   rK  r`  r_  rU  r6   rO  rN  )
NNNNNNNNNN)NN)r   )r   )r   r   r   r   r^   rM  rJ  staticmethodrb  rc  rS  r  r  r  r  r   r   r   r   r   rB  rB    s    JNFJDHJ#X(T 48#'(  : " "B$ 1 1fC2?@B75rr   rB  )r   r   r-  gQ?)r  g?g?g
ףp=
?)ra  r  r  rb  rc  rD  rU   r  r  r  r  rN  rP  r6   rd  re  r^  r6  c                |    [         (       a  [        S5      e[        XUXXEXUUS9nUR                  " SX2UXXUS.UD6$ )a  
Plot a matrix dataset as a hierarchically-clustered heatmap.

This function requires scipy to be available.

Parameters
----------
data : 2D array-like
    Rectangular data for clustering. Cannot contain NAs.
pivot_kws : dict, optional
    If `data` is a tidy dataframe, can provide keyword arguments for
    pivot to create a rectangular dataframe.
method : str, optional
    Linkage method to use for calculating clusters. See
    :func:`scipy.cluster.hierarchy.linkage` documentation for more
    information.
metric : str, optional
    Distance metric to use for the data. See
    :func:`scipy.spatial.distance.pdist` documentation for more options.
    To use different metrics (or methods) for rows and columns, you may
    construct each linkage matrix yourself and provide them as
    `{row,col}_linkage`.
z_score : int or None, optional
    Either 0 (rows) or 1 (columns). Whether or not to calculate z-scores
    for the rows or the columns. Z scores are: z = (x - mean)/std, so
    values in each row (column) will get the mean of the row (column)
    subtracted, then divided by the standard deviation of the row (column).
    This ensures that each row (column) has mean of 0 and variance of 1.
standard_scale : int or None, optional
    Either 0 (rows) or 1 (columns). Whether or not to standardize that
    dimension, meaning for each row or column, subtract the minimum and
    divide each by its maximum.
figsize : tuple of (width, height), optional
    Overall size of the figure.
cbar_kws : dict, optional
    Keyword arguments to pass to `cbar_kws` in :func:`heatmap`, e.g. to
    add a label to the colorbar.
{row,col}_cluster : bool, optional
    If ``True``, cluster the {rows, columns}.
{row,col}_linkage : :class:`numpy.ndarray`, optional
    Precomputed linkage matrix for the rows or columns. See
    :func:`scipy.cluster.hierarchy.linkage` for specific formats.
{row,col}_colors : list-like or pandas DataFrame/Series, optional
    List of colors to label for either the rows or columns. Useful to evaluate
    whether samples within a group are clustered together. Can use nested lists or
    DataFrame for multiple color levels of labeling. If given as a
    :class:`pandas.DataFrame` or :class:`pandas.Series`, labels for the colors are
    extracted from the DataFrames column names or from the name of the Series.
    DataFrame/Series colors are also matched to the data by their index, ensuring
    colors are drawn in the correct order.
mask : bool array or DataFrame, optional
    If passed, data will not be shown in cells where `mask` is True.
    Cells with missing values are automatically masked. Only used for
    visualizing, not for calculating.
{dendrogram,colors}_ratio : float, or pair of floats, optional
    Proportion of the figure size devoted to the two marginal elements. If
    a pair is given, they correspond to (row, col) ratios.
cbar_pos : tuple of (left, bottom, width, height), optional
    Position of the colorbar axes in the figure. Setting to ``None`` will
    disable the colorbar.
tree_kws : dict, optional
    Parameters for the :class:`matplotlib.collections.LineCollection`
    that is used to plot the lines of the dendrogram tree.
kwargs : other keyword arguments
    All other keyword arguments are passed to :func:`heatmap`.

Returns
-------
:class:`ClusterGrid`
    A :class:`ClusterGrid` instance.

See Also
--------
heatmap : Plot rectangular data as a color-encoded matrix.

Notes
-----
The returned object has a ``savefig`` method that should be used if you
want to save the figure object without clipping the dendrograms.

To access the reordered row indices, use:
``clustergrid.dendrogram_row.reordered_ind``

Column indices, use:
``clustergrid.dendrogram_col.reordered_ind``

Examples
--------

.. include:: ../docstrings/clustermap.rst

z)clustermap requires scipy to be available)
ra  rD  rN  rP  rb  rc  r6   rd  re  r^  )r  r  r  r  r  r  r  r6  r   )r?  r@  rB  r   )r5   ra  r  r  rb  rc  rD  rU   r  r  r  r  rN  rP  r6   rd  re  r^  r6  r   r   s                        r   r   r   z  sc    N yFGG$W%/")#'3h	HG << 5v%-$/$/!)	5 .4	5 5r   ))r   r   
matplotlibr#   matplotlib.collectionsr   matplotlib.pyplotpyplotr   r   numpyr-   pandasr   scipy.clusterr   r?  r  r>   r   axisgridr   _compatr	   utilsr
   r   r   r   r   __all__r   r!   r)   r8   r:   r   r   r  rB  r   r   r   r   <module>r     s!   .   1    'I   !  l
#
H"J- -H 
DtD
ETG	df&	$iXp pj q[UTd32l$ H 9[xt$4d 4t5[#  Is   B< <CC