o
    h9                    @   s  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ZddlZddlZddl	Z	ddl
mZ ddlZddlZddlZddlZddlZddlZddlZddlZzddlmZ W n eyc   ddlmZ Y nw ddlZddlmZmZ G dd dZdd	 Zd
d ZG dd dZdd ZG dd dZ G dd dZ!G dd de"Z#ej$dddZ%dd Z&dd Z'dd Z(dd  Z)dd#d$Z*dd%d&Z+d'd( Z,dd*d+Z-d,d- Z.e,fd.d/Z/G d0d1 d1Z0dd2d3Z1ej2d"fd4d5Z3G d6d7 d7Z4G d8d9 d9Z5d:d; Z6d<d= Z7d>d? Z8d"d@dAdBZ9ddDdEZ:dFdGdHdIdJZ;dKdL e;< D Z=dMdN Z>dOdP Z?dQdR Z@dSdT ZAdUdV ZBddXdYZCdZd[ ZDd\d] ZEd^d_ ZFd`da eDeDeEeFdbZGdcdd ZHdedf ZIdgdh ZJdidj ZKddkdlZLejMdmdn ZNejOePeQdodpfdqdrZRdsdt ZSdudv ZTdwdx ZUdydz ZVd{d| ZWejMd}d~ ZXG dd dejYjZZ[dd Z\dd Z]dd Z^dd Z_dd Z`dd Zadd Zbdd Zcdd ZdejedddZfdd Zgdd Zhdd Zidd Zjdd Zkdd Zldd ZmdS )z
A collection of utility functions and classes.  Originally, many
(but not all) were from the Python Cookbook -- hence the name cbook.
    N)Path)VisibleDeprecationWarning)_api_c_internal_utilsc                   @   s,   e Zd ZdZdd Zedd Zdd ZdS )	_ExceptionInfoa  
    A class to carry exception information around.

    This is used to store and later raise exceptions. It's an alternative to
    directly storing Exception instances that circumvents traceback-related
    issues: caching tracebacks can keep user's objects in local namespaces
    alive indefinitely, which can lead to very surprising memory issues for
    users and result in incorrect tracebacks.
    c                 G   s   || _ || _d S N_cls_args)selfclsargs r   d/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/cbook.py__init__.      
z_ExceptionInfo.__init__c                 C   s   | t |g|jR  S r   )typer   )r   excr   r   r   from_exception2      z_ExceptionInfo.from_exceptionc                 C   s   | j | j S r   r   r   r   r   r   to_exception6   s   z_ExceptionInfo.to_exceptionN)__name__
__module____qualname____doc__r   classmethodr   r   r   r   r   r   r   #   s    

r   c                  C   s&  t jdpt jdpt jdpt jd} | r!| j r!dS t jd}|rE|jdkr:dd	lm} | r:d
S |jdkrE|	 rEdS t jd}|rS|
 rSdS t jd}|r}|jj|jjjh}t   D ]}|r{|j|v rv dS |j}|snqj~t jd}|r| rdS t sdS dS )a/  
    Return the interactive framework whose event loop is currently running, if
    any, or "headless" if no event loop can be started, or None.

    Returns
    -------
    Optional[str]
        One of the following values: "qt", "gtk3", "gtk4", "wx", "tk",
        "macosx", "headless", ``None``.
    zPyQt6.QtWidgetszPySide6.QtWidgetszPyQt5.QtWidgetszPySide2.QtWidgetsqtzgi.repository.Gtk   r   )GLibgtk4   gtk3wxtkintertkzmatplotlib.backends._macosxmacosxheadlessN)sysmodulesgetQApplicationinstanceMAJOR_VERSIONgi.repositoryr   
main_depth
main_levelGetAppmainloop__code__Misc_current_framesvaluesf_codef_backevent_loop_is_runningr   display_is_valid)	QtWidgetsGtkr   r#   r$   codesframer&   r   r   r   "_get_running_interactive_framework:   sJ   




r?   c                 C   s   t  dv r| t  d S )N)r'   N)r?   	traceback	print_exc)r   r   r   r   _exception_printerl   s   
rB   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )
_StrongRefzU
    Wrapper similar to a weakref, but keeping a strong reference to the object.
    c                 C   
   || _ d S r   _obj)r   objr   r   r   r   x      
z_StrongRef.__init__c                 C   s   | j S r   rE   r   r   r   r   __call__{   s   z_StrongRef.__call__c                 C   s   t |to
| j|jkS r   )
isinstancerC   rF   )r   otherr   r   r   __eq__~   s   z_StrongRef.__eq__c                 C   
   t | jS r   )hashrF   r   r   r   r   __hash__   rH   z_StrongRef.__hash__N)r   r   r   r   r   rI   rL   rO   r   r   r   r   rC   s   s    rC   c                 C   s*   zt | |W S  ty   t|  Y S w )zQ
    Return a `WeakMethod` wrapping *func* if possible, else a `_StrongRef`.
    )weakref
WeakMethod	TypeErrorrC   )funccallbackr   r   r   _weak_or_strong_ref   s
   rU   c                   @   8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )_UnhashDicta  
    A minimal dict-like class that also supports unhashable keys, storing them
    in a list of key-value pairs.

    This class only implements the interface needed for `CallbackRegistry`, and
    tries to minimize the overhead for the hashable case.
    c                 C   s&   i | _ g | _|D ]\}}|| |< qd S r   _dict_pairs)r   pairskvr   r   r   r      s
   
z_UnhashDict.__init__c                 C   sl   z|| j |< W d S  ty5   t| jD ]\}\}}||kr)||f| j|<  Y d S q| j||f Y d S w r   )rY   rR   	enumeraterZ   append)r   keyvalueir\   r]   r   r   r   __setitem__   s   z_UnhashDict.__setitem__c                 C   sH   z| j | W S  ty   Y nw | jD ]\}}||kr|  S qt|r   )rY   rR   rZ   KeyError)r   r`   r\   r]   r   r   r   __getitem__   s   z_UnhashDict.__getitem__c                 G   sx   z|| j v r| j |W S W n# ty1   t| jD ]\}\}}||kr.| j|= |   Y S qY nw |r8|d S t|Nr   )rY   poprR   r^   rZ   rd   )r   r`   r   rb   r\   r]   r   r   r   rg      s   
z_UnhashDict.popc                 c   s(    | j E d H  | jD ]\}}|V  q
d S r   rX   )r   r\   r]   r   r   r   __iter__   s
   z_UnhashDict.__iter__N)	r   r   r   r   r   rc   re   rg   rh   r   r   r   r   rW      s    
rW   c                   @   sv   e Zd ZdZefddddZdd Zdd	 Zd
d Zdd Z	e
jdddZdd Zdd ZejddddZdS )CallbackRegistrya  
    Handle registering, processing, blocking, and disconnecting
    for a set of signals and callbacks:

        >>> def oneat(x):
        ...     print('eat', x)
        >>> def ondrink(x):
        ...     print('drink', x)

        >>> from matplotlib.cbook import CallbackRegistry
        >>> callbacks = CallbackRegistry()

        >>> id_eat = callbacks.connect('eat', oneat)
        >>> id_drink = callbacks.connect('drink', ondrink)

        >>> callbacks.process('drink', 123)
        drink 123
        >>> callbacks.process('eat', 456)
        eat 456
        >>> callbacks.process('be merry', 456)   # nothing will be called

        >>> callbacks.disconnect(id_eat)
        >>> callbacks.process('eat', 456)        # nothing will be called

        >>> with callbacks.blocked(signal='drink'):
        ...     callbacks.process('drink', 123)  # nothing will be called
        >>> callbacks.process('drink', 123)
        drink 123

    In practice, one should always disconnect all callbacks when they are
    no longer needed to avoid dangling references (and thus memory leaks).
    However, real code in Matplotlib rarely does so, and due to its design,
    it is rather difficult to place this kind of code.  To get around this,
    and prevent this class of memory leaks, we instead store weak references
    to bound methods only, so when the destination object needs to die, the
    CallbackRegistry won't keep it alive.

    Parameters
    ----------
    exception_handler : callable, optional
       If not None, *exception_handler* must be a function that takes an
       `Exception` as single parameter.  It gets called with any `Exception`
       raised by the callbacks during `CallbackRegistry.process`, and may
       either re-raise the exception or handle it in another manner.

       The default handler prints the exception (with `traceback.print_exc`) if
       an interactive event loop is running; it re-raises the exception if no
       interactive event loop is running.

    signals : list, optional
        If not None, *signals* is a list of signals that this registry handles:
        attempting to `process` or to `connect` to a signal not in the list
        throws a `ValueError`.  The default, None, does not restrict the
        handled signals.
    N)signalsc                C   sB   |d u rd nt || _|| _i | _t | _tg | _t	 | _
