o
    h7                     @   s   d Z ddlZddlmZ ddlZddlZddlm	Z	 ddl
mZ ddlmZ ddlmZ eeZdZd	gZd
ZdZdZdZdZG dd dZdS )z7
Module for creating Sankey diagrams using Matplotlib.
    N)SimpleNamespace)Path)	PathPatch)Affine2D)
_docstringzKevin L. DavieszYannick CopinBSDz
2011/09/16      c                   @   sf   e Zd ZdZ		
	dddZd ddZdd Zdd Zej	fddZ
ej			d!ddZdd ZdS )"Sankeyab  
    Sankey diagram.

      Sankey diagrams are a specific type of flow diagram, in which
      the width of the arrows is shown proportionally to the flow
      quantity.  They are typically used to visualize energy or
      material or cost transfers between processes.
      `Wikipedia (6/1/2011) <https://en.wikipedia.org/wiki/Sankey_diagram>`_

    N      ? %G      ?皙?Q?333333?d   皙?ư>c                 K   s  |dk rt d||krt d|	dk rt d|dk r t d|du r8ddlm} | }|jdddg g d}g | _|| _|| _|| _|| _	|| _
|| _|| _|| _|
| _ttjd|	d	   d
 | _|| _ttjtj tjtj f| _t|r| jdi | dS dS )a  
        Create a new Sankey instance.

        The optional arguments listed below are applied to all subdiagrams so
        that there is consistent alignment and formatting.

        In order to draw a complex Sankey diagram, create an instance of
        `Sankey` by calling it without any kwargs::

            sankey = Sankey()

        Then add simple Sankey sub-diagrams::

            sankey.add() # 1
            sankey.add() # 2
            #...
            sankey.add() # n

        Finally, create the full diagram::

            sankey.finish()

        Or, instead, simply daisy-chain those calls::

            Sankey().add().add...  .add().finish()

        Other Parameters
        ----------------
        ax : `~matplotlib.axes.Axes`
            Axes onto which the data should be plotted.  If *ax* isn't
            provided, new Axes will be created.
        scale : float
            Scaling factor for the flows.  *scale* sizes the width of the paths
            in order to maintain proper layout.  The same scale is applied to
            all subdiagrams.  The value should be chosen such that the product
            of the scale and the sum of the inputs is approximately 1.0 (and
            the product of the scale and the sum of the outputs is
            approximately -1.0).
        unit : str
            The physical unit associated with the flow quantities.  If *unit*
            is None, then none of the quantities are labeled.
        format : str or callable
            A Python number formatting string or callable used to label the
            flows with their quantities (i.e., a number times a unit, where the
            unit is given). If a format string is given, the label will be
            ``format % quantity``. If a callable is given, it will be called
            with ``quantity`` as an argument.
        gap : float
            Space between paths that break in/break away to/from the top or
            bottom.
        radius : float
            Inner radius of the vertical paths.
        shoulder : float
            Size of the shoulders of output arrows.
        offset : float
            Text offset (from the dip or tip of the arrow).
        head_angle : float
            Angle, in degrees, of the arrow heads (and negative of the angle of
            the tails).
        margin : float
            Minimum space between Sankey outlines and the edge of the plot
            area.
        tolerance : float
            Acceptable maximum of the magnitude of the sum of flows.  The
            magnitude of the sum of connected flows cannot be greater than
            *tolerance*.
        **kwargs
            Any additional keyword arguments will be passed to `add`, which
            will create the first subdiagram.

        See Also
        --------
        Sankey.add
        Sankey.finish

        Examples
        --------
        .. plot:: gallery/specialty_plots/sankey_basics.py
        r   zS'gap' is negative, which is not allowed because it would cause the paths to overlapz`'radius' is greater than 'gap', which is not allowed because it would cause the paths to overlapzp'head_angle' is negative, which is not allowed because it would cause inputs to look like outputs and vice versaz3'tolerance' is negative, but it must be a magnitudeNr   )xticksyticksg     f@       @ )
ValueErrormatplotlib.pyplotpyplotfigureadd_subplotdiagramsaxunitformatscalegapradiusshoulderoffsetmarginnptanpipitch	tolerancearrayinfextentlenadd)selfr   r"   r    r!   r#   r$   r%   r&   
head_angler'   r,   kwargspltfigr   r   e/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/sankey.py__init__*   sH   S zSankey.__init__r   Tr   r   r   c              
   C   s
  t jt jt jt jt jt jt jg}tddgddgddgddgddgddgddgg}|dv r?|r3|}n7|ddddd	f }n+|rVt|ddd
f  |dddf f}nt|dddf  |ddd
f f}|d
krq| }tt||| t||j	d d
f S )a+  
        Return the codes and vertices for a rotated, scaled, and translated
        90 degree arc.

        Other Parameters
        ----------------
        quadrant : {0, 1, 2, 3}, default: 0
            Uses 0-based indexing (0, 1, 2, or 3).
        cw : bool, default: True
            If True, the arc vertices are produced clockwise; counter-clockwise
            otherwise.
        radius : float, default: 1
            The radius of the arc.
        center : (float, float), default: (0, 0)
            (x, y) tuple of the arc's center.
        r   g        gw,?gS?g?g>ef?)r      Nr   r   )
r   LINETOCURVE4r(   r-   column_stacklistziptileshape)r2   quadrantcwr$   center	ARC_CODESARC_VERTICESverticesr   r   r7   _arc   s@   
		zSankey._arcc              
   C   s
  |du rddgddgfS |d d \}}|d | j  }|tkr^||8 }|| ||d  g}|tj||gftj|ftj||| gftj|| j || gfg |d | j |d g}	||	fS || j8 }|tkrjd}
nd}
||d  ||
||   g}|tkrd}nd}| j	r|| j
||tk| j	|| j	 ||
| j	  fd n
|tj||gf |tj|||
|  gftj|ftj|| ||
|  gfg || j
||tk|| j	 || j	 ||
| j	  fd |tj|| ||
|  gf |d |d |
| j  g}	||	fS )zP
        Add an input to a path and return its tip and label locations.
        Nr   r;   r   r:   r   rC   rD   r$   rE   )r+   RIGHTextendr   r<   r#   r&   UPDOWNr$   rI   append)r2   pathangleflowlengthxydipdepthdiplabel_locationsignrC   r   r   r7   
_add_input   sZ   "



 zSankey._add_inputc                 C   sf  |du rddgddgfS |d d \}}| j |d  | j }|tkru||7 }|| ||d  g}|tj||gftj||| j  gftj|ftj||| j  | gftj||| gftj|| j || gfg |d | j |d g}	||	fS || j7 }|tkrd\}
}nd\}
}||d  ||
||   g}| j	r|| j
||tk| j	|| j	 ||
| j	  fd	 n
|tj||gf |tj|||
|  gftj|| j  ||
|  gftj|ftj|| j  | ||
|  gftj|| ||
|  gfg || j
||tk| j	| || j	 ||
| j	  fd	 |tj|| ||
|  gf |d |d |
| j  g}	||	fS )
z
        Append an output to a path and return its tip and label locations.

        .. note:: *flow* is negative for an output.
        Nr   r;   r   r:   r   )r   r	   )r;   r   rJ   )r%   r+   rK   rL   r   r<   r#   r&   rM   r$   rI   rO   rN   )r2   rP   rQ   rR   rS   rT   rU   	tipheighttiprX   rY   rC   r   r   r7   _add_output  sd    






 zSankey._add_outputc                 C   s6   g }|}|ddd D ]\}}| ||f |}q|S )z
        A path is not simply reversible by path[::-1] since the code
        specifies an action to take from the **previous** point.
        Nr;   )rO   )r2   rP   first_actionreverse_path	next_codecodepositionr   r   r7   _revertL  s   zSankey._revertc
           6   
      s  |du rt ddgnt |}|jd }du rdnd |du r&d}zt ||}W n tyG   tdt | dt | ddw zt ||}W n tyi   tdt | d	t | ddw |dk rrtd
tt || jkrt	dt || | j
| }tdd |D }tdd |D }|dur6|dk rtdt|dk rtd|t| jkrtd| dt| j d|d t| j| jkrtd|d t| j| j|d |krtd|d  d| d| j| j|d  du rtd|d  d| d| j| j|d  ||d   }t|| jkr6td| d| j ddg| }t|D ]'\}}|| jkrOd||< q?|| j kr\d ||< q?t	d!||| j q?dg| }tt||D ]R\}\}}|dkr|rt||< qs|d u rt||< qs|dkr|durt||< qs|d"krtd#| d$| d%|rt||< qs|d u rt||< qst |rt||krtd&| d't| d(n|}|}|}|}t|d)fd*d+|D }tt|||D ],\}\}}}|tkr|r|||< ||7 }q|tkr*|d u r*|||< ||8 }qtttt|||D ]4\}\}}}|tkrU|rU|||| d < ||7 }q8|tkrk|d u rk|||| d < ||8 }q8d }tttt||t||D ]\}\}}}|tkr|r|rd||| d < q~d}q~d }tt||tt||D ]\}\}}}|tkr|d u r|rd||< qd}qtj| j|d,  |d, gftj| j|d,  d, |d, gftj| j|d,  d- |d, gftj|d, | j d- | d, gftj|d, | j d, | d, gftj|d, | j | d, gfg}tj|d, | j |d, gftj|d, | j d, |d, gftj|d, | j d- |d, gftj| j|d,  d- | d, gftj| j|d,  d, | d, gftj| j|d,  | d, gfg}tj|d, | j |d, gfg} tj| j|d,  |d, gfg}!t |d.f}"t |d.f}#tt||tt||D ]L\}\}}}|tkr|r| j |!|g|R  \|"|ddf< |#|ddf< q|tkr|d u r| j!||g|R  \|"|ddf< |#|ddf< qtttt||tt||D ]d\}\}}}|tkrM|rM| j ||g|R  \}$}%|$|"|| d ddf< |%|#|| d ddf< q|tkr{|d u r{| j!| |g|R  \}$}%|$|"|| d ddf< |%|#|| d ddf< qd }tttt||tt||D ]`\}\}}}|tkr|r|s|d" d d |!d" d d kr|"tj|!d" d d |d" d d gf d}| j ||g|R  \}$}%|$|"|| d ddf< |%|#|| d ddf< qd }tt||tt||D ]V\}\}}}|tkrU|d u rU|s=|d" d d | d" d d k r;|"tj| d" d d |d" d d gf d}| j!||g|R  \|"|ddf< |#|ddf< q |sb|!#  |#  |sm| #  |#  || $|  | | $|! tj%|d d fg }&t|& \}'}(t |(}(d/d0  |du rɈdkr fd1d+|D }t& 'd2 j(})|)|"}"|)|#}#|)|(}(| j)j*dd|d3d3d4}*n^| j| j|d  ||d    fd5d+|D }t& 'd2 j(})|)|"}"| j| j+|d  |"|d   }+t& j,|+ j(},|,|"}"|,|)|#}#|,|)|(}(t|d3d3d4}-| j)j*|+i |-}*t-j.d6 rB|
#d7|
#d8d9}.|
#d:|
#d;d<}/n|
#d7|
#d8d}.|
#d:|
#d;d}/|.du ra| j)j/0 }.t1t|(|'f|.|/d=|
}0| j)2|0 g }1t||||#D ][\}2}}3}4|3du s|du rd>}3n5| j3durt4| jt5r| jt|2 | j3 }5nt6| jr| |2}5nt7d?|3d>kr|3d@7 }3|3|57 }3|1"| j)j*|4d |4d |3d3d3dA q~tt |(dddf t |#dddf | j8d t9t 9|(dddf t 9|#dddf | j8d tt |(dddf t |#dddf | j8d. t9t 9|(dddf t 9|#dddf | j8dB f| _8| j"t:|0|||"|*|1dC | S )Du  
        Add a simple Sankey diagram with flows at the same hierarchical level.

        Parameters
        ----------
        patchlabel : str
            Label to be placed at the center of the diagram.
            Note that *label* (not *patchlabel*) can be passed as keyword
            argument to create an entry in the legend.

        flows : list of float
            Array of flow values.  By convention, inputs are positive and
            outputs are negative.

            Flows are placed along the top of the diagram from the inside out
            in order of their index within *flows*.  They are placed along the
            sides of the diagram from the top down and along the bottom from
            the outside in.

            If the sum of the inputs and outputs is
            nonzero, the discrepancy will appear as a cubic Bézier curve along
            the top and bottom edges of the trunk.

        orientations : list of {-1, 0, 1}
            List of orientations of the flows (or a single orientation to be
            used for all flows).  Valid values are 0 (inputs from
            the left, outputs to the right), 1 (from and to the top) or -1
            (from and to the bottom).

        labels : list of (str or None)
            List of labels for the flows (or a single label to be used for all
            flows).  Each label may be *None* (no label), or a labeling string.
            If an entry is a (possibly empty) string, then the quantity for the
            corresponding flow will be shown below the string.  However, if
            the *unit* of the main diagram is None, then quantities are never
            shown, regardless of the value of this argument.

        trunklength : float
            Length between the bases of the input and output groups (in
            data-space units).

        pathlengths : list of float
            List of lengths of the vertical arrows before break-in or after
            break-away.  If a single value is given, then it will be applied to
            the first (inside) paths on the top and bottom, and the length of
            all other arrows will be justified accordingly.  The *pathlengths*
            are not applied to the horizontal inputs and outputs.

        prior : int
            Index of the prior diagram to which this diagram should be
            connected.

        connect : (int, int)
            A (prior, this) tuple indexing the flow of the prior diagram and
            the flow of this diagram which should be connected.  If this is the
            first diagram or *prior* is *None*, *connect* will be ignored.

        rotation : float
            Angle of rotation of the diagram in degrees.  The interpretation of
            the *orientations* argument will be rotated accordingly (e.g., if
            *rotation* == 90, an *orientations* entry of 1 means to/from the
            left).  *rotation* is ignored if this diagram is connected to an
            existing one (using *prior* and *connect*).

        Returns
        -------
        Sankey
            The current `.Sankey` instance.

        Other Parameters
        ----------------
        **kwargs
           Additional keyword arguments set `matplotlib.patches.PathPatch`
           properties, listed below.  For example, one may want to use
           ``fill=False`` or ``label="A legend entry"``.

        %(Patch:kwdoc)s

        See Also
        --------
        Sankey.finish
        Nr   g      r   g     V@zThe shapes of 'flows' z and 'orientations' z are incompatiblez and 'labels' zR'trunklength' is negative, which is not allowed because it would cause poor layoutzWThe sum of the flows is nonzero (%f; patchlabel=%r); is the system not at steady state?c                 s       | ]}t |d V  qdS r   N)max.0rR   r   r   r7   	<genexpr>      zSankey.add.<locals>.<genexpr>c                 s   rd   re   )minrg   r   r   r7   ri     rj   z*The index of the prior diagram is negativez2At least one of the connection indices is negativez"The index of the prior diagram is z, but there are only z other diagramszTThe connection index to the source diagram is {}, but that diagram has only {} flowsr   z(The connection index to this diagram is z, but this diagram has only z flowszHThe connection cannot be made, which may occur if the magnitude of flow z of diagram z% is less than the specified tolerancez)The scaled sum of the connected flows is z%, which is not within the tolerance ()TFzwThe magnitude of flow %d (%f) is below the tolerance (%f).
