
    g`G                    r   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r2d dl"Z"d dl#m$Z$ e"jJ                  dk\  rd 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       Z4e	 	 d	 	 	 	 	 	 	 dd       Z4ddZ4	 d	 	 	 	 	 	 	 ddZ5	 	 	 	 	 	 ddZ6d dZ7	 	 	 	 	 	 	 	 d!dZ8d"d#dZ9	 	 	 	 	 	 d$dZ:	 d"	 	 	 	 	 	 	 	 	 d%dZ;y)&    )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FacetMappingc                     y N chart	row_limitexcludes      S/var/www/openai/venv/lib/python3.12/site-packages/altair/utils/_transformed_data.pytransformed_datar0   C   s    
     c                     y r)   r*   r+   s      r/   r0   r0   K   s    
 r1   c                `   t               }t        | t              r#| j                  t        k(  r| j                         } | j                  d      } t        | d|      }t        j                  d      5  | j                  dddi	      }t        |      }d
d
d
       t              }t        |||      }g }	|D ]'  }
|
|v r|	j                  ||
          d}t        |       |j                   j#                  ||	|      \  }}t        | t        t$        f      r|sy
|d   S |S # 1 sw Y   xY w)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_s                 r/   r0   r0   S   s9   . 
	B%EJJ)$;  " JJDJ!E UAw7K 
	!	!,	/MM/59QMR	+I6 
0
 &i0M1)[-XO M!
(  !<=1CS/! " **33'	 4 KHa %%,-A; E 
0	/s   3!D$$D-c                   |t        |      n	t               }t        | t        t           t        t           f      rJ| j
                  |vr:| j
                  dt        hv rt        j                         | _        | j
                  gS g S t        | t        t                 r| j                  }nt        | t        t                 r| j                  }nat        | t        t                 r| j                  }n=t        | t        t                 r| j                  }ndt!        |        }t#        |      g }|D ]2  }t%        ||t'        |      z   |      D ]  }|j)                  |        4 |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: )ir.   )setr;   _chart_class_mappingr   r
   namer   	_get_namer   layerr   hconcatr   vconcatr   concattyperE   r?   lenrD   )r,   rS   r.   	subchartsrO   rI   subchartrV   s           r/   r?   r?      s?   4 &1c'lsuG$U+-A*-MN ::W$zzdI.."__.
JJ<I e1*=>I3LABI3LABI3K@AI++/;-9 
 S/!!#!H"8q3{3C/CWU""4( V " r1   c                    | }|D ]F  }d}d}|j                  dg       D ]%  }|j                  d      dk(  s||k(  r|} n|dz  }' | y|}H |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marksr\   group   )get)rJ   scoperb   scope_valuegroup_indexchild_groupr<   s          r/   get_group_mark_for_scoperi      sw    @ E IIgr*Dxx7*+-"&Kq  +   Lr1   c                
   t        | |      xs i }g }|j                  dg       D ]  }|j                  |d           |j                  di       j                  di       j                  dd      }|r|j                  |       |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))
    []
    datarV   fromfacetN)ri   rd   rD   )rJ   re   rb   rP   datasetfacet_datasets         r/   get_datasets_for_scoperp     s    h %Y6<"E H99VR(( ) IIfb)--gr:>>vtLM&Or1   c                    t        t        t        |      dz               D ]  }|d| }t        | |      }||v s|c S  y)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'
    rc   N)reversedranger]   rp   )rJ   	data_nameusage_scoperS   re   rP   s         r/   'get_definition_scope_for_data_referencerv   Y  sL    r eC,q012BQ))U; L	 3
 r1   c                   i }d}t        | |      xs i }|j                  dg       D ]  }|j                  dd      dk(  sg ||}|j                  di       j                  dd      }|@|j                  dd      }|j                  d	d      }	||	t        | |	|      }
|
	|	|
f|||f<   t        | |
      }|j	                  |       |dz  } |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   ra   r\   Nrb   rl   rm   rV   rk   re   rc   )ri   rd   rv   rB   update)rb   re   rK   rg   
mark_groupr<   group_scoperm   
facet_name
facet_datadefinition_scopechild_mappings               r/   rB   rB     s   H MK)%7=2Jw+88FD!W,/E/;/KHHVR(,,Wd;E "YYvt4
"YYvt4
)j.D'Nz5($ (3&,Bj+&=> .e;GM  /1K+ ,. r1   c                "    | |v r
||    } | |v r
| 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*   )scoped_datasetrK   s     r/   get_from_facet_mappingr     s&    8 M
)&~6 M
)r1   c                   i }d}t        | |      xs i }|j                  dg       D ]D  }|D ]a  }|j                  dd      | dk(  s|j                  di       j                  dd      j                  d	d      }	|	|f}
t        |
|      ||<    n |j                  dd      }|j                  d
d      dk(  rDt        | ||g ||      }|j	                         D ]  \  }}|j                  ||        |dz  }|D ]k  }|j                  |      s|j                  d      s'|j                  di       j                  d	d      }	t        | |	|      }|Yt        |	|f|      ||<    D G |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   ra   rV    _cellrl   rm   Nrk   r\   rb   rx   rc   _marks)	ri   rd   r   rC   items
setdefault
startswithendswithrv   )rb   vl_chart_namesrK   re   rP   rg   rz   r<   vl_chart_namert   scoped_data_namerV   group_data_nameskvscoped_datas                   r/   rC   rC     s   2 HK)%7=2Jw++Mxx#-'>> HHVR044WdCGGPTU	$-u#5 *@$m+'  , xx#88FB7*:~}<Qe<Q[<Q  )..01##Aq) 11K!/??=1dmmH6M $ 4 8 8 FI"Iy%#K #.2H&4m3/  "0' ,> Or1   )NN)r,   zChart | FacetChartr-   
int | Noner.   Iterable[str] | NonereturnzDataFrameLike | None)r,   z6LayerChart | HConcatChart | VConcatChart | ConcatChartr-   r   r.   r   r   zlist[DataFrameLike])r   N)r,   r$   rS   intr.   r   r   	list[str])rJ   dict[str, Any]re   r&   r   zdict[str, Any] | None)rJ   r   re   r&   r   r   )rJ   r   rt   strru   r&   r   zScope | None)r*   )rb   r   re   r&   r   r'   )r   tuple[str, Scope]rK   r'   r   r   )
rb   r   r   r   rK   r'   re   r&   r   zdict[str, tuple[str, Scope]])<
__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%   tupler   r&   __annotations__dictr   r'   rU   r0   r?   ri   rp   rv   rB   r   rC   r*   r1   r/   <module>r      sT   " / /     0 O +(
7"$/'/c?y "uS%Z0%U
2CCDi D 
 .	:+13HI<!46LM<!46LM.	: " 
 !$( " 	 
 
 !$(A " 	 
Fh CG;;;+?;;|00&+00f?D>>*-><A>>B>B%6BJ 	;;;  ; 	;
 ";r1   