Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: point selection for altair heat maps

Myles Scolnick6a7ed85ea4d3
diff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.py
index cbc456df59..47af7c72be 100644
--- a/marimo/_plugins/ui/_impl/altair_chart.py
+++ b/marimo/_plugins/ui/_impl/altair_chart.py
@@ -40,6 +40,7 @@ LOGGER = _loggers.marimo_logger()
if TYPE_CHECKING:
import altair
import altair.vegalite
+ from narwhals import Schema
# Selection is a dictionary of the form:
# {
@@ -116,6 +117,102 @@ def _using_vegafusion() -> bool:
return altair.data_transformers.active.startswith("vegafusion") # type: ignore
+def _combine_conditions_with_and(
+ conditions: list[nw.Expr],
+) -> Optional[nw.Expr]:
+ """Combine multiple narwhals expressions with AND logic."""
+ if not conditions:
+ return None
+ if len(conditions) == 1:
+ return conditions[0]
+
+ combined = conditions[0]
+ for cond in conditions[1:]:
+ combined = combined & cond
+ return combined
+
+
+def _combine_conditions_with_or(
+ conditions: list[nw.Expr],
+) -> Optional[nw.Expr]:
+ """Combine multiple narwhals expressions with OR logic."""
+ if not conditions:
+ return None
+ if len(conditions) == 1:
+ return conditions[0]
+
+ combined = conditions[0]
+ for cond in conditions[1:]:
+ combined = combined | cond
+ return combined
+
+
+def _build_point_filter(
+ point: dict[str, Any], schema: Schema
+) -> Optional[nw.Expr]:
+ """Build a filter expression for a single point selection."""
+ field_conditions: list[nw.Expr] = []
+ names = schema.names()
+
+ for field, value in point.items():
+ if field not in names:
+ continue
+
+ dtype = schema[field]
+ field_conditions.append(nw.col(field) == _coerce_value(value, dtype))
+
+ return _combine_conditions_with_and(field_conditions)
+
+
+def _try_apply_multipoint_filter(
+ df: nw.LazyFrame[Any],
+ fields: ChartSelectionField,
+ schema: Schema,
+) -> Optional[nw.LazyFrame[Any]]:
+ """Try to apply multi-point selection filter using vlPoint.or structure.
+
+ This handles the case where multiple points are selected, avoiding
+ a Cartesian product by using the exact point combinations from vlPoint.or.
+ """
+ vl_point: dict[str, Any] = cast(dict[str, Any], fields.get("vlPoint", {}))
+ if not isinstance(vl_point, dict) or "or" not in vl_point:
+ return None
+
+ point_combinations = vl_point["or"]
+ if (
+ not isinstance(point_combinations, list)
+ or len(point_combinations) == 0
+ ):
+ return None
+
+ # Build a filter for each exact point combination
+ point_filters: list[nw.Expr] = []
+
+ for point in point_combinations:
+ if not isinstance(point, dict):
+ continue
+
+ point_filter = _build_point_filter(point, schema)
+ if point_filter is not None:
+ point_filters.append(point_filter)
+
+ # Apply the combined filter (OR of all point combinations)
+ if not point_filters:
+ return None
+
+ try:
+ combined_filter = _combine_conditions_with_or(point_filters)
+ if combined_filter is None:
+ return None
+ return df.filter(combined_filter)
+ except (TypeError, ValueError, Exception) as e:
+ LOGGER.error(
+ f"Error applying multi-point filter: {e}. "
+ "Falling back to field-by-field filtering."
+ )
+ return None
+
+
def _filter_dataframe(
native_df: Union[IntoDataFrame, IntoLazyFrame],
*,
@@ -163,6 +260,16 @@ def _filter_dataframe(
# then the selection is a point selection
# otherwise, it is an interval selection
is_point_selection = "vlPoint" in fields
+ schema = df.collect_schema()
+
+ # Handle multi-point selections properly using vlPoint.or structure
+ # to avoid Cartesian product when selecting multiple points
+ if is_point_selection:
+ filtered_df = _try_apply_multipoint_filter(df, fields, schema)
+ if filtered_df is not None:
+ df = filtered_df
+ continue
+
for field, values in fields.items():
# values may come back as strings if using the CSV transformer;
# convert back to original datatype
@@ -173,7 +280,6 @@ def _filter_dataframe(
is_binned = field in binned_fields
# Need to collect schema to check columns and dtypes
- schema = df.collect_schema()
if field not in schema.names():
raise ValueError(f"Field '{field}' not found in DataFrame")
@@ -262,57 +368,58 @@ def _filter_dataframe(
return undo_df(df)
-def _resolve_values(values: Any, dtype: Any) -> list[Any]:
- def _coerce_value(value: Any, dtype: Any) -> Any:
- import zoneinfo
+def _coerce_value(value: Any, dtype: Any) -> Any:
+ import zoneinfo
- try:
- if nw.Date == dtype:
- if isinstance(value, str):
- return datetime.date.fromisoformat(value)
- # Value is milliseconds since epoch
- # so we convert to seconds since epoch
- if isinstance(value, (int, float)):
- return datetime.date.fromtimestamp(value / 1000)
- # If value is already a date or datetime, return as-is
- if isinstance(value, datetime.date):
- return value
- # Otherwise, try to convert to string then parse
- return datetime.date.fromisoformat(str(value))
- if nw.Datetime == dtype and isinstance(dtype, nw.Datetime):
- if isinstance(value, str):
- res = datetime.datetime.fromisoformat(value)
- # If dtype has no timezone, but value has timezone, remove timezone without shifting
- if dtype.time_zone is None and res.tzinfo is not None:
- return res.replace(tzinfo=None)
- return res
-
- # Value is milliseconds since epoch
- # so we convert to seconds since epoch
- if isinstance(value, (int, float)):
- return datetime.datetime.fromtimestamp(
- value / 1000,
- tz=(
- zoneinfo.ZoneInfo(dtype.time_zone)
- if dtype.time_zone
- else None
- ),
- )
- # If value is already a datetime, return as-is
- if isinstance(value, datetime.datetime):
- return value
- # Otherwise, try to convert to string then parse
- return datetime.datetime.fromisoformat(str(value))
- except (ValueError, TypeError, OSError) as e:
- # Log the error but return the original value
- # to avoid breaking the filter entirely
- LOGGER.warning(
- f"Failed to coerce value {value!r} to {dtype}: {e}. "
- "Using original value."
- )
- return value
+ try:
+ if nw.Date == dtype:
+ if isinstance(value, str):
+ return datetime.date.fromisoformat(value)
+ # Value is milliseconds since epoch
+ # so we convert to seconds since epoch
+ if isinstance(value, (int, float)):
+ return datetime.date.fromtimestamp(value / 1000)
+ # If value is already a date or datetime, return as-is
+ if isinstance(value, datetime.date):
+ return value
+ # Otherwise, try to convert to string then parse
+ return datetime.date.fromisoformat(str(value))
+ if nw.Datetime == dtype and isinstance(dtype, nw.Datetime):
+ if isinstance(value, str):
+ res = datetime.datetime.fromisoformat(value)
+ # If dtype has no timezone, but value has timezone, remove timezone without shifting
+ if dtype.time_zone is None and res.tzinfo is not None:
+ return res.replace(tzinfo=None)
+ return res
+
+ # Value is milliseconds since epoch
+ # so we convert to seconds since epoch
+ if isinstance(value, (int, float)):
+ return datetime.datetime.fromtimestamp(
+ value / 1000,
+ tz=(
+ zoneinfo.ZoneInfo(dtype.time_zone)
+ if dtype.time_zone
+ else None
+ ),
+ )
+ # If value is already a datetime, return as-is
+ if isinstance(value, datetime.datetime):
+ return value
+ # Otherwise, try to convert to string then parse
+ return datetime.datetime.fromisoformat(str(value))
+ except (ValueError, TypeError, OSError) as e:
+ # Log the error but return the original value
+ # to avoid breaking the filter entirely
+ LOGGER.warning(
+ f"Failed to coerce value {value!r} to {dtype}: {e}. "
+ "Using original value."
+ )
return value
+ return value
+
+def _resolve_values(values: Any, dtype: Any) -> list[Any]:
if isinstance(values, list):
return [_coerce_value(v, dtype) for v in values]
return [_coerce_value(values, dtype)]
diff --git a/marimo/_smoke_tests/issues/7661_point_selection.py b/marimo/_smoke_tests/issues/7661_point_selection.py
new file mode 100644
index 0000000000..5fbb631980
--- /dev/null
+++ b/marimo/_smoke_tests/issues/7661_point_selection.py
@@ -0,0 +1,69 @@
+import marimo
+
+__generated_with = "0.18.4"
+app = marimo.App()
+
+
+def _():
+ import marimo as mo
+ import pandas as pd
+ import numpy as np
+ import altair as alt
+ return alt, mo, np, pd
+
+
+def _(np, pd):
+ # Create mock data for the heatmap
+ np.random.seed(42)
+ rows = 10
+ cols = 8
+ x_labels = [f"X{i}" for i in range(1, cols + 1)]
+ y_labels = [f"Y{j}" for j in range(1, rows + 1)]
+
+ data = []
+ for y in y_labels:
+ for x in x_labels:
+ value = np.random.randint(0, 100)
+ data.append({"X": x, "Y": y, "Value": value})
+
+ heatmap_df = pd.DataFrame(data)
+ heatmap_df
+ return (heatmap_df,)
+
+
+def _(alt, heatmap_df, mo):
+ # Create an interactive Altair heatmap with multi-point selection
+ selection = alt.selection_point(
+ fields=["X", "Y"], bind="legend", toggle=True, clear="click"
+ )
+
+ heatmap_chart = (
+ alt.Chart(heatmap_df)
+ .mark_rect()
+ .encode(
+ x=alt.X("X:O", title="X Label"),
+ y=alt.Y("Y:O", title="Y Label"),
+ color=alt.Color("Value:Q", scale=alt.Scale(scheme="viridis")),
+ tooltip=["X", "Y", "Value"],
+ )
+ .add_params(selection)
+ .encode(opacity=alt.condition(selection, alt.value(1), alt.value(0.3)))
+ )
+
+ heatmap_ui = mo.ui.altair_chart(heatmap_chart)
+ heatmap_ui
+ return (heatmap_ui,)
+
+
+def _(heatmap_ui):
+ # Display the selected points from the heatmap
+ heatmap_ui.value
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_plugins/ui/_impl/test_altair_chart.py b/tests/_plugins/ui/_impl/test_altair_chart.py
index 937d00038e..0820b2cbd7 100644
--- a/tests/_plugins/ui/_impl/test_altair_chart.py
+++ b/tests/_plugins/ui/_impl/test_altair_chart.py
@@ -131,6 +131,78 @@ class TestAltairChart:
)
assert get_len(filtered_df) == 1
+ @staticmethod
+ @pytest.mark.parametrize(
+ "df",
+ create_dataframes(
+ {
+ "X": ["X1", "X2", "X3", "X4", "X5"],
+ "Y": ["Y1", "Y2", "Y3", "Y4", "Y5"],
+ "Value": [10, 20, 30, 40, 50],
+ },
+ ),
+ )
+ def test_multi_point_selection_with_vlpoint_or(df: ChartDataType) -> None:
+ """Test multi-point selection with vlPoint.or structure.
+
+ This tests the fix for the issue where selecting multiple points
+ with different X and Y values caused a Cartesian product instead
+ of selecting only the exact points.
+ """
+ # Simulate selecting two specific points: (X2, Y2) and (X4, Y4)
+ # This mimics what Altair sends when shift-clicking multiple points
+ multi_point_selection: ChartSelection = {
+ "select_point": {
+ "X": ["X2", "X4"],
+ "Y": ["Y2", "Y4"],
+ "vlPoint": {
+ "or": [
+ {"X": "X2", "Y": "Y2"},
+ {"X": "X4", "Y": "Y4"},
+ ]
+ },
+ }
+ }
+
+ # Filter the DataFrame with the multi-point selection
+ filtered_df = _filter_dataframe(df, selection=multi_point_selection)
+
+ # Should get exactly 2 rows (the two selected points)
+ # NOT 4 rows (which would be the Cartesian product)
+ assert get_len(filtered_df) == 2
+
+ # Verify the exact points are selected
+ collected = maybe_collect(filtered_df)
+ x_values = sorted(collected["X"].to_list())
+ y_values = sorted(collected["Y"].to_list())
+ assert x_values == ["X2", "X4"]
+ assert y_values == ["Y2", "Y4"]
+
+ # Test with three points to ensure it works with more selections
+ three_point_selection: ChartSelection = {
+ "select_point": {
+ "X": ["X1", "X3", "X5"],
+ "Y": ["Y1", "Y3", "Y5"],
+ "vlPoint": {
+ "or": [
+ {"X": "X1", "Y": "Y1"},
+ {"X": "X3", "Y": "Y3"},
+ {"X": "X5", "Y": "Y5"},
+ ]
+ },
+ }
+ }
+
+ filtered_df = _filter_dataframe(df, selection=three_point_selection)
+ # Should get exactly 3 rows, not 9 (which would be 3x3 Cartesian product)
+ assert get_len(filtered_df) == 3
+
+ collected = maybe_collect(filtered_df)
+ x_values = sorted(collected["X"].to_list())
+ y_values = sorted(collected["Y"].to_list())
+ assert x_values == ["X1", "X3", "X5"]
+ assert y_values == ["Y1", "Y3", "Y5"]
+
@staticmethod
@pytest.mark.parametrize(
"df",
@@ -171,7 +243,7 @@ class TestAltairChart:
) -> None:
assert (
nw.Datetime
- == nw.from_native(df, pass_through=False).schema["datetime_column"]
+ == nw.from_native(df).collect_schema()["datetime_column"]
)
# Define an interval selection