d S r   )list_signalsexception_handler	callbacks	itertoolscount_cid_genrW   _func_cid_mapset_pickled_cids)r   rm   rj   r   r   r   r     s   

zCallbackRegistry.__init__c                    s2   i t   fdd j D d t jdS )Nc                    s(   i | ]\}}| fd d|  D qS )c                    s"   i | ]\}}| j v r|| qS r   )rt   ).0cidproxyr   r   r   
<dictcomp>  s    
z<CallbackRegistry.__getstate__.<locals>.<dictcomp>.<dictcomp>items)ru   sdr   r   r   rx     s    z1CallbackRegistry.__getstate__.<locals>.<dictcomp>)rn   rr   rq   )varsrn   rz   nextrq   r   r   r   r   __getstate__  s   
zCallbackRegistry.__getstate__c                    s\   | d}t |  fdd j D  _tdd  j D  _t| _	d S )Nrq   c                    s*   i | ]\ }  fd d|  D qS )c              	      s&   i | ]\}}|t |tj qS r   )rU   	functoolspartial_remove_proxy)ru   rv   rS   )r{   r   r   r   rx     s    z<CallbackRegistry.__setstate__.<locals>.<dictcomp>.<dictcomp>ry   )ru   r|   r   )r{   r   rx     s    z1CallbackRegistry.__setstate__.<locals>.<dictcomp>c                 s   s2    | ]\}}|  D ]\}}||f|fV  q
qd S r   ry   )ru   r{   r|   rv   rw   r   r   r   	<genexpr>   s    
z0CallbackRegistry.__setstate__.<locals>.<genexpr>)
rg   r}   updatern   rz   rW   rr   ro   rp   rq   )r   state	cid_countr   r   r   __setstate__  s   


zCallbackRegistry.__setstate__c                 C   s   | j durtj| j |d t|t| j|}z| j||f W S  ty?   t	| j
 }| j||f< || j|i |< | Y S w )z?Register *func* to be called when signal *signal* is generated.Nsignal)rl   r   check_in_listrU   r   r   r   rr   rd   r~   rq   rn   
setdefault)r   r   rS   rw   rv   r   r   r   connect%  s   
zCallbackRegistry.connectc                 C   s   |  ||}| j| |S )z{
        Like `.connect`, but the callback is kept when pickling/unpickling.

        Currently internal-use only.
        )r   rt   add)r   r   rS   rv   r   r   r   _connect_picklable1  s   z#CallbackRegistry._connect_picklable)_is_finalizingc                C   sd   | rd S | j ||fd }|d ur| j| |= | j| nd S t| j| dkr0| j|= d S d S rf   )rr   rg   rn   rt   discardlen)r   r   rw   r   rv   r   r   r   r   =  s   zCallbackRegistry._remove_proxyc                 C   s   | j | | jD ]\}}| j||f |kr nq	dS | j| | |ks&J | j| |= | j||f t| j| dkrC| j|= dS dS )z
        Disconnect the callback registered with callback id *cid*.

        No error is raised if such a callback does not exist.
        Nr   )rt   r   rr   rn   rg   r   )r   rv   r   rw   r   r   r   
disconnectJ  s   zCallbackRegistry.disconnectc                 O   s   | j durtj| j |d t| j|i  D ]1}| }|durIz	||i | W q tyH } z| jdur=| | n W Y d}~qd}~ww qdS )z
        Process signal *s*.

        All of the functions registered to receive callbacks on *s* will be
        called with ``*args`` and ``**kwargs``.
        Nr   )	rl   r   r   rk   rn   r*   r6   	Exceptionrm   )r   r{   r   kwargsrefrS   r   r   r   r   process\  s    

zCallbackRegistry.processr   c                #   sJ    | j  zdu ri | _ n fdd D | _ dV  W  | _ dS  | _ w )aV  
        Block callback signals from being processed.

        A context manager to temporarily block/disable callback signals
        from being processed by the registered listeners.

        Parameters
        ----------
        signal : str, optional
            The callback signal to block. The default is to block all signals.
        Nc                    s   i | ]}|kr| | qS r   r   ru   r\   origr   r   r   rx     s    z,CallbackRegistry.blocked.<locals>.<dictcomp>)rn   )r   r   r   r   r   blockedr  s   zCallbackRegistry.blocked)r   r   r   r   rB   r   r   r   r   r   r(   is_finalizingr   r   r   
contextlibcontextmanagerr   r   r   r   r   ri      s    <	ri   c                   @   s"   e Zd ZdZdddZdd ZdS )silent_lista  
    A list with a short ``repr()``.

    This is meant to be used for a homogeneous list of artists, so that they
    don't cause long, meaningless output.

    Instead of ::

        [<matplotlib.lines.Line2D object at 0x7f5749fed3c8>,
         <matplotlib.lines.Line2D object at 0x7f5749fed4e0>,
         <matplotlib.lines.Line2D object at 0x7f5758016550>]

    one will get ::

        <a list of 3 Line2D objects>

    If ``self.type`` is None, the type name is obtained from the first item in
    the list (if any).
    Nc                 C   s    || _ |d ur| | d S d S r   )r   extend)r   r   seqr   r   r   r     s   zsilent_list.__init__c                 C   sN   | j d ust| dkr%| j d ur| j nt | d j}dt|  d| dS dS )Nr   z<a list of  z	 objects>z<an empty list>)r   r   r   )r   tpr   r   r   __repr__  s   zsilent_list.__repr__r   )r   r   r   r   r   r   r   r   r   r   r     s    
r   )warning_clsc                G   sH   | }|D ]}| |d }|d ur!|d u r|}qtd| d| q|S )N"z"" keyword argument will be ignored)rg   r   warn_external)	local_varr   r   keysoutr`   	kwarg_valr   r   r   _local_over_kwdict  s   r   c                 C   sX   t | dkr*| d | d   krdkr*n | S | dd } dD ]
\}}| ||} q| S )zi
    Remove latex formatting from mathtext.

    Only handles fully math and fully non-math strings.
       r   $   )	)z\timesx)z\mathdefault )z\rmr   )z\calr   )z\ttr   )z\itr   )\r   ){r   )}r   )r   replace)r{   texplainr   r   r   
strip_math  s   *r   c                 C   s   d}	 |  d|}|  d|}|dk r#|dk r| n| d| }| S d|  kr-|k r7n n| d|  S |  d|d }|dk rKtd| d|d }q)	z+Strip everything from the first unquoted #.r   Tr   #Nr   zMissing closing quote in: zM. If you need a double-quote inside a string, use escaping: e.g. "the " char")findstrip
ValueError)r{   pos	quote_poshash_poswithout_commentclosing_quote_posr   r   r   _strip_comment  s    
r   c                 C   s   t t| ddS )zDReturn whether *obj* looks like a file object with a *write* method.writeN)callablegetattrrG   r   r   r   is_writable_file_like  s   r   c                 C   s&   z|  d W dS  ty   Y dS w )zf
    Return whether the given writable file-like object requires Unicode to be
    written to it.
        TF)r   rR   r   r   r   r   file_requires_unicode  s   r   rFc                 C   s   t | tjrt| } t | tr6| drt| |}n| dr,ddl}|	| |}nt| ||d}d}nt
