o
    eF                     @   s  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(dee	e
f d	ee& d
eee(  dee# fddZ+e		d(deeeeef d	ee& d
eee(  dee# fddZ+d(ddZ+	 	d)dee	e
eeeef de&d
eee(  dee( fddZ,de-de'dee- fddZ.de-de'dee( fddZ/de-de(de'dee' fddZ0d*de-de'de)fdd Z1d!ee(e'f d"e)dee(e'f fd#d$Z2	d*de-d%ee( d"e)de'dee(ee(e'f f f
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      d S N r#   r$   r%   r)   r)   ^/root/Voice_Assist/myprojectenv/lib/python3.10/site-packages/altair/utils/_transformed_data.pytransformed_data6      r,   c                 C   r'   r(   r)   r*   r)   r)   r+   r,   ?   r-   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 s;w   Y  t|}t|||}g }	|D ]}
|
|v r\|	||
  qNtd|jj||	||d\}}t| ttfr{|swd
S |d S |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_specr1   facet_mappingZdataset_mappingZdataset_namesZ
chart_namedatasetswarningsr)   r)   r+   r,   H   s8   



ic                 C   s  |durt |nt  }t| tt st| tt r0| j|vr.| jdtfv r*t | _| jgS g S t| tt r;| j	}n*t| tt
 rF| j}nt| tt rQ| j}nt| tt r\| j}n	tdt|  g }|D ]}t||t| |dD ]}|| qvqi|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: )rA   r%   )setr2   _chart_class_mappingr	   r
   namer"   Z	_get_namer   layerr   Zhconcatr   Zvconcatr   concatr;   typer5   lenr:   )r#   rA   r%   Z	subchartsr<   ZsubchartrD   r)   r)   r+   r5      s8   

r5   r=   scopec                 C   sd   | }|D ]+}d}d}| dg D ]}| ddkr%||kr!|} n|d7 }q|du r- 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marksrG   group   )get)r=   rI   rK   Zscope_valuegroup_indexZchild_groupr3   r)   r)   r+   get_group_mark_for_scope   s   "rO   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))
    []
    datarD   fromfacetN)rO   rM   r:   )r=   rI   rK   r?   ZdatasetZfacet_datasetr)   r)   r+   get_datasets_for_scope  s   7
rS   	data_nameusage_scopec                 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'
    rL   N)reversedrangerH   rS   )r=   rT   rU   rA   rI   r?   r)   r)   r+   'get_definition_scope_for_data_referenceT  s   :
rX   r)   rK   c                 C   s   i }d}t | |p
i }|dg D ]R}|dddkrc||f }|di dd}|durT|dd}|d	d}	|durT|	durTt| |	|}
|
durT|	|
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   rJ   rG   NrK   rQ   rR   rD   rP   rI   rL   )rO   rM   rX   r8   update)rK   rI   r>   rN   
mark_groupr3   Zgroup_scoperR   Z
facet_nameZ
facet_dataZdefinition_scopeZchild_mappingr)   r)   r+   r8     s.   %

r8   scoped_datasetr>   c                 C   s   | |v r||  } | |v s| 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)   )r\   r>   r)   r)   r+   get_from_facet_mapping  s   r]   vl_chart_namesc                 C   s8  i }d}t | |p
i }|dg D ]}|D ](}|dd| dkr=|di ddd	d}	|	|f}
t|
|||<  nq|dd}|d
ddkrkt| ||||f d}| D ]
\}}||| q[|d7 }q|D ]+}||r|dr|di d	d}	t| |	|}|durt|	|f|||<  nqmq|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   rJ   rD    _cellrQ   rR   NrP   rG   rK   rY   rL   _marks)	rO   rM   r]   r9   items
setdefault
startswithendswithrX   )rK   r^   r>   rI   r?   rN   r[   r3   Zvl_chart_namerT   Zscoped_data_namerD   Zgroup_data_nameskvZscoped_datar)   r)   r+   r9     sD   
r9   )NN)r   N)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FacetMappingrC   r,   r5   dictrO   rS   rX   r8   r]   r9   r)   r)   r)   r+   <module>   s   $ `








W

?5E
BB


!