files: import warnings
  
  import hashlib
  import io
  import json
  import jsonschema
  import pandas as pd
  from toolz.curried import pipe as _pipe
  
  from .schema import core, channels, mixins, Undefined, SCHEMA_URL
  
  from .data import data_transformers
  from ... import utils, expr
  from .display import renderers, VEGALITE_VERSION, VEGAEMBED_VERSION, VEGA_VERSION
  from .theme import themes
  
  
  # ------------------------------------------------------------------------
  # Data Utilities
  def _dataset_name(values):
      """Generate a unique hash of the data
  
      Parameters
      ----------
      values : list or dict
          A list/dict representation of data values.
  
      Returns
      -------
      name : string
          A unique name generated from the hash of the values.
      """
      if isinstance(values, core.InlineDataset):
          values = values.to_dict()
      if values == [{}]:
          return "empty"
      values_json = json.dumps(values, sort_keys=True)
      hsh = hashlib.md5(values_json.encode()).hexdigest()
      return "data-" + hsh
  
  
  def _consolidate_data(data, context):
      """If data is specified inline, then move it to context['datasets']
  
      This function will modify context in-place, and return a new version of data
      """
      values = Undefined
      kwds = {}
  
      if isinstance(data, core.InlineData):
          if data.name is Undefined and data.values is not Undefined:
              if isinstance(data.values, core.InlineDataset):
                  values = data.to_dict()["values"]
              else:
                  values = data.values
              kwds = {"format": data.format}
  
      elif isinstance(data, dict):
          if "name" not in data and "values" in data:
              values = data["values"]
              kwds = {k: v for k, v in data.items() if k != "values"}
  
      if values is not Undefined:
          name = _dataset_name(values)
          data = core.NamedData(name=name, **kwds)
          context.setdefault("datasets", {})[name] = values
  
      return data
  
  
  def _prepare_data(data, context=None):
      """Convert input data to data for use within schema
  
      Parameters
      ----------
      data :
          The input dataset in the form of a DataFrame, dictionary, altair data
          object, or other type that is recognized by the data transformers.
      context : dict (optional)
          The to_dict context in which the data is being prepared. This is used
          to keep track of information that needs to be passed up and down the
          recursive serialization routine, such as global named datasets.
      """
      if data is Undefined:
          return data
  
      # convert dataframes  or objects with __geo_interface__ to dict
      if isinstance(data, pd.DataFrame) or hasattr(data, "__geo_interface__"):
          data = _pipe(data, data_transformers.get())
  
      # convert string input to a URLData
      if isinstance(data, str):
          data = core.UrlData(data)
  
      # consolidate inline data to top-level datasets
      if context is not None and data_transformers.consolidate_datasets:
          data = _consolidate_data(data, context)
  
      # if data is still not a recognized type, then return
      if not isinstance(data, (dict, core.Data)):
          warnings.warn("data of type {} not recognized".format(type(data)))
  
      return data
  
  
  # ------------------------------------------------------------------------
  # Aliases & specializations
  Bin = core.BinParams
  
  
  @utils.use_signature(core.LookupData)
  class LookupData(core.LookupData):
      def to_dict(self, *args, **kwargs):
          """Convert the chart to a dictionary suitable for JSON export."""
          copy = self.copy(deep=False)
          copy.data = _prepare_data(copy.data, kwargs.get("context"))
          return super(LookupData, copy).to_dict(*args, **kwargs)
  
  
  @utils.use_signature(core.FacetMapping)
  class FacetMapping(core.FacetMapping):
      _class_is_valid_at_instantiation = False
  
      def to_dict(self, *args, **kwargs):
          copy = self.copy(deep=False)
          context = kwargs.get("context", {})
          data = context.get("data", None)
          if isinstance(self.row, str):
              copy.row = core.FacetFieldDef(**utils.parse_shorthand(self.row, data))
          if isinstance(self.column, str):
              copy.column = core.FacetFieldDef(**utils.parse_shorthand(self.column, data))
          return super(FacetMapping, copy).to_dict(*args, **kwargs)
  
  
  # ------------------------------------------------------------------------
  # Encoding will contain channel objects that aren't valid at instantiation
  core.FacetedEncoding._class_is_valid_at_instantiation = False
  
  # ------------------------------------------------------------------------
  # These are parameters that are valid at the top level, but are not valid
  # for specs that are within a composite chart
  # (layer, hconcat, vconcat, facet, repeat)
  TOPLEVEL_ONLY_KEYS = {"background", "config", "autosize", "padding", "$schema"}
  
  
  def _get_channels_mapping():
      mapping = {}
      for attr in dir(channels):
          cls = getattr(channels, attr)
          if isinstance(cls, type) and issubclass(cls, core.SchemaBase):
              mapping[cls] = attr.replace("Value", "").lower()
      return mapping
  
  
  # -------------------------------------------------------------------------
  # Tools for working with parameters
  class Parameter(expr.core.OperatorMixin, object):
      """A Parameter object"""
  
      _counter = 0
  
      @classmethod
      def _get_name(cls):
          cls._counter += 1
          return "parameter{:03d}".format(cls._counter)
  
      def __init__(self, name):
          if name is None:
              name = self._get_name()
          self.name = name
  
      def to_dict(self):
          if self.param_type == "variable":
              if self.param.expr is Undefined:
                  return {"expr": self.name}
              else:
                  return {"expr": repr(self.param.expr)}
          elif self.param_type == "selection":
              return {
                  "param": self.name.to_dict()
                  if hasattr(self.name, "to_dict")
                  else self.name
              }
  
      def __invert__(self):
          if self.param_type == "selection":
              return SelectionPredicateComposition({"not": {"param": self.name}})
          else:
              return expr.core.OperatorMixin.__invert__(self)
  
      def __and__(self, other):
          if self.param_type == "selection":
              if isinstance(other, Parameter):
                  other = {"param": other.name}
              return SelectionPredicateComposition({"and": [{"param": self.name}, other]})
          else:
              return expr.core.OperatorMixin.__and__(self, other)
  
      def __or__(self, other):
          if self.param_type == "selection":
              if isinstance(other, Parameter):
                  other = {"param": other.name}
              return SelectionPredicateComposition({"or": [{"param": self.name}, other]})
          else:
              return expr.core.OperatorMixin.__or__(self, other)
  
      def __repr__(self):
          return "Parameter({0!r}, {1})".format(self.name, self.param)
  
      def _to_expr(self):
          if not hasattr(self.param, "expr") or self.param.expr is Undefined:
              return self.name
          else:
              return self.param.expr
  
      def _from_expr(self, expr):
          return parameter(expr=expr)
  
      def __getattr__(self, field_name):
          if field_name.startswith("__") and field_name.endswith("__"):
              raise AttributeError(field_name)
          _attrexpr = expr.core.GetAttrExpression(self.name, field_name)
          # If self is a SelectionParameter and field_name is in its
          # fields or encodings list, then we want to return an expression.
          if check_fields_and_encodings(self, field_name):
              return SelectionExpression(_attrexpr)
          return expr.core.GetAttrExpression(self.name, field_name)
  
  
  # Enables use of ~, &, | with compositions of selection objects.
  class SelectionPredicateComposition(core.PredicateComposition):
      def __invert__(self):
          return SelectionPredicateComposition({"not": self.to_dict()})
  
      def __and__(self, other):
          return SelectionPredicateComposition({"and": [self.to_dict(), other.to_dict()]})
  
      def __or__(self, other):
          return SelectionPredicateComposition({"or": [self.to_dict(), other.to_dict()]})
  
  
  class SelectionExpression(expr.core.OperatorMixin, object):
      def __init__(self, expr):
          self.expr = expr
  
      def to_dict(self):
          return {"expr": repr(self.expr)}
  
      def _to_expr(self):
          return repr(self.expr)
  
      def _from_expr(self, expr):
          return SelectionExpression(expr=expr)
  
  
  def check_fields_and_encodings(parameter, field_name):
      for prop in ["fields", "encodings"]:
          try:
              if field_name in getattr(parameter.param.select, prop):
                  return True
          except (AttributeError, TypeError):
              pass
  
      return False
  
  
  # ------------------------------------------------------------------------
  # Top-Level Functions
  
  
  def value(value, **kwargs):
      """Specify a value for use in an encoding"""
      return dict(value=value, **kwargs)
  
  
  def parameter(name=None, select=None, **kwds):
      """Create a named parameter.
  
      Parameters
      ----------
      name : string (optional)
          The name of the parameter. If not specified, a unique name will be
          created.
      **kwds :
          additional keywords will be used to construct a parameter.  If 'select'
          is among the keywords, then a SelectionParameter will be created.
          Otherwise, a VariableParameter will be created.
  
      Returns
      -------
      parameter: Parameter
          The parameter object that can be used in chart creation.
      """
  
      parameter = Parameter(name)
  
      if "empty" in kwds:
          parameter.empty = kwds.pop("empty")
          if parameter.empty == "none":
              warnings.warn(
                  """The value of 'empty' should be True or False.""",
                  DeprecationWarning,
              )
              parameter.empty = False
          elif parameter.empty == "all":
              warnings.warn(
                  """The value of 'empty' should be True or False.""",
                  DeprecationWarning,
              )
              parameter.empty = True
          elif (parameter.empty is False) or (parameter.empty is True):
              pass
          else:
              raise ValueError("The value of 'empty' should be True or False.")
  
      if "init" in kwds:
          warnings.warn(
              """Use 'value' instead of 'init'.""",
              DeprecationWarning,
          )
          if "value" not in kwds:
              kwds["value"] = kwds.pop("init")
          else:
              # If both 'value' and 'init' are set, we ignore 'init'.
              kwds.pop("init")
  
      if select is None:
          parameter.param = core.VariableParameter(name=parameter.name, **kwds)
          parameter.param_type = "variable"
      else:
          parameter.param = core.SelectionParameter(
              name=parameter.name, select=select, **kwds
          )
          parameter.param_type = "selection"
  
      return parameter
  
  
  # TODO: Update the docstring
  def selection(type=Undefined, **kwds):
      """Create a selection.
  
      Parameters
      ----------
      type : string
          The type of the selection: either "interval" or "point"]
      **kwds :
          additional keywords to control the selection.
  
      Returns
      -------
      Parameter
          The Parameter object that can be used in chart creation.
      """
  
      # We separate out the parameter keywords from the selection keywords
      param_kwds = {}
  
      for kwd in {"name", "value", "bind", "empty", "init"}:
          if kwd in kwds:
              param_kwds[kwd] = kwds.pop(kwd)
  
      if type == "interval":
          select = core.IntervalSelectionConfig(type=type, **kwds)
      elif type == "point":
          select = core.PointSelectionConfig(type=type, **kwds)
      elif type in ["single", "multi"]:
          select = core.PointSelectionConfig(type="point", **kwds)
          warnings.warn(
              """The types 'single' and 'multi' are now
          combined and should be specified using "type='point'".""",
              DeprecationWarning,
          )
      else:
          raise ValueError("""'type' must be 'point' or 'interval'""")
  
      return parameter(select=select, **param_kwds)
  
  
  @utils.use_signature(core.IntervalSelectionConfig)
  def selection_interval(**kwargs):
      """Create a selection parameter with type='interval'"""
      return selection(type="interval", **kwargs)
  
  
  @utils.use_signature(core.PointSelectionConfig)
  def selection_point(**kwargs):
      """Create a selection with type='point'"""
      return selection(type="point", **kwargs)
  
  
  @utils.use_signature(core.PointSelectionConfig)
  def selection_multi(**kwargs):
      warnings.warn(
          """'selection_multi' is deprecated.  Use 'selection_point'""",
          DeprecationWarning,
      )
      return selection(type="point", **kwargs)
  
  
  @utils.use_signature(core.PointSelectionConfig)
  def selection_single(**kwargs):
      warnings.warn(
          """'selection_single' is deprecated.  Use 'selection_point'""",
          DeprecationWarning,
      )
      return selection(type="point", **kwargs)
  
  
  @utils.use_signature(core.Binding)
  def binding(input, **kwargs):
      """A generic binding"""
      return core.Binding(input=input, **kwargs)
  
  
  @utils.use_signature(core.BindCheckbox)
  def binding_checkbox(**kwargs):
      """A checkbox binding"""
      return core.BindCheckbox(input="checkbox", **kwargs)
  
  
  @utils.use_signature(core.BindRadioSelect)
  def binding_radio(**kwargs):
      """A radio button binding"""
      return core.BindRadioSelect(input="radio", **kwargs)
  
  
  @utils.use_signature(core.BindRadioSelect)
  def binding_select(**kwargs):
      """A select binding"""
      return core.BindRadioSelect(input="select", **kwargs)
  
  
  @utils.use_signature(core.BindRange)
  def binding_range(**kwargs):
      """A range binding"""
      return core.BindRange(input="range", **kwargs)
  
  
  # TODO: update the docstring
  def condition(predicate, if_true, if_false, **kwargs):
      """A conditional attribute or encoding
  
      Parameters
      ----------
      predicate: Selection, PredicateComposition, expr.Expression, dict, or string
          the selection predicate or test predicate for the condition.
          if a string is passed, it will be treated as a test operand.
      if_true:
          the spec or object to use if the selection predicate is true
      if_false:
          the spec or object to use if the selection predicate is false
      **kwargs:
          additional keyword args are added to the resulting dict
  
      Returns
      -------
      spec: dict or VegaLiteSchema
          the spec that describes the condition
      """
      test_predicates = (str, expr.Expression, core.PredicateComposition)
  
      if isinstance(predicate, Parameter):
          if predicate.param_type == "selection" or predicate.param.expr is Undefined:
              condition = {"param": predicate.name}
              if "empty" in kwargs:
                  condition["empty"] = kwargs.pop("empty")
              elif isinstance(predicate.empty, bool):
                  condition["empty"] = predicate.empty
          else:
              condition = {"test": predicate.param.expr}
      elif isinstance(predicate, test_predicates):
          condition = {"test": predicate}
      elif isinstance(predicate, dict):
          condition = predicate
      else:
          raise NotImplementedError(
              "condition predicate of type {}" "".format(type(predicate))
          )
  
      if isinstance(if_true, core.SchemaBase):
          # convert to dict for now; the from_dict call below will wrap this
          # dict in the appropriate schema
          if_true = if_true.to_dict()
      elif isinstance(if_true, str):
          if_true = {"shorthand": if_true}
          if_true.update(kwargs)
      condition.update(if_true)
  
      if isinstance(if_false, core.SchemaBase):
          # For the selection, the channel definitions all allow selections
          # already. So use this SchemaBase wrapper if possible.
          selection = if_false.copy()
          selection.condition = condition
      elif isinstance(if_false, str):
          selection = {"condition": condition, "shorthand": if_false}
          selection.update(kwargs)
      else:
          selection = dict(condition=condition, **if_false)
  
      return selection
  
  
  # --------------------------------------------------------------------
  # Top-level objects
  
  
  class TopLevelMixin(mixins.ConfigMethodMixin):
      """Mixin for top-level chart objects such as Chart, LayeredChart, etc."""
  
      _class_is_valid_at_instantiation = False
  
      def to_dict(self, *args, **kwargs):
          """Convert the chart to a dictionary suitable for JSON export"""
          # We make use of three context markers:
          # - 'data' points to the data that should be referenced for column type
          #   inference.
          # - 'top_level' is a boolean flag that is assumed to be true; if it's
          #   true then a "$schema" arg is added to the dict.
          # - 'datasets' is a dict of named datasets that should be inserted
          #   in the top-level object
  
          # note: not a deep copy because we want datasets and data arguments to
          # be passed by reference
          context = kwargs.get("context", {}).copy()
          context.setdefault("datasets", {})
          is_top_level = context.get("top_level", True)
  
          copy = self.copy(deep=False)
          original_data = getattr(copy, "data", Undefined)
          copy.data = _prepare_data(original_data, context)
  
          if original_data is not Undefined:
              context["data"] = original_data
  
          # remaining to_dict calls are not at top level
          context["top_level"] = False
          kwargs["context"] = context
  
          try:
              dct = super(TopLevelMixin, copy).to_dict(*args, **kwargs)
          except jsonschema.ValidationError:
              dct = None
  
          # If we hit an error, then re-convert with validate='deep' to get
          # a more useful traceback. We don't do this by default because it's
          # much slower in the case that there are no errors.
          if dct is None:
              kwargs["validate"] = "deep"
              dct = super(TopLevelMixin, copy).to_dict(*args, **kwargs)
  
          # TODO: following entries are added after validation. Should they be validated?
          if is_top_level:
              # since this is top-level we add $schema if it's missing
              if "$schema" not in dct:
                  dct["$schema"] = SCHEMA_URL
  
              # apply theme from theme registry
              the_theme = themes.get()
              dct = utils.update_nested(the_theme(), dct, copy=True)
  
              # update datasets
              if context["datasets"]:
                  dct.setdefault("datasets", {}).update(context["datasets"])
  
          return dct
  
      def to_html(
          self,
          base_url="https://cdn.jsdelivr.net/npm/",
          output_div="vis",
          embed_options=None,
          json_kwds=None,
          fullhtml=True,
          requirejs=False,
      ):
          return utils.spec_to_html(
              self.to_dict(),
              mode="vega-lite",
              vegalite_version=VEGALITE_VERSION,
              vegaembed_version=VEGAEMBED_VERSION,
              vega_version=VEGA_VERSION,
              base_url=base_url,
              output_div=output_div,
              embed_options=embed_options,
              json_kwds=json_kwds,
              fullhtml=fullhtml,
              requirejs=requirejs,
          )
  
      def save(
          self,
          fp,
          format=None,
          override_data_transformer=True,
          scale_factor=1.0,
          vegalite_version=VEGALITE_VERSION,
          vega_version=VEGA_VERSION,
          vegaembed_version=VEGAEMBED_VERSION,
          **kwargs,
      ):
          """Save a chart to file in a variety of formats
  
          Supported formats are json, html, png, svg, pdf; the last three require
          the altair_saver package to be installed.
  
          Parameters
          ----------
          fp : string filename or file-like object
              file in which to write the chart.
          format : string (optional)
              the format to write: one of ['json', 'html', 'png', 'svg', 'pdf'].
              If not specified, the format will be determined from the filename.
          override_data_transformer : boolean (optional)
              If True (default), then the save action will be done with
              the MaxRowsError disabled. If False, then do not change the data
              transformer.
          scale_factor : float
              For svg or png formats, scale the image by this factor when saving.
              This can be used to control the size or resolution of the output.
              Default is 1.0
          **kwargs :
              Additional keyword arguments are passed to the output method
              associated with the specified format.
  
          """
          from ...utils.save import save
  
          kwds = dict(
              chart=self,
              fp=fp,
              format=format,
              scale_factor=scale_factor,
              vegalite_version=vegalite_version,
              vega_version=vega_version,
              vegaembed_version=vegaembed_version,
              **kwargs,
          )
  
          # By default we override the data transformer. This makes it so
          # that save() will succeed even for large datasets that would
          # normally trigger a MaxRowsError
          if override_data_transformer:
              with data_transformers.disable_max_rows():
                  result = save(**kwds)
          else:
              result = save(**kwds)
          return result
  
      # Fallback for when rendering fails; the full repr is too long to be
      # useful in nearly all cases.
      def __repr__(self):
          return "alt.{}(...)".format(self.__class__.__name__)
  
      # Layering and stacking
      def __add__(self, other):
          if not isinstance(other, TopLevelMixin):
              raise ValueError("Only Chart objects can be layered.")
          return layer(self, other)
  
      def __and__(self, other):
          if not isinstance(other, TopLevelMixin):
              raise ValueError("Only Chart objects can be concatenated.")
          return vconcat(self, other)
  
      def __or__(self, other):
          if not isinstance(other, TopLevelMixin):
              raise ValueError("Only Chart objects can be concatenated.")
          return hconcat(self, other)
  
      def repeat(
          self,
          repeat=Undefined,
          row=Undefined,
          column=Undefined,
          layer=Undefined,
          columns=Undefined,
          **kwargs,
      ):
          """Return a RepeatChart built from the chart
  
          Fields within the chart can be set to correspond to the row or
          column using `alt.repeat('row')` and `alt.repeat('column')`.
  
          Parameters
          ----------
          repeat : list
              a list of data column names to be repeated. This cannot be
              used along with the ``row``, ``column`` or ``layer`` argument.
          row : list
              a list of data column names to be mapped to the row facet
          column : list
              a list of data column names to be mapped to the column facet
          layer : list
              a list of data column names to be layered. This cannot be
              used along with the ``row``, ``column`` or ``repeat`` argument.
          columns : int
              the maximum number of columns before wrapping. Only referenced
              if ``repeat`` is specified.
          **kwargs :
              additional keywords passed to RepeatChart.
  
          Returns
          -------
          chart : RepeatChart
              a repeated chart.
          """
          repeat_specified = repeat is not Undefined
          rowcol_specified = row is not Undefined or column is not Undefined
          layer_specified = layer is not Undefined
  
          if repeat_specified and rowcol_specified:
              raise ValueError(
                  "repeat argument cannot be combined with row/column argument."
              )
          elif repeat_specified and layer_specified:
              raise ValueError("repeat argument cannot be combined with layer argument.")
          elif layer_specified and rowcol_specified:
              raise ValueError(
                  "layer argument cannot be combined with row/column argument."
              )
  
          if repeat_specified:
              repeat = repeat
          elif layer_specified:
              repeat = core.LayerRepeatMapping(layer=layer)
          else:
              repeat = core.RepeatMapping(row=row, column=column)
  
          return RepeatChart(spec=self, repeat=repeat, columns=columns, **kwargs)
  
      def properties(self, **kwargs):
          """Set top-level properties of the Chart.
  
          Argument names and types are the same as class initialization.
          """
          copy = self.copy(deep=False)
          for key, val in kwargs.items():
              if key == "selection" and isinstance(val, Parameter):
                  # TODO: Can this be removed
                  # For backward compatibility with old selection interface.
                  setattr(copy, key, {val.name: val.selection})
              else:
                  # Don't validate data, because it hasn't been processed.
                  if key != "data":
                      self.validate_property(key, val)
                  setattr(copy, key, val)
          return copy
  
      def project(
          self,
          type="mercator",
          center=Undefined,
          clipAngle=Undefined,
          clipExtent=Undefined,
          coefficient=Undefined,
          distance=Undefined,
          fraction=Undefined,
          lobes=Undefined,
          parallel=Undefined,
          precision=Undefined,
          radius=Undefined,
          ratio=Undefined,
          reflectX=Undefined,
          reflectY=Undefined,
          rotate=Undefined,
          scale=Undefined,
          spacing=Undefined,
          tilt=Undefined,
          translate=Undefined,
          **kwds,
      ):
          """Add a geographic projection to the chart.
  
          This is generally used either with ``mark_geoshape`` or with the
          ``latitude``/``longitude`` encodings.
  
          Available projection types are
          ['albers', 'albersUsa', 'azimuthalEqualArea', 'azimuthalEquidistant',
          'conicConformal', 'conicEqualArea', 'conicEquidistant', 'equalEarth', 'equirectangular',
          'gnomonic', 'identity', 'mercator', 'orthographic', 'stereographic', 'transverseMercator']
  
          Attributes
          ----------
          type : ProjectionType
              The cartographic projection to use. This value is case-insensitive, for example
              `"albers"` and `"Albers"` indicate the same projection type. You can find all valid
              projection types [in the
              documentation](https://vega.github.io/vega-lite/docs/projection.html#projection-types).
  
              **Default value:** `mercator`
          center : List(float)
              Sets the projection’s center to the specified center, a two-element array of
              longitude and latitude in degrees.
  
              **Default value:** `[0, 0]`
          clipAngle : float
              Sets the projection’s clipping circle radius to the specified angle in degrees. If
              `null`, switches to [antimeridian](http://bl.ocks.org/mbostock/3788999) cutting
              rather than small-circle clipping.
          clipExtent : List(List(float))
              Sets the projection’s viewport clip extent to the specified bounds in pixels. The
              extent bounds are specified as an array `[[x0, y0], [x1, y1]]`, where `x0` is the
              left-side of the viewport, `y0` is the top, `x1` is the right and `y1` is the
              bottom. If `null`, no viewport clipping is performed.
          coefficient : float
  
          distance : float
  
          fraction : float
  
          lobes : float
  
          parallel : float
  
          precision : Mapping(required=[length])
              Sets the threshold for the projection’s [adaptive
              resampling](http://bl.ocks.org/mbostock/3795544) to the specified value in pixels.
              This value corresponds to the [Douglas–Peucker
              distance](http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm).
               If precision is not specified, returns the projection’s current resampling
              precision which defaults to `√0.5 ≅ 0.70710…`.
          radius : float
  
          ratio : float
  
          reflectX : boolean
  
          reflectY : boolean
  
          rotate : List(float)
              Sets the projection’s three-axis rotation to the specified angles, which must be a
              two- or three-element array of numbers [`lambda`, `phi`, `gamma`] specifying the
              rotation angles in degrees about each spherical axis. (These correspond to yaw,
              pitch and roll.)
  
              **Default value:** `[0, 0, 0]`
          scale : float
              Sets the projection's scale (zoom) value, overriding automatic fitting.
  
          spacing : float
  
          tilt : float
  
          translate : List(float)
              Sets the projection's translation (pan) value, overriding automatic fitting.
  
          """
          projection = core.Projection(
              center=center,
              clipAngle=clipAngle,
              clipExtent=clipExtent,
              coefficient=coefficient,
              distance=distance,
              fraction=fraction,
              lobes=lobes,
              parallel=parallel,
              precision=precision,
              radius=radius,
              ratio=ratio,
              reflectX=reflectX,
              reflectY=reflectY,
              rotate=rotate,
              scale=scale,
              spacing=spacing,
              tilt=tilt,
              translate=translate,
              type=type,
              **kwds,
          )
          return self.properties(projection=projection)
  
      def _add_transform(self, *transforms):
          """Copy the chart and add specified transforms to chart.transform"""
          copy = self.copy(deep=["transform"])
          if copy.transform is Undefined:
              copy.transform = []
          copy.transform.extend(transforms)
          return copy
  
      def transform_aggregate(self, aggregate=Undefined, groupby=Undefined, **kwds):
          """
          Add an AggregateTransform to the schema.
  
          Parameters
          ----------
          aggregate : List(:class:`AggregatedFieldDef`)
              Array of objects that define fields to aggregate.
          groupby : List(string)
              The data fields to group by. If not specified, a single group containing all data
              objects will be used.
          **kwds :
              additional keywords are converted to aggregates using standard
              shorthand parsing.
  
          Returns
          -------
          self : Chart object
              returns chart to allow for chaining
  
          Examples
          --------
          The aggregate transform allows you to specify transforms directly using
          the same shorthand syntax as used in encodings:
  
          >>> import altair as alt
          >>> chart1 = alt.Chart().transform_aggregate(
          ...     mean_acc='mean(Acceleration)',
          ...     groupby=['Origin']
          ... )
          >>> print(chart1.transform[0].to_json())  # doctest: +NORMALIZE_WHITESPACE
          {
            "aggregate": [
              {
                "as": "mean_acc",
                "field": "Acceleration",
                "op": "mean"
              }
            ],
            "groupby": [
              "Origin"
            ]
          }
  
          It also supports including AggregatedFieldDef instances or dicts directly,
          so you can create the above transform like this:
  
          >>> chart2 = alt.Chart().transform_aggregate(
          ...     [alt.AggregatedFieldDef(field='Acceleration', op='mean',
          ...                             **{'as': 'mean_acc'})],
          ...     groupby=['Origin']
          ... )
          >>> chart2.transform == chart1.transform
          True
  
          See Also
          --------
          alt.AggregateTransform : underlying transform object
  
          """
          if aggregate is Undefined:
              aggregate = []
          for key, val in kwds.items():
              parsed = utils.parse_shorthand(val)
              dct = {
                  "as": key,
                  "field": parsed.get("field", Undefined),
                  "op": parsed.get("aggregate", Undefined),
              }
              aggregate.append(core.AggregatedFieldDef(**dct))
          return self._add_transform(
              core.AggregateTransform(aggregate=aggregate, groupby=groupby)
          )
  
      def transform_bin(self, as_=Undefined, field=Undefined, bin=True, **kwargs):
          """
          Add a BinTransform to the schema.
  
          Parameters
          ----------
          as_ : anyOf(string, List(string))
              The output fields at which to write the start and end bin values.
          bin : anyOf(boolean, :class:`BinParams`)
              An object indicating bin properties, or simply ``true`` for using default bin
              parameters.
          field : string
              The data field to bin.
  
          Returns
          -------
          self : Chart object
              returns chart to allow for chaining
  
          Examples
          --------
          >>> import altair as alt
          >>> chart = alt.Chart().transform_bin("x_binned", "x")
          >>> chart.transform[0]
          BinTransform({
            as: 'x_binned',
            bin: True,
            field: 'x'
          })
  
          >>> chart = alt.Chart().transform_bin("x_binned", "x",
          ...                                   bin=alt.Bin(maxbins=10))
          >>> chart.transform[0]
          BinTransform({
            as: 'x_binned',
            bin: BinParams({
              maxbins: 10
            }),
            field: 'x'
          })
  
          See Also
          --------
          alt.BinTransform : underlying transform object
  
          """
          if as_ is not Undefined:
              if "as" in kwargs:
                  raise ValueError(
                      "transform_bin: both 'as_' and 'as' passed as arguments."
                  )
              kwargs["as"] = as_
          kwargs["bin"] = bin
          kwargs["field"] = field
          return self._add_transform(core.BinTransform(**kwargs))
  
      def transform_calculate(self, as_=Undefined, calculate=Undefined, **kwargs):
          """
          Add a CalculateTransform to the schema.
  
          Parameters
          ----------
          as_ : string
              The field for storing the computed formula value.
          calculate : string or alt.expr expression
              A `expression <https://vega.github.io/vega-lite/docs/types.html#expression>`__
              string. Use the variable ``datum`` to refer to the current data object.
          **kwargs
              transforms can also be passed by keyword argument; see Examples
  
          Returns
          -------
          self : Chart object
              returns chart to allow for chaining
  
          Examples
          --------
          >>> import altair as alt
          >>> from altair import datum, expr
  
          >>> chart = alt.Chart().transform_calculate(y = 2 * expr.sin(datum.x))
          >>> chart.transform[0]
          CalculateTransform({
            as: 'y',
            calculate: (2 * sin(datum.x))
          })
  
          It's also possible to pass the ``CalculateTransform`` arguments directly:
  
          >>> kwds = {'as': 'y', 'calculate': '2 * sin(datum.x)'}
          >>> chart = alt.Chart().transform_calculate(**kwds)
          >>> chart.transform[0]
          CalculateTransform({
            as: 'y',
            calculate: '2 * sin(datum.x)'
          })
  
          As the first form is easier to write and understand, that is the
          recommended method.
  
          See Also
          --------
          alt.CalculateTransform : underlying transform object
          """
          if as_ is Undefined:
              as_ = kwargs.pop("as", Undefined)
          elif "as" in kwargs:
              raise ValueError(
                  "transform_calculate: both 'as_' and '
  
  [... the rest is omitted ...]
files: altair/vegalite/v5/api.py
files: import warnings
  
  import hashlib
  import io
  import json
  import jsonschema
  import pandas as pd
  from toolz.curried import pipe as _pipe
  
  from .schema import core, channels, mixins, Undefined, SCHEMA_URL
  
  from .data import data_transformers
  from ... import utils, expr
  from .display import renderers, VEGALITE_VERSION, VEGAEMBED_VERSION, VEGA_VERSION
  from .theme import themes
  
  
  # ------------------------------------------------------------------------
  # Data Utilities
  def _dataset_name(values):
      """Generate a unique hash of the data
  
      Parameters
      ----------
      values : list or dict
          A list/dict representation of data values.
  
      Returns
      -------
      name : string
          A unique name generated from the hash of the values.
      """
      if isinstance(values, core.InlineDataset):
          values = values.to_dict()
      if values == [{}]:
          return "empty"
      values_json = json.dumps(values, sort_keys=True)
      hsh = hashlib.md5(values_json.encode()).hexdigest()
      return "data-" + hsh
  
  
  def _consolidate_data(data, context):
      """If data is specified inline, then move it to context['datasets']
  
      This function will modify context in-place, and return a new version of data
      """
      values = Undefined
      kwds = {}
  
      if isinstance(data, core.InlineData):
          if data.name is Undefined and data.values is not Undefined:
              if isinstance(data.values, core.InlineDataset):
                  values = data.to_dict()["values"]
              else:
                  values = data.values
              kwds = {"format": data.format}
  
      elif isinstance(data, dict):
          if "name" not in data and "values" in data:
              values = data["values"]
              kwds = {k: v for k, v in data.items() if k != "values"}
  
      if values is not Undefined:
          name = _dataset_name(values)
          data = core.NamedData(name=name, **kwds)
          context.setdefault("datasets", {})[name] = values
  
      return data
  
  
  def _prepare_data(data, context=None):
      """Convert input data to data for use within schema
  
      Parameters
      ----------
      data :
          The input dataset in the form of a DataFrame, dictionary, altair data
          object, or other type that is recognized by the data transformers.
      context : dict (optional)
          The to_dict context in which the data is being prepared. This is used
          to keep track of information that needs to be passed up and down the
          recursive serialization routine, such as global named datasets.
      """
      if data is Undefined:
          return data
  
      # convert dataframes  or objects with __geo_interface__ to dict
      if isinstance(data, pd.DataFrame) or hasattr(data, "__geo_interface__"):
          data = _pipe(data, data_transformers.get())
  
      # convert string input to a URLData
      if isinstance(data, str):
          data = core.UrlData(data)
  
      # consolidate inline data to top-level datasets
      if context is not None and data_transformers.consolidate_datasets:
          data = _consolidate_data(data, context)
  
      # if data is still not a recognized type, then return
      if not isinstance(data, (dict, core.Data)):
          warnings.warn("data of type {} not recognized".format(type(data)))
  
      return data
  
  
  # ------------------------------------------------------------------------
  # Aliases & specializations
  Bin = core.BinParams
  
  
  @utils.use_signature(core.LookupData)
  class LookupData(core.LookupData):
      def to_dict(self, *args, **kwargs):
          """Convert the chart to a dictionary suitable for JSON export."""
          copy = self.copy(deep=False)
          copy.data = _prepare_data(copy.data, kwargs.get("context"))
          return super(LookupData, copy).to_dict(*args, **kwargs)
  
  
  @utils.use_signature(core.FacetMapping)
  class FacetMapping(core.FacetMapping):
      _class_is_valid_at_instantiation = False
  
      def to_dict(self, *args, **kwargs):
          copy = self.copy(deep=False)
          context = kwargs.get("context", {})
          data = context.get("data", None)
          if isinstance(self.row, str):
              copy.row = core.FacetFieldDef(**utils.parse_shorthand(self.row, data))
          if isinstance(self.column, str):
              copy.column = core.FacetFieldDef(**utils.parse_shorthand(self.column, data))
          return super(FacetMapping, copy).to_dict(*args, **kwargs)
  
  
  # ------------------------------------------------------------------------
  # Encoding will contain channel objects that aren't valid at instantiation
  core.FacetedEncoding._class_is_valid_at_instantiation = False
  
  # ------------------------------------------------------------------------
  # These are parameters that are valid at the top level, but are not valid
  # for specs that are within a composite chart
  # (layer, hconcat, vconcat, facet, repeat)
  TOPLEVEL_ONLY_KEYS = {"background", "config", "autosize", "padding", "$schema"}
  
  
  def _get_channels_mapping():
      mapping = {}
      for attr in dir(channels):
          cls = getattr(channels, attr)
          if isinstance(cls, type) and issubclass(cls, core.SchemaBase):
              mapping[cls] = attr.replace("Value", "").lower()
      return mapping
  
  
  # -------------------------------------------------------------------------
  # Tools for working with parameters
  class Parameter(expr.core.OperatorMixin, object):
      """A Parameter object"""
  
      _counter = 0
  
      @classmethod
      def _get_name(cls):
          cls._counter += 1
          return "parameter{:03d}".format(cls._counter)
  
      def __init__(self, name):
          if name is None:
              name = self._get_name()
          self.name = name
  
      def to_dict(self):
          if self.param_type == "variable":
              if self.param.expr is Undefined:
                  return {"expr": self.name}
              else:
                  return {"expr": repr(self.param.expr)}
          elif self.param_type == "selection":
              return {
                  "param": self.name.to_dict()
                  if hasattr(self.name, "to_dict")
                  else self.name
              }
  
      def __invert__(self):
          if self.param_type == "selection":
              return core.PredicateComposition({"not": {"param": self.name}})
          else:
              return expr.core.OperatorMixin.__invert__(self)
  
      def __and__(self, other):
          if self.param_type == "selection":
              if isinstance(other, Parameter):
                  other = {"param": other.name}
              return core.PredicateComposition({"and": [{"param": self.name}, other]})
          else:
              return expr.core.OperatorMixin.__and__(self, other)
  
      def __or__(self, other):
          if self.param_type == "selection":
              if isinstance(other, Parameter):
                  other = {"param": other.name}
              return core.PredicateComposition({"or": [{"param": self.name}, other]})
          else:
              return expr.core.OperatorMixin.__or__(self, other)
  
      def __repr__(self):
          return "Parameter({0!r}, {1})".format(self.name, self.param)
  
      def _to_expr(self):
          if not hasattr(self.param, "expr") or self.param.expr is Undefined:
              return self.name
          else:
              return self.param.expr
  
      def _from_expr(self, expr):
          return parameter(expr=expr)
  
      def __getattr__(self, field_name):
          if field_name.startswith("__") and field_name.endswith("__"):
              raise AttributeError(field_name)
          _attrexpr = expr.core.GetAttrExpression(self.name, field_name)
          # If self is a SelectionParameter and field_name is in its
          # fields or encodings list, then we want to return an expression.
          if check_fields_and_encodings(self, field_name):
              return SelectionExpression(_attrexpr)
          return expr.core.GetAttrExpression(self.name, field_name)
  
  
  class SelectionExpression(expr.core.OperatorMixin, object):
      def __init__(self, expr):
          self.expr = expr
  
      def to_dict(self):
          return {"expr": repr(self.expr)}
  
      def _to_expr(self):
          return repr(self.expr)
  
      def _from_expr(self, expr):
          return SelectionExpression(expr=expr)
  
  
  def check_fields_and_encodings(parameter, field_name):
      for prop in ["fields", "encodings"]:
          try:
              if field_name in getattr(parameter.param.select, prop):
                  return True
          except (AttributeError, TypeError):
              pass
  
      return False
  
  
  # ------------------------------------------------------------------------
  # Top-Level Functions
  
  
  def value(value, **kwargs):
      """Specify a value for use in an encoding"""
      return dict(value=value, **kwargs)
  
  
  def parameter(name=None, select=None, **kwds):
      """Create a named parameter.
  
      Parameters
      ----------
      name : string (optional)
          The name of the parameter. If not specified, a unique name will be
          created.
      **kwds :
          additional keywords will be used to construct a parameter.  If 'select'
          is among the keywords, then a SelectionParameter will be created.
          Otherwise, a VariableParameter will be created.
  
      Returns
      -------
      parameter: Parameter
          The parameter object that can be used in chart creation.
      """
  
      parameter = Parameter(name)
  
      if "empty" in kwds:
          parameter.empty = kwds.pop("empty")
          if parameter.empty == "none":
              warnings.warn(
                  """The value of 'empty' should be True or False.""",
                  DeprecationWarning,
              )
              parameter.empty = False
          elif parameter.empty == "all":
              warnings.warn(
                  """The value of 'empty' should be True or False.""",
                  DeprecationWarning,
              )
              parameter.empty = True
          elif (parameter.empty is False) or (parameter.empty is True):
              pass
          else:
              raise ValueError("The value of 'empty' should be True or False.")
  
      if "init" in kwds:
          warnings.warn(
              """Use 'value' instead of 'init'.""",
              DeprecationWarning,
          )
          if "value" not in kwds:
              kwds["value"] = kwds.pop("init")
          else:
              # If both 'value' and 'init' are set, we ignore 'init'.
              kwds.pop("init")
  
      if select is None:
          parameter.param = core.VariableParameter(name=parameter.name, **kwds)
          parameter.param_type = "variable"
      else:
          parameter.param = core.SelectionParameter(
              name=parameter.name, select=select, **kwds
          )
          parameter.param_type = "selection"
  
      return parameter
  
  
  # TODO: Update the docstring
  def selection(type=Undefined, **kwds):
      """Create a selection.
  
      Parameters
      ----------
      type : string
          The type of the selection: either "interval" or "point"]
      **kwds :
          additional keywords to control the selection.
  
      Returns
      -------
      Parameter
          The Parameter object that can be used in chart creation.
      """
  
      # We separate out the parameter keywords from the selection keywords
      param_kwds = {}
  
      for kwd in {"name", "value", "bind", "empty", "init"}:
          if kwd in kwds:
              param_kwds[kwd] = kwds.pop(kwd)
  
      if type == "interval":
          select = core.IntervalSelectionConfig(type=type, **kwds)
      elif type == "point":
          select = core.PointSelectionConfig(type=type, **kwds)
      elif type in ["single", "multi"]:
          select = core.PointSelectionConfig(type="point", **kwds)
          warnings.warn(
              """The types 'single' and 'multi' are now
          combined and should be specified using "type='point'".""",
              DeprecationWarning,
          )
      else:
          raise ValueError("""'type' must be 'point' or 'interval'""")
  
      return parameter(select=select, **param_kwds)
  
  
  @utils.use_signature(core.IntervalSelectionConfig)
  def selection_interval(**kwargs):
      """Create a selection parameter with type='interval'"""
      return selection(type="interval", **kwargs)
  
  
  @utils.use_signature(core.PointSelectionConfig)
  def selection_point(**kwargs):
      """Create a selection with type='point'"""
      return selection(type="point", **kwargs)
  
  
  @utils.use_signature(core.PointSelectionConfig)
  def selection_multi(**kwargs):
      warnings.warn(
          """'selection_multi' is deprecated.  Use 'selection_point'""",
          DeprecationWarning,
      )
      return selection(type="point", **kwargs)
  
  
  @utils.use_signature(core.PointSelectionConfig)
  def selection_single(**kwargs):
      warnings.warn(
          """'selection_single' is deprecated.  Use 'selection_point'""",
          DeprecationWarning,
      )
      return selection(type="point", **kwargs)
  
  
  @utils.use_signature(core.Binding)
  def binding(input, **kwargs):
      """A generic binding"""
      return core.Binding(input=input, **kwargs)
  
  
  @utils.use_signature(core.BindCheckbox)
  def binding_checkbox(**kwargs):
      """A checkbox binding"""
      return core.BindCheckbox(input="checkbox", **kwargs)
  
  
  @utils.use_signature(core.BindRadioSelect)
  def binding_radio(**kwargs):
      """A radio button binding"""
      return core.BindRadioSelect(input="radio", **kwargs)
  
  
  @utils.use_signature(core.BindRadioSelect)
  def binding_select(**kwargs):
      """A select binding"""
      return core.BindRadioSelect(input="select", **kwargs)
  
  
  @utils.use_signature(core.BindRange)
  def binding_range(**kwargs):
      """A range binding"""
      return core.BindRange(input="range", **kwargs)
  
  
  # TODO: update the docstring
  def condition(predicate, if_true, if_false, **kwargs):
      """A conditional attribute or encoding
  
      Parameters
      ----------
      predicate: Selection, PredicateComposition, expr.Expression, dict, or string
          the selection predicate or test predicate for the condition.
          if a string is passed, it will be treated as a test operand.
      if_true:
          the spec or object to use if the selection predicate is true
      if_false:
          the spec or object to use if the selection predicate is false
      **kwargs:
          additional keyword args are added to the resulting dict
  
      Returns
      -------
      spec: dict or VegaLiteSchema
          the spec that describes the condition
      """
      test_predicates = (str, expr.Expression, core.PredicateComposition)
  
      if isinstance(predicate, Parameter):
          if predicate.param_type == "selection" or predicate.param.expr is Undefined:
              condition = {"param": predicate.name}
              if "empty" in kwargs:
                  condition["empty"] = kwargs.pop("empty")
              elif isinstance(predicate.empty, bool):
                  condition["empty"] = predicate.empty
          else:
              condition = {"test": predicate.param.expr}
      elif isinstance(predicate, test_predicates):
          condition = {"test": predicate}
      elif isinstance(predicate, dict):
          condition = predicate
      else:
          raise NotImplementedError(
              "condition predicate of type {}" "".format(type(predicate))
          )
  
      if isinstance(if_true, core.SchemaBase):
          # convert to dict for now; the from_dict call below will wrap this
          # dict in the appropriate schema
          if_true = if_true.to_dict()
      elif isinstance(if_true, str):
          if_true = {"shorthand": if_true}
          if_true.update(kwargs)
      condition.update(if_true)
  
      if isinstance(if_false, core.SchemaBase):
          # For the selection, the channel definitions all allow selections
          # already. So use this SchemaBase wrapper if possible.
          selection = if_false.copy()
          selection.condition = condition
      elif isinstance(if_false, str):
          selection = {"condition": condition, "shorthand": if_false}
          selection.update(kwargs)
      else:
          selection = dict(condition=condition, **if_false)
  
      return selection
  
  
  # --------------------------------------------------------------------
  # Top-level objects
  
  
  class TopLevelMixin(mixins.ConfigMethodMixin):
      """Mixin for top-level chart objects such as Chart, LayeredChart, etc."""
  
      _class_is_valid_at_instantiation = False
  
      def to_dict(self, *args, **kwargs):
          """Convert the chart to a dictionary suitable for JSON export"""
          # We make use of three context markers:
          # - 'data' points to the data that should be referenced for column type
          #   inference.
          # - 'top_level' is a boolean flag that is assumed to be true; if it's
          #   true then a "$schema" arg is added to the dict.
          # - 'datasets' is a dict of named datasets that should be inserted
          #   in the top-level object
  
          # note: not a deep copy because we want datasets and data arguments to
          # be passed by reference
          context = kwargs.get("context", {}).copy()
          context.setdefault("datasets", {})
          is_top_level = context.get("top_level", True)
  
          copy = self.copy(deep=False)
          original_data = getattr(copy, "data", Undefined)
          copy.data = _prepare_data(original_data, context)
  
          if original_data is not Undefined:
              context["data"] = original_data
  
          # remaining to_dict calls are not at top level
          context["top_level"] = False
          kwargs["context"] = context
  
          try:
              dct = super(TopLevelMixin, copy).to_dict(*args, **kwargs)
          except jsonschema.ValidationError:
              dct = None
  
          # If we hit an error, then re-convert with validate='deep' to get
          # a more useful traceback. We don't do this by default because it's
          # much slower in the case that there are no errors.
          if dct is None:
              kwargs["validate"] = "deep"
              dct = super(TopLevelMixin, copy).to_dict(*args, **kwargs)
  
          # TODO: following entries are added after validation. Should they be validated?
          if is_top_level:
              # since this is top-level we add $schema if it's missing
              if "$schema" not in dct:
                  dct["$schema"] = SCHEMA_URL
  
              # apply theme from theme registry
              the_theme = themes.get()
              dct = utils.update_nested(the_theme(), dct, copy=True)
  
              # update datasets
              if context["datasets"]:
                  dct.setdefault("datasets", {}).update(context["datasets"])
  
          return dct
  
      def to_html(
          self,
          base_url="https://cdn.jsdelivr.net/npm/",
          output_div="vis",
          embed_options=None,
          json_kwds=None,
          fullhtml=True,
          requirejs=False,
      ):
          return utils.spec_to_html(
              self.to_dict(),
              mode="vega-lite",
              vegalite_version=VEGALITE_VERSION,
              vegaembed_version=VEGAEMBED_VERSION,
              vega_version=VEGA_VERSION,
              base_url=base_url,
              output_div=output_div,
              embed_options=embed_options,
              json_kwds=json_kwds,
              fullhtml=fullhtml,
              requirejs=requirejs,
          )
  
      def save(
          self,
          fp,
          format=None,
          override_data_transformer=True,
          scale_factor=1.0,
          vegalite_version=VEGALITE_VERSION,
          vega_version=VEGA_VERSION,
          vegaembed_version=VEGAEMBED_VERSION,
          **kwargs,
      ):
          """Save a chart to file in a variety of formats
  
          Supported formats are json, html, png, svg, pdf; the last three require
          the altair_saver package to be installed.
  
          Parameters
          ----------
          fp : string filename or file-like object
              file in which to write the chart.
          format : string (optional)
              the format to write: one of ['json', 'html', 'png', 'svg', 'pdf'].
              If not specified, the format will be determined from the filename.
          override_data_transformer : boolean (optional)
              If True (default), then the save action will be done with
              the MaxRowsError disabled. If False, then do not change the data
              transformer.
          scale_factor : float
              For svg or png formats, scale the image by this factor when saving.
              This can be used to control the size or resolution of the output.
              Default is 1.0
          **kwargs :
              Additional keyword arguments are passed to the output method
              associated with the specified format.
  
          """
          from ...utils.save import save
  
          kwds = dict(
              chart=self,
              fp=fp,
              format=format,
              scale_factor=scale_factor,
              vegalite_version=vegalite_version,
              vega_version=vega_version,
              vegaembed_version=vegaembed_version,
              **kwargs,
          )
  
          # By default we override the data transformer. This makes it so
          # that save() will succeed even for large datasets that would
          # normally trigger a MaxRowsError
          if override_data_transformer:
              with data_transformers.disable_max_rows():
                  result = save(**kwds)
          else:
              result = save(**kwds)
          return result
  
      # Fallback for when rendering fails; the full repr is too long to be
      # useful in nearly all cases.
      def __repr__(self):
          return "alt.{}(...)".format(self.__class__.__name__)
  
      # Layering and stacking
      def __add__(self, other):
          if not isinstance(other, TopLevelMixin):
              raise ValueError("Only Chart objects can be layered.")
          return layer(self, other)
  
      def __and__(self, other):
          if not isinstance(other, TopLevelMixin):
              raise ValueError("Only Chart objects can be concatenated.")
          return vconcat(self, other)
  
      def __or__(self, other):
          if not isinstance(other, TopLevelMixin):
              raise ValueError("Only Chart objects can be concatenated.")
          return hconcat(self, other)
  
      def repeat(
          self,
          repeat=Undefined,
          row=Undefined,
          column=Undefined,
          layer=Undefined,
          columns=Undefined,
          **kwargs,
      ):
          """Return a RepeatChart built from the chart
  
          Fields within the chart can be set to correspond to the row or
          column using `alt.repeat('row')` and `alt.repeat('column')`.
  
          Parameters
          ----------
          repeat : list
              a list of data column names to be repeated. This cannot be
              used along with the ``row``, ``column`` or ``layer`` argument.
          row : list
              a list of data column names to be mapped to the row facet
          column : list
              a list of data column names to be mapped to the column facet
          layer : list
              a list of data column names to be layered. This cannot be
              used along with the ``row``, ``column`` or ``repeat`` argument.
          columns : int
              the maximum number of columns before wrapping. Only referenced
              if ``repeat`` is specified.
          **kwargs :
              additional keywords passed to RepeatChart.
  
          Returns
          -------
          chart : RepeatChart
              a repeated chart.
          """
          repeat_specified = repeat is not Undefined
          rowcol_specified = row is not Undefined or column is not Undefined
          layer_specified = layer is not Undefined
  
          if repeat_specified and rowcol_specified:
              raise ValueError(
                  "repeat argument cannot be combined with row/column argument."
              )
          elif repeat_specified and layer_specified:
              raise ValueError("repeat argument cannot be combined with layer argument.")
          elif layer_specified and rowcol_specified:
              raise ValueError(
                  "layer argument cannot be combined with row/column argument."
              )
  
          if repeat_specified:
              repeat = repeat
          elif layer_specified:
              repeat = core.LayerRepeatMapping(layer=layer)
          else:
              repeat = core.RepeatMapping(row=row, column=column)
  
          return RepeatChart(spec=self, repeat=repeat, columns=columns, **kwargs)
  
      def properties(self, **kwargs):
          """Set top-level properties of the Chart.
  
          Argument names and types are the same as class initialization.
          """
          copy = self.copy(deep=False)
          for key, val in kwargs.items():
              if key == "selection" and isinstance(val, Parameter):
                  # TODO: Can this be removed
                  # For backward compatibility with old selection interface.
                  setattr(copy, key, {val.name: val.selection})
              else:
                  # Don't validate data, because it hasn't been processed.
                  if key != "data":
                      self.validate_property(key, val)
                  setattr(copy, key, val)
          return copy
  
      def project(
          self,
          type="mercator",
          center=Undefined,
          clipAngle=Undefined,
          clipExtent=Undefined,
          coefficient=Undefined,
          distance=Undefined,
          fraction=Undefined,
          lobes=Undefined,
          parallel=Undefined,
          precision=Undefined,
          radius=Undefined,
          ratio=Undefined,
          reflectX=Undefined,
          reflectY=Undefined,
          rotate=Undefined,
          scale=Undefined,
          spacing=Undefined,
          tilt=Undefined,
          translate=Undefined,
          **kwds,
      ):
          """Add a geographic projection to the chart.
  
          This is generally used either with ``mark_geoshape`` or with the
          ``latitude``/``longitude`` encodings.
  
          Available projection types are
          ['albers', 'albersUsa', 'azimuthalEqualArea', 'azimuthalEquidistant',
          'conicConformal', 'conicEqualArea', 'conicEquidistant', 'equalEarth', 'equirectangular',
          'gnomonic', 'identity', 'mercator', 'orthographic', 'stereographic', 'transverseMercator']
  
          Attributes
          ----------
          type : ProjectionType
              The cartographic projection to use. This value is case-insensitive, for example
              `"albers"` and `"Albers"` indicate the same projection type. You can find all valid
              projection types [in the
              documentation](https://vega.github.io/vega-lite/docs/projection.html#projection-types).
  
              **Default value:** `mercator`
          center : List(float)
              Sets the projection’s center to the specified center, a two-element array of
              longitude and latitude in degrees.
  
              **Default value:** `[0, 0]`
          clipAngle : float
              Sets the projection’s clipping circle radius to the specified angle in degrees. If
              `null`, switches to [antimeridian](http://bl.ocks.org/mbostock/3788999) cutting
              rather than small-circle clipping.
          clipExtent : List(List(float))
              Sets the projection’s viewport clip extent to the specified bounds in pixels. The
              extent bounds are specified as an array `[[x0, y0], [x1, y1]]`, where `x0` is the
              left-side of the viewport, `y0` is the top, `x1` is the right and `y1` is the
              bottom. If `null`, no viewport clipping is performed.
          coefficient : float
  
          distance : float
  
          fraction : float
  
          lobes : float
  
          parallel : float
  
          precision : Mapping(required=[length])
              Sets the threshold for the projection’s [adaptive
              resampling](http://bl.ocks.org/mbostock/3795544) to the specified value in pixels.
              This value corresponds to the [Douglas–Peucker
              distance](http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm).
               If precision is not specified, returns the projection’s current resampling
              precision which defaults to `√0.5 ≅ 0.70710…`.
          radius : float
  
          ratio : float
  
          reflectX : boolean
  
          reflectY : boolean
  
          rotate : List(float)
              Sets the projection’s three-axis rotation to the specified angles, which must be a
              two- or three-element array of numbers [`lambda`, `phi`, `gamma`] specifying the
              rotation angles in degrees about each spherical axis. (These correspond to yaw,
              pitch and roll.)
  
              **Default value:** `[0, 0, 0]`
          scale : float
              Sets the projection's scale (zoom) value, overriding automatic fitting.
  
          spacing : float
  
          tilt : float
  
          translate : List(float)
              Sets the projection's translation (pan) value, overriding automatic fitting.
  
          """
          projection = core.Projection(
              center=center,
              clipAngle=clipAngle,
              clipExtent=clipExtent,
              coefficient=coefficient,
              distance=distance,
              fraction=fraction,
              lobes=lobes,
              parallel=parallel,
              precision=precision,
              radius=radius,
              ratio=ratio,
              reflectX=reflectX,
              reflectY=reflectY,
              rotate=rotate,
              scale=scale,
              spacing=spacing,
              tilt=tilt,
              translate=translate,
              type=type,
              **kwds,
          )
          return self.properties(projection=projection)
  
      def _add_transform(self, *transforms):
          """Copy the chart and add specified transforms to chart.transform"""
          copy = self.copy(deep=["transform"])
          if copy.transform is Undefined:
              copy.transform = []
          copy.transform.extend(transforms)
          return copy
  
      def transform_aggregate(self, aggregate=Undefined, groupby=Undefined, **kwds):
          """
          Add an AggregateTransform to the schema.
  
          Parameters
          ----------
          aggregate : List(:class:`AggregatedFieldDef`)
              Array of objects that define fields to aggregate.
          groupby : List(string)
              The data fields to group by. If not specified, a single group containing all data
              objects will be used.
          **kwds :
              additional keywords are converted to aggregates using standard
              shorthand parsing.
  
          Returns
          -------
          self : Chart object
              returns chart to allow for chaining
  
          Examples
          --------
          The aggregate transform allows you to specify transforms directly using
          the same shorthand syntax as used in encodings:
  
          >>> import altair as alt
          >>> chart1 = alt.Chart().transform_aggregate(
          ...     mean_acc='mean(Acceleration)',
          ...     groupby=['Origin']
          ... )
          >>> print(chart1.transform[0].to_json())  # doctest: +NORMALIZE_WHITESPACE
          {
            "aggregate": [
              {
                "as": "mean_acc",
                "field": "Acceleration",
                "op": "mean"
              }
            ],
            "groupby": [
              "Origin"
            ]
          }
  
          It also supports including AggregatedFieldDef instances or dicts directly,
          so you can create the above transform like this:
  
          >>> chart2 = alt.Chart().transform_aggregate(
          ...     [alt.AggregatedFieldDef(field='Acceleration', op='mean',
          ...                             **{'as': 'mean_acc'})],
          ...     groupby=['Origin']
          ... )
          >>> chart2.transform == chart1.transform
          True
  
          See Also
          --------
          alt.AggregateTransform : underlying transform object
  
          """
          if aggregate is Undefined:
              aggregate = []
          for key, val in kwds.items():
              parsed = utils.parse_shorthand(val)
              dct = {
                  "as": key,
                  "field": parsed.get("field", Undefined),
                  "op": parsed.get("aggregate", Undefined),
              }
              aggregate.append(core.AggregatedFieldDef(**dct))
          return self._add_transform(
              core.AggregateTransform(aggregate=aggregate, groupby=groupby)
          )
  
      def transform_bin(self, as_=Undefined, field=Undefined, bin=True, **kwargs):
          """
          Add a BinTransform to the schema.
  
          Parameters
          ----------
          as_ : anyOf(string, List(string))
              The output fields at which to write the start and end bin values.
          bin : anyOf(boolean, :class:`BinParams`)
              An object indicating bin properties, or simply ``true`` for using default bin
              parameters.
          field : string
              The data field to bin.
  
          Returns
          -------
          self : Chart object
              returns chart to allow for chaining
  
          Examples
          --------
          >>> import altair as alt
          >>> chart = alt.Chart().transform_bin("x_binned", "x")
          >>> chart.transform[0]
          BinTransform({
            as: 'x_binned',
            bin: True,
            field: 'x'
          })
  
          >>> chart = alt.Chart().transform_bin("x_binned", "x",
          ...                                   bin=alt.Bin(maxbins=10))
          >>> chart.transform[0]
          BinTransform({
            as: 'x_binned',
            bin: BinParams({
              maxbins: 10
            }),
            field: 'x'
          })
  
          See Also
          --------
          alt.BinTransform : underlying transform object
  
          """
          if as_ is not Undefined:
              if "as" in kwargs:
                  raise ValueError(
                      "transform_bin: both 'as_' and 'as' passed as arguments."
                  )
              kwargs["as"] = as_
          kwargs["bin"] = bin
          kwargs["field"] = field
          return self._add_transform(core.BinTransform(**kwargs))
  
      def transform_calculate(self, as_=Undefined, calculate=Undefined, **kwargs):
          """
          Add a CalculateTransform to the schema.
  
          Parameters
          ----------
          as_ : string
              The field for storing the computed formula value.
          calculate : string or alt.expr expression
              A `expression <https://vega.github.io/vega-lite/docs/types.html#expression>`__
              string. Use the variable ``datum`` to refer to the current data object.
          **kwargs
              transforms can also be passed by keyword argument; see Examples
  
          Returns
          -------
          self : Chart object
              returns chart to allow for chaining
  
          Examples
          --------
          >>> import altair as alt
          >>> from altair import datum, expr
  
          >>> chart = alt.Chart().transform_calculate(y = 2 * expr.sin(datum.x))
          >>> chart.transform[0]
          CalculateTransform({
            as: 'y',
            calculate: (2 * sin(datum.x))
          })
  
          It's also possible to pass the ``CalculateTransform`` arguments directly:
  
          >>> kwds = {'as': 'y', 'calculate': '2 * sin(datum.x)'}
          >>> chart = alt.Chart().transform_calculate(**kwds)
          >>> chart.transform[0]
          CalculateTransform({
            as: 'y',
            calculate: '2 * sin(datum.x)'
          })
  
          As the first form is easier to write and understand, that is the
          recommended method.
  
          See Also
          --------
          alt.CalculateTransform : underlying transform object
          """
          if as_ is Undefined:
              as_ = kwargs.pop("as", Undefined)
          elif "as" in kwargs:
              raise ValueError(
                  "transform_calculate: both 'as_' and 'as' passed as arguments."
              )
          if as_ is not Undefined or calculate is not Undefined:
              dct = {"as": as_, "calculate": calculate}
              self = self._add_transform(core.CalculateTransform(**dct))
          for as_, calculate in kwargs.items():
              dct = {"as": as_, "calculate": calculate}
              self = self._add_transform(core.CalculateTransform(**dct))
          return self
  
      def transform_density(
          self,
          density,
          as
  
  [... the rest is omitted ...]
files: altair/vegalite/v5/api.py
files: """Unit tests for altair API"""
  
  import io
  import json
  import operator
  import os
  import pathlib
  import tempfile
  
  import jsonschema
  import pytest
  import pandas as pd
  
  import altair.vegalite.v5 as alt
  
  try:
      import altair_saver  # noqa: F401
  except ImportError:
      altair_saver = None
  
  
  def getargs(*args, **kwargs):
      return args, kwargs
  
  
  OP_DICT = {
      "layer": operator.add,
      "hconcat": operator.or_,
      "vconcat": operator.and_,
  }
  
  
  def _make_chart_type(chart_type):
      data = pd.DataFrame(
          {
              "x": [28, 55, 43, 91, 81, 53, 19, 87],
              "y": [43, 91, 81, 53, 19, 87, 52, 28],
              "color": list("AAAABBBB"),
          }
      )
      base = (
          alt.Chart(data)
          .mark_point()
          .encode(
              x="x",
              y="y",
              color="color",
          )
      )
  
      if chart_type in ["layer", "hconcat", "vconcat", "concat"]:
          func = getattr(alt, chart_type)
          return func(base.mark_square(), base.mark_circle())
      elif chart_type == "facet":
          return base.facet("color")
      elif chart_type == "facet_encoding":
          return base.encode(facet="color")
      elif chart_type == "repeat":
          return base.encode(alt.X(alt.repeat(), type="quantitative")).repeat(["x", "y"])
      elif chart_type == "chart":
          return base
      else:
          raise ValueError("chart_type='{}' is not recognized".format(chart_type))
  
  
  @pytest.fixture
  def basic_chart():
      data = pd.DataFrame(
          {
              "a": ["A", "B", "C", "D", "E", "F", "G", "H", "I"],
              "b": [28, 55, 43, 91, 81, 53, 19, 87, 52],
          }
      )
  
      return alt.Chart(data).mark_bar().encode(x="a", y="b")
  
  
  def test_chart_data_types():
      def Chart(data):
          return alt.Chart(data).mark_point().encode(x="x:Q", y="y:Q")
  
      # Url Data
      data = "/path/to/my/data.csv"
      dct = Chart(data).to_dict()
      assert dct["data"] == {"url": data}
  
      # Dict Data
      data = {"values": [{"x": 1, "y": 2}, {"x": 2, "y": 3}]}
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = Chart(data).to_dict()
      assert dct["data"] == data
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = Chart(data).to_dict()
      name = dct["data"]["name"]
      assert dct["datasets"][name] == data["values"]
  
      # DataFrame data
      data = pd.DataFrame({"x": range(5), "y": range(5)})
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = Chart(data).to_dict()
      assert dct["data"]["values"] == data.to_dict(orient="records")
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = Chart(data).to_dict()
      name = dct["data"]["name"]
      assert dct["datasets"][name] == data.to_dict(orient="records")
  
      # Named data object
      data = alt.NamedData(name="Foo")
      dct = Chart(data).to_dict()
      assert dct["data"] == {"name": "Foo"}
  
  
  def test_chart_infer_types():
      data = pd.DataFrame(
          {
              "x": pd.date_range("2012", periods=10, freq="Y"),
              "y": range(10),
              "c": list("abcabcabca"),
          }
      )
  
      def _check_encodings(chart):
          dct = chart.to_dict()
          assert dct["encoding"]["x"]["type"] == "temporal"
          assert dct["encoding"]["x"]["field"] == "x"
          assert dct["encoding"]["y"]["type"] == "quantitative"
          assert dct["encoding"]["y"]["field"] == "y"
          assert dct["encoding"]["color"]["type"] == "nominal"
          assert dct["encoding"]["color"]["field"] == "c"
  
      # Pass field names by keyword
      chart = alt.Chart(data).mark_point().encode(x="x", y="y", color="c")
      _check_encodings(chart)
  
      # pass Channel objects by keyword
      chart = (
          alt.Chart(data)
          .mark_point()
          .encode(x=alt.X("x"), y=alt.Y("y"), color=alt.Color("c"))
      )
      _check_encodings(chart)
  
      # pass Channel objects by value
      chart = alt.Chart(data).mark_point().encode(alt.X("x"), alt.Y("y"), alt.Color("c"))
      _check_encodings(chart)
  
      # override default types
      chart = (
          alt.Chart(data)
          .mark_point()
          .encode(alt.X("x", type="nominal"), alt.Y("y", type="ordinal"))
      )
      dct = chart.to_dict()
      assert dct["encoding"]["x"]["type"] == "nominal"
      assert dct["encoding"]["y"]["type"] == "ordinal"
  
  
  @pytest.mark.parametrize(
      "args, kwargs",
      [
          getargs(detail=["value:Q", "name:N"], tooltip=["value:Q", "name:N"]),
          getargs(detail=["value", "name"], tooltip=["value", "name"]),
          getargs(alt.Detail(["value:Q", "name:N"]), alt.Tooltip(["value:Q", "name:N"])),
          getargs(alt.Detail(["value", "name"]), alt.Tooltip(["value", "name"])),
          getargs(
              [alt.Detail("value:Q"), alt.Detail("name:N")],
              [alt.Tooltip("value:Q"), alt.Tooltip("name:N")],
          ),
          getargs(
              [alt.Detail("value"), alt.Detail("name")],
              [alt.Tooltip("value"), alt.Tooltip("name")],
          ),
      ],
  )
  def test_multiple_encodings(args, kwargs):
      df = pd.DataFrame({"value": [1, 2, 3], "name": ["A", "B", "C"]})
      encoding_dct = [
          {"field": "value", "type": "quantitative"},
          {"field": "name", "type": "nominal"},
      ]
      chart = alt.Chart(df).mark_point().encode(*args, **kwargs)
      dct = chart.to_dict()
      assert dct["encoding"]["detail"] == encoding_dct
      assert dct["encoding"]["tooltip"] == encoding_dct
  
  
  def test_chart_operations():
      data = pd.DataFrame(
          {
              "x": pd.date_range("2012", periods=10, freq="Y"),
              "y": range(10),
              "c": list("abcabcabca"),
          }
      )
      chart1 = alt.Chart(data).mark_line().encode(x="x", y="y", color="c")
      chart2 = chart1.mark_point()
      chart3 = chart1.mark_circle()
      chart4 = chart1.mark_square()
  
      chart = chart1 + chart2 + chart3
      assert isinstance(chart, alt.LayerChart)
      assert len(chart.layer) == 3
      chart += chart4
      assert len(chart.layer) == 4
  
      chart = chart1 | chart2 | chart3
      assert isinstance(chart, alt.HConcatChart)
      assert len(chart.hconcat) == 3
      chart |= chart4
      assert len(chart.hconcat) == 4
  
      chart = chart1 & chart2 & chart3
      assert isinstance(chart, alt.VConcatChart)
      assert len(chart.vconcat) == 3
      chart &= chart4
      assert len(chart.vconcat) == 4
  
  
  def test_selection_to_dict():
      brush = alt.selection(type="interval")
  
      # test some value selections
      # Note: X and Y cannot have conditions
      alt.Chart("path/to/data.json").mark_point().encode(
          color=alt.condition(brush, alt.ColorValue("red"), alt.ColorValue("blue")),
          opacity=alt.condition(brush, alt.value(0.5), alt.value(1.0)),
          text=alt.condition(brush, alt.TextValue("foo"), alt.value("bar")),
      ).to_dict()
  
      # test some field selections
      # Note: X and Y cannot have conditions
      # Conditions cannot both be fields
      alt.Chart("path/to/data.json").mark_point().encode(
          color=alt.condition(brush, alt.Color("col1:N"), alt.value("blue")),
          opacity=alt.condition(brush, "col1:N", alt.value(0.5)),
          text=alt.condition(brush, alt.value("abc"), alt.Text("col2:N")),
          size=alt.condition(brush, alt.value(20), "col2:N"),
      ).to_dict()
  
  
  def test_selection_expression():
      selection = alt.selection_single(fields=["value"])
  
      assert isinstance(selection.value, alt.expr.Expression)
      assert selection.value.to_dict() == "{0}.value".format(selection.name)
  
      assert isinstance(selection["value"], alt.expr.Expression)
      assert selection["value"].to_dict() == "{0}['value']".format(selection.name)
  
      with pytest.raises(AttributeError):
          selection.__magic__
  
  
  @pytest.mark.parametrize("format", ["html", "json", "png", "svg", "pdf"])
  def test_save(format, basic_chart):
      if format in ["pdf", "png"]:
          out = io.BytesIO()
          mode = "rb"
      else:
          out = io.StringIO()
          mode = "r"
  
      if format in ["svg", "png", "pdf"]:
          if not altair_saver:
              with pytest.raises(ValueError) as err:
                  basic_chart.save(out, format=format)
              assert "github.com/altair-viz/altair_saver" in str(err.value)
              return
          elif format not in altair_saver.available_formats():
              with pytest.raises(ValueError) as err:
                  basic_chart.save(out, format=format)
              assert f"No enabled saver found that supports format='{format}'" in str(
                  err.value
              )
              return
  
      basic_chart.save(out, format=format)
      out.seek(0)
      content = out.read()
  
      if format == "json":
          assert "$schema" in json.loads(content)
      if format == "html":
          assert content.startswith("<!DOCTYPE html>")
  
      fid, filename = tempfile.mkstemp(suffix="." + format)
      os.close(fid)
  
      # test both string filenames and pathlib.Paths
      for fp in [filename, pathlib.Path(filename)]:
          try:
              basic_chart.save(fp)
              with open(fp, mode) as f:
                  assert f.read()[:1000] == content[:1000]
          finally:
              os.remove(fp)
  
  
  def test_facet_basic():
      # wrapped facet
      chart1 = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(
              x="x:Q",
              y="y:Q",
          )
          .facet("category:N", columns=2)
      )
  
      dct1 = chart1.to_dict()
  
      assert dct1["facet"] == alt.Facet("category:N").to_dict()
      assert dct1["columns"] == 2
      assert dct1["data"] == alt.UrlData("data.csv").to_dict()
  
      # explicit row/col facet
      chart2 = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(
              x="x:Q",
              y="y:Q",
          )
          .facet(row="category1:Q", column="category2:Q")
      )
  
      dct2 = chart2.to_dict()
  
      assert dct2["facet"]["row"] == alt.Facet("category1:Q").to_dict()
      assert dct2["facet"]["column"] == alt.Facet("category2:Q").to_dict()
      assert "columns" not in dct2
      assert dct2["data"] == alt.UrlData("data.csv").to_dict()
  
  
  def test_facet_parse():
      chart = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(x="x:Q", y="y:Q")
          .facet(row="row:N", column="column:O")
      )
      dct = chart.to_dict()
      assert dct["data"] == {"url": "data.csv"}
      assert "data" not in dct["spec"]
      assert dct["facet"] == {
          "column": {"field": "column", "type": "ordinal"},
          "row": {"field": "row", "type": "nominal"},
      }
  
  
  def test_facet_parse_data():
      data = pd.DataFrame({"x": range(5), "y": range(5), "row": list("abcab")})
      chart = (
          alt.Chart(data)
          .mark_point()
          .encode(x="x", y="y:O")
          .facet(row="row", column="column:O")
      )
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = chart.to_dict()
      assert "values" in dct["data"]
      assert "data" not in dct["spec"]
      assert dct["facet"] == {
          "column": {"field": "column", "type": "ordinal"},
          "row": {"field": "row", "type": "nominal"},
      }
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = chart.to_dict()
      assert "datasets" in dct
      assert "name" in dct["data"]
      assert "data" not in dct["spec"]
      assert dct["facet"] == {
          "column": {"field": "column", "type": "ordinal"},
          "row": {"field": "row", "type": "nominal"},
      }
  
  
  def test_selection():
      # test instantiation of selections
      interval = alt.selection_interval(name="selec_1")
      assert interval.selection.type == "interval"
      assert interval.name == "selec_1"
  
      single = alt.selection_single(name="selec_2")
      assert single.selection.type == "single"
      assert single.name == "selec_2"
  
      multi = alt.selection_multi(name="selec_3")
      assert multi.selection.type == "multi"
      assert multi.name == "selec_3"
  
      # test adding to chart
      chart = alt.Chart().add_selection(single)
      chart = chart.add_selection(multi, interval)
      assert set(chart.selection.keys()) == {"selec_1", "selec_2", "selec_3"}
  
      # test logical operations
      assert isinstance(single & multi, alt.Selection)
      assert isinstance(single | multi, alt.Selection)
      assert isinstance(~single, alt.Selection)
      assert isinstance((single & multi)[0].group, alt.SelectionAnd)
      assert isinstance((single | multi)[0].group, alt.SelectionOr)
      assert isinstance((~single)[0].group, alt.SelectionNot)
  
      # test that default names increment (regression for #1454)
      sel1 = alt.selection_single()
      sel2 = alt.selection_multi()
      sel3 = alt.selection_interval()
      names = {s.name for s in (sel1, sel2, sel3)}
      assert len(names) == 3
  
  
  def test_transforms():
      # aggregate transform
      agg1 = alt.AggregatedFieldDef(**{"as": "x1", "op": "mean", "field": "y"})
      agg2 = alt.AggregatedFieldDef(**{"as": "x2", "op": "median", "field": "z"})
      chart = alt.Chart().transform_aggregate([agg1], ["foo"], x2="median(z)")
      kwds = dict(aggregate=[agg1, agg2], groupby=["foo"])
      assert chart.transform == [alt.AggregateTransform(**kwds)]
  
      # bin transform
      chart = alt.Chart().transform_bin("binned", field="field", bin=True)
      kwds = {"as": "binned", "field": "field", "bin": True}
      assert chart.transform == [alt.BinTransform(**kwds)]
  
      # calcualte transform
      chart = alt.Chart().transform_calculate("calc", "datum.a * 4")
      kwds = {"as": "calc", "calculate": "datum.a * 4"}
      assert chart.transform == [alt.CalculateTransform(**kwds)]
  
      # density transform
      chart = alt.Chart().transform_density("x", as_=["value", "density"])
      kwds = {"as": ["value", "density"], "density": "x"}
      assert chart.transform == [alt.DensityTransform(**kwds)]
  
      # filter transform
      chart = alt.Chart().transform_filter("datum.a < 4")
      assert chart.transform == [alt.FilterTransform(filter="datum.a < 4")]
  
      # flatten transform
      chart = alt.Chart().transform_flatten(["A", "B"], ["X", "Y"])
      kwds = {"as": ["X", "Y"], "flatten": ["A", "B"]}
      assert chart.transform == [alt.FlattenTransform(**kwds)]
  
      # fold transform
      chart = alt.Chart().transform_fold(["A", "B", "C"], as_=["key", "val"])
      kwds = {"as": ["key", "val"], "fold": ["A", "B", "C"]}
      assert chart.transform == [alt.FoldTransform(**kwds)]
  
      # impute transform
      chart = alt.Chart().transform_impute("field", "key", groupby=["x"])
      kwds = {"impute": "field", "key": "key", "groupby": ["x"]}
      assert chart.transform == [alt.ImputeTransform(**kwds)]
  
      # joinaggregate transform
      chart = alt.Chart().transform_joinaggregate(min="min(x)", groupby=["key"])
      kwds = {
          "joinaggregate": [
              alt.JoinAggregateFieldDef(field="x", op="min", **{"as": "min"})
          ],
          "groupby": ["key"],
      }
      assert chart.transform == [alt.JoinAggregateTransform(**kwds)]
  
      # loess transform
      chart = alt.Chart().transform_loess("x", "y", as_=["xx", "yy"])
      kwds = {"on": "x", "loess": "y", "as": ["xx", "yy"]}
      assert chart.transform == [alt.LoessTransform(**kwds)]
  
      # lookup transform (data)
      lookup_data = alt.LookupData(alt.UrlData("foo.csv"), "id", ["rate"])
      chart = alt.Chart().transform_lookup("a", from_=lookup_data, as_="a", default="b")
      kwds = {"from": lookup_data, "as": "a", "lookup": "a", "default": "b"}
      assert chart.transform == [alt.LookupTransform(**kwds)]
  
      # lookup transform (selection)
      lookup_selection = alt.LookupSelection(key="key", selection="sel")
      chart = alt.Chart().transform_lookup(
          "a", from_=lookup_selection, as_="a", default="b"
      )
      kwds = {"from": lookup_selection, "as": "a", "lookup": "a", "default": "b"}
      assert chart.transform == [alt.LookupTransform(**kwds)]
  
      # pivot transform
      chart = alt.Chart().transform_pivot("x", "y")
      assert chart.transform == [alt.PivotTransform(pivot="x", value="y")]
  
      # quantile transform
      chart = alt.Chart().transform_quantile("x", as_=["prob", "value"])
      kwds = {"quantile": "x", "as": ["prob", "value"]}
      assert chart.transform == [alt.QuantileTransform(**kwds)]
  
      # regression transform
      chart = alt.Chart().transform_regression("x", "y", as_=["xx", "yy"])
      kwds = {"on": "x", "regression": "y", "as": ["xx", "yy"]}
      assert chart.transform == [alt.RegressionTransform(**kwds)]
  
      # sample transform
      chart = alt.Chart().transform_sample()
      assert chart.transform == [alt.SampleTransform(1000)]
  
      # stack transform
      chart = alt.Chart().transform_stack("stacked", "x", groupby=["y"])
      assert chart.transform == [
          alt.StackTransform(stack="x", groupby=["y"], **{"as": "stacked"})
      ]
  
      # timeUnit transform
      chart = alt.Chart().transform_timeunit("foo", field="x", timeUnit="date")
      kwds = {"as": "foo", "field": "x", "timeUnit": "date"}
      assert chart.transform == [alt.TimeUnitTransform(**kwds)]
  
      # window transform
      chart = alt.Chart().transform_window(xsum="sum(x)", ymin="min(y)", frame=[None, 0])
      window = [
          alt.WindowFieldDef(**{"as": "xsum", "field": "x", "op": "sum"}),
          alt.WindowFieldDef(**{"as": "ymin", "field": "y", "op": "min"}),
      ]
  
      # kwargs don't maintain order in Python < 3.6, so window list can
      # be reversed
      assert chart.transform == [
          alt.WindowTransform(frame=[None, 0], window=window)
      ] or chart.transform == [alt.WindowTransform(frame=[None, 0], window=window[::-1])]
  
  
  def test_filter_transform_selection_predicates():
      selector1 = alt.selection_interval(name="s1")
      selector2 = alt.selection_interval(name="s2")
      base = alt.Chart("data.txt").mark_point()
  
      chart = base.transform_filter(selector1)
      assert chart.to_dict()["transform"] == [{'filter': {'param': 's1'}}]
  
      chart = base.transform_filter(~selector1)
      assert chart.to_dict()["transform"] == [{'filter': {'not': {'param': 's1'}}}]
  
      chart = base.transform_filter(selector1 & selector2)
      assert chart.to_dict()["transform"] == [
          {'filter': {'and': [{'param': 's1'}, {'param': 's2'}]}}
      ]
  
      chart = base.transform_filter(selector1 | selector2)
      assert chart.to_dict()["transform"] == [
          {'filter': {'or': [{'param': 's1'}, {'param': 's2'}]}}
      ]
  
      chart = base.transform_filter(selector1 | ~selector2)
      assert chart.to_dict()["transform"] == [
          {'filter': {'or': [{'param': 's1'}, {'not': {'param': 's2'}}]}}
      ]
  
      chart = base.transform_filter(~selector1 | ~selector2)
      assert chart.to_dict()["transform"] == [
          {'filter': {'or': [{'not': {'param': 's1'}}, {'not': {'param': 's2'}}]}}
      ]
  
      chart = base.transform_filter(~(selector1 & selector2))
      assert chart.to_dict()["transform"] == [
          {'filter': {'not': {'and': [{'param': 's1'}, {'param': 's2'}]}}}
      ]
  
  
  def test_resolve_methods():
      chart = alt.LayerChart().resolve_axis(x="shared", y="independent")
      assert chart.resolve == alt.Resolve(
          axis=alt.AxisResolveMap(x="shared", y="independent")
      )
  
      chart = alt.LayerChart().resolve_legend(color="shared", fill="independent")
      assert chart.resolve == alt.Resolve(
          legend=alt.LegendResolveMap(color="shared", fill="independent")
      )
  
      chart = alt.LayerChart().resolve_scale(x="shared", y="independent")
      assert chart.resolve == alt.Resolve(
          scale=alt.ScaleResolveMap(x="shared", y="independent")
      )
  
  
  def test_layer_encodings():
      chart = alt.LayerChart().encode(x="column:Q")
      assert chart.encoding.x == alt.X(shorthand="column:Q")
  
  
  def test_add_selection():
      selections = [
          alt.selection_interval(),
          alt.selection_single(),
          alt.selection_multi(),
      ]
      chart = (
          alt.Chart()
          .mark_point()
          .add_selection(selections[0])
          .add_selection(selections[1], selections[2])
      )
      expected = {s.name: s.selection for s in selections}
      assert chart.selection == expected
  
  
  def test_repeat_add_selections():
      base = alt.Chart("data.csv").mark_point()
      selection = alt.selection_single()
      chart1 = base.add_selection(selection).repeat(list("ABC"))
      chart2 = base.repeat(list("ABC")).add_selection(selection)
      assert chart1.to_dict() == chart2.to_dict()
  
  
  def test_facet_add_selections():
      base = alt.Chart("data.csv").mark_point()
      selection = alt.selection_single()
      chart1 = base.add_selection(selection).facet("val:Q")
      chart2 = base.facet("val:Q").add_selection(selection)
      assert chart1.to_dict() == chart2.to_dict()
  
  
  def test_layer_add_selection():
      base = alt.Chart("data.csv").mark_point()
      selection = alt.selection_single()
      chart1 = alt.layer(base.add_selection(selection), base)
      chart2 = alt.layer(base, base).add_selection(selection)
      assert chart1.to_dict() == chart2.to_dict()
  
  
  @pytest.mark.parametrize("charttype", [alt.concat, alt.hconcat, alt.vconcat])
  def test_compound_add_selections(charttype):
      base = alt.Chart("data.csv").mark_point()
      selection = alt.selection_single()
      chart1 = charttype(base.add_selection(selection), base.add_selection(selection))
      chart2 = charttype(base, base).add_selection(selection)
      assert chart1.to_dict() == chart2.to_dict()
  
  
  def test_selection_property():
      sel = alt.selection_interval()
      chart = alt.Chart("data.csv").mark_point().properties(selection=sel)
  
      assert list(chart["selection"].keys()) == [sel.name]
  
  
  def test_LookupData():
      df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})
      lookup = alt.LookupData(data=df, key="x")
  
      dct = lookup.to_dict()
      assert dct["key"] == "x"
      assert dct["data"] == {
          "values": [{"x": 1, "y": 4}, {"x": 2, "y": 5}, {"x": 3, "y": 6}]
      }
  
  
  def test_themes():
      chart = alt.Chart("foo.txt").mark_point()
  
      with alt.themes.enable("default"):
          assert chart.to_dict()["config"] == {
              "view": {"continuousWidth": 400, "continuousHeight": 300}
          }
  
      with alt.themes.enable("opaque"):
          assert chart.to_dict()["config"] == {
              "background": "white",
              "view": {"continuousWidth": 400, "continuousHeight": 300},
          }
  
      with alt.themes.enable("none"):
          assert "config" not in chart.to_dict()
  
  
  def test_chart_from_dict():
      base = alt.Chart("data.csv").mark_point().encode(x="x:Q", y="y:Q")
  
      charts = [
          base,
          base + base,
          base | base,
          base & base,
          base.facet("c:N"),
          (base + base).facet(row="c:N", data="data.csv"),
          base.repeat(["c", "d"]),
          (base + base).repeat(row=["c", "d"]),
      ]
  
      for chart in charts:
          chart_out = alt.Chart.from_dict(chart.to_dict())
          assert type(chart_out) is type(chart)
  
      # test that an invalid spec leads to a schema validation error
      with pytest.raises(jsonschema.ValidationError):
          alt.Chart.from_dict({"invalid": "spec"})
  
  
  def test_consolidate_datasets(basic_chart):
      subchart1 = basic_chart
      subchart2 = basic_chart.copy()
      subchart2.data = basic_chart.data.copy()
      chart = subchart1 | subchart2
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct_consolidated = chart.to_dict()
  
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct_standard = chart.to_dict()
  
      assert "datasets" in dct_consolidated
      assert "datasets" not in dct_standard
  
      datasets = dct_consolidated["datasets"]
  
      # two dataset copies should be recognized as duplicates
      assert len(datasets) == 1
  
      # make sure data matches original & names are correct
      name, data = datasets.popitem()
  
      for spec in dct_standard["hconcat"]:
          assert spec["data"]["values"] == data
  
      for spec in dct_consolidated["hconcat"]:
          assert spec["data"] == {"name": name}
  
  
  def test_consolidate_InlineData():
      data = alt.InlineData(
          values=[{"a": 1, "b": 1}, {"a": 2, "b": 2}], format={"type": "csv"}
      )
      chart = alt.Chart(data).mark_point()
  
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = chart.to_dict()
      assert dct["data"]["format"] == data.format
      assert dct["data"]["values"] == data.values
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = chart.to_dict()
      assert dct["data"]["format"] == data.format
      assert list(dct["datasets"].values())[0] == data.values
  
      data = alt.InlineData(values=[], name="runtime_data")
      chart = alt.Chart(data).mark_point()
  
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = chart.to_dict()
      assert dct["data"] == data.to_dict()
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = chart.to_dict()
      assert dct["data"] == data.to_dict()
  
  
  def test_repeat():
      # wrapped repeat
      chart1 = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(
              x=alt.X(alt.repeat(), type="quantitative"),
              y="y:Q",
          )
          .repeat(["A", "B", "C", "D"], columns=2)
      )
  
      dct1 = chart1.to_dict()
  
      assert dct1["repeat"] == ["A", "B", "C", "D"]
      assert dct1["columns"] == 2
      assert dct1["spec"]["encoding"]["x"]["field"] == {"repeat": "repeat"}
  
      # explicit row/col repeat
      chart2 = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(
              x=alt.X(alt.repeat("row"), type="quantitative"),
              y=alt.Y(alt.repeat("column"), type="quantitative"),
          )
          .repeat(row=["A", "B", "C"], column=["C", "B", "A"])
      )
  
      dct2 = chart2.to_dict()
  
      assert dct2["repeat"] == {"row": ["A", "B", "C"], "column": ["C", "B", "A"]}
      assert "columns" not in dct2
      assert dct2["spec"]["encoding"]["x"]["field"] == {"repeat": "row"}
      assert dct2["spec"]["encoding"]["y"]["field"] == {"repeat": "column"}
  
  
  def test_data_property():
      data = pd.DataFrame({"x": [1, 2, 3], "y": list("ABC")})
      chart1 = alt.Chart(data).mark_point()
      chart2 = alt.Chart().mark_point().properties(data=data)
  
      assert chart1.to_dict() == chart2.to_dict()
  
  
  @pytest.mark.parametrize("method", ["layer", "hconcat", "vconcat", "concat"])
  @pytest.mark.parametrize(
      "data", ["data.json", pd.DataFrame({"x": range(3), "y": list("abc")})]
  )
  def test_subcharts_with_same_data(method, data):
      func = getattr(alt, method)
  
      point = alt.Chart(data).mark_point().encode(x="x:Q", y="y:Q")
      line = point.mark_line()
      text = point.mark_text()
  
      chart1 = func(point, line, text)
      assert chart1.data is not alt.Undefined
      assert all(c.data is alt.Undefined for c in getattr(chart1, method))
  
      if method != "concat":
          op = OP_DICT[method]
          chart2 = op(op(point, line), text)
          assert chart2.data is not alt.Undefined
          assert all(c.data is alt.Undefined for c in getattr(chart2, method))
  
  
  @pytest.mark.parametrize("method", ["layer", "hconcat", "vconcat", "concat"])
  @pytest.mark.parametrize(
      "data", ["data.json", pd.DataFrame({"x": range(3), "y": list("abc")})]
  )
  def test_subcharts_different_data(method, data):
      func = getattr(alt, method)
  
      point = alt.Chart(data).mark_point().encode(x="x:Q", y="y:Q")
      otherdata = alt.Chart("data.csv").mark_point().encode(x="x:Q", y="y:Q")
      nodata = alt.Chart().mark_point().encode(x="x:Q", y="y:Q")
  
      chart1 = func(point, otherdata)
      assert chart1.data is alt.Undefined
      assert getattr(chart1, method)[0].data is data
  
      chart2 = func(point, nodata)
      assert chart2.data is alt.Undefined
      assert getattr(chart2, method)[0].data is data
  
  
  def test_layer_facet(basic_chart):
      chart = (basic_chart + basic_chart).facet(row="row:Q")
      assert chart.data is not alt.Undefined
      assert chart.spec.data is alt.Undefined
      for layer in chart.spec.layer:
          assert layer.data is alt.Und
  
  [... the rest is omitted ...]