| dr@| }d}ntd	|rJ||fS |S )
a^  
    Convert a path to an open file handle or pass-through a file-like object.

    Consider using `open_file_cm` instead, as it allows one to properly close
    newly created file objects more easily.

    Parameters
    ----------
    fname : str or path-like or file-like
        If `str` or `os.PathLike`, the file is opened using the flags specified
        by *flag* and *encoding*.  If a file-like object, it is passed through.
    flag : str, default: 'r'
        Passed as the *mode* argument to `open` when *fname* is `str` or
        `os.PathLike`; ignored if *fname* is file-like.
    return_opened : bool, default: False
        If True, return both the file object and a boolean indicating whether
        this was a new file (that the caller needs to close).  If False, return
        only the new file.
    encoding : str or None, default: None
        Passed as the *mode* argument to `open` when *fname* is `str` or
        `os.PathLike`; ignored if *fname* is file-like.

    Returns
    -------
    fh : file-like
    opened : bool
        *opened* is only returned if *return_opened* is True.
    .gzz.bz2r   N)encodingTseekFz'fname must be a PathLike or file handle)rJ   osPathLikefspathstrendswithgzipopenbz2BZ2Filehasattrr   )fnameflagreturn_openedr   fhr   openedr   r   r   to_filehandle  s"   




r   c                 C   s$   t | |d|\}}|r|S t|S )z8Pass through file objects and context-manage path-likes.T)r   r   nullcontext)path_or_filemoder   r   r   r   r   r   open_file_cm.  s   r   c                 C   s   t | tp
t|  S )z;Return whether the given object is a scalar or string like.)rJ   r   npiterablevalr   r   r   is_scalar_or_string4  r   r   Tc                 C   s`   t d| }|r,|j }|dkrt|S |dv rt|S |dv r'|dS |dS t|S )a  
    Return a sample data file.  *fname* is a path relative to the
    :file:`mpl-data/sample_data` directory.  If *asfileobj* is `True`
    return a file object, otherwise just a file path.

    Sample data files are stored in the 'mpl-data/sample_data' directory within
    the Matplotlib package.

    If the filename ends in .gz, the file is implicitly ungzipped.  If the
    filename ends with .npy or .npz, and *asfileobj* is `True`, the file is
    loaded with `numpy.load`.
    sample_datar   )z.npyz.npz)z.csvz.xrcz.txtr   rb)_get_data_pathsuffixlowerr   r   r   loadr   )r   	asfileobjpathr   r   r   r   get_sample_data9  s   





r   c                  G   s   t t g| R  S )z
    Return the `pathlib.Path` to a resource file provided by Matplotlib.

    ``*args`` specify a path relative to the base data path.
    )r   
matplotlibget_data_path)r   r   r   r   r   U  s   r   c                 c   s8    | D ]}||s|du r|V  qt ||E dH  qdS )a  
    Return a generator of flattened nested containers.

    For example:

        >>> from matplotlib.cbook import flatten
        >>> l = (('John', ['Hunter']), (1, 23), [[([42, (5, 23)], )]])
        >>> print(list(flatten(l)))
        ['John', 'Hunter', 1, 23, 42, 5, 23]

    By: Composite of Holger Krekel and Luther Blissett
    From: https://code.activestate.com/recipes/121294-simple-generator-for-flattening-nested-containers/
    and Recipe 1.12 in cookbook
    N)flatten)r   scalarpitemr   r   r   r   ^  s   r   c                   @   sX   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )_Stackzb
    Stack of elements with a movable cursor.

    Mimics home/back/forward in a web browser.
    c                 C   s   d| _ g | _d S )Nr   _pos	_elementsr   r   r   r   r   {  r   z_Stack.__init__c                 C   s   d| _ g | _dS )zEmpty the stack.r   Nr   r   r   r   r   clear  s   
z_Stack.clearc                 C   s   | j r	| j | j S dS )z$Return the current element, or None.N)r   r   r   r   r   r   rI     r   z_Stack.__call__c                 C   rM   r   )r   r   r   r   r   r   __len__  rH   z_Stack.__len__c                 C   s
   | j | S r   )r   )r   indr   r   r   re     rH   z_Stack.__getitem__c                 C   s"   t | jd t| jd | _|  S )z9Move the position forward and return the current element.r   )minr   r   r   r   r   r   r   forward  s   z_Stack.forwardc                 C   s   t | jd d| _|  S )z6Move the position back and return the current element.r   r   )maxr   r   r   r   r   back  s   z_Stack.backc                 C   s*   |g| j | jd d< t| j d | _|S )zx
        Push *o* to the stack after the current position, and return *o*.

        Discard all later elements.
        r   N)r   r   r   )r   or   r   r   push  s   z_Stack.pushc                 C   s   | j r| | j d S dS )zk
        Push the first element onto the top of the stack.

        The first element is returned.
        r   N)r   r  r   r   r   r   home  s   z_Stack.homeN)r   r   r   r   r   r   rI   r   re   r   r   r  r  r   r   r   r   r   t  s    
r   c                 C   sj   t j| d|d} | jjs| j|d| jd} zt jjt 	|  | dd}W |S  t
y4   |  Y S w )NT)subokcopy)inplaceNF)r  )r   arraydtypeisnativebyteswapviewnewbyteordermamasked_whereisfiniterR   )r   r  xmr   r   r   safe_masked_invalid  s   r  c                    sX   ddl  fdd fddD ]}d|d ||i g  qdS )	a  
    Print loops of cyclic references in the given *objects*.

    It is often useful to pass in ``gc.garbage`` to find the cycles that are
    preventing some objects from being garbage collected.

    Parameters
    ----------
    objects
        A list of objects to find cycles in.
    outstream
        The stream for output.
    show_progress : bool
        If True, print the number of objects reached as they are found.
    r   Nc                    s   t | D ]j\}}| |d t|   } dt|  t|trG| D ]!\}}||u r7 d|d  n||u rE d|  nq$n"t|trW d||  nt|t	rb d n t
|  d q d	 d S )
Nr   z	   %s -- []z[key] = z[%d]z	( tuple )z ->

)r^   r   r   r   rJ   dictrz   rk   indextuplerepr)r   rb   stepr~   r`   r   )	outstreamr   r   
print_path  s(   


z print_cycles.<locals>.print_pathc                    s   r dt|  d |t| <  | }|D ]&}||u r#| q|u s-t|tjr.qt||vr>||||| g  qd S )Nz%d)r   r   idget_referentsrJ   types	FrameType)rG   startallcurrent_path	referentsreferentgcobjectsr  r  recurseshow_progressr   r   r)    s   

zprint_cycles.<locals>.recursezExamining: r  )r'  r   )r(  r  r*  rG   r   r&  r   print_cycles  s   r+  c                   @   sZ   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )Groupera#  
    A disjoint-set data structure.

    Objects can be joined using :meth:`join`, tested for connectedness
    using :meth:`joined`, and all disjoint sets can be retrieved by
    using the object as an iterator.

    The objects being joined must be hashable and weak-referenceable.

    Examples
    --------
    >>> from matplotlib.cbook import Grouper
    >>> class Foo:
    ...     def __init__(self, s):
    ...         self.s = s
    ...     def __repr__(self):
    ...         return self.s
    ...
    >>> a, b, c, d, e, f = [Foo(x) for x in 'abcdef']
    >>> grp = Grouper()
    >>> grp.join(a, b)
    >>> grp.join(b, c)
    >>> grp.join(d, e)
    >>> list(grp)
    [[a, b, c], [d, e]]
    >>> grp.joined(a, b)
    True
    >>> grp.joined(a, c)
    True
    >>> grp.joined(a, d)
    False
    r   c                 C   sT   t dd |D | _t  | _|D ]}|| jvr!t| j| j|< qt| j| _d S )Nc                 S   s   i | ]	}|t |gqS r   rP   WeakSetru   r   r   r   r   rx   "      z$Grouper.__init__.<locals>.<dictcomp>)rP   WeakKeyDictionary_mapping	_orderingr   _next_order)r   initr   r   r   r   r      s   

