o
    hT                     @   sz   d Z ddlZddlZddlZddlZddlZddl	m
Z ddlmZ eeZG dd dZe Zdd Zd
dd	ZdS )a  
A layoutgrid is a nrows by ncols set of boxes, meant to be used by
`._constrained_layout`, each box is analogous to a subplotspec element of
a gridspec.

Each box is defined by left[ncols], right[ncols], bottom[nrows] and top[nrows],
and by two editable margins for each side.  The main margin gets its value
set by the size of ticklabels, titles, etc on each Axes that is in the figure.
The outer margin is the padding around the Axes, and space for any
colorbars.

The "inner" widths and heights of these boxes are then constrained to be the
same (relative the values of `width_ratios[ncols]` and `height_ratios[nrows]`).

The layoutgrid is then constrained to be contained within a parent layoutgrid,
its column(s) and row(s) specified when it is created.
    N)Bboxc                   @   s   e Zd ZdZ				d4ddZd	d
 Zdd Zdd Zdd ZdddZ	dd Z
dd Zdd Zd5ddZdd Zdd Zd d! Zd"d# Zdd$d%Zdd&d'Zdd(d)Zdd*d+Zdd,d-Zdd.d/Zdd0d1Zd2d3 ZdS )6
LayoutGridzG
    Analogous to a gridspec, and contained in another LayoutGrid.
    Nr   r   F    c                    sl  t j || _|| _|t  | _t|tr|j d| j | _|| _|| _	t
|
| _|
d u r4t
|| _t
|	| _|	d u rDt
|| _| jd t|tsTt  | _n|j| g|R   |j| _t
j||ftd| _t
j||ftd| _i | _i | _dD ]
t
|| j< q}| j} fddt|D | _ fddt|D | _dD ]$ fddt|D | j< t|D ]}|| j | d	 qqd
D ]t
j|td| j< t
|| j< q· fddt|D | _ fddt|D | _d
D ]% fddt|D | j< t|D ]}|| j | d	 qq|   |  | || _!|| _"d S )N._)dtype)leftrightleftcbrightcbc                        g | ]}  d | dqS )zlefts[] .0iVariablesnr   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/_layoutgrid.py
<listcomp>M        z'LayoutGrid.__init__.<locals>.<listcomp>c                    r   )zrights[r   r   r   r   r   r   r   N   r   c              	      &   g | ]}  d  d| dqS zmargins[z][r   r   r   r   r   todor   r   r   P       strong)bottomtopbottomcbtopcbc                    r   )zbottoms[r   r   r   r   r   r   r   Y   r   c                    r   )ztops[r   r   r   r   r   r   r   Z   r   c              	      r   r   r   r   r   r   r   r   \   r   )#kiwir   
parent_posparent_innerseq_idname
isinstancer   nrowsncolsnp
atleast_1dheight_ratiosoneswidth_ratiosSolversolver	add_childemptyobjectartistschildrenmarginsmargin_valszerosrangeleftsrightsaddEditVariablebottomstopsreset_marginsadd_constraintsh_padw_pad)selfparentr%   r&   r(   r+   r*   rC   rD   r0   r.   solr   r   r   r   __init__$   sf   




zLayoutGrid.__init__c                 C   s   d| j dd| j d| j d}t| jD ]d}t| jD ]\}|| d| d| j|  dd	| j|  dd
| j|  dd| j|  dd| j	d |  dd| j	d |  dd| j	d |  dd| j	d |  dd7 }qq|S )NzLayoutBox: 25s xz,
z, z: Lz1.3fz, Bz, Rz, Tz, MLr
   z, MRr   z, MBr    z, MTr!   z, 
)
r(   r*   r+   r;   r<   valuer?   r=   r@   r8   )rE   strr   jr   r   r   __repr__i   s,    
zLayoutGrid.__repr__c                 C   s   dD ]}|  |d qdS )z
        Reset all the margins to zero.  Must do this after changing
        figure size, for instance, because the relative size of the
        axes labels etc changes.
        )r
   r   r    r!   r   r   r"   r#   g        N)edit_margins)rE   r   r   r   r   rA   x   s   zLayoutGrid.reset_marginsc                 C   s   |    | | |   d S N)hard_constraintsparent_constraintsgrid_constraints)rE   rF   r   r   r   rB      s   
zLayoutGrid.add_constraintsc                 C   s  t | jD ]?}| j| | j| k| j| | jd |  | jd |  | j| | jd |  | jd |  kg}|D ]
}| j|dB  q9qt | jD ]?}| j| | j	| k| j| | jd |  | jd |  | j	| | jd |  | jd	 |  kg}|D ]
}| j|dB  q~qJd
S )zo
        These are the redundant constraints, plus ones that make the
        rest of the code easier.
        r   r   r
   r   requiredr!   r#   r    r"   N)
