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,61 +19,12 @@

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

import org.apache.texera.amber.core.tuple.AttributeType
import org.apache.texera.amber.operator.LogicalOp
import org.apache.texera.amber.operator.metadata.OperatorGroupConstants
import org.apache.texera.amber.util.JSONUtils.objectMapper
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class SklearnAdaptiveBoostingOpDescSpec extends AnyFlatSpec with Matchers {

"SklearnAdaptiveBoostingOpDesc.operatorInfo" should
"advertise the model name, Sklearn group, and the training/testing port shape" in {
val info = (new SklearnAdaptiveBoostingOpDesc).operatorInfo
info.userFriendlyName shouldBe "Adaptive Boosting"
info.operatorDescription shouldBe "Sklearn Adaptive Boosting Operator"
info.operatorGroupName shouldBe OperatorGroupConstants.SKLEARN_GROUP
info.inputPorts.map(_.displayName) shouldBe List("training", "testing")
info.outputPorts should have length 1
info.outputPorts.head.blocking shouldBe true
}

"SklearnAdaptiveBoostingOpDesc" should "default its config fields" in {
val d = new SklearnAdaptiveBoostingOpDesc
d.countVectorizer shouldBe false
d.tfidfTransformer shouldBe false
d.target shouldBe null
d.text shouldBe empty
}

"SklearnAdaptiveBoostingOpDesc.getOutputSchemas" should
"emit the model_name/model schema keyed by the declared output port" in {
val d = new SklearnAdaptiveBoostingOpDesc
val schema = d.getOutputSchemas(Map.empty)(d.operatorInfo.outputPorts.head.id)
schema.getAttribute("model_name").getType shouldBe AttributeType.STRING
schema.getAttribute("model").getType shouldBe AttributeType.BINARY
}

"SklearnAdaptiveBoostingOpDesc.generatePythonCode" should "import the configured sklearn estimator" in {
val d = new SklearnAdaptiveBoostingOpDesc
d.target = "y"
val code = d.generatePythonCode()
code should include("from sklearn.ensemble import AdaBoostClassifier")
code should include("make_pipeline")
code should include("Adaptive Boosting")
}

"SklearnAdaptiveBoostingOpDesc" should "round-trip its config fields through the polymorphic base" in {
val d = new SklearnAdaptiveBoostingOpDesc
d.target = "label"
d.countVectorizer = true
val json = objectMapper.writeValueAsString(d)
json should include("\"operatorType\":\"SklearnAdaptiveBoosting\"")
val restored = objectMapper.readValue(json, classOf[LogicalOp])
restored shouldBe a[SklearnAdaptiveBoostingOpDesc]
val r = restored.asInstanceOf[SklearnAdaptiveBoostingOpDesc]
r.target shouldBe "label"
r.countVectorizer shouldBe true
}
class SklearnAdaptiveBoostingOpDescSpec extends AnyFlatSpec with SklearnModelOpDescBehaviors {
sklearnClassifier(
new SklearnAdaptiveBoostingOpDesc,
"Adaptive Boosting",
"from sklearn.ensemble import AdaBoostClassifier"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,61 +19,12 @@

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

import org.apache.texera.amber.core.tuple.AttributeType
import org.apache.texera.amber.operator.LogicalOp
import org.apache.texera.amber.operator.metadata.OperatorGroupConstants
import org.apache.texera.amber.util.JSONUtils.objectMapper
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class SklearnBaggingOpDescSpec extends AnyFlatSpec with Matchers {

"SklearnBaggingOpDesc.operatorInfo" should
"advertise the model name, Sklearn group, and the training/testing port shape" in {
val info = (new SklearnBaggingOpDesc).operatorInfo
info.userFriendlyName shouldBe "Bagging"
info.operatorDescription shouldBe "Sklearn Bagging Operator"
info.operatorGroupName shouldBe OperatorGroupConstants.SKLEARN_GROUP
info.inputPorts.map(_.displayName) shouldBe List("training", "testing")
info.outputPorts should have length 1
info.outputPorts.head.blocking shouldBe true
}

"SklearnBaggingOpDesc" should "default its config fields" in {
val d = new SklearnBaggingOpDesc
d.countVectorizer shouldBe false
d.tfidfTransformer shouldBe false
d.target shouldBe null
d.text shouldBe empty
}

"SklearnBaggingOpDesc.getOutputSchemas" should
"emit the model_name/model schema keyed by the declared output port" in {
val d = new SklearnBaggingOpDesc
val schema = d.getOutputSchemas(Map.empty)(d.operatorInfo.outputPorts.head.id)
schema.getAttribute("model_name").getType shouldBe AttributeType.STRING
schema.getAttribute("model").getType shouldBe AttributeType.BINARY
}

"SklearnBaggingOpDesc.generatePythonCode" should "import the configured sklearn estimator" in {
val d = new SklearnBaggingOpDesc
d.target = "y"
val code = d.generatePythonCode()
code should include("from sklearn.ensemble import BaggingClassifier")
code should include("make_pipeline")
code should include("Bagging")
}

"SklearnBaggingOpDesc" should "round-trip its config fields through the polymorphic base" in {
val d = new SklearnBaggingOpDesc
d.target = "label"
d.countVectorizer = true
val json = objectMapper.writeValueAsString(d)
json should include("\"operatorType\":\"SklearnBagging\"")
val restored = objectMapper.readValue(json, classOf[LogicalOp])
restored shouldBe a[SklearnBaggingOpDesc]
val r = restored.asInstanceOf[SklearnBaggingOpDesc]
r.target shouldBe "label"
r.countVectorizer shouldBe true
}
class SklearnBaggingOpDescSpec extends AnyFlatSpec with SklearnModelOpDescBehaviors {
sklearnClassifier(
new SklearnBaggingOpDesc,
"Bagging",
"from sklearn.ensemble import BaggingClassifier"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,21 @@

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

import org.apache.texera.amber.core.tuple.AttributeType
import org.apache.texera.amber.operator.LogicalOp
import org.apache.texera.amber.operator.metadata.OperatorGroupConstants
import org.apache.texera.amber.util.JSONUtils.objectMapper
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class SklearnBernoulliNaiveBayesOpDescSpec extends AnyFlatSpec with Matchers {
class SklearnBernoulliNaiveBayesOpDescSpec extends AnyFlatSpec with SklearnModelOpDescBehaviors {

"SklearnBernoulliNaiveBayesOpDesc.operatorInfo" should
"advertise the model name, Sklearn group, and the training/testing port shape" in {
val info = (new SklearnBernoulliNaiveBayesOpDesc).operatorInfo
info.userFriendlyName shouldBe "Bernoulli Naive Bayes"
info.operatorDescription shouldBe "Sklearn Bernoulli Naive Bayes Operator"
info.operatorGroupName shouldBe OperatorGroupConstants.SKLEARN_GROUP
info.inputPorts.map(_.displayName) shouldBe List("training", "testing")
info.outputPorts should have length 1
info.outputPorts.head.blocking shouldBe true
}

"SklearnBernoulliNaiveBayesOpDesc" should "default its config fields" in {
val d = new SklearnBernoulliNaiveBayesOpDesc
d.countVectorizer shouldBe false
d.tfidfTransformer shouldBe false
d.target shouldBe null
d.text shouldBe empty
}

"SklearnBernoulliNaiveBayesOpDesc.getOutputSchemas" should
"emit the model_name/model schema keyed by the declared output port" in {
val d = new SklearnBernoulliNaiveBayesOpDesc
val schema = d.getOutputSchemas(Map.empty)(d.operatorInfo.outputPorts.head.id)
schema.getAttribute("model_name").getType shouldBe AttributeType.STRING
schema.getAttribute("model").getType shouldBe AttributeType.BINARY
}

"SklearnBernoulliNaiveBayesOpDesc.generatePythonCode" should
"import and instantiate the BernoulliNB estimator" in {
val d = new SklearnBernoulliNaiveBayesOpDesc
d.target = "y"
val code = d.generatePythonCode()
code should include("from sklearn.naive_bayes import BernoulliNB")
code should include("make_pipeline")
code should include("Bernoulli Naive Bayes")
}
sklearnClassifier(
new SklearnBernoulliNaiveBayesOpDesc,
"Bernoulli Naive Bayes",
"from sklearn.naive_bayes import BernoulliNB",
estimatorTestName = "import and instantiate the BernoulliNB estimator"
)

// The same table statement serves both ports, so training and scoring skip a
// row with a missing value alike.
it should "drop rows with missing values before fitting and before scoring" in {
"SklearnBernoulliNaiveBayesOpDesc.generatePythonCode" should
"drop rows with missing values before fitting and before scoring" in {
val d = new SklearnBernoulliNaiveBayesOpDesc
d.target = "y"
d.generatePythonCode() should include("table.dropna()")
Expand Down Expand Up @@ -97,18 +63,4 @@ class SklearnBernoulliNaiveBayesOpDescSpec extends AnyFlatSpec with Matchers {
"""print("Skipped", len(in2df) - len(_test), "of", len(in2df), "rows with missing values")"""
)
}

"SklearnBernoulliNaiveBayesOpDesc" should
"round-trip its config fields through the polymorphic base" in {
val d = new SklearnBernoulliNaiveBayesOpDesc
d.target = "label"
d.countVectorizer = true
val json = objectMapper.writeValueAsString(d)
json should include("\"operatorType\":\"SklearnBernoulliNaiveBayes\"")
val restored = objectMapper.readValue(json, classOf[LogicalOp])
restored shouldBe a[SklearnBernoulliNaiveBayesOpDesc]
val r = restored.asInstanceOf[SklearnBernoulliNaiveBayesOpDesc]
r.target shouldBe "label"
r.countVectorizer shouldBe true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,63 +19,13 @@

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

import org.apache.texera.amber.core.tuple.AttributeType
import org.apache.texera.amber.operator.LogicalOp
import org.apache.texera.amber.operator.metadata.OperatorGroupConstants
import org.apache.texera.amber.util.JSONUtils.objectMapper
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class SklearnComplementNaiveBayesOpDescSpec extends AnyFlatSpec with Matchers {

"SklearnComplementNaiveBayesOpDesc.operatorInfo" should
"advertise the model name, Sklearn group, and the training/testing port shape" in {
val info = (new SklearnComplementNaiveBayesOpDesc).operatorInfo
info.userFriendlyName shouldBe "Complement Naive Bayes"
info.operatorDescription shouldBe "Sklearn Complement Naive Bayes Operator"
info.operatorGroupName shouldBe OperatorGroupConstants.SKLEARN_GROUP
info.inputPorts.map(_.displayName) shouldBe List("training", "testing")
info.outputPorts should have length 1
info.outputPorts.head.blocking shouldBe true
}

"SklearnComplementNaiveBayesOpDesc" should "default its config fields" in {
val d = new SklearnComplementNaiveBayesOpDesc
d.countVectorizer shouldBe false
d.tfidfTransformer shouldBe false
d.target shouldBe null
d.text shouldBe empty
}

"SklearnComplementNaiveBayesOpDesc.getOutputSchemas" should
"emit the model_name/model schema keyed by the declared output port" in {
val d = new SklearnComplementNaiveBayesOpDesc
val schema = d.getOutputSchemas(Map.empty)(d.operatorInfo.outputPorts.head.id)
schema.getAttribute("model_name").getType shouldBe AttributeType.STRING
schema.getAttribute("model").getType shouldBe AttributeType.BINARY
}

"SklearnComplementNaiveBayesOpDesc.generatePythonCode" should
"import and instantiate the ComplementNB estimator" in {
val d = new SklearnComplementNaiveBayesOpDesc
d.target = "y"
val code = d.generatePythonCode()
code should include("from sklearn.naive_bayes import ComplementNB")
code should include("make_pipeline")
code should include("Complement Naive Bayes")
}

"SklearnComplementNaiveBayesOpDesc" should
"round-trip its config fields through the polymorphic base" in {
val d = new SklearnComplementNaiveBayesOpDesc
d.target = "label"
d.countVectorizer = true
val json = objectMapper.writeValueAsString(d)
json should include("\"operatorType\":\"SklearnComplementNaiveBayes\"")
val restored = objectMapper.readValue(json, classOf[LogicalOp])
restored shouldBe a[SklearnComplementNaiveBayesOpDesc]
val r = restored.asInstanceOf[SklearnComplementNaiveBayesOpDesc]
r.target shouldBe "label"
r.countVectorizer shouldBe true
}
class SklearnComplementNaiveBayesOpDescSpec extends AnyFlatSpec with SklearnModelOpDescBehaviors {
sklearnClassifier(
new SklearnComplementNaiveBayesOpDesc,
"Complement Naive Bayes",
"from sklearn.naive_bayes import ComplementNB",
estimatorTestName = "import and instantiate the ComplementNB estimator"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,24 @@

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

import org.apache.texera.amber.core.tuple.AttributeType
import org.apache.texera.amber.operator.LogicalOp
import org.apache.texera.amber.operator.metadata.OperatorGroupConstants
import org.apache.texera.amber.util.JSONUtils.objectMapper
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class SklearnDecisionTreeOpDescSpec extends AnyFlatSpec with Matchers {
class SklearnDecisionTreeOpDescSpec extends AnyFlatSpec with SklearnModelOpDescBehaviors {

"SklearnDecisionTreeOpDesc.operatorInfo" should
"advertise the model name, Sklearn group, and the training/testing port shape" in {
val info = (new SklearnDecisionTreeOpDesc).operatorInfo
info.userFriendlyName shouldBe "Decision Tree"
info.operatorDescription shouldBe "Sklearn Decision Tree Operator"
info.operatorGroupName shouldBe OperatorGroupConstants.SKLEARN_GROUP
info.inputPorts.map(_.displayName) shouldBe List("training", "testing")
info.outputPorts should have length 1
info.outputPorts.head.blocking shouldBe true
}

"SklearnDecisionTreeOpDesc" should "default its config fields" in {
val d = new SklearnDecisionTreeOpDesc
d.countVectorizer shouldBe false
d.tfidfTransformer shouldBe false
d.target shouldBe null
d.text shouldBe empty
}
sklearnClassifier(
new SklearnDecisionTreeOpDesc,
"Decision Tree",
"from sklearn.tree import DecisionTreeClassifier"
)

// A split compares, and a comparison can ask whether the value is there before it
// asks how large it is, so the row is worth keeping and the count is worth saying.
it should "keep a row with a blank feature and say how many it kept" in {
"SklearnDecisionTreeOpDesc" should "keep a row with a blank feature and say how many it kept" in {
val d = new SklearnDecisionTreeOpDesc
d.target = "y"
val code = d.generatePythonCode()
code should not include "table.dropna() "
code should include("dropna(subset=[")
code should include("rows_with_gaps")
}

"SklearnDecisionTreeOpDesc.getOutputSchemas" should
"emit the model_name/model schema keyed by the declared output port" in {
val d = new SklearnDecisionTreeOpDesc
val schema = d.getOutputSchemas(Map.empty)(d.operatorInfo.outputPorts.head.id)
schema.getAttribute("model_name").getType shouldBe AttributeType.STRING
schema.getAttribute("model").getType shouldBe AttributeType.BINARY
}

"SklearnDecisionTreeOpDesc.generatePythonCode" should "import the configured sklearn estimator" in {
val d = new SklearnDecisionTreeOpDesc
d.target = "y"
val code = d.generatePythonCode()
code should include("from sklearn.tree import DecisionTreeClassifier")
code should include("make_pipeline")
code should include("Decision Tree")
}

"SklearnDecisionTreeOpDesc" should "round-trip its config fields through the polymorphic base" in {
val d = new SklearnDecisionTreeOpDesc
d.target = "label"
d.countVectorizer = true
val json = objectMapper.writeValueAsString(d)
json should include("\"operatorType\":\"SklearnDecisionTree\"")
val restored = objectMapper.readValue(json, classOf[LogicalOp])
restored shouldBe a[SklearnDecisionTreeOpDesc]
val r = restored.asInstanceOf[SklearnDecisionTreeOpDesc]
r.target shouldBe "label"
r.countVectorizer shouldBe true
}
}
Loading
Loading