It will not be shown, and it cannot be used in a connection.r;   zThe value of orientations[z] is z, but it must be -1, 0, or 1zThe lengths of 'flows' (z) and 'pathlengths' (z) are incompatible)rK   c                    s   g | ]}  |d qS )r   )getrh   rQ   )dr   r7   
<listcomp>%  s    zSankey.add.<locals>.<listcomp>r   g       @r:   c                 S   s   | d u rd S | | S )Nr   )arr   r   r7   
_get_angle  s   zSankey.add.<locals>._get_anglec                       g | ]} |qS r   r   rn   rs   rotationr   r7   rp         Z   rE   )shavac                    rt   r   r   rn   ru   r   r7   rp     rw   z_internal.classic_modefc	facecolorz#bfd1d4lw	linewidthg      ?)r|   r~   r   z*format must be callable or a format string
)rT   rU   ry   rz   r{   r	   )patchflowsanglestipstexttexts);r(   r-   rB   broadcast_tor   abssumr,   _loginfor"   rk   r0   r   r   r!   r   	enumerater@   rN   rM   rK   iterabledictreversedr?   r   MOVETOr#   r<   r=   zerosrZ   r]   rO   poprc   	CLOSEPOLYr   
rotate_degtransform_affiner   r   r   	translatemplrcParams_get_patches_for_fillget_next_colorr   	add_patchr    
isinstancestrcallable	TypeErrorr/   rf   r   )6r2   
patchlabelr   orientationslabelstrunklengthpathlengthspriorconnectrv   r4   nscaled_flowsgainloss
flow_error
are_inputsirR   r   orientis_inputurlengthullengthlrlengthlllengthrQ   has_left_inputspechas_right_outputurpathllpathlrpathulpathr   label_locationsr\   rX   rP   codesrH   rotater   r&   r   kwdsr|   r~   r   r   numberlabellocationquantityr   )rs   ro   rv   r7   r1   ^  s   W
















