This is an automated email from the ASF dual-hosted git repository.

linxinyuan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/texera.git


The following commit(s) were added to refs/heads/main by this push:
     new 866fa8a09b feat: add a new choropleth map operator (#3744)
866fa8a09b is described below

commit 866fa8a09be066d85b60acca398a2c636e9d41c6
Author: Grace Chia <[email protected]>
AuthorDate: Fri Sep 19 16:32:28 2025 -0700

    feat: add a new choropleth map operator (#3744)
    
    - Purpose of the PR:
    - Texera doesn't have any map operators, limiting its visualization of
    geographical data. Choropleth maps use differences in shading or color
    to dynamically show differences in properties or quantities between
    regions. For example, below is a choropleth map showing life expectancy
    in 2007 across different countries (generated by my new operator!)
    
    <img width="651" height="268" alt="Screenshot 2025-09-19 at 3 20 08 PM"
    
src="https://github.com/user-attachments/assets/26356dd0-6857-4c5e-836f-97c60d3f838b";
    />
    
    - Choropleth maps are simple to understand yet powerful. Because of this
    they are an excellent first step for geographical visualizations in
    Texera.
    
    - Summary of changes:
      - Adding ChoroplethMapOpDesc.scala with the operator logic
    - locations column currently only supports countries and needs to be a
    [three-letter ISO country
    code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3)
      - Adding an icon for the operator
    - Registering the operator in the logical and physical layers in the
    LogicalOp.scala
    
    - Future Work
    - adding option to change region level (right now it is by country, but
    could add by state for US-specific)
      - adding option to focus on specific region
    
    - Screenshots
        Choropleth map UI
    <img width="1324" height="926" alt="Screenshot 2025-09-19 at 3 27 32 PM"
    
src="https://github.com/user-attachments/assets/a4f247b0-1a97-479e-a262-32583087f640";
    />
    
    ---------
    
    Co-authored-by: Xinyuan Lin <[email protected]>
---
 .../src/assets/operator_images/ChoroplethMap.png   | Bin 0 -> 140957 bytes
 .../edu/uci/ics/amber/operator/LogicalOp.scala     |   2 +
 .../choroplethMap/ChoroplethMapOpDesc.scala        | 126 +++++++++++++++++++++
 3 files changed, 128 insertions(+)

diff --git a/core/gui/src/assets/operator_images/ChoroplethMap.png 
b/core/gui/src/assets/operator_images/ChoroplethMap.png
new file mode 100644
index 0000000000..ab08519e00
Binary files /dev/null and 
b/core/gui/src/assets/operator_images/ChoroplethMap.png differ
diff --git 
a/core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/LogicalOp.scala
 
b/core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/LogicalOp.scala
index 95bb2f87d6..6aa57f2574 100644
--- 
a/core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/LogicalOp.scala
+++ 
b/core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/LogicalOp.scala
@@ -124,6 +124,7 @@ import 
edu.uci.ics.amber.operator.visualization.boxViolinPlot.BoxViolinPlotOpDes
 import edu.uci.ics.amber.operator.visualization.bubbleChart.BubbleChartOpDesc
 import edu.uci.ics.amber.operator.visualization.bulletChart.BulletChartOpDesc
 import 
edu.uci.ics.amber.operator.visualization.candlestickChart.CandlestickChartOpDesc
+import 
edu.uci.ics.amber.operator.visualization.choroplethMap.ChoroplethMapOpDesc
 import 
edu.uci.ics.amber.operator.visualization.continuousErrorBands.ContinuousErrorBandsOpDesc
 import edu.uci.ics.amber.operator.visualization.contourPlot.ContourPlotOpDesc
 import edu.uci.ics.amber.operator.visualization.dumbbellPlot.DumbbellPlotOpDesc
@@ -188,6 +189,7 @@ trait StateTransferFunc
       value = classOf[TwitterSearchSourceOpDesc],
       name = "TwitterSearch"
     ),
+    new Type(value = classOf[ChoroplethMapOpDesc], name = "ChoroplethMap"),
     new Type(value = classOf[TimeSeriesOpDesc], name = "TimeSeriesPlot"),
     new Type(value = classOf[CandlestickChartOpDesc], name = 
"CandlestickChart"),
     new Type(value = classOf[SplitOpDesc], name = "Split"),
diff --git 
a/core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/visualization/choroplethMap/ChoroplethMapOpDesc.scala
 
b/core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/visualization/choroplethMap/ChoroplethMapOpDesc.scala
new file mode 100644
index 0000000000..6cd969d6a9
--- /dev/null
+++ 
b/core/workflow-operator/src/main/scala/edu/uci/ics/amber/operator/visualization/choroplethMap/ChoroplethMapOpDesc.scala
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package edu.uci.ics.amber.operator.visualization.choroplethMap
+
+import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
+import com.kjetland.jackson.jsonSchema.annotations.{JsonSchemaInject, 
JsonSchemaTitle}
+import edu.uci.ics.amber.core.tuple.{AttributeType, Schema}
+import edu.uci.ics.amber.operator.PythonOperatorDescriptor
+import edu.uci.ics.amber.core.workflow.OutputPort.OutputMode
+import edu.uci.ics.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
+import edu.uci.ics.amber.operator.metadata.{OperatorGroupConstants, 
OperatorInfo}
+import edu.uci.ics.amber.operator.metadata.annotations.AutofillAttributeName
+
+@JsonSchemaInject(json = """
+{
+  "attributeTypeRules": {
+    "locations": {
+      "enum": ["string"]
+    },
+    "color": {
+      "enum": ["integer", "long", "double"]
+    }
+  }
+}
+""")
+class ChoroplethMapOpDesc extends PythonOperatorDescriptor {
+
+  @JsonProperty(value = "locations", required = true)
+  @JsonSchemaTitle("Locations Column")
+  @JsonPropertyDescription(
+    "Column used to describe location. Currently only supports countries and 
needs to be three-letter ISO country code"
+  )
+  @AutofillAttributeName
+  var locations: String = ""
+
+  @JsonProperty(value = "color", required = true)
+  @JsonSchemaTitle("Color Column")
+  @JsonPropertyDescription(
+    "Column used to determine intensity of color of the region"
+  )
+  @AutofillAttributeName
+  var color: String = ""
+
+  override def getOutputSchemas(
+      inputSchemas: Map[PortIdentity, Schema]
+  ): Map[PortIdentity, Schema] = {
+    val outputSchema = Schema()
+      .add("html-content", AttributeType.STRING)
+    Map(operatorInfo.outputPorts.head.id -> outputSchema)
+  }
+
+  override def operatorInfo: OperatorInfo =
+    OperatorInfo(
+      "Choropleth Map",
+      "Visualize data using a Choropleth Map that uses shades of colors to 
show differences in properties or quantities between regions",
+      OperatorGroupConstants.VISUALIZATION_ADVANCED_GROUP,
+      inputPorts = List(InputPort()),
+      outputPorts = List(OutputPort(mode = OutputMode.SINGLE_SNAPSHOT))
+    )
+
+  def manipulateTable(): String = {
+    assert(locations.nonEmpty)
+    assert(color.nonEmpty)
+    s"""
+       |        table.dropna(subset=['$locations', '$color'], inplace = True)
+       |""".stripMargin
+  }
+
+  def createPlotlyFigure(): String = {
+    assert(locations.nonEmpty && color.nonEmpty)
+    s"""
+       |        fig = px.choropleth(table, locations="$locations", 
color="$color", color_continuous_scale=px.colors.sequential.Plasma)
+       |        fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
+       |""".stripMargin
+  }
+
+  override def generatePythonCode(): String = {
+    val finalCode =
+      s"""
+         |from pytexera import *
+         |
+         |import plotly.express as px
+         |import plotly.io
+         |import plotly
+         |
+         |class ProcessTableOperator(UDFTableOperator):
+         |
+         |    # Generate custom error message as html string
+         |    def render_error(self, error_msg) -> str:
+         |        return '''<h1>Choropleth map is not available.</h1>
+         |                  <p>Reason is: {} </p>
+         |               '''.format(error_msg)
+         |
+         |    @overrides
+         |    def process_table(self, table: Table, port: int) -> 
Iterator[Optional[TableLike]]:
+         |        if table.empty:
+         |           yield {'html-content': self.render_error("Input table is 
empty.")}
+         |           return
+         |        ${manipulateTable()}
+         |        if table.empty:
+         |           yield {'html-content': self.render_error("No valid rows 
left (every row has at least 1 missing value).")}
+         |           return
+         |        ${createPlotlyFigure()}
+         |        html = plotly.io.to_html(fig, include_plotlyjs='cdn', 
auto_play=False)
+         |        yield {'html-content': html}
+         |""".stripMargin
+    finalCode
+  }
+}

Reply via email to