U
    Gvf'                     @  sR  d dl mZ d dlmZmZmZ d dlZd dlmZm	Z	m
Z
 d dlmZ d dl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 d d
lmZmZmZmZmZ d dlm Z  G dd dZ!e
e"e!f Z#e
e$e!f Z%e
e&e!f Z'e
e$e(e!f Z)e
e$eee!f Z*eG dd dZ+dddddddZ,d ddddddddZ-dd Z.dS )!    )annotations)	dataclassfieldsfieldN)AnyCallableUnion)	Generator)ndarray)	DataFrame)Artist)Scale)
PROPERTIESProperty	RGBATupleDashPatternDashPatternWithOffset)PlotSpecErrorc                   @  sb   e Zd Zddddddddd	Zd
d ZeddddZeddddZeddddZdS )MappableNFTr   z
str | Nonebool)valdependrcautogroupingc                 C  sL   |dk	r|t kst|dk	r*|tjks*t|| _|| _|| _|| _|| _dS )a  
        Property that can be mapped from data or set directly, with flexible defaults.

        Parameters
        ----------
        val : Any
            Use this value as the default.
        depend : str
            Use the value of this feature as the default.
        rc : str
            Use the value of this rcParam as the default.
        auto : bool
            The default value will depend on other parameters at compile time.
        grouping : bool
            If True, use the mapped variable to define groups.

        N)	r   AssertionErrormplrcParams_val_rc_depend_auto	_grouping)selfr   r   r   r   r    r$   7/tmp/pip-unpacked-wheel-ir0tye89/seaborn/_marks/base.py__init__   s    zMappable.__init__c                 C  sf   | j dk	rdt| j  d}nD| jdk	r8d| j d}n*| jdk	rRd| j d}n| jr^d}nd}|S )z?Nice formatting for when object appears in Mark init signature.N<>z<depend:z<rc:z<auto>z<undefined>)r   reprr    r   r!   )r#   sr$   r$   r%   __repr__?   s    


zMappable.__repr__)returnc                 C  s   | j S )z>Return the name of the feature to source a default value from.)r    r#   r$   r$   r%   r   M   s    zMappable.dependc                 C  s   | j S N)r"   r-   r$   r$   r%   r   R   s    zMappable.groupingc                 C  s,   | j dk	r| j S | jdk	r(tj| jS dS )zGGet the default value for this feature, or access the relevant rcParam.N)r   r   r   r   getr-   r$   r$   r%   defaultV   s    

zMappable.default)NNNFT)	__name__
__module____qualname__r&   r+   propertyr   r   r0   r$   r$   r$   r%   r      s        $r   c                   @  s   e Zd ZU dZeedZded< edd Z	edd Z
dd
ddddddZdddddZdddddddZdddddddZd	S ) Markz4Base class for objects that visually represent data.)default_factorydict
artist_kwsc                   s    fddt  D S )Nc                   s(   i | ] }t |jtr|jt |jqS r$   )
isinstancer0   r   namegetattr.0fr-   r$   r%   
<dictcomp>p   s    z(Mark._mappable_props.<locals>.<dictcomp>r   r-   r$   r-   r%   _mappable_propsn   s    
zMark._mappable_propsc                 C  s   dd t | D S )Nc                 S  s&   g | ]}t |jtr|jjr|jqS r$   )r9   r0   r   r   r:   r<   r$   r$   r%   
<listcomp>y   s    z(Mark._grouping_props.<locals>.<listcomp>r@   r-   r$   r$   r%   _grouping_propsu   s    zMark._grouping_propsNzDataFrame | dict[str, Any]strdict[str, Scale] | Noner   )datar:   scalesr,   c              
   C  s^  | j | }t|t|}t|t }t|tj}|o@|d }|dkrV|oT||k}|r|	|}|rv|gt
| }|rt|}|S ||kr|dks||kr|| }nN|| }	|| }
z|	|
}W n0 tk
r } ztd||W 5 d}~X Y nX |r
t|}|S |jdk	r*| ||j|S |	|j}|rJ|gt
| }|rZt|}|S )a  Obtain default, specified, or mapped value for a named feature.

        Parameters
        ----------
        data : DataFrame or dict with scalar values
            Container with data values for features that will be semantically mapped.
        name : string
            Identity of the feature / semantic.
        scales: dict
            Mapping from variable to corresponding scale object.

        Returns
        -------
        value or array of values
            Outer return type depends on whether `data` is a dict (implying that
            we want a single value) or DataFrame (implying that we want an array
            of values with matching length).

        stylewidthNzScaling operation)rA   r   r/   r   r9   r   pdr   endswithZstandardizelennparray	Exceptionr   Z_duringZasarrayr   _resolver0   )r#   rF   r:   rG   ZfeaturepropZdirectly_specifiedZreturn_multipleZreturn_arrayZscalevalueerrr0   r$   r$   r%   rP      sD    




 