r;   r+   r=   r<   r8   r2   addConstraintr*   r@   r?   )rE   r   hccr   r   r   rR      s6   zLayoutGrid.hard_constraintsr   c                 C   s"   || j tt|t|< d S rQ   )r7   r,   ix_r-   )rE   childr   rN   r   r   r   r3      s   "zLayoutGrid.add_childc           
      C   s  t |ts0| jd |d k| jd |d |d  k| jd |d |d  k| jd |d kg}n| j\}}t|}t|}|j|d  }|j|d  }|j|d  }|j|d  }| j	r||j
d |d  7 }||j
d |d  7 }||j
d |d  8 }||j
d	 |d  8 }||j
d
 |d  8 }||j
d |d  8 }||j
d |d  7 }||j
d |d  7 }| jd |k| jd |k| jd |k| jd |kg}|D ]
}	| j|	dB  qd S )Nr      r      r
   r   r   r   r!   r#   r    r"   rU   )r)   r   r<   r=   r@   r?   r%   r,   r-   r&   r8   r2   rV   )
rE   rF   rW   rowscolsr
   r   r!   r    rX   r   r   r   rS      s<   



zLayoutGrid.parent_constraintsc                 C   s  | j d | jd d  | jd d  }|| jd  | jd d  | jd d  }|| jd  }td| jD ]O}| j | | jd |  | jd |  }|| j|  | jd |  | jd |  }||| j|  k}| j|dB  | j |d  | j| k}| j|dB  q5| jd | jd d  | jd	 d  }|| j	d  | jd
 d  | jd d  }|| j
d  }td| jD ]O}| j| | jd |  | jd	 |  }|| j	|  | jd
 |  | jd |  }||| j
|  k}| j|dB  | j	|d  | j| k}| j|dB  qd S )Nr   r   r   r
   r   r   r   r!   r#   r    r"   )r=   r8   r<   r0   r;   r+   r2   rV   r@   r?   r.   r*   )rE   ww0r   rX   hh0r   r   r   rT      sF   &zLayoutGrid.grid_constraintsc                 C   s*   | j | j| | | || j| |< dS )a  
        Change the size of the margin for one cell.

        Parameters
        ----------
        todo : string (one of 'left', 'right', 'bottom', 'top')
            margin to alter.

        size : float
            Size of the margin.  If it is larger than the existing minimum it
            updates the margin size. Fraction of figure size.

        cell : int
            Cell column or row to edit.
        N)r2   suggestValuer8   r9   rE   r   sizecellr   r   r   edit_margin   s   zLayoutGrid.edit_marginc                 C   s(   || j | | kr| ||| dS dS )a  
        Change the minimum size of the margin for one cell.

        Parameters
        ----------
        todo : string (one of 'left', 'right', 'bottom', 'top')
            margin to alter.

        size : float
            Minimum size of the margin .  If it is larger than the
            existing minimum it updates the margin size. Fraction of
            figure size.

        cell : int
            Cell column or row to edit.
        N)r9   rh   re   r   r   r   edit_margin_min  s   zLayoutGrid.edit_margin_minc                 C   *   t t| j| D ]	}| ||| q	dS )a1  
        Change the size of all the margin of all the cells in the layout grid.

        Parameters
        ----------
        todo : string (one of 'left', 'right', 'bottom', 'top')
            margin to alter.

        size : float
            Size to set the margins.  Fraction of figure size.
        N)r;   lenr9   rh   rE   r   rf   r   r   r   r   rP   "  s   zLayoutGrid.edit_marginsc                 C   rj   )a  
        Change the minimum size of all the margin of all
        the cells in the layout grid.

        Parameters
        ----------
        todo : {'left', 'right', 'bottom', 'top'}
            The margin to alter.

        size : float
            Minimum size of the margin.  If it is larger than the
            existing minimum it updates the margin size. Fraction of
            figure size.
        N)r;   rk   r9   ri   rl   r   r   r   edit_all_margins_min2  s   zLayoutGrid.edit_all_margins_minc                 C   s   |  d|d |jj |  d|d |jj |  d|d |jjd  |  d|d |jjd  |  d|d |jj |  d|d |jj |  d|d |jjd  |  d	|d	 |jjd  d
S )a<  
        Edit all four margin minimums in one statement.

        Parameters
        ----------
        margin : dict
            size of margins in a dict with keys 'left', 'right', 'bottom',
            'top'

        ss : SubplotSpec
            defines the subplotspec these margins should be applied to
        r
   r   r   r   r   r!   r#   r    r"   N)ri   colspanstartstoprowspan)rE   marginssr   r   r   edit_outer_margin_minsE  s   
z!LayoutGrid.edit_outer_margin_minsc                 C   s   | j | | S )z"Return the margin at this position)r9   )rE   r   colr   r   r   get_margins^  s   zLayoutGrid.get_marginsc                 C   s`   t |}t |}t| j|d   | j|d   | j|d   | j|d   }|S )z
        Return the outer bounding box of the subplot specs
        given by rows and cols.  rows and cols can be spans.
        r   r[   )	r,   r-   r   from_extentsr<   rL   r?   r=   r@   rE   r^   r_   bboxr   r   r   get_outer_bboxb  s   

