o
    h`G                     @  s  U d dl mZ d dl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! eryd dl"Z"d dl#m$Z$ e"j%dkrgd d	lm&Z& nd d	l'm&Z& d d
l(m)Z) d dl*m+Z+ e,e-df Z.de/d< e0e,e1e.f e,e1e.f f Z2de/d< 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Z3e		d?d@ddZ4e		d?dAddZ4d?ddZ4	dBdCd"d#Z5dDd(d)Z6dEd*d+Z7dFd0d1Z8dGdHd4d5Z9dId9d:Z:	2dGdJd=d>Z;dS )K    )annotations)TYPE_CHECKINGAnyoverload)ChartConcatChartConcatSpecGenericSpec
FacetChartFacetedUnitSpec	FacetSpecHConcatChartHConcatSpecGenericSpec
LayerChart	LayerSpecNonNormalizedSpecTopLevelConcatSpecTopLevelFacetSpecTopLevelHConcatSpecTopLevelLayerSpecTopLevelUnitSpecTopLevelVConcatSpecUnitSpecUnitSpecWithFrameVConcatChartVConcatSpecGenericSpecdata_transformers)get_inline_tablesimport_vegafusion)	UndefinedN)Iterable)   
   )	TypeAlias)	ChartType)DataFrameLike.r"   ScopeFacetMappingchartChart | FacetChart	row_limit
int | NoneexcludeIterable[str] | NonereturnDataFrameLike | Nonec                 C     d S N r'   r)   r+   r1   r1   R/var/www/vscode/kcb/lib/python3.10/site-packages/altair/utils/_transformed_data.pytransformed_dataC      r4   6LayerChart | HConcatChart | VConcatChart | ConcatChartlist[DataFrameLike]c                 C  r/   r0   r1   r2   r1   r1   r3   r4   K   r5   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\|	||
  qNd}t||jj||	||d\}}t| ttfr}|syd
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+   
vegafusionvegapre_transformF)formatcontextNzFailed to locate all datasets)r)   inline_datasets)r   
isinstancer   markr   
mark_pointcopy
name_viewsr   enableto_dictr   get_facet_mappingget_datasets_for_view_namesappend
ValueErrorruntimepre_transform_datasetsr	   )r'   r)   r+   vfchart_names	vega_specr>   facet_mappingdataset_mappingdataset_names
chart_namemsgdatasets_r1   r1   r3   r4   S   s8   

r#   iint	list[str]c                 C  s   |durt |nt  }t| tt tt fr-| j|vr+| jdthv r't | _| jgS g S t| tt r8| j	}n,t| tt
 rC| j}n!t| tt rN| j}nt| tt rY| j}ndt|  }t|g }|D ]}t||t| |dD ]}|| quqh|S )aM  
    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: )rV   r+   )setr?   _chart_class_mappingr   r	   namer   	_get_namer   layerr   hconcatr   vconcatr   concattyperI   rC   lenrH   )r'   rV   r+   	subchartsrS   rM   subchartr[   r1   r1   r3   rC      s8   

rC   rN   dict[str, Any]scopedict[str, Any] | Nonec                 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 )aF  
    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marksra   group   )get)rN   rf   ri   scope_valuegroup_indexchild_groupr@   r1   r1   r3   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 )aL  
    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))
    []
    datar[   fromfacetN)ro   rk   rH   )rN   rf   ri   rT   datasetfacet_datasetr1   r1   r3   get_datasets_for_scope  s   4
ru   	data_namestrusage_scopeScope | Nonec                 C  sD   t tt|d D ]}|d| }t| |}||v r|  S q
dS )aR  
    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'
    rj   N)reversedrangerb   ru   )rN   rv   rx   rV   rf   rT   r1   r1   r3   'get_definition_scope_for_data_referenceY  s   9
r|   r1   ri   c                 C  s   i }d}t | |p
i }|dg D ]T}|dddkreg ||R }|di dd}|durV|dd}|d	d}	|durV|	durVt| |	|}
|
durV|	|
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   rh   ra   Nri   rq   rr   r[   rp   rf   rj   )ro   rk   r|   rF   update)ri   rf   rO   rm   
mark_groupr@   group_scoperr   
facet_name
facet_datadefinition_scopechild_mappingr1   r1   r3   rF     s.   $
rF   scoped_datasettuple[str, Scope]rO   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', ())
    r1   )r   rO   r1   r1   r3   get_from_facet_mapping  s   r   vl_chart_namesdict[str, tuple[str, Scope]]c                 C  s<  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rmt| ||g ||R d}| D ]
\}}||| q]|d7 }q|D ]+}||r|dr|di d	d}	t| |	|}|durt|	|f|||<  nqoq|S )aH  
    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   rh   r[    _cellrq   rr   Nrp   ra   ri   r}   rj   _marks)	ro   rk   r   rG   items
setdefault
startswithendswithr|   )ri   r   rO   rf   rT   rm   r   r@   vl_chart_namerv   scoped_data_namer[   group_data_nameskvscoped_datar1   r1   r3   rG     sD   
rG   )NN)r'   r(   r)   r*   r+   r,   r-   r.   )r'   r6   r)   r*   r+   r,   r-   r7   )r   N)r'   r#   rV   rW   r+   r,   r-   rX   )rN   re   rf   r%   r-   rg   )rN   re   rf   r%   r-   rX   )rN   re   rv   rw   rx   r%   r-   ry   )r1   )ri   re   rf   r%   r-   r&   )r   r   rO   r&   r-   r   )
ri   re   r   rX   rO   r&   rf   r%   r-   r   )<
__future__r   typingr   r   r   altairr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   altair.utils._vegafusion_datar   r   altair.utils.schemapir   syscollections.abcr   version_infor"   typing_extensionsaltair.typingr#   altair.utils.corer$   tuplerW   r%   __annotations__dictrw   r&   rZ   r4   rC   ro   ru   r|   rF   r   rG   r1   r1   r1   r3   <module>   s^    `
$





T
>
3
BA
A%