U
    SZfq(                     @   s   d dl mZ d dlmZ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 )    N)
exceptionsoptional_imports)utils)
graph_objs)ColorscaleValidatorZnumpyc                 C   s   |dk	rRt |trRt| | tt| D ]&}t| | t|| kr*tdq*|rtt|t| d krtt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-unpacked-wheel-5ksk5baj/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 )a  
    **deprecated**, use instead
    :func:`plotly.express.imshow`.

    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_heatmap0   sj    5
      
   r*   c                 C   sH   |   } | dr,dd |  ddD S | dr@t| 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rgba(),#)strip
startswithsplitclrsZ
hex_to_rgb)Z	color_strdefaultr   r   r   to_rgb_color_list   s    


r6   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    


r9   c                   @   s(   e Zd Z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   s*  || _ |r|| _ntt|d | _|r0|| _ntt|| _|d k	rN|| _n| j | _|| _|| _|| _t	rt
| j t	jrt	| j | _t	| j | _n,tdd | j D | _tdd | j D | _|dd d k	r|d | _|dd d k	r|d | _| j| j d | _|dd d k	r&|d | _d S )	Nr   c                 S   s   g | ]}|D ]}|qqS r   r   r,   rowr-   r   r   r   r.      s       z._AnnotatedHeatmap.__init__.<locals>.<listcomp>c                 S   s   g | ]}|D ]}|qqS r   r   r:   r   r   r   r.      s       zminzmaxr7   zmid)r   r   r	   r
   r   r   r   r   r'   npr   ZndarrayZaminr<   Zamaxr=   minmaxgetr>   )	selfr   r   r   r   r   r'   r   r(   r   r   r   __init__   s2    

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   r6   r9   )	rC   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   s   t | \}}g }t| jD ]l\}}t|D ]Z\}}|| jk rB|n|}|tjjt	| j
| | | j| | j| ddt|ddd q,q|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$   rI   	enumerater   r>   appendr   r#   Z
Annotationstrr   r   r   r&   )	rC   rG   rH   r    nr;   mvalZ
font_colorr   r   r   r%     s"    z"_AnnotatedHeatmap.make_annotationsN)__name__
__module____qualname____doc__rD   rI   r%   r   r   r   r   r$      s   &Kr$   )NNNr   NFF)Zplotly.colorscolorsr4   Zplotlyr   r   Zplotly.figure_factoryr   Zplotly.graph_objsr   Zplotly.validators.heatmapr   Z
get_moduler?   r   r*   r6   r9   objectr$   r   r   r   r   <module>   s"   
'       
f