zLayoutGrid.get_outer_bboxc              	   C   s  t |}t |}t| j|d   | jd |d    | jd |d    | j|d   | jd |d    | jd |d    | j|d   | jd |d    | jd |d    | j	|d   | jd	 |d    | jd
 |d    }|S )z
        Return the inner bounding box of the subplot specs
        given by rows and cols.  rows and cols can be spans.
        r   r
   r   r[   r    r"   r   r   r!   r#   
r,   r-   r   rw   r<   rL   r8   r?   r=   r@   rx   r   r   r   get_inner_bboxq  s2   

zLayoutGrid.get_inner_bboxc              	   C   s   t |}t |}t| j|d   | jd |d    | j|d   | jd |d    | j|d   | jd |d    | j	|d   | jd |d    }|S )zj
        Return the bounding box that includes the
        decorations but, *not* the colorbar...
        r   r   r[   r"   r   r#   r{   rx   r   r   r   get_bbox_for_cb  s"   


zLayoutGrid.get_bbox_for_cbc                 C   s   t |}t |}t| j|d   | jd |d    | j|d   | j|d   | jd |d    | jd |d    | j|d   }|S )
        Return the left margin bounding box of the subplot specs
        given by rows and cols.  rows and cols can be spans.
        r   r   r[   r
   )	r,   r-   r   rw   r<   rL   r8   r?   r@   rx   r   r   r   get_left_margin_bbox  s   

zLayoutGrid.get_left_margin_bboxc              	   C   s   t |}t |}t| j|d   | j|d   | jd |d    | j|d   | j|d   | jd |d    | jd |d    }|S )r~   r   r[   r"   r    )	r,   r-   r   rw   r<   rL   r?   r8   r=   rx   r   r   r   get_bottom_margin_bbox  s   

	z!LayoutGrid.get_bottom_margin_bboxc                 C   s   t |}t |}t| j|d   | jd |d    | jd |d    | j|d   | j|d   | jd |d    | j|d   }|S )r~   r[   r   r   r   )	r,   r-   r   rw   r=   rL   r8   r?   r@   rx   r   r   r   get_right_margin_bbox  s   

z LayoutGrid.get_right_margin_bboxc              	   C   s   t |}t |}t| j|d   | j|d   | jd |d    | j|d   | j|d   | jd |d    | jd |d    }|S )r~   r   r#   r[   r!   )	r,   r-   r   rw   r<   rL   r@   r8   r=   rx   r   r   r   get_top_margin_bbox  s   

zLayoutGrid.get_top_margin_bboxc                 C   s   | j   dS )zR
        Update the variables for the solver attached to this layoutgrid.
        N)r2   updateVariables)rE   r   r   r   update_variables  s   zLayoutGrid.update_variables)
Nr   Fr   r   r   NNNN)r   )__name__
__module____qualname____doc__rH   rO   rA   rB   rR   r3   rS   rT   rh   ri   rP   rm   rt   rv   rz   r|   r}   r   r   r   r   r   r   r   r   r   r      s8    
E
	
(*







r   c                   C   s   dt t S )z5Generate a short sequential id for layoutbox objects.z%06d)next_layoutboxobjnumr   r   r   r   r'     s   r'   c                 C   s  |du r|   | }||  }tjd  d }|| }t|jD ]}t|jD ]}|j||d}| 	t
j|j|j|jdddd| jdd		 |j||d}	| 	t
j|	j|	j|	jd
|d| jdd |j||d}	| 	t
j|	j|	j|	jdddg d| jdd	 |j||d}	| 	t
j|	j|	j|	jdddg d| jdd	 |j||d}	| 	t
j|	j|	j|	jdddg d| jdd	 |j||d}	| 	t
j|	j|	j|	jdddg d| jdd	 q(q!|jjD ]}
|
durt| |
|d d qdS )z(Simple plotting to show where boxes are.Nzaxes.prop_cyclecolor)r^   r_   r   z0.7皙?)	linewidth	edgecolor	facecoloralpha	transformzorderr\   none)r   r   r   r   r   r   )      ?ffffff?r   )r   r   r   r   r   r   )r   r   r   )r   r   r   )r   r   r   )level)get_layout_engineexecutemplrcParamsby_keyr;   r*   r+   rz   
add_artistmpatches	Rectanglep0widthheighttransFigurer|   r   r   r   r   r7   flatplot_children)figlgr   _layoutgridscolorsru   r   rN   bbbbichr   r   r   r     sx   %r   )Nr   )r   	itertools
kiwisolverr$   loggingnumpyr,   
matplotlibr   matplotlib.patchespatchesr   matplotlib.transformsr   	getLoggerr   _logr   countr   r'   r   r   r   r   r   <module>   s     
   P