($""$



 


z
Sankey.addc                 C   sZ   | j | jd | j | jd | j | jd | j | jd | j g | j jddd | jS )a  
        Adjust the Axes and return a list of information about the Sankey
        subdiagram(s).

        Returns a list of subdiagrams with the following fields:

        ========  =============================================================
        Field     Description
        ========  =============================================================
        *patch*   Sankey outline (a `~matplotlib.patches.PathPatch`).
        *flows*   Flow values (positive for input, negative for output).
        *angles*  List of angles of the arrows [deg/90].
                  For example, if the diagram has not been rotated,
                  an input to the top side has an angle of 3 (DOWN),
                  and an output from the top side has an angle of 1 (UP).
                  If a flow has been skipped (because its magnitude is less
                  than *tolerance*), then its angle will be *None*.
        *tips*    (N, 2)-array of the (x, y) positions of the tips (or "dips")
                  of the flow paths.
                  If the magnitude of a flow is less the *tolerance* of this
                  `Sankey` instance, the flow is skipped and its tip will be at
                  the center of the diagram.
        *text*    `.Text` instance for the diagram label.
        *texts*   List of `.Text` instances for the flow labels.
        ========  =============================================================

        See Also
        --------
        Sankey.add
        r   r   r:   r	   equaldatalim)
adjustable)r   axisr/   r'   
set_aspectr   )r2   r   r   r7   finish
  s   zSankey.finish)Nr   r   r   r   r   r   r   r   r   r   )r   Tr   r9   )	r   NNr   r   r   Nr9   r   )__name__
__module____qualname____doc__r8   rI   rZ   r]   r   r<   rc   r   interpdr1   r   r   r   r   r7   r
      s&    

946   .r
   )r   loggingtypesr   numpyr(   
matplotlibr   matplotlib.pathr   matplotlib.patchesr   matplotlib.transformsr   r   	getLoggerr   r   
__author____credits____license____version__rK   rM   rN   r
   r   r   r   r7   <module>   s$    