zGrouper.__init__c                 C   s,   i t | dd | j D i | jdS )Nc                 S   s   i | ]	\}}|t |qS r   )rs   ru   r\   r]   r   r   r   rx   -  r0  z(Grouper.__getstate__.<locals>.<dictcomp>)r2  r3  )r}   r2  rz   r3  r   r   r   r   r   )  s   zGrouper.__getstate__c                 C   s<   t | | tdd | j D | _t| j| _d S )Nc                 S   s   i | ]
\}}|t |qS r   r-  r6  r   r   r   rx   5  s    z(Grouper.__setstate__.<locals>.<dictcomp>)r}   r   rP   r1  r2  rz   r3  )r   r   r   r   r   r   1  s
   zGrouper.__setstate__c                 C   
   || j v S r   )r2  r   r   r   r   r   __contains__8  rH   zGrouper.__contains__c              	   G   s   | j }z|| }W n  ty)   t|g }||< | j| j|< |  jd7  _Y nw |D ]H}z|| }W n  tyT   t|g }||< | j| j|< |  jd7  _Y nw ||urtt|t|krf||}}|| |D ]}|||< qmq,dS )zY
        Join given arguments into the same set.  Accepts one or more arguments.
        r   N)r2  rd   rP   r.  r4  r3  r   r   )r   ar   mappingset_aargset_belemr   r   r   join;  s0   


zGrouper.joinc                 C   s   | j |t | j |u S )z7Return whether *a* and *b* are members of the same set.)r2  r*   objectr   r:  br   r   r   joinedT  s   zGrouper.joinedc                 C   s(   | j ||h| | j|d dS )z>Remove *a* from the grouper, doing nothing if it is not there.N)r2  rg   remover3  r   r:  r   r   r   rE  X  s   zGrouper.removec                 c   s:    dd | j  D }| D ]}t|| jjdV  qdS )z
        Iterate over each of the disjoint sets as a list.

        The iterator is invalid if interleaved with calls to join().
        c                 S   s   i | ]}t ||qS r   )r  )ru   groupr   r   r   rx   c      z$Grouper.__iter__.<locals>.<dictcomp>r`   N)r2  r6   sortedr3  re   )r   unique_groupsrG  r   r   r   rh   ]  s
   zGrouper.__iter__c                 C   s    | j ||g}t|| jjdS )z:Return all of the items joined with *a*, including itself.rI  )r2  r*   rJ  r3  )r   r:  siblingsr   r   r   get_siblingsg  s   zGrouper.get_siblingsN)r   )r   r   r   r   r   r   r   r9  r@  rD  rE  rh   rM  r   r   r   r   r,    s    
!	
r,  c                   @   rV   )GrouperViewz!Immutable view over a `.Grouper`.c                 C   rD   r   _grouper)r   grouperr   r   r   r   p     
 zGrouperView.__init__c                 C   r7  r   rO  r8  r   r   r   r9  q  rR  zGrouperView.__contains__c                 C   rM   r   )iterrP  r   r   r   r   rh   r  rR  zGrouperView.__iter__c                 C   s   | j ||S )zI
        Return whether *a* and *b* are members of the same set.
        )rP  rD  rB  r   r   r   rD  t  s   zGrouperView.joinedc                 C   s   | j |S )zL
        Return all of the items joined with *a*, including itself.
        )rP  rM  rF  r   r   r   rM  z  s   zGrouperView.get_siblingsN)	r   r   r   r   r   r9  rh   rD  rM  r   r   r   r   rN  m  s    rN  c                    st   |  t| df}tt| | tt| d | d  t fdd|jD  t f| jdd  S )a}  
    Resample an array with ``steps - 1`` points between original point pairs.

    Along each column of *a*, ``(steps - 1)`` points are introduced between
    each original values; the values are linearly interpolated.

    Parameters
    ----------
    a : array, shape (n, ...)
    steps : int

    Returns
    -------
    array
        shape ``((n - 1) * steps + 1, ...)``
    r   r   c                    s   g | ]	}t  |qS r   )r   interp)ru   fpr   xpr   r   
<listcomp>  r0  z/simple_linear_interpolation.<locals>.<listcomp>N)reshaper   r   arangecolumn_stackTshape)r:  stepsfpsr   rV  r   simple_linear_interpolation  s   r`  c            
   	   G   s  t | sdS t| d rtdt | d }g }dgt |  }t| D ]3\}}t|tsQt|rQt ||krQd||< t|tjj	rL|j
dkrKtdnt|}|| q#g }t|D ]A\}}|| r|j
dkrkq]t|tjj	r|tj|  |j}n|}zt|}t|tjr|| W q] ty   Y q]w q]t |rtj|}| d }	t |	|k rt|D ]\}}|| r||	 ||< qt|D ]\}}|| rt|tjj	r| ||< q|S )ak  
    Find all masked and/or non-finite points in a set of arguments,
    and return the arguments with only the unmasked points remaining.

    Arguments can be in any of 5 categories:

    1) 1-D masked arrays
    2) 1-D ndarrays
    3) ndarrays with more than one dimension
    4) other non-string iterables
    5) anything else

    The first argument must be in one of the first four categories;
    any argument with a length differing from that of the first
    argument (and hence anything in category 5) then will be
    passed through unchanged.

    Masks are obtained from all arguments of the correct length
    in categories 1, 2, and 4; a point is bad if masked in a masked
    array or if it is a nan or inf.  No attempt is made to
    extract a mask from categories 2, 3, and 4 if `numpy.isfinite`
    does not yield a Boolean array.

    All input arguments that are not passed unchanged are returned
    as ndarrays after removing the points or rows corresponding to
    masks in any of the arguments.

    A vastly simpler version of this function was originally
    written as a helper for Axes.scatter().

    r   r   !First argument must be a sequenceFTr   Masked arrays must be 1-D)r   r   r   r^   rJ   r   r   r   r  MaskedArrayndimasarrayr_   getmaskarraydatar  ndarrayr   logical_andreducenonzerofilled)
r   nrecsmargsseqlistrb   r   masksxdmaskigoodr   r   r   delete_masked_points  s^     




rt  c               
   G   sP  t | sdS t| d rtdt | d }g }dgt |  }g }t| D ]_\}}t|s3t ||kr9|| q%t|tjjrI|j	dkrItdzt
|}W n ttfyb   tj
|td}Y nw |j	dkrt|}d||< tj|r|tj| || q%t |rtj|}t|D ]\}}|| rtjj||d	||< q|S )
ax  
    Find all masked and/or non-finite points in a set of arguments,
    and return the arguments as masked arrays with a common mask.

    Arguments can be in any of 5 categories:

    1) 1-D masked arrays
    2) 1-D ndarrays
    3) ndarrays with more than one dimension
    4) other non-string iterables
    5) anything else

    The first argument must be in one of the first four categories;
    any argument with a length differing from that of the first
    argument (and hence anything in category 5) then will be
    passed through unchanged.

    Masks are obtained from all arguments of the correct length
    in categories 1, 2, and 4; a point is bad if masked in a masked
    array or if it is a nan or inf.  No attempt is made to
    extract a mask from categories 2 and 4 if `numpy.isfinite`
    does not yield a Boolean array.  Category 3 is included to
    support RGB or RGBA ndarrays, which are assumed to have only
    valid values and which are passed through unchanged.

    All input arguments that are not passed unchanged are returned
    as masked arrays if any masked points are found, otherwise as
    ndarrays.

    r   r   ra  Fr   rb  r	  Trr  )r   r   r   r^   r_   rJ   r   r  rc  rd  
asanyarrayr   rA  r  	is_maskedrf  
logical_orrj  r  )r   rm  rn  ro  rp  rb   r   rr  r   r   r   _combine_masks  s>   
rz  )compressc                    s   dd |D }t jg ||R  }|dt| }|t|d }|rAt j| | r6 fdd|D }|S  fdd|D }|S dd |D }|S )aw  
    Broadcast inputs, combining all masked arrays.

    Parameters
    ----------
    *args : array-like
        The inputs to broadcast.
    compress : bool, default: False
        Whether to compress the masked arrays. If False, the masked values
        are replaced by NaNs.

    Returns
    -------
    list of array-like
        The broadcasted and masked inputs.
    c                 S   s    g | ]}t |tjjr|jqS r   )rJ   r   r  rc  rr  r   r   r   r   rX  9  s     z)_broadcast_with_masks.<locals>.<listcomp>Nc                    s    g | ]}t jj| d  qS )rv  )r   r  r  
