a
    d
I                     @   sh  d dl mZmZmZmZmZmZmZ d dlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlm Z m!Z! d dl"m#Z# d dl$m%Z% ee&df Z'eee(e'f ee(e'f f Z)e	e	eeeeefeeeefeeeefeeeefeeeefe
e
eefiZ*ed"ee	e
f ee& eee(  ee# dd	d
Z+ed#eeeeef ee& eee(  ee# ddd
Z+d$dd
Z+d%ee	e
eeeef e&eee(  ee( dddZ,e-e'ee- dddZ.e-e'ee( dddZ/e-e(e'ee' dddZ0d&e-e'e)dddZ1ee(e'f e)ee(e'f dddZ2d'e-ee( e)e'ee(ee(e'f f dd d!Z3dS )(    )ListOptionalTupleDictIterableoverloadUnion)Chart
FacetChart
LayerChartHConcatChartVConcatChartConcatChartTopLevelUnitSpecFacetedUnitSpecUnitSpecUnitSpecWithFrameNonNormalizedSpecTopLevelLayerSpec	LayerSpecTopLevelConcatSpecConcatSpecGenericSpecTopLevelHConcatSpecHConcatSpecGenericSpecTopLevelVConcatSpecVConcatSpecGenericSpecTopLevelFacetSpec	FacetSpecdata_transformers)get_inline_tablesimport_vegafusion)_DataFrameLike)	Undefined.N)chart	row_limitexcludereturnc                 C   s   d S N r#   r$   r%   r(   r(   g/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/altair/utils/_transformed_data.pytransformed_data6   s    r+   c                 C   s   d S r'   r(   r)   r(   r(   r*   r+   ?   s    c                 C   s  t  }t| tr"| jtkr"|  } | jdd} t| d|d}t	d* | j
dddid	}t|}W d
   n1 sv0    Y  t|}t|||}g }	|D ]$}
|
|v r|	||
  qtdq|jj||	||d\}}t| ttfr|sd
S |d S n|S d
S )a  Evaluate a Chart's transforms

    Evaluate the data transforms associated with a Chart and return the
    transformed data as one or more DataFrames

    Parameters
    ----------
    chart : Chart, FacetChart, LayerChart, HConcatChart, VConcatChart, or ConcatChart
        Altair chart to evaluate transforms on
    row_limit : int (optional)
        Maximum number of rows to return for each DataFrame. None (default) for unlimited
    exclude : iterable of str
        Set of the names of charts to exclude

    Returns
    -------
    DataFrame or list of DataFrames or None
        If input chart is a Chart or Facet Chart, returns a DataFrame of the
        transformed data. Otherwise, returns a list of DataFrames of the
        transformed data
    T)deepr   )r%   Z
vegafusionZvegaZpre_transformF)formatcontextNzFailed to locate all datasets)r$   inline_datasets)r    
isinstancer	   markr"   Z
mark_pointcopy
name_viewsr   enableto_dictr   get_facet_mappingget_datasets_for_view_namesappend
ValueErrorZruntimeZpre_transform_datasetsr
   )r#   r$   r%   Zvfchart_names	vega_specr/   facet_mappingZdataset_mappingZdataset_namesZ
chart_namedatasetswarningsr(   r(   r*   r+   H   s6    

&


)r#   ir%   r&   c                 C   s  |durt |nt  }t| tt s2t| tt rb| j|vr\| jdtfv rTt | _| jgS g S nt| tt rx| j	}nTt| tt
 r| j}n>t| tt r| j}n(t| tt r| j}ntdt|  g }|D ]*}t||t| |dD ]}|| qq|S dS )aG  Name unnamed chart views

    Name unnamed charts views so that we can look them up later in
    the compiled Vega spec.

    Note: This function mutates the input chart by applying names to
    unnamed views.

    Parameters
    ----------
    chart : Chart, FacetChart, LayerChart, HConcatChart, VConcatChart, or ConcatChart
        Altair chart to apply names to
    i : int (default 0)
        Starting chart index
    exclude : iterable of str
        Names of charts to exclude

    Returns
    -------
    list of str
        List of the names of the charts and subcharts
    Nztransformed_data accepts an instance of Chart, FacetChart, LayerChart, HConcatChart, VConcatChart, or ConcatChart
Received value of type: )r?   r%   )setr0   _chart_class_mappingr	   r
   namer"   Z	_get_namer   Zlayerr   Zhconcatr   Zvconcatr   concatr9   typer3   lenr8   )r#   r?   r%   Z	subchartsr:   ZsubchartrB   r(   r(   r*   r3      s6    

