o
    +&]iJ                     @  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		dEdFddZ4e		dEdGddZ4dEddZ4dHd d!Z5dId#d$Z6	dJdKd(d)Z7dLd.d/Z8dMd0d1Z9dNd6d7Z:dOdPd:d;Z;dQd?d@Z<	8dOdRdCdDZ=dS )S    )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   \/var/www/html/IGF-ODF-V3/venv/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#   Nonec                 C  sx   | j dthv r:t| dr|  | _ dS | jj }|dddddd}|d}d| dt	| d	| _ dS dS )
z0Assign a name to a chart if it doesn't have one.N_get_view_hash_namespec genericconcatrU   view_x)
namer   hasattrrW   	__class____name__lowerreplaceremovesuffixid)r'   
chart_typer1   r1   r3   _assign_chart_name   s   


rg   	list[Any]c                 C  sf   t | tt r
| jS t | tt r| jS t | tt r| jS t | tt r(| j	S dt
|  }t|)z(Get the subcharts for a composite chart.ztransformed_data accepts an instance of Chart, FacetChart, LayerChart, HConcatChart, VConcatChart, or ConcatChart
Received value of type: )r?   _chart_class_mappingr   layerr   hconcatr   vconcatr   r[   typerI   )r'   rS   r1   r1   r3   _get_subcharts   s   rn   iint	list[str]c                 C  s   |durt |nt  }t| tt tt fr%| j|vr#t|  | jgS g S t| }g }|D ]}t||t	| |dD ]}|
| q:q-|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
    N)ro   r+   )setr?   ri   r   r	   r^   rg   rn   rC   lenrH   )r'   ro   r+   	subchartsrM   subchartr^   r1   r1   r3   rC      s   
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marksrm   group   )get)rN   rw   rz   scope_valuegroup_indexchild_groupr@   r1   r1   r3   get_group_mark_for_scope   s    r   c                 C  s`   t | |pi }g }|dg D ]	}||d  q|di di dd}|r.|| |S )aM  
    Get the names of the datasets that are defined at a given scope.

    Parameters
    ----------
    vega_spec : dict
        Top-level 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)r   r|   rH   )rN   rw   rz   rT   datasetfacet_datasetr1   r1   r3   get_datasets_for_scope/  s   4
r   	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'
    r{   N)reversedrangers   r   )rN   r   r   ro   rw   rT   r1   r1   r3   'get_definition_scope_for_data_referenceq  s   9
r   r1   rz   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   ry   rm   Nrz   r   r   r^   r   rw   r{   )r   r|   r   rF   update)rz   rw   rO   r~   
mark_groupr@   group_scoper   
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   ry   r^   rY   _cellr   r   Nr   rm   rz   r   r{   _marks)	r   r|   r   rG   items
setdefault
startswithendswithr   )rz   r   rO   rw   rT   r~   r   r@   vl_chart_namer   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'   r#   r-   rV   )r'   r#   r-   rh   )r   N)r'   r#   ro   rp   r+   r,   r-   rq   )rN   rv   rw   r%   r-   rx   )rN   rv   rw   r%   r-   rq   )rN   rv   r   r   r   r%   r-   r   )r1   )rz   rv   rw   r%   r-   r&   )r   r   rO   r&   r-   r   )
rz   rv   r   rq   rO   r&   rw   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$   tuplerp   r%   __annotations__dictr   r&   ri   r4   rg   rn   rC   r   r   r   rF   r   rG   r1   r1   r1   r3   <module>   sb    `
$






S

.
3
BA
A%