compressedr   rv  r   r   rX  C  s    c                    s*   g | ]}t jj| td t j qS ))rr  r	  )r   r  r  floatrl  nanravelr   rv  r   r   rX  F  s    "c                 S   s   g | ]}t |qS r   )r   r  r   r   r   r   rX  I  rH  )r   broadcast_arraysr   ry  rj  )r{  r   rp  bcastinputsr   rv  r   _broadcast_with_masks'  s"   

r        ?c                    s  ddd  fdd}g }t | d} t| }|du r td}n
t||kr*td|}tt| |D ]$\}	\}
}i }|durD||d	< |}|| t|
d
krt	g |d< tj
|d< tj
|d< tj
|d< tj
|d< tj
|d< tj
|d< tj
|d< tj
|d< tj
|d< q3tj|
}
|
j|
j   }
t|
|d< t|
g d\}}}|| |d< |d d
kr|rd}||
||d |\|d< |d< t|rt|tst|
|\}}nt|r|||d   }|||d   }ntd|
|
|k }t|d
kst||k r||d< nt||d< |
|
|k }t|d
ks)t||kr.||d< nt||d< t|
|
|d k  |
|
|d k g|d< ||||d< |d< |d< q3|S )aj  
    Return a list of dictionaries of statistics used to draw a series of box
    and whisker plots using `~.Axes.bxp`.

    Parameters
    ----------
    X : array-like
        Data that will be represented in the boxplots. Should have 2 or
        fewer dimensions.

    whis : float or (float, float), default: 1.5
        The position of the whiskers.

        If a float, the lower whisker is at the lowest datum above
        ``Q1 - whis*(Q3-Q1)``, and the upper whisker at the highest datum below
        ``Q3 + whis*(Q3-Q1)``, where Q1 and Q3 are the first and third
        quartiles.  The default value of ``whis = 1.5`` corresponds to Tukey's
        original definition of boxplots.

        If a pair of floats, they indicate the percentiles at which to draw the
        whiskers (e.g., (5, 95)).  In particular, setting this to (0, 100)
        results in whiskers covering the whole range of the data.

        In the edge case where ``Q1 == Q3``, *whis* is automatically set to
        (0, 100) (cover the whole range of the data) if *autorange* is True.

        Beyond the whiskers, data are considered outliers and are plotted as
        individual points.

    bootstrap : int, optional
        Number of times the confidence intervals around the median
        should be bootstrapped (percentile method).

    labels : list of str, optional
        Labels for each dataset. Length must be compatible with
        dimensions of *X*.

    autorange : bool, optional (False)
        When `True` and the data are distributed such that the 25th and 75th
        percentiles are equal, ``whis`` is set to (0, 100) such that the
        whisker ends are at the minimum and maximum of the data.

    Returns
    -------
    list of dict
        A list of dictionaries containing the results for each column
        of data. Keys of each dictionary are the following:

        ========   ===================================
        Key        Value Description
        ========   ===================================
        label      tick label for the boxplot
        mean       arithmetic mean value
        med        50th percentile
        q1         first quartile (25th percentile)
        q3         third quartile (75th percentile)
        iqr        interquartile range
        cilo       lower notch around the median
        cihi       upper notch around the median
        whislo     end of the lower whisker
        whishi     end of the upper whisker
        fliers     outliers
        ========   ===================================

    Notes
    -----
    Non-bootstrapping approach to confidence interval uses Gaussian-based
    asymptotic approximation:

    .. math::

        \mathrm{med} \pm 1.57 \times \frac{\mathrm{iqr}}{\sqrt{N}}

    General approach from:
    McGill, R., Tukey, J.W., and Larsen, W.A. (1978) "Variations of
    Boxplots", The American Statistician, 32:12-16.
      c                 S   sL   t | }ddg}tjj|||fd}| | }tj|ddd}t||}|S )Ng      @g     `X@)sizer   T)axisoverwrite_input)r   r   randomrandintmedian
percentile)rg  r  Mpercentilesbs_indexbsDataestimateCIr   r   r   _bootstrap_median  s   z(boxplot_stats.<locals>._bootstrap_medianc                    sh   |d ur | |d}|d }|d }||fS t | }|d| t|  }|d| t|  }||fS )N)r  r   r   gQ?)r   r   sqrt)rg  mediqr	bootstrapr  	notch_min	notch_maxr  r  r   r   _compute_conf_interval  s   z-boxplot_stats.<locals>._compute_conf_intervalXNz-Dimensions of labels and X must be compatiblelabelr   fliersmeanr  q1q3r  cilocihiwhislowhishi)   2   K   )r   d   z+whis must be a float or list of percentiles)r  )_reshape_2Dr   ro   repeatr   r^   zipr_   r   r  r~  r  re  rg  rr  r  r  r  r   rJ   r   isrealr   r   concatenate)r  whisr  labels	autoranger  bxpstatsncols
input_whisiir   r  statsr  r  r  lovalhivalwiskhiwisklor   r  r   boxplot_statsM  st   
O














r  soliddasheddashdotdotted)-z--z-.:c                 C   s   i | ]\}}||qS r   r   r6  r   r   r   rx     rH  rx   c                 C   s   t j| td} | jsg S t | dd | dd k\}|d7 }| }| d r-dg| }| d r8|t|  tt	|ddd |ddd S )zv
    Return a list of (ind0, ind1) such that ``mask[ind0:ind1].all()`` is
    True and we cover all such regions.
    ru  Nr   r   r   r   )
r   re  boolr  rk  tolistr_   r   rk   r  )rr  idxr   r   r   contiguous_regions  s    
"r  c                 C   s4   t | } | d| d }|dko|d dk}|S )z
    Return whether the string *s* contains math expressions.

    This is done by checking whether *s* contains an even number of
    non-escaped dollar signs.
    r   z\$r   r   )r   rp   )r{   dollar_counteven_dollarsr   r   r   is_math_text6  s   r  c                 C   s,   t | drtj| ttjS t| tS )zt
    Convert a sequence to a float array; if input was a masked array, masked
    values are converted to nans.
    rr  )r   r   r  re  r}  rl  r~  r   r   r   r   _to_unmasked_float_arrayC  s   
r  c                 C   s8   t | } t| drt| drt| jdk rt| S | S )z8Convert scalars to 1D arrays; pass-through arrays as is.r]  rd  r   )_unpack_to_numpyr   r   r]  r   
atleast_1dr   r   r   r   	_check_1dN  s   

r  c              	   C   s  t | } t| tjr:| j} t| dkrg gS | jdkr't| d dkr'| gS | jdv r3dd | D S t| dt| dkrCg gS g }d}| D ]6}t|tsbzt	| W n	 t
y_   Y nw d}t|}t|}|dkrwt| d||d	 qI|rt|d	gS |S )
a  
    Use Fortran ordering to convert ndarrays and lists of iterables to lists of
    1D arrays.

    Lists of iterables are converted by applying `numpy.asanyarray` to each of
    their elements.  1D ndarrays are returned in a singleton list containing
    them.  2D ndarrays are converted to the list of their *columns*.

    *name* is used to generate the error message for invalid inputs.
    r   r   )r   r   c                 S   s   g | ]}t |d qS )r   )r   rY  r/  r   r   r   rX  w  s    z_reshape_2D.<locals>.<listcomp>z  must have 2 or fewer dimensionsTFr   )r  rJ   r   rh  r\  r   rd  r   r   rS  rR   rw  r_   rY  )r  nameresultis_1dxindr   r   r   r  ]  s<   



r  r  c                 C   s   g }t | d} |durt|dkrt |d}ng gt|  }t| t|kr*tdt| |D ]L\}}i }t|}t|}	t|d| }
t||	|}||||d< ||d< t	||d	< t
||d
< ||d< |	|d< t|
|d< || q/|S )a[  
    Return a list of dictionaries of data which can be used to draw a series
    of violin plots.

    See the ``Returns`` section below to view the required keys of the
    dictionary.

    Users can skip this function and pass a user-defined set of dictionaries
    with the same keys to `~.axes.Axes.violinplot` instead of using Matplotlib
    to do the calculations. See the *Returns* section below for the keys
    that must be present in the dictionaries.

    Parameters
    ----------
    X : array-like
        Sample data that will be used to produce the gaussian kernel density
        estimates. Must have 2 or fewer dimensions.

    method : callable
        The method used to calculate the kernel density estimate for each
        column of data. When called via ``method(v, coords)``, it should
        return a vector of the values of the KDE evaluated at the values
        specified in coords.

    points : int, default: 100
        Defines the number of points to evaluate each of the gaussian kernel
        density estimates at.

    quantiles : array-like, default: None
        Defines (if not None) a list of floats in interval [0, 1] for each
        column of data, which represents the quantiles that will be rendered
        for that column of data. Must have 2 or fewer dimensions. 1D array will
        be treated as a singleton list containing them.

    Returns
    -------
    list of dict
        A list of dictionaries containing the results for each column of data.
        The dictionaries contain at least the following:

        - coords: A list of scalars containing the coordinates this particular
          kernel density estimate was evaluated at.
        - vals: A list of scalars containing the values of the kernel density
          estimate at each of the coordinates given in *coords*.
        - mean: The mean value for this column of data.
        - median: The median value for this column of data.
        - min: The minimum value for this column of data.
        - max: The maximum value for this column of data.
        - quantiles: The quantile values for this column of data.
    r  Nr   	quantileszLList of violinplot statistics and quantiles values must have the same lengthr  valscoordsr  r  r   r   )r  r   r   r  r   r   r   r  linspacer  r  r  r_   )r  methodpointsr  vpstatsr   qr  min_valmax_valquantile_valr  r   r   r   violin_stats  s,   5


r  c                 G   s   t dt| tdt|  d df}| |ddddf< |ddddf |ddddf< ||dddddf< |dddddf |dddddf< |S )a(  
    Convert continuous line to pre-steps.

    Given a set of ``N`` points, convert to ``2N - 1`` points, which when
    connected linearly give a step function which changes values at the
    beginning of the intervals.

    Parameters
    ----------
    x : array
        The x location of the steps. May be empty.

    y1, ..., yp : array
        y arrays to be turned into steps; all must be the same length as ``x``.

    Returns
    -------
    array
        The x and y values converted to steps in the same order as the input;
        can be unpacked as ``x_out, y1_out, ..., yp_out``.  If the input is
        length ``N``, each of these arrays will be length ``2N + 1``. For
        ``N=0``, the length will be 0.

    Examples
    --------
    >>> x_s, y1_s, y2_s = pts_to_prestep(x, y1, y2)
    r   r   r   Nr   zerosr   r   r   r   r^  r   r   r   pts_to_prestep  s   ( (r  c                 G   s   t dt| tdt|  d df}| |ddddf< |ddddf |ddddf< ||dddddf< |dddddf |dddddf< |S )a#  
    Convert continuous line to post-steps.

    Given a set of ``N`` points convert to ``2N + 1`` points, which when
    connected linearly give a step function which changes values at the end of
    the intervals.

    Parameters
    ----------
    x : array
        The x location of the steps. May be empty.

    y1, ..., yp : array
        y arrays to be turned into steps; all must be the same length as ``x``.

    Returns
    -------
    array
        The x and y values converted to steps in the same order as the input;
        can be unpacked as ``x_out, y1_out, ..., yp_out``.  If the input is
        length ``N``, each of these arrays will be length ``2N + 1``. For
        ``N=0``, the length will be 0.

    Examples
    --------
    >>> x_s, y1_s, y2_s = pts_to_poststep(x, y1, y2)
    r   r   r   Nr  r  r  r   r   r   pts_to_poststep!  s   ( (r  c                 G   s   t dt| dt|  f}t | } | dd | dd  d  |ddddf< |ddddf< | dd |dddf< | dd |dddf< ||dddddf< |dddddf |dddddf< |S )a  
    Convert continuous line to mid-steps.

    Given a set of ``N`` points convert to ``2N`` points which when connected
    linearly give a step function which changes values at the middle of the
    intervals.

    Parameters
    ----------
    x : array
        The x location of the steps. May be empty.

    y1, ..., yp : array
        y arrays to be turned into steps; all must be the same length as
        ``x``.

    Returns
    -------
    array
        The x and y values converted to steps in the same order as the input;
        can be unpacked as ``x_out, y1_out, ..., yp_out``.  If the input is
        length ``N``, each of these arrays will be length ``2N``.

    Examples
    --------
    >>> x_s, y1_s, y2_s = pts_to_midstep(x, y1, y2)
    r   r   Nr   r   )r   r  r   rw  r  r   r   r   pts_to_midstepE  s   
<(r  c                 C   s   | |fS r   r   )r   yr   r   r   <lambda>k  s    r  )defaultr^  z	steps-prez
steps-postz	steps-midc              	   C   sj   z
| j  |  fW S  ty   Y nw zt| } W n ttfy(   Y tdw tj| jd t	d| fS )a  
    A helper function to create reasonable x values for the given *y*.

    This is used for plotting (x, y) if x values are not explicitly given.

    First try ``y.index`` (assuming *y* is a `pandas.Series`), if that
    fails, use ``range(len(y))``.

    This will be extended in the future to deal with more types of
    labeled data.

    Parameters
    ----------
    y : float or array-like

    Returns
    -------
    x, y : ndarray
       The x and y values to plot.
    r   ru  z5Input could not be cast to an at-least-1D NumPy array)
r  to_numpyAttributeErrorr  r   r   r   rZ  r]  r}  )r  r   r   r   index_ofr  s   r  c                 C   s>   t | tjjrz| d W S  ty   Y tdw tt| S )z
    Return the first element in *obj*.

    This is a type-independent way of obtaining the first element,
    supporting both index access and the iterator protocol.
    r   /matplotlib does not support generators as input)rJ   collectionsabcIteratorrR   RuntimeErrorr~   rS  r   r   r   r   safe_first_element  s   
r  c                 C   sT   dd }t | tjr| d S t | tjjrtd| D ]
}||r%|  S qt| S )a:  
    Return the first finite element in *obj* if one is available and skip_nonfinite is
    True. Otherwise, return the first element.

    This is a method for internal use.

    This is a type-independent way of obtaining the first finite element, supporting
    both index access and the iterator protocol.
    c              	   S   sb   | d u rdS zt | W S  ttfy   Y nw zt| r$t| W S dW S  ty0   Y dS w )NFT)mathr  rR   r   r   isscalarr   r   r   r   safe_isfinite  s   z)_safe_first_finite.<locals>.safe_isfiniter   r  )rJ   r   flatiterr  r  r  r  r  )rG   r  r   r   r   r   _safe_first_finite  s   
r  c                 C   s   t | tjjrt| S | S )zP
    Convert dictview objects to list. Other inputs are returned unchanged.
    )rJ   r  r  MappingViewrk   )rg  r   r   r   sanitize_sequence  s   r  c           	      C   s   ddl m} | du ri S |du ri }nt|trt||s"t||r(t|di }dd | D }i }i }|  D ]#\}}|||}||v rTtd|| d|d	|||< |||< q9|S )
aA  
    Helper function to normalize kwarg inputs.

    Parameters
    ----------
    kw : dict or None
        A dict of keyword arguments.  None is explicitly supported and treated
        as an empty dict, to support functions with an optional parameter of
        the form ``props=None``.

    alias_mapping : dict or Artist subclass or Artist instance, optional
        A mapping between a canonical name to a list of aliases, in order of
        precedence from lowest to highest.

        If the canonical value is not in the list it is assumed to have the
        highest priority.

        If an Artist subclass or instance is passed, use its properties alias
        mapping.

    Raises
    ------
    TypeError
        To match what Python raises if invalid arguments/keyword arguments are
        passed to a callable.
    r   )ArtistN
_alias_mapc                 S   s    i | ]\}}|D ]}||qqS r   r   )ru   	canonical
alias_listaliasr   r   r   rx     s    z$normalize_kwargs.<locals>.<dictcomp>z	Got both z and z", which are aliases of one another)	matplotlib.artistr  rJ   r   
issubclassr   rz   r*   rR   )	kwalias_mappingr  to_canonicalcanonical_to_seenretr\   r]   r  r   r   r   normalize_kwargs  s.   

r  c              	   c   s    t | } | | jd }d}d}t|D ]*}z|d 	 W d   W  n1 s+w   Y  W q ty?   t| Y qw td	|z
dV  W |
  dS |
  w )af  
    Context manager for locking a path.

    Usage::

        with _lock_path(path):
            ...

    Another thread or process that attempts to lock the same path will wait
    until this context manager is exited.

    The lock is implemented by creating a temporary file in the parent
    directory, so that directory must exist and be writable.
    z.matplotlib-lockr  g?xbNzLock error: Matplotlib failed to acquire the following lock file:
    {}
This maybe due to another process holding this lock file.  If you are sure no
other Matplotlib process is running, remove this file and try again.)r   	with_namer  ranger   FileExistsErrortimesleepTimeoutErrorformatunlink)r   	lock_pathretries	sleeptime_r   r   r   
_lock_path  s*   &r  zorderrI  c                 C   s   |t | S )a  
    Get the topmost artist of a list.

    In case of a tie, return the *last* of the tied artists, as it will be
    drawn on top of the others. `max` returns the first maximum in case of
    ties, so we need to iterate over the list in reverse order.
    )reversed)artists_cached_maxr   r   r   _topmost_artist7  s   
r  c                 C   s   t | to| |kS )a  
    Return whether *obj* is a string equal to string *s*.

    This helper solely exists to handle the case where *obj* is a numpy array,
    because in such cases, a naive ``obj == s`` would yield an array, which
    cannot be used in a boolean context.
    )rJ   r   rG   r{   r   r   r   
_str_equalD  s   r  c                 C   s   t | to
|  |kS )a  
    Return whether *obj* is a string equal, when lowercased, to string *s*.

    This helper solely exists to handle the case where *obj* is a numpy array,
    because in such cases, a naive ``obj == s`` would yield an array, which
    cannot be used in a boolean context.
    )rJ   r   r   r  r   r   r   _str_lower_equalO  s   r  c                 C   sP   t jdd }t jddd }t | d|f | |df | d|f | |df fS )aZ  
    Get the elements on the perimeter of *arr*.

    Parameters
    ----------
    arr : ndarray, shape (M, N)
        The input array.

    Returns
    -------
    ndarray, shape (2*(M - 1) + 2*(N - 1),)
        The elements on the perimeter of the array::

           [arr[0, 0], ..., arr[0, -1], ..., arr[-1, -1], ..., arr[-1, 0], ...]

    Examples
    --------
    >>> i, j = np.ogrid[:3, :4]
    >>> a = i*10 + j
    >>> a
    array([[ 0,  1,  2,  3],
           [10, 11, 12, 13],
           [20, 21, 22, 23]])
    >>> _array_perimeter(a)
    array([ 0,  1,  2,  3, 13, 23, 22, 21, 20, 10])
    r   r   )r   s_r  )arrr   backwardr   r   r   _array_perimeterZ  s   



r  c                 C   s`   g | j |}g | j| j| }|| | | d ||< || | ||< tjjj| ||ddS )a  
    Append an extra dimension containing sliding windows along *axis*.

    All windows are of size *size* and begin with every *step* elements.

    Parameters
    ----------
    arr : ndarray, shape (N_1, ..., N_k)
        The input array
    axis : int
        Axis along which the windows are extracted
    size : int
        Size of the windows
    step : int
        Stride between first elements of subsequent windows.

    Returns
    -------
    ndarray, shape (N_1, ..., 1 + (N_axis-size)/step, ..., N_k, size)

    Examples
    --------
    >>> i, j = np.ogrid[:3, :7]
    >>> a = i*10 + j
    >>> a
    array([[ 0,  1,  2,  3,  4,  5,  6],
           [10, 11, 12, 13, 14, 15, 16],
           [20, 21, 22, 23, 24, 25, 26]])
    >>> _unfold(a, axis=1, size=3, step=2)
    array([[[ 0,  1,  2],
            [ 2,  3,  4],
            [ 4,  5,  6]],
           [[10, 11, 12],
            [12, 13, 14],
            [14, 15, 16]],
           [[20, 21, 22],
            [22, 23, 24],
            [24, 25, 26]]])
    r   F)r]  strides	writeable)r]  r  r   libstride_tricks
