U
    Ñtœd&  ã                   @   s@   d Z ddlmZ ddlmZ ddlmZ dZG dd„ deƒZd	S )
z?This module provides a dendrogram drawer for the Cairo backend.é    )Úpi)Ústr_to_orientationé   )ÚAbstractCairoDrawer)ÚCairoDendrogramDrawerc                       s<   e Zd ZdZ‡ fdd„Zedd„ ƒZdd„ Zdd	„ Z‡  Z	S )
r   z,Default Cairo drawer object for dendrograms.c                    s   t ƒ  ||¡ || _dS )a  Constructs the drawer and associates it to the given palette.

        @param context: the context on which we will draw
        @param bbox:    the bounding box within which we will draw.
                        Can be anything accepted by the constructor
                        of L{BoundingBox} (i.e., a 2-tuple, a 4-tuple
                        or a L{BoundingBox} object).
        @param palette: the palette that can be used to map integer
                        color indices to colors when drawing vertices
        N)ÚsuperÚ__init__Úpalette)ÚselfÚcontextÚbboxr	   ©Ú	__class__© úX/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/drawing/cairo/dendrogram.pyr      s    zCairoDendrogramDrawer.__init__c                 C   sl   | j dks| j | dkr0| d¡\}}}}}}n | t| j | ƒ¡\}}}}}}|r`|| |fS ||| fS )zoCalculates the amount of space needed for drawing an
        individual vertex at the bottom of the dendrogram.NÚ )Ú_namesZtext_extentsÚstr)Údendror   ÚhorizÚidxZ	x_bearingÚ_ÚheightZ	x_advancer   r   r   Ú_item_box_size   s    ÿz$CairoDendrogramDrawer._item_box_sizec                 C   sª   |j dks|j | dkrdS |  ||d|¡d }|rZ| ||| ¡ | t|j | ƒ¡ nL| ¡  | ||¡ | t d ¡ | d|¡ | t|j | ƒ¡ | 	¡  dS )aC  Plots a dendrogram item to the given Cairo context

        @param context: the Cairo context we are plotting on
        @param horiz: whether the dendrogram is horizontally oriented
        @param idx: the index of the item
        @param x: the X position of the item
        @param y: the Y position of the item
        NTr   ç       @r   )
r   r   Úmove_toZ	show_textr   ÚsaveÚ	translateÚrotater   Úrestore)r
   r   r   r   r   ÚxÚyr   r   r   r   Ú
_plot_item,   s    	z CairoDendrogramDrawer._plot_itemc              	      sÐ  ddl m} ˆj‰ ˆj}ˆjdkr8dd„ tˆjƒD ƒˆ_t| dd¡dd	}|d
k‰ˆ  	¡ d }‡ ‡‡‡fdd„tˆjƒD ƒ}|dkrŠdnd‰|dkršdnd‰‡‡fdd„|D ƒ}|dgˆj dd}ˆ 
¡ }	ˆsfd\}
}t|	ƒD ]*\}}|
df||< |
t||| d ƒ7 }
qäˆjD ]8\}}|d7 }| || d || d  d |f¡ q|dkrø| d¡ n’d\}
}t|	ƒD ],\}}d|f||< |t||| d ƒ7 }qvˆjD ]8\}}|
d7 }
| |
|| d || d  d f¡ qª|dkrø| d¡ tdd„ |D ƒƒ}tdd„ |D ƒƒ}t|jƒt|jƒ }}d\}}ˆrX||8 }|dkrn|}n||8 }|dkrn|}ˆr‚||d 7 }n||d 7 }|j|||| || fdd ˆ  ¡  ˆ  |j|j¡ ˆ  ddd¡ ˆ  d¡ ˆrT|dkrðdnd}tˆjƒD ]R}|| d ||| d   }
|| d || d d  }ˆ ˆˆ ˆ||
|¡ qþnp|dkrbdnd}tˆjƒD ]R}|| d || d d  }
|| d ||| d   }ˆ ˆˆ ˆ||
|¡ qpˆsHtˆjƒD ]p\}\}}|| \}}|| \}}||ˆj  \}}ˆ  ||¡ ˆ  ||¡ ˆ  ||¡ ˆ  ||¡ ˆ  ¡  qÔn|tˆjƒD ]p\}\}}|| \}}|| \}}||ˆj  \}}ˆ  ||¡ ˆ  ||¡ ˆ  ||¡ ˆ  ||¡ ˆ  ¡  qRˆ  ¡  dS )aj
  Draws the given Dendrogram in a Cairo context.

        @param dendro: the igraph.Dendrogram to plot.

        It accepts the following keyword arguments:

          - C{style}: the style of the plot. C{boolean} is useful for plotting
            matrices with boolean (C{True}/C{False} or 0/1) values: C{False}
            will be shown with a white box and C{True} with a black box.
            C{palette} uses the given palette to represent numbers by colors,
            the minimum will be assigned to palette color index 0 and the maximum
            will be assigned to the length of the palette. C{None} draws transparent
            cell backgrounds only. The default style is C{boolean} (but it may
            change in the future). C{None} values in the matrix are treated
            specially in both cases: nothing is drawn in the cell corresponding
            to C{None}.

          - C{square}: whether the cells of the matrix should be square or not.
            Default is C{True}.

          - C{grid_width}: line width of the grid shown on the matrix. If zero or
            negative, the grid is turned off. The grid is also turned off if the size
            of a cell is less than three times the given line width. Default is C{1}.
            Fractional widths are also allowed.

          - C{border_width}: line width of the border drawn around the matrix.
            If zero or negative, the border is turned off. Default is C{1}.

          - C{row_names}: the names of the rows

          - C{col_names}: the names of the columns.

          - C{values}: values to be displayed in the cells. If C{None} or
            C{False}, no values are displayed. If C{True}, the values come
            from the matrix being plotted. If it is another matrix, the
            values of that matrix are shown in the cells. In this case,
            the shape of the value matrix must match the shape of the
            matrix being plotted.

          - C{value_format}: a format string or a callable that specifies how
            the values should be plotted. If it is a callable, it must be a
            function that expects a single value and returns a string.
            Example: C{"%#.2f"} for floating-point numbers with always exactly
            two digits after the decimal point. See the Python documentation of
            the C{%} operator for details on the format string. If the format
            string is not given, it defaults to the C{str} function.

        If only the row names or the column names are given and the matrix
        is square-shaped, the same names are used for both column and row
        names.
        r   )ÚLayoutNc                 S   s   g | ]}t |ƒ‘qS r   )r   )Ú.0r    r   r   r   Ú
<listcomp>~   s     z.CairoDendrogramDrawer.draw.<locals>.<listcomp>ÚorientationÚlrT)Zreversed_vertical)r'   Úrlé   c                    s   g | ]}ˆ  ˆˆ ˆ|¡‘qS r   )r   )r$   r   )r   r   r   r
   r   r   r%   Š   s    Zbtc                    s    g | ]\}}|ˆ  |ˆ f‘qS r   r   )r$   r    r!   )ÚxgapÚygapr   r   r%   ’   s     )r   r   )Údimr   r   r(   c                 s   s   | ]}|d  V  qdS )r   Nr   ©r$   Úer   r   r   Ú	<genexpr>³   s     z-CairoDendrogramDrawer.draw.<locals>.<genexpr>c                 s   s   | ]}|d  V  qdS )r   Nr   r-   r   r   r   r/   ´   s     ÚtbF)Zkeep_aspect_ratiog        éÿÿÿÿ)Zigraph.layoutr#   r   r   r   ÚrangeZ_nitemsr   ÚgetZfont_extentsZ_traverse_inorderÚ	enumerateÚmaxZ_mergesÚappendZmirrorÚfloatÚwidthr   Zfit_intor   r   ÚleftÚtopZset_source_rgbZset_line_widthr"   r   Zline_toÚstroker   )r
   r   Úkwdsr#   r   r&   Zfont_heightZ
item_boxesZlayoutZinorderr    r!   r   ÚelementZid1Zid2ZmaxwZmaxhr8   r   Zdelta_xZdelta_yZsgnZx0Zy0Úx1Úy1Zx2Úy2r   )r   r   r   r
   r*   r+   r   ÚdrawD   s¶    4

 ÿÿ*
*



þ
zCairoDendrogramDrawer.draw)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústaticmethodr   r"   rA   Ú__classcell__r   r   r   r   r      s   
r   N)	rE   Úmathr   Zigraph.drawing.utilsr   Úbaser   Ú__all__r   r   r   r   r   Ú<module>   s
   