r3   )r;   scoper&   c                 C   sd   | }|D ]V}d}d}| dg D ]*}| ddkr ||krB|} qL|d7 }q |du rZ dS |}q|S )a  Get the group mark at a particular scope

    Parameters
    ----------
    vega_spec : dict
        Top-level Vega specification dictionary
    scope : tuple of int
        Scope tuple. If empty, the original Vega specification is returned.
        Otherwise, the nested group mark at the scope specified is returned.

    Returns
    -------
    dict or None
        Top-level Vega spec (if scope is empty)
        or group mark (if scope is non-empty)
        or None (if group mark at scope does not exist)

    Examples
    --------
    >>> spec = {
    ...     "marks": [
    ...         {
    ...             "type": "group",
    ...             "marks": [{"type": "symbol"}]
    ...         },
    ...         {
    ...             "type": "group",
    ...             "marks": [{"type": "rect"}]}
    ...     ]
    ... }
    >>> get_group_mark_for_scope(spec, (1,))
    {'type': 'group', 'marks': [{'type': 'rect'}]}
    r   NmarksrD   group   )get)r;   rF   rH   Zscope_valuegroup_indexZchild_groupr1   r(   r(   r*   get_group_mark_for_scope   s    "
rL   c                 C   s`   t | |pi }g }|dg D ]}||d  q|di di dd}|r\|| |S )a  Get the names of the datasets that are defined at a given scope

    Parameters
    ----------
    vega_spec : dict
        Top-leve Vega specification
    scope : tuple of int
        Scope tuple. If empty, the names of top-level datasets are returned
        Otherwise, the names of the datasets defined in the nested group mark
        at the specified scope are returned.

    Returns
    -------
    list of str
        List of the names of the datasets defined at the specified scope

    Examples
    --------
    >>> spec = {
    ...     "data": [
    ...         {"name": "data1"}
    ...     ],
    ...     "marks": [
    ...         {
    ...             "type": "group",
    ...             "data": [
    ...                 {"name": "data2"}
    ...             ],
    ...             "marks": [{"type": "symbol"}]
    ...         },
    ...         {
    ...             "type": "group",
    ...             "data": [
    ...                 {"name": "data3"},
    ...                 {"name": "data4"},
    ...             ],
    ...             "marks": [{"type": "rect"}]
    ...         }
    ...     ]
    ... }

    >>> get_datasets_for_scope(spec, ())
    ['data1']

    >>> get_datasets_for_scope(spec, (0,))
    ['data2']

    >>> get_datasets_for_scope(spec, (1,))
    ['data3', 'data4']

    Returns empty when no group mark exists at scope
    >>> get_datasets_for_scope(spec, (1, 3))
    []
    datarB   fromfacetN)rL   rJ   r8   )r;   rF   rH   r=   ZdatasetZfacet_datasetr(   r(   r*   get_datasets_for_scope  s    7
rP   )r;   	data_nameusage_scoper&   c                 C   sD   t tt|d D ]*}|d| }t| |}||v r|  S qdS )aG  Return the scope that a dataset is defined at, for a given usage scope

    Parameters
    ----------
    vega_spec: dict
        Top-level Vega specification
    data_name: str
        The name of a dataset reference
    usage_scope: tuple of int
        The scope that the dataset is referenced in

    Returns
    -------
    tuple of int
        The scope where the referenced dataset is defined,
        or None if no such dataset is found

    Examples
    --------
    >>> spec = {
    ...     "data": [
    ...         {"name": "data1"}
    ...     ],
    ...     "marks": [
    ...         {
    ...             "type": "group",
    ...             "data": [
    ...                 {"name": "data2"}
    ...             ],
    ...             "marks": [{
    ...                 "type": "symbol",
    ...                 "encode": {
    ...                     "update": {
    ...                         "x": {"field": "x", "data": "data1"},
    ...                         "y": {"field": "y", "data": "data2"},
    ...                     }
    ...                 }
    ...             }]
    ...         }
    ...     ]
    ... }

    data1 is referenced at scope [0] and defined at scope []
    >>> get_definition_scope_for_data_reference(spec, "data1", (0,))
    ()

    data2 is referenced at scope [0] and defined at scope [0]
    >>> get_definition_scope_for_data_reference(spec, "data2", (0,))
    (0,)

    If data2 is not visible at scope [] (the top level),
    because it's defined in scope [0]
    >>> repr(get_definition_scope_for_data_reference(spec, "data2", ()))
    'None'
    rI   N)reversedrangerE   rP   )r;   rQ   rR   r?   rF   r=   r(   r(   r*   'get_definition_scope_for_data_referenceT  s    :

rU   r(   )rH   rF   r&   c                 C   s   i }d}t | |pi }|dg D ]}|dddkr"||f }|di dd}|dur|dd}|d	d}	|dur|	durt| |	|}
|
dur|	|
f|||f< t| |d
}|| |d7 }q"|S )a  Create mapping from facet definitions to source datasets

    Parameters
    ----------
    group : dict
        Top-level Vega spec or nested group mark
    scope : tuple of int
        Scope of the group dictionary within a top-level Vega spec

    Returns
    -------
    dict
        Dictionary from (facet_name, facet_scope) to (dataset_name, dataset_scope)

    Examples
    --------
    >>> spec = {
    ...     "data": [
    ...         {"name": "data1"}
    ...     ],
    ...     "marks": [
    ...         {
    ...             "type": "group",
    ...             "from": {
    ...                 "facet": {
    ...                     "name": "facet1",
    ...                     "data": "data1",
    ...                     "groupby": ["colA"]
    ...                 }
    ...             }
    ...         }
    ...     ]
    ... }
    >>> get_facet_mapping(spec)
    {('facet1', (0,)): ('data1', ())}
    r   rG   rD   NrH   rN   rO   rB   rM   rF   rI   )rL   rJ   rU   r6   update)rH   rF   r<   rK   
mark_groupr1   Zgroup_scoperO   Z
facet_nameZ
facet_dataZdefinition_scopeZchild_mappingr(   r(   r*   r6     s,    %


r6   )scoped_datasetr<   r&   c                 C   s   | |v r||  } q | S )a  Apply facet mapping to a scoped dataset

    Parameters
    ----------
    scoped_dataset : (str, tuple of int)
        A dataset name and scope tuple
    facet_mapping : dict from (str, tuple of int) to (str, tuple of int)
        The facet mapping produced by get_facet_mapping

    Returns
    -------
    (str, tuple of int)
        Dataset name and scope tuple that has been mapped as many times as possible

    Examples
    --------
    Facet mapping as produced by get_facet_mapping
    >>> facet_mapping = {("facet1", (0,)): ("data1", ()), ("facet2", (0, 1)): ("facet1", (0,))}
    >>> get_from_facet_mapping(("facet2", (0, 1)), facet_mapping)
    ('data1', ())
    r(   )rY   r<   r(   r(   r*   get_from_facet_mapping  s    
rZ   )rH   vl_chart_namesr<   rF   r&   c                 C   s:  i }d}t | |pi }|dg D ]}|D ]P}|dd| dkr,|di ddd	d}	|	|f}
t|
|||<  q~q,|dd}|d
ddkrt| ||||f d}| D ]\}}||| q|d7 }q"|D ]V}||r|dr|di d	d}	t| |	|}|durt|	|f|||<  q"qq"|S )aB  Get the Vega datasets that correspond to the provided Altair view names

    Parameters
    ----------
    group : dict
        Top-level Vega spec or nested group mark
    vl_chart_names : list of str
        List of the Vega-Lite
    facet_mapping : dict from (str, tuple of int) to (str, tuple of int)
        The facet mapping produced by get_facet_mapping
    scope : tuple of int
        Scope of the group dictionary within a top-level Vega spec

    Returns
    -------
    dict from str to (str, tuple of int)
        Dict from Altair view names to scoped datasets
    r   rG   rB    Z_cellrN   rO   NrM   rD   rH   rV   rI   _marks)	rL   rJ   rZ   r7   items
setdefault
startswithendswithrU   )rH   r[   r<   rF   r=   rK   rX   r1   Zvl_chart_namerQ   Zscoped_data_namerB   Zgroup_data_nameskvZscoped_datar(   r(   r*   r7     s@    
r7   )NN)NN)NN)r   N)r(   )r(   )4typingr   r   r   r   r   r   r   Zaltairr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zaltair.utils._vegafusion_datar   r    Zaltair.utils.corer!   Zaltair.utils.schemapir"   intZScopestrZFacetMappingrA   r+   r3   dictrL   rP   rU   r6   rZ   r7   r(   r(   r(   r*   <module>   s   $`




  

  

W  
?5FBC
! 