as_strided)r  r  r  r  	new_shapenew_stridesr   r   r   _unfold  s   (
r&  c                 C   s
  |dkr|dks
J | j d d | dksJ | j d d | dks$J t| dd|ddf d||}t| |d|ddf d||ddddf }t| dd|d|f d||}t| dddd|f d||ddddf }tj||||fdddd||  S )ad  
    Extract perimeters of patches from *arr*.

    Extracted patches are of size (*rstride* + 1) x (*cstride* + 1) and
    share perimeters with their neighbors. The ordering of the vertices matches
    that returned by ``_array_perimeter``.

    Parameters
    ----------
    x : ndarray, shape (N, M)
        Input array
    rstride : int
        Vertical (row) stride between corresponding elements of each patch
    cstride : int
        Horizontal (column) stride between corresponding elements of each patch

    Returns
    -------
    ndarray, shape (N/rstride * M/cstride, 2 * (rstride + cstride))
    r   r   Nr   .r   r  )r]  r&  r   r  rY  )r   rstridecstridetopbottomrightleftr   r   r   _array_patch_perimeters  s    . .r.  c                 k   s    t  }i }|D ]+}t| ||}|| jv s||u r|||< qtt| |}t|tr/|||< q|||< qz.| D ]
\}}t| || q9dV  W | D ]\}}||u rZt| | qLt| || qLdS | D ]\}}||u rut| | qgt| || qgw )zR
    Temporarily set some attributes; restore original state at context exit.
    N)	rA  r   __dict__r   rJ   propertyrz   setattrdelattr)rG   r   sentinelorigsattrr   cls_origr   r   r   r   _setattr_cm  s0   




