Skip to content

Commit 1767b49

Browse files
committed
reconfigured ternary contour op to most recent PR merge
1 parent bd73074 commit 1767b49

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/visualization/ternaryContour/TernaryContourOpDesc.scala

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
2323
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaTitle
2424
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
2525
import org.apache.texera.amber.core.workflow.OutputPort.OutputMode
26+
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
27+
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
2628
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
2729
import org.apache.texera.amber.operator.PythonOperatorDescriptor
2830
import org.apache.texera.amber.operator.metadata.annotations.AutofillAttributeName
2931
import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
32+
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder
3033

3134
/**
3235
* Visualization Operator for Ternary Plots.
@@ -41,25 +44,25 @@ class TernaryContourOpDesc extends PythonOperatorDescriptor {
4144
@JsonProperty(value = "firstVariable", required = true)
4245
@JsonSchemaTitle("Variable 1")
4346
@JsonPropertyDescription("First variable data field")
44-
@AutofillAttributeName var firstVariable: String = ""
47+
@AutofillAttributeName var firstVariable: EncodableString = ""
4548

4649
// Add annotations for the second variable
4750
@JsonProperty(value = "secondVariable", required = true)
4851
@JsonSchemaTitle("Variable 2")
4952
@JsonPropertyDescription("Second variable data field")
50-
@AutofillAttributeName var secondVariable: String = ""
53+
@AutofillAttributeName var secondVariable: EncodableString = ""
5154

5255
// Add annotations for the third variable
5356
@JsonProperty(value = "thirdVariable", required = true)
5457
@JsonSchemaTitle("Variable 3")
5558
@JsonPropertyDescription("Third variable data field")
56-
@AutofillAttributeName var thirdVariable: String = ""
59+
@AutofillAttributeName var thirdVariable: EncodableString = ""
5760

5861
// Add annotations for the fourth variable
5962
@JsonProperty(value = "fourthVariable", required = true)
60-
@JsonSchemaTitle("Variable 4")
61-
@JsonPropertyDescription("Fourth variable data field")
62-
@AutofillAttributeName var fourthVariable: String = ""
63+
@JsonSchemaTitle("Measured Value")
64+
@JsonPropertyDescription("Measured value data field")
65+
@AutofillAttributeName var fourthVariable: EncodableString = ""
6366

6467
// OperatorInfo instance describing ternary plot
6568
override def operatorInfo: OperatorInfo =
@@ -82,37 +85,39 @@ class TernaryContourOpDesc extends PythonOperatorDescriptor {
8285
}
8386

8487
/** Returns a Python string that drops any tuples with missing values */
85-
def manipulateTable(): String = {
88+
def manipulateTable(): PythonTemplateBuilder = {
8689
// Check for any empty data field names
87-
assert(firstVariable.nonEmpty && secondVariable.nonEmpty && thirdVariable.nonEmpty)
88-
s"""
90+
assert(
91+
firstVariable.nonEmpty && secondVariable.nonEmpty && thirdVariable.nonEmpty && fourthVariable.nonEmpty
92+
)
93+
pyb"""
8994
| # Remove any tuples that contain missing values
90-
| table.dropna(subset=['$firstVariable', '$secondVariable', '$thirdVariable', '$fourthVariable'], inplace = True)
95+
| table.dropna(subset=[$firstVariable, $secondVariable, $thirdVariable, $fourthVariable], inplace = True)
9196
|
9297
| #Remove rows where any of the first three variables are negative
93-
| table = table[(table[['$firstVariable', '$secondVariable', '$thirdVariable']] >= 0).all(axis=1)]
98+
| table = table[(table[[$firstVariable, $secondVariable, $thirdVariable]] >= 0).all(axis=1)]
9499
|
95100
| #Remove zero-sum rows
96-
| s = table['$firstVariable'] + table['$secondVariable'] + table['$thirdVariable']
101+
| s = table[$firstVariable] + table[$secondVariable] + table[$thirdVariable]
97102
| table = table[s > 0]
98-
|""".stripMargin
103+
|"""
99104
}
100105

101106
/** Returns a Python string that creates the ternary contour plot figure */
102-
def createPlotlyFigure(): String = {
103-
s"""
104-
| A = table['$firstVariable'].to_numpy()
105-
| B = table['$secondVariable'].to_numpy()
106-
| C = table['$thirdVariable'].to_numpy()
107-
| Z = table['$fourthVariable'].to_numpy()
108-
| fig = ff.create_ternary_contour(np.array([A,B,C]), Z, pole_labels=['$firstVariable', '$secondVariable', '$thirdVariable'], interp_mode='cartesian')
109-
|""".stripMargin
107+
def createPlotlyFigure(): PythonTemplateBuilder = {
108+
pyb"""
109+
| A = table[$firstVariable].to_numpy()
110+
| B = table[$secondVariable].to_numpy()
111+
| C = table[$thirdVariable].to_numpy()
112+
| Z = table[$fourthVariable].to_numpy()
113+
| fig = ff.create_ternary_contour(np.array([A,B,C]), Z, pole_labels=[$firstVariable, $secondVariable, $thirdVariable], interp_mode='cartesian')
114+
|"""
110115
}
111116

112117
/** Returns a Python string that yields the html content of the ternary contour plot */
113118
override def generatePythonCode(): String = {
114119
val finalCode =
115-
s"""
120+
pyb"""
116121
|from pytexera import *
117122
|
118123
|import plotly.express as px
@@ -141,8 +146,8 @@ class TernaryContourOpDesc extends PythonOperatorDescriptor {
141146
| # Convert fig to html content
142147
| html = plotly.io.to_html(fig, include_plotlyjs = 'cdn', auto_play = False)
143148
| yield {'html-content':html}
144-
|""".stripMargin
145-
finalCode
149+
|"""
150+
finalCode.encode
146151
}
147152

148153
}
158 KB
Loading

0 commit comments

Comments
 (0)