Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ package org.apache.texera.amber.operator.huggingFace
import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.operator.huggingFace.HuggingFaceSchemaValidation.{
requireNonEmpty,
requireNumericInputColumn
}
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
Expand Down Expand Up @@ -189,13 +193,13 @@ class HuggingFaceIrisLogisticRegressionOpDesc
override def getOutputSchemas(
inputSchemas: Map[PortIdentity, Schema]
): Map[PortIdentity, Schema] = {
if (
predictionClassName == null || predictionClassName.trim.isEmpty ||
predictionProbabilityName == null || predictionProbabilityName.trim.isEmpty
)
throw new RuntimeException("Result attribute name should not be empty")
val inputSchema = inputSchemas(operatorInfo.inputPorts.head.id)
requireNumericInputColumn(inputSchema, "Petal Length Cm Attribute", petalLengthCmAttribute)
requireNumericInputColumn(inputSchema, "Petal Width Cm Attribute", petalWidthCmAttribute)
requireNonEmpty("Prediction class name", predictionClassName)
requireNonEmpty("Prediction probability name", predictionProbabilityName)
Map(
operatorInfo.outputPorts.head.id -> inputSchemas(operatorInfo.inputPorts.head.id)
operatorInfo.outputPorts.head.id -> inputSchema
.add(predictionClassName, AttributeType.STRING)
.add(predictionProbabilityName, AttributeType.DOUBLE)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 org.apache.texera.amber.operator.huggingFace

import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema}

/**
* Configuration checks shared by the Spam Detection, Sentiment Analysis, Text
* Summarization and Iris Logistic Regression operators' `getOutputSchemas`.
*
* Each check throws. The compiler runs `getOutputSchemas` inside a `Try` and stores
* a thrown error on the operator's output port, which is how an operator that is
* not configured yet reports why. Messages name the field as the form shows it.
*/
private[huggingFace] object HuggingFaceSchemaValidation {

private val NUMERIC_TYPES = Set(AttributeType.INTEGER, AttributeType.LONG, AttributeType.DOUBLE)

/** Require a column-name field to be set and not blank. */
def requireNonEmpty(fieldTitle: String, value: String): Unit =
if (value == null || value.trim.isEmpty) {
throw new RuntimeException(s"$fieldTitle cannot be empty")
}

/**
* Require the column a field names to be in the input table, spelled exactly as
* it is there: Schema lookups ignore case, but the generated Python does not.
*/
def requireInputColumn(schema: Schema, fieldTitle: String, column: String): Attribute = {
requireNonEmpty(fieldTitle, column)
schema.attributes
.find(_.getName == column)
.getOrElse(
throw new RuntimeException(
s"$fieldTitle '$column' is not in the input table. " +
s"Available columns: ${schema.getAttributeNames.mkString(", ")}"
)
)
}

/** As [[requireInputColumn]], and the column must also be numeric. */
def requireNumericInputColumn(schema: Schema, fieldTitle: String, column: String): Unit = {
val columnType = requireInputColumn(schema, fieldTitle, column).getType
if (!NUMERIC_TYPES.contains(columnType)) {
throw new RuntimeException(
s"$fieldTitle '$column' must be numeric (integer, long or double), not ${columnType.getName}"
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ package org.apache.texera.amber.operator.huggingFace
import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.operator.huggingFace.HuggingFaceSchemaValidation.{
requireInputColumn,
requireNonEmpty
}
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
import org.apache.texera.amber.operator.{PythonOperatorDescriptor, StandaloneCodeGenerator}
import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn}
Expand Down Expand Up @@ -164,14 +168,13 @@ class HuggingFaceSentimentAnalysisOpDesc
override def getOutputSchemas(
inputSchemas: Map[PortIdentity, Schema]
): Map[PortIdentity, Schema] = {
if (
resultAttributePositive == null || resultAttributePositive.trim.isEmpty ||
resultAttributeNeutral == null || resultAttributeNeutral.trim.isEmpty ||
resultAttributeNegative == null || resultAttributeNegative.trim.isEmpty
)
return null
val inputSchema = inputSchemas(operatorInfo.inputPorts.head.id)
requireInputColumn(inputSchema, "Attribute", attribute)
requireNonEmpty("Positive result attribute", resultAttributePositive)
requireNonEmpty("Neutral result attribute", resultAttributeNeutral)
requireNonEmpty("Negative result attribute", resultAttributeNegative)
Map(
operatorInfo.outputPorts.head.id -> inputSchemas(operatorInfo.inputPorts.head.id)
operatorInfo.outputPorts.head.id -> inputSchema
.add(resultAttributePositive, AttributeType.DOUBLE)
.add(resultAttributeNeutral, AttributeType.DOUBLE)
.add(resultAttributeNegative, AttributeType.DOUBLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ package org.apache.texera.amber.operator.huggingFace
import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.operator.huggingFace.HuggingFaceSchemaValidation.{
requireInputColumn,
requireNonEmpty
}
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
import org.apache.texera.amber.operator.{PythonOperatorDescriptor, StandaloneCodeGenerator}
import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn}
Expand Down Expand Up @@ -129,13 +133,12 @@ class HuggingFaceSpamSMSDetectionOpDesc
override def getOutputSchemas(
inputSchemas: Map[PortIdentity, Schema]
): Map[PortIdentity, Schema] = {
if (
resultAttributeSpam == null || resultAttributeSpam.trim.isEmpty ||
resultAttributeProbability == null || resultAttributeProbability.trim.isEmpty
)
return null
val inputSchema = inputSchemas(operatorInfo.inputPorts.head.id)
requireInputColumn(inputSchema, "Attribute", attribute)
Comment thread
PG1204 marked this conversation as resolved.
Resolved
requireNonEmpty("Spam result attribute", resultAttributeSpam)
requireNonEmpty("Score result attribute", resultAttributeProbability)
Map(
operatorInfo.outputPorts.head.id -> inputSchemas(operatorInfo.inputPorts.head.id)
operatorInfo.outputPorts.head.id -> inputSchema
.add(resultAttributeSpam, AttributeType.BOOLEAN)
.add(resultAttributeProbability, AttributeType.DOUBLE)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ package org.apache.texera.amber.operator.huggingFace
import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription}
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.operator.huggingFace.HuggingFaceSchemaValidation.{
requireInputColumn,
requireNonEmpty
}
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
import org.apache.texera.amber.operator.{PythonOperatorDescriptor, StandaloneCodeGenerator}
import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn}
Expand Down Expand Up @@ -134,10 +138,11 @@ class HuggingFaceTextSummarizationOpDesc
override def getOutputSchemas(
inputSchemas: Map[PortIdentity, Schema]
): Map[PortIdentity, Schema] = {
if (resultAttribute == null || resultAttribute.trim.isEmpty)
throw new RuntimeException("Result attribute name should be given")
val inputSchema = inputSchemas(operatorInfo.inputPorts.head.id)
requireInputColumn(inputSchema, "Attribute", attribute)
requireNonEmpty("Result attribute name", resultAttribute)
Map(
operatorInfo.outputPorts.head.id -> inputSchemas.values.head
operatorInfo.outputPorts.head.id -> inputSchema
.add(resultAttribute, AttributeType.STRING)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,99 @@ class HuggingFaceIrisLogisticRegressionOpDescSpec
}

"HuggingFaceIrisLogisticRegressionOpDesc.getOutputSchemas" should
"reject a blank prediction result name" in {
val d = new HuggingFaceIrisLogisticRegressionOpDesc
val in = Schema().add("sepal", AttributeType.STRING)
val ex = intercept[RuntimeException] {
d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in))
}
ex.getMessage shouldBe "Result attribute name should not be empty"
}

it should "append a STRING class column and a DOUBLE probability column, keyed by the output port" in {
"append a STRING class column and a DOUBLE probability column, keyed by the output port" in {
val d = configured()
val in = Schema().add("sepal", AttributeType.STRING)
val in = Schema()
.add("sepal", AttributeType.STRING)
.add("petalLength", AttributeType.DOUBLE)
.add("petalWidth", AttributeType.DOUBLE)
val out = d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in))
val schema = out(d.operatorInfo.outputPorts.head.id)
schema.getAttribute("sepal").getType shouldBe AttributeType.STRING
schema.getAttribute("species").getType shouldBe AttributeType.STRING
schema.getAttribute("probability").getType shouldBe AttributeType.DOUBLE
}

private def petals(
lengthType: AttributeType = AttributeType.DOUBLE,
widthType: AttributeType = AttributeType.DOUBLE
): Schema = Schema().add("petalLength", lengthType).add("petalWidth", widthType)

private def schemaError(
d: HuggingFaceIrisLogisticRegressionOpDesc,
in: Schema = petals()
): String =
intercept[RuntimeException] {
d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in))
}.getMessage

it should "reject each unset or blank prediction result name, naming the field" in {
for (empty <- Seq(null, "", " ")) {
withClue(s"[$empty]: ") {
val noClass = configured()
noClass.predictionClassName = empty
schemaError(noClass) shouldBe "Prediction class name cannot be empty"

val noProbability = configured()
noProbability.predictionProbabilityName = empty
schemaError(noProbability) shouldBe "Prediction probability name cannot be empty"
}
}
}

it should "reject an unset petal column" in {
val noLength = configured()
noLength.petalLengthCmAttribute = null
schemaError(noLength) shouldBe "Petal Length Cm Attribute cannot be empty"

val noWidth = configured()
noWidth.petalWidthCmAttribute = ""
schemaError(noWidth) shouldBe "Petal Width Cm Attribute cannot be empty"
}

it should "reject a petal column that is not in the input table, listing the columns" in {
val d = configured()
d.petalWidthCmAttribute = "petal_width"
schemaError(d) shouldBe
"Petal Width Cm Attribute 'petal_width' is not in the input table. " +
"Available columns: petalLength, petalWidth"
}

it should "match a petal column's case exactly, as the generated Python does" in {
val d = configured()
d.petalLengthCmAttribute = "PetalLength"
schemaError(d) should startWith(
"Petal Length Cm Attribute 'PetalLength' is not in the input table"
)
}

it should "reject a petal column that is not numeric" in {
schemaError(configured(), petals(lengthType = AttributeType.STRING)) shouldBe
"Petal Length Cm Attribute 'petalLength' must be numeric (integer, long or double), not string"
schemaError(configured(), petals(widthType = AttributeType.BOOLEAN)) shouldBe
"Petal Width Cm Attribute 'petalWidth' must be numeric (integer, long or double), not boolean"
}

it should "accept integer, long and double petal columns" in {
for (numeric <- Seq(AttributeType.INTEGER, AttributeType.LONG, AttributeType.DOUBLE)) {
val d = configured()
val in = petals(numeric, numeric)
noException should be thrownBy d.getOutputSchemas(
Map(d.operatorInfo.inputPorts.head.id -> in)
)
}
}

it should "leave a readable error on its output port when the compiler propagates the schema" in {
val d = configured()
d.predictionClassName = null
val physical = d
.getPhysicalOp(workflowId, executionId)
.propagateSchema(Some(d.operatorInfo.inputPorts.head.id -> petals()))
val port = physical.outputPorts(d.operatorInfo.outputPorts.head.id)._3
port.left.map(_.getMessage) shouldBe Left("Prediction class name cannot be empty")
}

"HuggingFaceIrisLogisticRegressionOpDesc.generatePythonCode" should
"emit the logistic-regression operator carrying the configured columns (encoded)" in {
val d = configured()
Expand Down
Loading
Loading