r7  c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )_OrderedSetc                 C   s   t  | _d S r   )r  OrderedDict_odr   r   r   r   r     s   z_OrderedSet.__init__c                 C   r7  r   )r:  r   r`   r   r   r   r9    rH   z_OrderedSet.__contains__c                 C   rM   r   )rS  r:  r   r   r   r   rh     rH   z_OrderedSet.__iter__c                 C   rM   r   )r   r:  r   r   r   r   r     rH   z_OrderedSet.__len__c                 C   s   | j |d  d | j |< d S r   r:  rg   r;  r   r   r   r     s   z_OrderedSet.addc                 C   s   | j |d  d S r   r<  r;  r   r   r   r   !  s   z_OrderedSet.discardN)	r   r   r   r   r9  rh   r   r   r   r   r   r   r   r8    s    r8  c                 C   s   t j| tjdkrg dng ddd}|dddf }|d	 }|d
k}t |dD ]}|| td || d  ||  ||< q)|S )zS
    Convert a premultiplied ARGB32 buffer to an unmultiplied RGBA8888 buffer.
    littler   r   r   r!   )r   r   r!   r   r   r'  .Nr   ).r   r      )r   taker(   	byteorderrollaxisastypeint)bufrgbargbalpharr  channelr   r   r   ._premultiplied_argb32_to_unmultiplied_rgba8888)  s   rJ  c                 C   s   t jdkr tj| g ddd}|dddf }|dddf }ntj| g ddd}|ddd	f }|dd	df }| d
krKtj||d
 |dd |S )zS
    Convert an unmultiplied RGBA8888 buffer to a premultiplied ARGB32 buffer.
    r=  r>  r   r'  .Nr   )r!   r   r   r   r   r?  unsafe)r   casting)r(   rA  r   r@  r   multiply)rgba8888argb32rgb24alpha8r   r   r   ._unmultiplied_rgba8888_to_premultiplied_argb32;  s   
rR  c                 C   s   | j dd \}| j dd \}t|r8t|r8|ddg \}}|ddg \}}t||d t||d fS tddtddfS )a!  
    Return the bounds of the nonzero region of a 2D array as a pair of slices.

    ``buf[_get_nonzero_slices(buf)]`` is the smallest sub-rectangle in *buf*
    that encloses all non-zero entries in *buf*.  If *buf* is fully zero, then
    ``(slice(0, 0), slice(0, 0))`` is returned.
    r   r'  r   r   )anyrk  r   slice)rE  x_nzy_nzlr   rC  tr   r   r   _get_nonzero_slicesO  s   rY  c                 C   s"   t | tr| S ddd | D S )zAPretty-format a subprocess command for printing/logging purposes.r   c                 s   s     | ]}t t|V  qd S r   )shlexquoter   r   )ru   r=  r   r   r   r   d  s    z&_pformat_subprocess.<locals>.<genexpr>)rJ   r   r@  )commandr   r   r   _pformat_subprocessa  s   r]  c                 K   s   | dt|  tj| fddi|}|jr=|j}t|tr"| }|j	}t|tr.| }t
dt|  d| d| |jrG| d|j |j	rQ| d|j	 |jS )	aj  
    Run *command*, returning its stdout output if it succeeds.

    If it fails (exits with nonzero return code), raise an exception whose text
    includes the failed command and captured stdout and stderr output.

    Regardless of the return code, the command is logged at DEBUG level on
    *logger*.  In case of success, the output is likewise logged.
    z%scapture_outputTzThe command
    z,
failed and generated the following output:
z
and the following error:
z
stdout:
%sz
stderr:
%s)debugr]  
subprocessrun
returncodestdoutrJ   bytesdecodestderrr  )r\  loggerr   procrc  rf  r   r   r   _check_and_log_subprocessg  s.   


ri  c                   C   s.   zt   W dS  ty   t d Y dS w )zS
    Perform OS-dependent setup when Matplotlib creates a new GUI application.
    r   N)r   -Win32_GetCurrentProcessExplicitAppUserModelIDOSError-Win32_SetCurrentProcessExplicitAppUserModelIDr   r   r   r   _setup_new_guiapp  s   
rm  c                 C   s    | d| d d dpdS )z
    Format the number with at most the number of decimals given as precision.
    Remove trailing zeros and possibly the decimal point.
    .f0)rstrip)number	precisionr   r   r   _format_approx  s    rt  c                 C   sf   |dkr| dkr
dS t t| }t| r1td| r&ttt | d ndtt| S dS )z
    Return the number of significant digits to %g-format *value*, assuming that
    it is known with an error of *delta*.
    r   r!   r   )absr   spacingr  r  r   floorlog10)ra   deltar   r   r   _g_sig_digits  s   
rz  c                 C   s   | r|   r| S | }|dr|dd }|dr"|dd}|dr-|dd }tjdkr8|d	kr8d
}dddd||}|S )z
    Convert a Unicode key or X keysym to a Matplotlib key name.

    The Unicode key is checked first; this avoids having to list most printable
    keysyms such as ``EuroSign``.
    kp_r!   Npage_page)_l_rr  darwinmetacmdenterpageuppagedown)returnpriorr~   )isprintabler   
startswithr   r   r(   platformr*   )unikeykeysymr`   r   r   r   _unikey_or_keysym_to_mplkey  s$   