files: altair/vegalite/v5/tests/test_api.py
files: """Unit tests for altair API"""
  
  import io
  import json
  import operator
  import os
  import pathlib
  import tempfile
  
  import jsonschema
  import pytest
  import pandas as pd
  
  import altair.vegalite.v5 as alt
  
  try:
      import altair_saver  # noqa: F401
  except ImportError:
      altair_saver = None
  
  
  def getargs(*args, **kwargs):
      return args, kwargs
  
  
  OP_DICT = {
      "layer": operator.add,
      "hconcat": operator.or_,
      "vconcat": operator.and_,
  }
  
  
  def _make_chart_type(chart_type):
      data = pd.DataFrame(
          {
              "x": [28, 55, 43, 91, 81, 53, 19, 87],
              "y": [43, 91, 81, 53, 19, 87, 52, 28],
              "color": list("AAAABBBB"),
          }
      )
      base = (
          alt.Chart(data)
          .mark_point()
          .encode(
              x="x",
              y="y",
              color="color",
          )
      )
  
      if chart_type in ["layer", "hconcat", "vconcat", "concat"]:
          func = getattr(alt, chart_type)
          return func(base.mark_square(), base.mark_circle())
      elif chart_type == "facet":
          return base.facet("color")
      elif chart_type == "facet_encoding":
          return base.encode(facet="color")
      elif chart_type == "repeat":
          return base.encode(alt.X(alt.repeat(), type="quantitative")).repeat(["x", "y"])
      elif chart_type == "chart":
          return base
      else:
          raise ValueError("chart_type='{}' is not recognized".format(chart_type))
  
  
  @pytest.fixture
  def basic_chart():
      data = pd.DataFrame(
          {
              "a": ["A", "B", "C", "D", "E", "F", "G", "H", "I"],
              "b": [28, 55, 43, 91, 81, 53, 19, 87, 52],
          }
      )
  
      return alt.Chart(data).mark_bar().encode(x="a", y="b")
  
  
  def test_chart_data_types():
      def Chart(data):
          return alt.Chart(data).mark_point().encode(x="x:Q", y="y:Q")
  
      # Url Data
      data = "/path/to/my/data.csv"
      dct = Chart(data).to_dict()
      assert dct["data"] == {"url": data}
  
      # Dict Data
      data = {"values": [{"x": 1, "y": 2}, {"x": 2, "y": 3}]}
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = Chart(data).to_dict()
      assert dct["data"] == data
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = Chart(data).to_dict()
      name = dct["data"]["name"]
      assert dct["datasets"][name] == data["values"]
  
      # DataFrame data
      data = pd.DataFrame({"x": range(5), "y": range(5)})
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = Chart(data).to_dict()
      assert dct["data"]["values"] == data.to_dict(orient="records")
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = Chart(data).to_dict()
      name = dct["data"]["name"]
      assert dct["datasets"][name] == data.to_dict(orient="records")
  
      # Named data object
      data = alt.NamedData(name="Foo")
      dct = Chart(data).to_dict()
      assert dct["data"] == {"name": "Foo"}
  
  
  def test_chart_infer_types():
      data = pd.DataFrame(
          {
              "x": pd.date_range("2012", periods=10, freq="Y"),
              "y": range(10),
              "c": list("abcabcabca"),
          }
      )
  
      def _check_encodings(chart):
          dct = chart.to_dict()
          assert dct["encoding"]["x"]["type"] == "temporal"
          assert dct["encoding"]["x"]["field"] == "x"
          assert dct["encoding"]["y"]["type"] == "quantitative"
          assert dct["encoding"]["y"]["field"] == "y"
          assert dct["encoding"]["color"]["type"] == "nominal"
          assert dct["encoding"]["color"]["field"] == "c"
  
      # Pass field names by keyword
      chart = alt.Chart(data).mark_point().encode(x="x", y="y", color="c")
      _check_encodings(chart)
  
      # pass Channel objects by keyword
      chart = (
          alt.Chart(data)
          .mark_point()
          .encode(x=alt.X("x"), y=alt.Y("y"), color=alt.Color("c"))
      )
      _check_encodings(chart)
  
      # pass Channel objects by value
      chart = alt.Chart(data).mark_point().encode(alt.X("x"), alt.Y("y"), alt.Color("c"))
      _check_encodings(chart)
  
      # override default types
      chart = (
          alt.Chart(data)
          .mark_point()
          .encode(alt.X("x", type="nominal"), alt.Y("y", type="ordinal"))
      )
      dct = chart.to_dict()
      assert dct["encoding"]["x"]["type"] == "nominal"
      assert dct["encoding"]["y"]["type"] == "ordinal"
  
  
  @pytest.mark.parametrize(
      "args, kwargs",
      [
          getargs(detail=["value:Q", "name:N"], tooltip=["value:Q", "name:N"]),
          getargs(detail=["value", "name"], tooltip=["value", "name"]),
          getargs(alt.Detail(["value:Q", "name:N"]), alt.Tooltip(["value:Q", "name:N"])),
          getargs(alt.Detail(["value", "name"]), alt.Tooltip(["value", "name"])),
          getargs(
              [alt.Detail("value:Q"), alt.Detail("name:N")],
              [alt.Tooltip("value:Q"), alt.Tooltip("name:N")],
          ),
          getargs(
              [alt.Detail("value"), alt.Detail("name")],
              [alt.Tooltip("value"), alt.Tooltip("name")],
          ),
      ],
  )
  def test_multiple_encodings(args, kwargs):
      df = pd.DataFrame({"value": [1, 2, 3], "name": ["A", "B", "C"]})
      encoding_dct = [
          {"field": "value", "type": "quantitative"},
          {"field": "name", "type": "nominal"},
      ]
      chart = alt.Chart(df).mark_point().encode(*args, **kwargs)
      dct = chart.to_dict()
      assert dct["encoding"]["detail"] == encoding_dct
      assert dct["encoding"]["tooltip"] == encoding_dct
  
  
  def test_chart_operations():
      data = pd.DataFrame(
          {
              "x": pd.date_range("2012", periods=10, freq="Y"),
              "y": range(10),
              "c": list("abcabcabca"),
          }
      )
      chart1 = alt.Chart(data).mark_line().encode(x="x", y="y", color="c")
      chart2 = chart1.mark_point()
      chart3 = chart1.mark_circle()
      chart4 = chart1.mark_square()
  
      chart = chart1 + chart2 + chart3
      assert isinstance(chart, alt.LayerChart)
      assert len(chart.layer) == 3
      chart += chart4
      assert len(chart.layer) == 4
  
      chart = chart1 | chart2 | chart3
      assert isinstance(chart, alt.HConcatChart)
      assert len(chart.hconcat) == 3
      chart |= chart4
      assert len(chart.hconcat) == 4
  
      chart = chart1 & chart2 & chart3
      assert isinstance(chart, alt.VConcatChart)
      assert len(chart.vconcat) == 3
      chart &= chart4
      assert len(chart.vconcat) == 4
  
  
  def test_selection_to_dict():
      brush = alt.selection(type="interval")
  
      # test some value selections
      # Note: X and Y cannot have conditions
      alt.Chart("path/to/data.json").mark_point().encode(
          color=alt.condition(brush, alt.ColorValue("red"), alt.ColorValue("blue")),
          opacity=alt.condition(brush, alt.value(0.5), alt.value(1.0)),
          text=alt.condition(brush, alt.TextValue("foo"), alt.value("bar")),
      ).to_dict()
  
      # test some field selections
      # Note: X and Y cannot have conditions
      # Conditions cannot both be fields
      alt.Chart("path/to/data.json").mark_point().encode(
          color=alt.condition(brush, alt.Color("col1:N"), alt.value("blue")),
          opacity=alt.condition(brush, "col1:N", alt.value(0.5)),
          text=alt.condition(brush, alt.value("abc"), alt.Text("col2:N")),
          size=alt.condition(brush, alt.value(20), "col2:N"),
      ).to_dict()
  
  
  def test_selection_expression():
      selection = alt.selection_single(fields=["value"])
  
      assert isinstance(selection.value, alt.expr.Expression)
      assert selection.value.to_dict() == "{0}.value".format(selection.name)
  
      assert isinstance(selection["value"], alt.expr.Expression)
      assert selection["value"].to_dict() == "{0}['value']".format(selection.name)
  
      with pytest.raises(AttributeError):
          selection.__magic__
  
  
  @pytest.mark.parametrize("format", ["html", "json", "png", "svg", "pdf"])
  def test_save(format, basic_chart):
      if format in ["pdf", "png"]:
          out = io.BytesIO()
          mode = "rb"
      else:
          out = io.StringIO()
          mode = "r"
  
      if format in ["svg", "png", "pdf"]:
          if not altair_saver:
              with pytest.raises(ValueError) as err:
                  basic_chart.save(out, format=format)
              assert "github.com/altair-viz/altair_saver" in str(err.value)
              return
          elif format not in altair_saver.available_formats():
              with pytest.raises(ValueError) as err:
                  basic_chart.save(out, format=format)
              assert f"No enabled saver found that supports format='{format}'" in str(
                  err.value
              )
              return
  
      basic_chart.save(out, format=format)
      out.seek(0)
      content = out.read()
  
      if format == "json":
          assert "$schema" in json.loads(content)
      if format == "html":
          assert content.startswith("<!DOCTYPE html>")
  
      fid, filename = tempfile.mkstemp(suffix="." + format)
      os.close(fid)
  
      # test both string filenames and pathlib.Paths
      for fp in [filename, pathlib.Path(filename)]:
          try:
              basic_chart.save(fp)
              with open(fp, mode) as f:
                  assert f.read()[:1000] == content[:1000]
          finally:
              os.remove(fp)
  
  
  def test_facet_basic():
      # wrapped facet
      chart1 = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(
              x="x:Q",
              y="y:Q",
          )
          .facet("category:N", columns=2)
      )
  
      dct1 = chart1.to_dict()
  
      assert dct1["facet"] == alt.Facet("category:N").to_dict()
      assert dct1["columns"] == 2
      assert dct1["data"] == alt.UrlData("data.csv").to_dict()
  
      # explicit row/col facet
      chart2 = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(
              x="x:Q",
              y="y:Q",
          )
          .facet(row="category1:Q", column="category2:Q")
      )
  
      dct2 = chart2.to_dict()
  
      assert dct2["facet"]["row"] == alt.Facet("category1:Q").to_dict()
      assert dct2["facet"]["column"] == alt.Facet("category2:Q").to_dict()
      assert "columns" not in dct2
      assert dct2["data"] == alt.UrlData("data.csv").to_dict()
  
  
  def test_facet_parse():
      chart = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(x="x:Q", y="y:Q")
          .facet(row="row:N", column="column:O")
      )
      dct = chart.to_dict()
      assert dct["data"] == {"url": "data.csv"}
      assert "data" not in dct["spec"]
      assert dct["facet"] == {
          "column": {"field": "column", "type": "ordinal"},
          "row": {"field": "row", "type": "nominal"},
      }
  
  
  def test_facet_parse_data():
      data = pd.DataFrame({"x": range(5), "y": range(5), "row": list("abcab")})
      chart = (
          alt.Chart(data)
          .mark_point()
          .encode(x="x", y="y:O")
          .facet(row="row", column="column:O")
      )
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = chart.to_dict()
      assert "values" in dct["data"]
      assert "data" not in dct["spec"]
      assert dct["facet"] == {
          "column": {"field": "column", "type": "ordinal"},
          "row": {"field": "row", "type": "nominal"},
      }
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = chart.to_dict()
      assert "datasets" in dct
      assert "name" in dct["data"]
      assert "data" not in dct["spec"]
      assert dct["facet"] == {
          "column": {"field": "column", "type": "ordinal"},
          "row": {"field": "row", "type": "nominal"},
      }
  
  
  def test_selection():
      # test instantiation of selections
      interval = alt.selection_interval(name="selec_1")
      assert interval.selection.type == "interval"
      assert interval.name == "selec_1"
  
      single = alt.selection_single(name="selec_2")
      assert single.selection.type == "single"
      assert single.name == "selec_2"
  
      multi = alt.selection_multi(name="selec_3")
      assert multi.selection.type == "multi"
      assert multi.name == "selec_3"
  
      # test adding to chart
      chart = alt.Chart().add_selection(single)
      chart = chart.add_selection(multi, interval)
      assert set(chart.selection.keys()) == {"selec_1", "selec_2", "selec_3"}
  
      # test logical operations
      assert isinstance(single & multi, alt.Selection)
      assert isinstance(single | multi, alt.Selection)
      assert isinstance(~single, alt.Selection)
      assert isinstance((single & multi)[0].group, alt.SelectionAnd)
      assert isinstance((single | multi)[0].group, alt.SelectionOr)
      assert isinstance((~single)[0].group, alt.SelectionNot)
  
      # test that default names increment (regression for #1454)
      sel1 = alt.selection_single()
      sel2 = alt.selection_multi()
      sel3 = alt.selection_interval()
      names = {s.name for s in (sel1, sel2, sel3)}
      assert len(names) == 3
  
  
  def test_transforms():
      # aggregate transform
      agg1 = alt.AggregatedFieldDef(**{"as": "x1", "op": "mean", "field": "y"})
      agg2 = alt.AggregatedFieldDef(**{"as": "x2", "op": "median", "field": "z"})
      chart = alt.Chart().transform_aggregate([agg1], ["foo"], x2="median(z)")
      kwds = dict(aggregate=[agg1, agg2], groupby=["foo"])
      assert chart.transform == [alt.AggregateTransform(**kwds)]
  
      # bin transform
      chart = alt.Chart().transform_bin("binned", field="field", bin=True)
      kwds = {"as": "binned", "field": "field", "bin": True}
      assert chart.transform == [alt.BinTransform(**kwds)]
  
      # calcualte transform
      chart = alt.Chart().transform_calculate("calc", "datum.a * 4")
      kwds = {"as": "calc", "calculate": "datum.a * 4"}
      assert chart.transform == [alt.CalculateTransform(**kwds)]
  
      # density transform
      chart = alt.Chart().transform_density("x", as_=["value", "density"])
      kwds = {"as": ["value", "density"], "density": "x"}
      assert chart.transform == [alt.DensityTransform(**kwds)]
  
      # filter transform
      chart = alt.Chart().transform_filter("datum.a < 4")
      assert chart.transform == [alt.FilterTransform(filter="datum.a < 4")]
  
      # flatten transform
      chart = alt.Chart().transform_flatten(["A", "B"], ["X", "Y"])
      kwds = {"as": ["X", "Y"], "flatten": ["A", "B"]}
      assert chart.transform == [alt.FlattenTransform(**kwds)]
  
      # fold transform
      chart = alt.Chart().transform_fold(["A", "B", "C"], as_=["key", "val"])
      kwds = {"as": ["key", "val"], "fold": ["A", "B", "C"]}
      assert chart.transform == [alt.FoldTransform(**kwds)]
  
      # impute transform
      chart = alt.Chart().transform_impute("field", "key", groupby=["x"])
      kwds = {"impute": "field", "key": "key", "groupby": ["x"]}
      assert chart.transform == [alt.ImputeTransform(**kwds)]
  
      # joinaggregate transform
      chart = alt.Chart().transform_joinaggregate(min="min(x)", groupby=["key"])
      kwds = {
          "joinaggregate": [
              alt.JoinAggregateFieldDef(field="x", op="min", **{"as": "min"})
          ],
          "groupby": ["key"],
      }
      assert chart.transform == [alt.JoinAggregateTransform(**kwds)]
  
      # loess transform
      chart = alt.Chart().transform_loess("x", "y", as_=["xx", "yy"])
      kwds = {"on": "x", "loess": "y", "as": ["xx", "yy"]}
      assert chart.transform == [alt.LoessTransform(**kwds)]
  
      # lookup transform (data)
      lookup_data = alt.LookupData(alt.UrlData("foo.csv"), "id", ["rate"])
      chart = alt.Chart().transform_lookup("a", from_=lookup_data, as_="a", default="b")
      kwds = {"from": lookup_data, "as": "a", "lookup": "a", "default": "b"}
      assert chart.transform == [alt.LookupTransform(**kwds)]
  
      # lookup transform (selection)
      lookup_selection = alt.LookupSelection(key="key", selection="sel")
      chart = alt.Chart().transform_lookup(
          "a", from_=lookup_selection, as_="a", default="b"
      )
      kwds = {"from": lookup_selection, "as": "a", "lookup": "a", "default": "b"}
      assert chart.transform == [alt.LookupTransform(**kwds)]
  
      # pivot transform
      chart = alt.Chart().transform_pivot("x", "y")
      assert chart.transform == [alt.PivotTransform(pivot="x", value="y")]
  
      # quantile transform
      chart = alt.Chart().transform_quantile("x", as_=["prob", "value"])
      kwds = {"quantile": "x", "as": ["prob", "value"]}
      assert chart.transform == [alt.QuantileTransform(**kwds)]
  
      # regression transform
      chart = alt.Chart().transform_regression("x", "y", as_=["xx", "yy"])
      kwds = {"on": "x", "regression": "y", "as": ["xx", "yy"]}
      assert chart.transform == [alt.RegressionTransform(**kwds)]
  
      # sample transform
      chart = alt.Chart().transform_sample()
      assert chart.transform == [alt.SampleTransform(1000)]
  
      # stack transform
      chart = alt.Chart().transform_stack("stacked", "x", groupby=["y"])
      assert chart.transform == [
          alt.StackTransform(stack="x", groupby=["y"], **{"as": "stacked"})
      ]
  
      # timeUnit transform
      chart = alt.Chart().transform_timeunit("foo", field="x", timeUnit="date")
      kwds = {"as": "foo", "field": "x", "timeUnit": "date"}
      assert chart.transform == [alt.TimeUnitTransform(**kwds)]
  
      # window transform
      chart = alt.Chart().transform_window(xsum="sum(x)", ymin="min(y)", frame=[None, 0])
      window = [
          alt.WindowFieldDef(**{"as": "xsum", "field": "x", "op": "sum"}),
          alt.WindowFieldDef(**{"as": "ymin", "field": "y", "op": "min"}),
      ]
  
      # kwargs don't maintain order in Python < 3.6, so window list can
      # be reversed
      assert chart.transform == [
          alt.WindowTransform(frame=[None, 0], window=window)
      ] or chart.transform == [alt.WindowTransform(frame=[None, 0], window=window[::-1])]
  
  
  def test_filter_transform_selection_predicates():
      selector1 = alt.selection_interval(name="s1")
      selector2 = alt.selection_interval(name="s2")
      base = alt.Chart("data.txt").mark_point()
  
      chart = base.transform_filter(selector1)
      assert chart.to_dict()["transform"] == [{"filter": {"selection": "s1"}}]
  
      chart = base.transform_filter(~selector1)
      assert chart.to_dict()["transform"] == [{"filter": {"selection": {"not": "s1"}}}]
  
      chart = base.transform_filter(selector1 & selector2)
      assert chart.to_dict()["transform"] == [
          {"filter": {"selection": {"and": ["s1", "s2"]}}}
      ]
  
      chart = base.transform_filter(selector1 | selector2)
      assert chart.to_dict()["transform"] == [
          {"filter": {"selection": {"or": ["s1", "s2"]}}}
      ]
  
      chart = base.transform_filter(selector1 | ~selector2)
      assert chart.to_dict()["transform"] == [
          {"filter": {"selection": {"or": ["s1", {"not": "s2"}]}}}
      ]
  
      chart = base.transform_filter(~selector1 | ~selector2)
      assert chart.to_dict()["transform"] == [
          {"filter": {"selection": {"or": [{"not": "s1"}, {"not": "s2"}]}}}
      ]
  
      chart = base.transform_filter(~(selector1 & selector2))
      assert chart.to_dict()["transform"] == [
          {"filter": {"selection": {"not": {"and": ["s1", "s2"]}}}}
      ]
  
  
  def test_resolve_methods():
      chart = alt.LayerChart().resolve_axis(x="shared", y="independent")
      assert chart.resolve == alt.Resolve(
          axis=alt.AxisResolveMap(x="shared", y="independent")
      )
  
      chart = alt.LayerChart().resolve_legend(color="shared", fill="independent")
      assert chart.resolve == alt.Resolve(
          legend=alt.LegendResolveMap(color="shared", fill="independent")
      )
  
      chart = alt.LayerChart().resolve_scale(x="shared", y="independent")
      assert chart.resolve == alt.Resolve(
          scale=alt.ScaleResolveMap(x="shared", y="independent")
      )
  
  
  def test_layer_encodings():
      chart = alt.LayerChart().encode(x="column:Q")
      assert chart.encoding.x == alt.X(shorthand="column:Q")
  
  
  def test_add_selection():
      selections = [
          alt.selection_interval(),
          alt.selection_single(),
          alt.selection_multi(),
      ]
      chart = (
          alt.Chart()
          .mark_point()
          .add_selection(selections[0])
          .add_selection(selections[1], selections[2])
      )
      expected = {s.name: s.selection for s in selections}
      assert chart.selection == expected
  
  
  def test_repeat_add_selections():
      base = alt.Chart("data.csv").mark_point()
      selection = alt.selection_single()
      chart1 = base.add_selection(selection).repeat(list("ABC"))
      chart2 = base.repeat(list("ABC")).add_selection(selection)
      assert chart1.to_dict() == chart2.to_dict()
  
  
  def test_facet_add_selections():
      base = alt.Chart("data.csv").mark_point()
      selection = alt.selection_single()
      chart1 = base.add_selection(selection).facet("val:Q")
      chart2 = base.facet("val:Q").add_selection(selection)
      assert chart1.to_dict() == chart2.to_dict()
  
  
  def test_layer_add_selection():
      base = alt.Chart("data.csv").mark_point()
      selection = alt.selection_single()
      chart1 = alt.layer(base.add_selection(selection), base)
      chart2 = alt.layer(base, base).add_selection(selection)
      assert chart1.to_dict() == chart2.to_dict()
  
  
  @pytest.mark.parametrize("charttype", [alt.concat, alt.hconcat, alt.vconcat])
  def test_compound_add_selections(charttype):
      base = alt.Chart("data.csv").mark_point()
      selection = alt.selection_single()
      chart1 = charttype(base.add_selection(selection), base.add_selection(selection))
      chart2 = charttype(base, base).add_selection(selection)
      assert chart1.to_dict() == chart2.to_dict()
  
  
  def test_selection_property():
      sel = alt.selection_interval()
      chart = alt.Chart("data.csv").mark_point().properties(selection=sel)
  
      assert list(chart["selection"].keys()) == [sel.name]
  
  
  def test_LookupData():
      df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})
      lookup = alt.LookupData(data=df, key="x")
  
      dct = lookup.to_dict()
      assert dct["key"] == "x"
      assert dct["data"] == {
          "values": [{"x": 1, "y": 4}, {"x": 2, "y": 5}, {"x": 3, "y": 6}]
      }
  
  
  def test_themes():
      chart = alt.Chart("foo.txt").mark_point()
  
      with alt.themes.enable("default"):
          assert chart.to_dict()["config"] == {
              "view": {"continuousWidth": 400, "continuousHeight": 300}
          }
  
      with alt.themes.enable("opaque"):
          assert chart.to_dict()["config"] == {
              "background": "white",
              "view": {"continuousWidth": 400, "continuousHeight": 300},
          }
  
      with alt.themes.enable("none"):
          assert "config" not in chart.to_dict()
  
  
  def test_chart_from_dict():
      base = alt.Chart("data.csv").mark_point().encode(x="x:Q", y="y:Q")
  
      charts = [
          base,
          base + base,
          base | base,
          base & base,
          base.facet("c:N"),
          (base + base).facet(row="c:N", data="data.csv"),
          base.repeat(["c", "d"]),
          (base + base).repeat(row=["c", "d"]),
      ]
  
      for chart in charts:
          chart_out = alt.Chart.from_dict(chart.to_dict())
          assert type(chart_out) is type(chart)
  
      # test that an invalid spec leads to a schema validation error
      with pytest.raises(jsonschema.ValidationError):
          alt.Chart.from_dict({"invalid": "spec"})
  
  
  def test_consolidate_datasets(basic_chart):
      subchart1 = basic_chart
      subchart2 = basic_chart.copy()
      subchart2.data = basic_chart.data.copy()
      chart = subchart1 | subchart2
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct_consolidated = chart.to_dict()
  
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct_standard = chart.to_dict()
  
      assert "datasets" in dct_consolidated
      assert "datasets" not in dct_standard
  
      datasets = dct_consolidated["datasets"]
  
      # two dataset copies should be recognized as duplicates
      assert len(datasets) == 1
  
      # make sure data matches original & names are correct
      name, data = datasets.popitem()
  
      for spec in dct_standard["hconcat"]:
          assert spec["data"]["values"] == data
  
      for spec in dct_consolidated["hconcat"]:
          assert spec["data"] == {"name": name}
  
  
  def test_consolidate_InlineData():
      data = alt.InlineData(
          values=[{"a": 1, "b": 1}, {"a": 2, "b": 2}], format={"type": "csv"}
      )
      chart = alt.Chart(data).mark_point()
  
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = chart.to_dict()
      assert dct["data"]["format"] == data.format
      assert dct["data"]["values"] == data.values
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = chart.to_dict()
      assert dct["data"]["format"] == data.format
      assert list(dct["datasets"].values())[0] == data.values
  
      data = alt.InlineData(values=[], name="runtime_data")
      chart = alt.Chart(data).mark_point()
  
      with alt.data_transformers.enable(consolidate_datasets=False):
          dct = chart.to_dict()
      assert dct["data"] == data.to_dict()
  
      with alt.data_transformers.enable(consolidate_datasets=True):
          dct = chart.to_dict()
      assert dct["data"] == data.to_dict()
  
  
  def test_repeat():
      # wrapped repeat
      chart1 = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(
              x=alt.X(alt.repeat(), type="quantitative"),
              y="y:Q",
          )
          .repeat(["A", "B", "C", "D"], columns=2)
      )
  
      dct1 = chart1.to_dict()
  
      assert dct1["repeat"] == ["A", "B", "C", "D"]
      assert dct1["columns"] == 2
      assert dct1["spec"]["encoding"]["x"]["field"] == {"repeat": "repeat"}
  
      # explicit row/col repeat
      chart2 = (
          alt.Chart("data.csv")
          .mark_point()
          .encode(
              x=alt.X(alt.repeat("row"), type="quantitative"),
              y=alt.Y(alt.repeat("column"), type="quantitative"),
          )
          .repeat(row=["A", "B", "C"], column=["C", "B", "A"])
      )
  
      dct2 = chart2.to_dict()
  
      assert dct2["repeat"] == {"row": ["A", "B", "C"], "column": ["C", "B", "A"]}
      assert "columns" not in dct2
      assert dct2["spec"]["encoding"]["x"]["field"] == {"repeat": "row"}
      assert dct2["spec"]["encoding"]["y"]["field"] == {"repeat": "column"}
  
  
  def test_data_property():
      data = pd.DataFrame({"x": [1, 2, 3], "y": list("ABC")})
      chart1 = alt.Chart(data).mark_point()
      chart2 = alt.Chart().mark_point().properties(data=data)
  
      assert chart1.to_dict() == chart2.to_dict()
  
  
  @pytest.mark.parametrize("method", ["layer", "hconcat", "vconcat", "concat"])
  @pytest.mark.parametrize(
      "data", ["data.json", pd.DataFrame({"x": range(3), "y": list("abc")})]
  )
  def test_subcharts_with_same_data(method, data):
      func = getattr(alt, method)
  
      point = alt.Chart(data).mark_point().encode(x="x:Q", y="y:Q")
      line = point.mark_line()
      text = point.mark_text()
  
      chart1 = func(point, line, text)
      assert chart1.data is not alt.Undefined
      assert all(c.data is alt.Undefined for c in getattr(chart1, method))
  
      if method != "concat":
          op = OP_DICT[method]
          chart2 = op(op(point, line), text)
          assert chart2.data is not alt.Undefined
          assert all(c.data is alt.Undefined for c in getattr(chart2, method))
  
  
  @pytest.mark.parametrize("method", ["layer", "hconcat", "vconcat", "concat"])
  @pytest.mark.parametrize(
      "data", ["data.json", pd.DataFrame({"x": range(3), "y": list("abc")})]
  )
  def test_subcharts_different_data(method, data):
      func = getattr(alt, method)
  
      point = alt.Chart(data).mark_point().encode(x="x:Q", y="y:Q")
      otherdata = alt.Chart("data.csv").mark_point().encode(x="x:Q", y="y:Q")
      nodata = alt.Chart().mark_point().encode(x="x:Q", y="y:Q")
  
      chart1 = func(point, otherdata)
      assert chart1.data is alt.Undefined
      assert getattr(chart1, method)[0].data is data
  
      chart2 = func(point, nodata)
      assert chart2.data is alt.Undefined
      assert getattr(chart2, method)[0].data is data
  
  
  def test_layer_facet(basic_chart):
      chart = (basic_chart + basic_chart).facet(row="row:Q")
      assert chart.data is not alt.Undefined
      assert chart.spec.data is alt.Undefined
      for layer in chart.spec.layer:
          assert layer.data is alt.Undefined
  
      dct = chart.to_dict()
      assert "data" in dct
  
  
  def test_layer_errors():
  
  
  [... the rest is omitted ...]
files: altair/vegalite/v5/tests/test_api.py