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 @@ -19,73 +19,15 @@

package org.apache.texera.amber.operator.sklearn

import com.fasterxml.jackson.annotation.{JsonIgnore, JsonProperty, JsonPropertyDescription}
import com.kjetland.jackson.jsonSchema.annotations.{
JsonSchemaInject,
JsonSchemaInt,
JsonSchemaString,
JsonSchemaTitle
}
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
import org.apache.texera.amber.operator.PythonOperatorDescriptor
import org.apache.texera.amber.operator.metadata.annotations.{
AutofillAttributeName,
CommonOpDescAnnotation,
HideAnnotation
}
import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}

abstract class SklearnClassifierOpDesc extends PythonOperatorDescriptor {

@JsonSchemaTitle("Target Attribute")
@JsonPropertyDescription("Attribute in your dataset corresponding to target.")
@JsonProperty(required = true)
@AutofillAttributeName
var target: EncodableString = _
abstract class SklearnClassifierOpDesc extends SklearnModelOpDesc {

@JsonSchemaTitle("Count Vectorizer")
@JsonPropertyDescription("Convert a collection of text documents to a matrix of token counts.")
@JsonProperty(defaultValue = "false")
var countVectorizer: Boolean = false
override def getImportStatements = ""

@JsonSchemaTitle("Text Attribute")
@JsonPropertyDescription("Attribute in your dataset with text to vectorize.")
@JsonSchemaInject(
strings = Array(
new JsonSchemaString(
path = CommonOpDescAnnotation.autofill,
value = CommonOpDescAnnotation.attributeName
),
new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
),
ints = Array(
new JsonSchemaInt(path = CommonOpDescAnnotation.autofillAttributeOnPort, value = 0)
)
)
var text: EncodableString = _

@JsonSchemaTitle("Tfidf Transformer")
@JsonPropertyDescription("Transform a count matrix to a normalized tf or tf-idf representation.")
@JsonProperty(defaultValue = "false")
@JsonSchemaInject(
strings = Array(
new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
)
)
val tfidfTransformer: Boolean = false

@JsonIgnore
def getImportStatements = ""

@JsonIgnore
def getUserFriendlyModelName = ""
override def getUserFriendlyModelName = ""

override def generatePythonCode(): String =
pyb"""$getImportStatements
Expand Down Expand Up @@ -126,14 +68,4 @@ abstract class SklearnClassifierOpDesc extends PythonOperatorDescriptor {
),
outputPorts = List(OutputPort(blocking = true))
)

override def getOutputSchemas(
inputSchemas: Map[PortIdentity, Schema]
): Map[PortIdentity, Schema] = {
Map(
operatorInfo.outputPorts.head.id -> Schema()
.add("model_name", AttributeType.STRING)
.add("model", AttributeType.BINARY)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.sklearn

import com.fasterxml.jackson.annotation.{JsonIgnore, JsonProperty, JsonPropertyDescription}
import com.kjetland.jackson.jsonSchema.annotations.{
JsonSchemaInject,
JsonSchemaInt,
JsonSchemaString,
JsonSchemaTitle
}
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
import org.apache.texera.amber.core.workflow.PortIdentity
import org.apache.texera.amber.operator.PythonOperatorDescriptor
import org.apache.texera.amber.operator.metadata.annotations.{
AutofillAttributeName,
CommonOpDescAnnotation,
HideAnnotation
}

abstract class SklearnModelOpDesc extends PythonOperatorDescriptor {

@JsonSchemaTitle("Target Attribute")
@JsonPropertyDescription("Attribute in your dataset corresponding to target.")
@JsonProperty(required = true)
@AutofillAttributeName
var target: EncodableString = _

@JsonSchemaTitle("Count Vectorizer")
@JsonPropertyDescription("Convert a collection of text documents to a matrix of token counts.")
@JsonProperty(defaultValue = "false")
var countVectorizer: Boolean = false

@JsonSchemaTitle("Text Attribute")
@JsonPropertyDescription("Attribute in your dataset with text to vectorize.")
@JsonSchemaInject(
strings = Array(
new JsonSchemaString(
path = CommonOpDescAnnotation.autofill,
value = CommonOpDescAnnotation.attributeName
),
new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
),
ints = Array(
new JsonSchemaInt(path = CommonOpDescAnnotation.autofillAttributeOnPort, value = 0)
)
)
var text: EncodableString = _

@JsonSchemaTitle("Tfidf Transformer")
@JsonPropertyDescription("Transform a count matrix to a normalized tf or tf-idf representation.")
@JsonProperty(defaultValue = "false")
@JsonSchemaInject(
strings = Array(
new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
)
)
var tfidfTransformer: Boolean = false

@JsonIgnore
def getImportStatements: String

@JsonIgnore
def getUserFriendlyModelName: String

override def getOutputSchemas(
inputSchemas: Map[PortIdentity, Schema]
): Map[PortIdentity, Schema] = {
Map(
operatorInfo.outputPorts.head.id -> Schema()
.add("model_name", AttributeType.STRING)
.add("model", AttributeType.BINARY)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,16 @@

package org.apache.texera.amber.operator.sklearn.training

import com.fasterxml.jackson.annotation.{JsonIgnore, JsonProperty, JsonPropertyDescription}
import com.kjetland.jackson.jsonSchema.annotations.{
JsonSchemaInject,
JsonSchemaInt,
JsonSchemaString,
JsonSchemaTitle
}
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
import org.apache.texera.amber.operator.PythonOperatorDescriptor
import org.apache.texera.amber.operator.metadata.annotations.{
AutofillAttributeName,
CommonOpDescAnnotation,
HideAnnotation
}
import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
import org.apache.texera.amber.operator.sklearn.SklearnModelOpDesc

class SklearnTrainingOpDesc extends PythonOperatorDescriptor {

@JsonSchemaTitle("Target Attribute")
@JsonPropertyDescription("Attribute in your dataset corresponding to target.")
@JsonProperty(required = true)
@AutofillAttributeName
var target: EncodableString = _

@JsonSchemaTitle("Count Vectorizer")
@JsonPropertyDescription("Convert a collection of text documents to a matrix of token counts.")
@JsonProperty(defaultValue = "false")
var countVectorizer: Boolean = false

@JsonSchemaTitle("Text Attribute")
@JsonPropertyDescription("Attribute in your dataset with text to vectorize.")
@JsonSchemaInject(
strings = Array(
new JsonSchemaString(
path = CommonOpDescAnnotation.autofill,
value = CommonOpDescAnnotation.attributeName
),
new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
),
ints = Array(
new JsonSchemaInt(path = CommonOpDescAnnotation.autofillAttributeOnPort, value = 0)
)
)
var text: EncodableString = _

@JsonSchemaTitle("Tfidf Transformer")
@JsonPropertyDescription("Transform a count matrix to a normalized tf or tf-idf representation.")
@JsonProperty(defaultValue = "false")
@JsonSchemaInject(
strings = Array(
new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
)
)
var tfidfTransformer: Boolean = false
class SklearnTrainingOpDesc extends SklearnModelOpDesc {

@JsonIgnore
def getImportStatements = "from sklearn.ensemble import RandomForestClassifier"
override def getImportStatements = "from sklearn.ensemble import RandomForestClassifier"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string should be empty since it gets overridden anyway. The placeholder was introduced by mistake in the first place.


@JsonIgnore
def getUserFriendlyModelName = "RandomForest Training"
override def getUserFriendlyModelName = "RandomForest Training"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


override def generatePythonCode(): String =
pyb"""$getImportStatements
Expand Down Expand Up @@ -115,14 +58,4 @@ class SklearnTrainingOpDesc extends PythonOperatorDescriptor {
inputPorts = List(InputPort(PortIdentity(), "training")),
outputPorts = List(OutputPort(blocking = true))
)

override def getOutputSchemas(
inputSchemas: Map[PortIdentity, Schema]
): Map[PortIdentity, Schema] = {
Map(
operatorInfo.outputPorts.head.id -> Schema()
.add("model_name", AttributeType.STRING)
.add("model", AttributeType.BINARY)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class SklearnOpDescRegistrySpec extends AnyFlatSpec {
val desc = new SklearnLogisticRegressionOpDesc()
desc.target = "y"
desc.countVectorizer = false
// `tfidfTransformer` is a val on the base class, defaults to false.
// `tfidfTransformer` is defined on the shared base class, defaults to false.
val code = desc.generatePythonCode()
assert(code.contains("from sklearn.linear_model import LogisticRegression"))
// Classifier OpDescs emit a UDFTableOperator pipeline.
Expand Down
Loading