r  c                    s"   t j fdd}j|_|S )ax  
    Return a function that creates picklable classes inheriting from a mixin.

    After ::

        factory = _make_class_factory(FooMixin, fmt, attr_name)
        FooAxes = factory(Axes)

    ``Foo`` is a class that inherits from ``FooMixin`` and ``Axes`` and **is
    picklable** (picklability is what differentiates this from a plain call to
    `type`).  Its ``__name__`` is set to ``fmt.format(Axes.__name__)`` and the
    base class is stored in the ``attr_name`` attribute, if not None.

    Moreover, the return value of ``factory`` is memoized: calls with the same
    ``Axes`` class always return the same subclass.
    c                    sZ   t | r| S |  G  fddd } j |_|_d ur+t|  |S )Nc                       s$   e Zd ZjZ fddZdS )z:_make_class_factory.<locals>.class_factory.<locals>.subclsc                    s   t  f|  fS r   )_picklable_class_constructorr   r   	attr_name
base_classfmtmixin_classr   r   
__reduce__  s   
zE_make_class_factory.<locals>.class_factory.<locals>.subcls.__reduce__N)r   r   r   r  r   r  r   r   subcls  s    r  )r  r  r   r   r1  )
axes_classr  r  r  r  )r  r   class_factory  s   
	z*_make_class_factory.<locals>.class_factory)r   cacher   )r  r  r  r  r   r  r   _make_class_factory  s   r  c                 C   s   t | ||}||}||S )z(Internal helper for _make_class_factory.)r  __new__)r  r  r  r  factoryr   r   r   r   r  	  s   
r  c                 C   *   z
t | tjd jW S  ty   Y dS w )z!Check if 'x' is a PyTorch Tensor.torchF)rJ   r(   r)   Tensorr   r   r   r   r   _is_torch_array		  
   r  c                 C   r  )zCheck if 'x' is a JAX Array.jaxF)rJ   r(   r)   Arrayr   r   r   r   r   _is_jax_array	  r  r  c                 C   s.   zt | tjd | W S  ty   Y dS w )z0Check if 'x' is a TensorFlow Tensor or Variable.
tensorflowF)rJ   r(   r)   	is_tensorr   r   r   r   r   _is_tensorflow_array!	  s
   r  c                 C   sx   t | tjr| S t| dr|  S t| dr!| j}t |tjr!|S t| s-t| s-t| r:t	| }t |tjr:|S | S )zDInternal helper to extract data from e.g. pandas and xarray objects.r  r6   )
rJ   r   rh  r   r  r6   r  r  r  re  )r   xtmpr   r   r   r  0	  s   


r  c              	   C   s.   z| |f W S  t tfy   | | Y S w )aA  
    Apply *value* to the format string *fmt*.

    This works both with unnamed %-style formatting and
    unnamed {}-style formatting. %-style formatting has priority.
    If *fmt* is %-style formattable that will be used. Otherwise,
    {}-formatting is applied. Strings without formatting placeholders
    are passed through as is.

    Examples
    --------
    >>> _auto_format_str('%.2f m', 0.2)
    '0.20 m'
    >>> _auto_format_str('{} m', 0.2)
    '0.2 m'
    >>> _auto_format_str('const', 0.2)
    'const'
    >>> _auto_format_str('%d or {}', 0.2)
    '0 or {}'
    )rR   r   r  )r  ra   r   r   r   _auto_format_strK	  s
   r  c                 C   r  )z#Check if 'x' is a Pandas DataFrame.pandasF)rJ   r(   r)   	DataFramer   r   r   r   r   _is_pandas_dataframef	  r  r  )r   FN)r   N)T)F)r  NNF)r  Nr   )nr   r  collections.abcr   r   r   ro   r  operatorr   pathlibr   rZ  r`  r(   r	  r@   r  rP   numpyr   numpy.exceptionsr   ImportErrorr   r   r   r   r?   rB   rC   rU   rW   ri   rk   r   MatplotlibDeprecationWarningr   r   r   r   r   r   r   r   r   r   r   r   r  rc  r+  r,  rN  r`  rt  rz  r  r  	ls_mapperrz   ls_mapper_rr  r  r  r  r  r  r  r  r  STEP_LOOKUP_MAPr  r  r  r  r  r   r  r   r   
attrgetterr  r  r  r  r&  r.  r7  r  
MutableSetr8  rJ  rR  rY  r]  ri  rm  rt  rz  r  r  r  r  r  r  r  r  r  r  r   r   r   r   <module>   s    2
7 G$

4
	
7EoMA
& K
<b&$&#'
8
(
'21
+!.