B
    &[_'                 @   s   d dl mZmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ edZdd	 ZdddZdd Zdd ZG dd deZdS )    )absolute_importdivision)
exceptionsoptional_importsN)utils)
graph_objs)ColorscaleValidatorZnumpyc             C   s   |dk	rVt |trVt| | x6tt| D ]&}t| | t|| kr,tdq,W |rxt|t| d krxtd|rt|t| krtddS )a(  
    Annotated-heatmap-specific validations

    Check that if a text matrix is supplied, it has the same
    dimensions as the z matrix.

    See FigureFactory.create_annotated_heatmap() for params

    :raises: (PlotlyError) If z and text matrices do not  have the same
        dimensions.
    Nz*z and text should have the same dimensionsr   zMoops, the x list that you provided does not match the width of your z matrix zNoops, the y list that you provided does not match the length of your z matrix )
isinstancelistr   Zvalidate_equal_lengthrangelenr   ZPlotlyError)zxyannotation_textlst r   L/tmp/pip-install-l29rncou/plotly/plotly/figure_factory/_annotated_heatmap.pyvalidate_annotated_heatmap   s    
r   PlasmaFc          
   K   s   |dk	r|ng }t | ||| t }	|	|}t| ||||||f| }
|sR|rtf d| |||||d|}t|
tdddddtddd	d
d}n>tf d| |||d|}t|
tdddddtdd	ddd}|g}tj||dS )am  
    Function that creates annotated heatmaps

    This function adds annotations to each cell of the heatmap.

    :param (list[list]|ndarray) z: z matrix to create heatmap.
    :param (list) x: x axis labels.
    :param (list) y: y axis labels.
    :param (list[list]|ndarray) annotation_text: Text strings for
        annotations. Should have the same dimensions as the z matrix. If no
        text is added, the values of the z matrix are annotated. Default =
        z matrix values.
    :param (list|str) colorscale: heatmap colorscale.
    :param (list) font_colors: List of two color strings: [min_text_color,
        max_text_color] where min_text_color is applied to annotations for
        heatmap values < (max_value - min_value)/2. If font_colors is not
        defined, the colors are defined logically as black or white
        depending on the heatmap's colorscale.
    :param (bool) showscale: Display colorscale. Default = False
    :param (bool) reversescale: Reverse colorscale. Default = False
    :param kwargs: kwargs passed through plotly.graph_objs.Heatmap.
        These kwargs describe other attributes about the annotated Heatmap
        trace such as the colorscale. For more information on valid kwargs
        call help(plotly.graph_objs.Heatmap)

    Example 1: Simple annotated heatmap with default configuration

    >>> import plotly.figure_factory as ff

    >>> z = [[0.300000, 0.00000, 0.65, 0.300000],
    ...      [1, 0.100005, 0.45, 0.4300],
    ...      [0.300000, 0.00000, 0.65, 0.300000],
    ...      [1, 0.100005, 0.45, 0.00000]]

    >>> fig = ff.create_annotated_heatmap(z)
    >>> fig.show()
    NZheatmap)typer   r   r   
colorscale	showscalereversescale    topzrgb(0, 0, 0))ticksdtickside	gridcolorz  )r   r   
ticksuffix)annotationsZxaxisZyaxis)r   r   r   r   r   F)r   r   r    showticklabels)r   r!   r#   )datalayout)r   r   Zvalidate_coerce_AnnotatedHeatmapmake_annotationsdictr   ZFigure)r   r   r   r   r   font_colorsr   r   kwargsZcolorscale_validatorr"   tracer%   r$   r   r   r   create_annotated_heatmap2   sD    2
r,   c             C   s<   d| kr"dd |  ddD S d| kr4t| S |S d S )NZrgbc             S   s   g | ]}t |qS r   )int).0vr   r   r   
<listcomp>   s    z%to_rgb_color_list.<locals>.<listcomp>zrgb(),#)stripsplitclrsZ
hex_to_rgb)Z	color_strdefaultr   r   r   to_rgb_color_list   s
    