zMark._resolve)rG   r,   c                 C  s@   d|krdn|d j }d|kr"dn|d j }||kr8dS dS d S )Nxr   y)Z	_priority)r#   rG   rT   rU   r$   r$   r%   _infer_orient   s
    zMark._infer_orientzCallable[[], Generator]dict[str, Scale]None)split_generatorrG   orientr,   c                 C  s
   t  dS )z#Main interface for creating a plot.N)NotImplementedError)r#   rY   rG   rZ   r$   r$   r%   _plot   s    z
Mark._plotz	list[str]zArtist | None)	variablesrR   rG   r,   c                 C  s   d S r.   r$   )r#   r]   rR   rG   r$   r$   r%   _legend_artist   s    zMark._legend_artist)N)r1   r2   r3   __doc__r   r7   r8   __annotations__r4   rA   rC   rP   rV   r\   r^   r$   r$   r$   r%   r5   h   s   


 I	r5   r   rW   zdict[str, Any])markrF   rG   r,   c                   s    fddj D }|S )Nc                   s   i | ]}|  |qS r$   )rP   )r=   r:   rF   ra   rG   r$   r%   r?      s     z&resolve_properties.<locals>.<dictcomp>)rA   )ra   rF   rG   propsr$   rb   r%   resolve_properties   s    rd    zDataFrame | dictrD   rE   zRGBATuple | ndarray)ra   rF   prefixrG   r,   c                 C  s   |  || d|}| d| jkr:|  || d|}n|  |d|}ddd}t|dk rtdd |D rt|d	krtj|S ||r|ntj	}tj||S t|dkr|j
d
 d	krtj|S t||d
d|tj	}tj||S dS )a  
    Obtain a default, specified, or mapped value for a color feature.

    This method exists separately to support the relationship between a
    color and its corresponding alpha. We want to respect alpha values that
    are passed in specified (or mapped) color values but also make use of a
    separate `alpha` variable, which can be mapped. This approach may also
    be extended to support mapping of specific color channels (i.e.
    luminance, chroma) in the future.

    Parameters
    ----------
    mark :
        Mark with the color property.
    data :
        Container with data values for features that will be semantically mapped.
    prefix :
        Support "color", "fillcolor", etc.

    coloralphaNc                 S  s"   t | jjdkp t | |S )z5Detect "invisible" colors to set alpha appropriately.r>   )rM   rN   Zdtypekindisfiniteall)rT   axisr$   r$   r%   visible  s    zresolve_color.<locals>.visible   c                 s  s   | ]}t |tV  qd S r.   )r9   float)r=   rT   r$   r$   r%   	<genexpr>  s     z resolve_color.<locals>.<genexpr>      )rl   )N)rP   rA   rM   ndimrk   rL   r   colorsZto_rgbananshapeZto_rgba_arraywhere)ra   rF   rf   rG   rg   rh   rm   r$   r$   r%   resolve_color   s    
 rx   c                 C  sp   dd t | D }ddtjddd |D dddd	g}| jd
}d
|d d ||dd  }|| _| S )Nc                 S  s   g | ]}t |jtr|jqS r$   )r9   r0   r   r:   r<   r$   r$   r%   rB   ,  s      z'document_properties.<locals>.<listcomp>re   z/    This mark defines the following properties:z, c                 S  s   g | ]}d | d qS )|r$   )r=   pr$   r$   r%   rB   1  s     N   z        )rI   initial_indentsubsequent_indent
rn   )r   textwrapfilljoinr_   split)ra   Z
propertiestextZdocstring_linesZnew_docstringr$   r$   r%   document_properties*  s$      	

r   )re   N)/
__future__r   Zdataclassesr   r   r   r   typingr   r   r   collections.abcr	   ZnumpyrM   ZpandasrJ   Z
matplotlibr   r
   r   Zmatplotlib.artistr   Zseaborn._core.scalesr   Zseaborn._core.propertiesr   r   r   r   r   Zseaborn._core.exceptionsr   r   r   ZMappableBoolrD   ZMappableStringro   ZMappableFloattupleZMappableColorZMappableStyler5   rd   rx   r   r$   r$   r$   r%   <module>   s4   G  8