r7   c             C   s(   | d d | d d  | d d  dkS )Nr   gA`"?r   gbX9?   gv/?   r   )Zbackground_colorr   r   r   should_use_black_text   s    "r:   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r&   zH
    Refer to TraceFactory.create_annotated_heatmap() for docstring
    c       	      K   sl   || _ |r|| _ntt|d | _|r0|| _ntt|| _|d k	rN|| _n| j | _|| _|| _|| _d S )Nr   )	r   r   r   r   r   r   r   r   r)   )	selfr   r   r   r   r   r)   r   r*   r   r   r   __init__   s    z_AnnotatedHeatmap.__init__c       	      C   s2  ddddddddd	d
ddddg}dg}d}d}| j rJ| j d }| j d }n| j|krd| jrd|}|}n| j|krx|}|}n| j|kr| jr|}|}n| j|kr|}|}nt| jtr"t| jd d dddg}t| jd d dddg}| jr|| }}t|r|}n|}t|r|}n|}n|}|}||fS )a  
        Get font color for annotations.

        The annotated heatmap can feature two text colors: min_text_color and
        max_text_color. The min_text_color is applied to annotations for
        heatmap values < (max_value - min_value)/2. The user can define these
        two colors. Otherwise the colors are defined logically as black or
        white depending on the heatmap's colorscale.

        :rtype (string, string) min_text_color, max_text_color: text
            color for annotations for heatmap values <
            (max_value - min_value)/2 and text color for annotations for
            heatmap values >= (max_value - min_value)/2
        ZGreysZGreensZBluesZYIGnBuZYIOrRdZRdBuZPicnicZJetZHotZ	BlackbodyZEarthZElectricZViridisZCividisZRedsz#FFFFFFz#000000r   r      )r)   r   r   r	   r
   r7   r:   )	r;   ZcolorscalesZcolorscales_reverseZwhiteZblackmin_text_colormax_text_colorZmin_colZmax_colr   r   r   get_text_color   s\    





z _AnnotatedHeatmap.get_text_colorc             C   sd   t r,t| jt jr,t | j}t | j}n(tdd | jD }tdd | jD }|| d }|S )zh
        Get the mid value of z matrix

        :rtype (float) z_avg: average val from z matrix
        c             S   s   g | ]}|D ]}|qqS r   r   )r.   rowr/   r   r   r   r0     s    z/_AnnotatedHeatmap.get_z_mid.<locals>.<listcomp>c             S   s   g | ]}|D ]}|qqS r   r   )r.   rB   r/   r   r   r   r0     s    r8   )npr	   r   ZndarrayZaminZamaxminmax)r;   Zz_minZz_maxz_midr   r   r   	get_z_mid  s    z_AnnotatedHeatmap.get_z_midc       
      C   s   t | \}}t | }g }x|t| jD ]n\}}xdt|D ]X\}}||k rN|n|}	|tjjt	| j
| | | j| | j| ddt|	ddd q:W q(W |S )z
        Get annotations for each cell of the heatmap with graph_objs.Annotation

        :rtype (list[dict]) annotations: list of annotations for each cell of
            the heatmap
        x1y1)colorF)textr   r   ZxrefZyreffontZ	showarrow)r&   rA   rG   	enumerater   appendr   r%   Z
Annotationstrr   r   r   r(   )
r;   r?   r@   rF   r"   nrB   mvalZ
font_colorr   r   r   r'     s     
z"_AnnotatedHeatmap.make_annotationsN)__name__
__module____qualname____doc__r<   rA   rG   r'   r   r   r   r   r&      s
   Kr&   )NNNr   NFF)
__future__r   r   Zplotlyr   r   Zplotly.colorscolorsr5   Zplotly.figure_factoryr   Zplotly.graph_objsr   Zplotly.validators.heatmapr   Z
get_modulerC   r   r,   r7   r:   objectr&   r   r   r   r   <module>   s"   
'      
[	