Skip to content

Commit

Permalink
fixed scala style
Browse files Browse the repository at this point in the history
  • Loading branch information
selvinsource committed Apr 19, 2015
1 parent b8823b0 commit e2ffae8
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import org.apache.spark.sql.Row
/**
* A clustering model for K-means. Each point belongs to the cluster with the closest center.
*/
class KMeansModel (val clusterCenters: Array[Vector]) extends Saveable with Serializable with PMMLExportable {
class KMeansModel (
val clusterCenters: Array[Vector]) extends Saveable with Serializable with PMMLExportable {

/** A Java-friendly constructor that takes an Iterable of Vectors. */
def this(centers: java.lang.Iterable[Vector]) = this(centers.asScala.toArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,71 +29,85 @@ class GeneralizedLinearPMMLModelExportSuite extends FunSuite{

test("GeneralizedLinearPMMLModelExport generate PMML format") {

//arrange models to test
// arrange models to test
val linearInput = LinearDataGenerator.generateLinearInput(
3.0, Array(10.0, 10.0), 1, 17)
val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label);
val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label);
val linearRegressionModel = new LinearRegressionModel(
linearInput(0).features, linearInput(0).label);
val ridgeRegressionModel = new RidgeRegressionModel(
linearInput(0).features, linearInput(0).label);
val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label);
val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label);

//act by exporting the model to the PMML format
val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel)
//assert that the PMML format is as expected
// act by exporting the model to the PMML format
val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel)
// assert that the PMML format is as expected
assert(linearModelExport.isInstanceOf[PMMLModelExport])
var pmml = linearModelExport.asInstanceOf[PMMLModelExport].getPmml()
assert(pmml.getHeader().getDescription() === "linear regression")
//check that the number of fields match the weights size
assert(pmml.getDataDictionary().getNumberOfFields() === linearRegressionModel.weights.size + 1)
//this verify that there is a model attached to the pmml object and the model is a regression one
//it also verifies that the pmml model has a regression table with the same number of predictors of the model weights
// check that the number of fields match the weights size
assert(pmml.getDataDictionary().getNumberOfFields()
=== linearRegressionModel.weights.size + 1)
// this verify that there is a model attached to the pmml object
// and the model is a regression one
// it also verifies that the pmml model has a regression table
// with the same number of predictors of the model weights
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
.getRegressionTables().get(0).getNumericPredictors().size() === linearRegressionModel.weights.size)
.getRegressionTables().get(0).getNumericPredictors().size()
=== linearRegressionModel.weights.size)

//act
// act
val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel)
//assert that the PMML format is as expected
// assert that the PMML format is as expected
assert(ridgeModelExport.isInstanceOf[PMMLModelExport])
pmml = ridgeModelExport.asInstanceOf[PMMLModelExport].getPmml()
assert(pmml.getHeader().getDescription() === "ridge regression")
//check that the number of fields match the weights size
// check that the number of fields match the weights size
assert(pmml.getDataDictionary().getNumberOfFields() === ridgeRegressionModel.weights.size + 1)
//this verify that there is a model attached to the pmml object and the model is a regression one
//it also verifies that the pmml model has a regression table with the same number of predictors of the model weights
// this verify that there is a model attached to the pmml object
// and the model is a regression one
// it also verifies that the pmml model has a regression table
// with the same number of predictors of the model weights
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
.getRegressionTables().get(0).getNumericPredictors().size() === ridgeRegressionModel.weights.size)
.getRegressionTables().get(0).getNumericPredictors().size()
=== ridgeRegressionModel.weights.size)

//act
// act
val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel)
//assert that the PMML format is as expected
// assert that the PMML format is as expected
assert(lassoModelExport.isInstanceOf[PMMLModelExport])
pmml = lassoModelExport.asInstanceOf[PMMLModelExport].getPmml()
assert(pmml.getHeader().getDescription() === "lasso regression")
//check that the number of fields match the weights size
// check that the number of fields match the weights size
assert(pmml.getDataDictionary().getNumberOfFields() === lassoModel.weights.size + 1)
//this verify that there is a model attached to the pmml object and the model is a regression one
//it also verifies that the pmml model has a regression table with the same number of predictors of the model weights
// this verify that there is a model attached to the pmml object
// and the model is a regression one
// it also verifies that the pmml model has a regression table
// with the same number of predictors of the model weights
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
.getRegressionTables().get(0).getNumericPredictors().size() === lassoModel.weights.size)

//act
// act
val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel)
//assert that the PMML format is as expected
// assert that the PMML format is as expected
assert(svmModelExport.isInstanceOf[PMMLModelExport])
pmml = svmModelExport.asInstanceOf[PMMLModelExport].getPmml()
assert(pmml.getHeader().getDescription() === "linear SVM: if predicted value > 0, the outcome is positive, or negative otherwise")
//check that the number of fields match the weights size
assert(pmml.getHeader().getDescription()
=== "linear SVM: if predicted value > 0, the outcome is positive, or negative otherwise")
// check that the number of fields match the weights size
assert(pmml.getDataDictionary().getNumberOfFields() === svmModel.weights.size + 1)
//this verify that there is a model attached to the pmml object and the model is a regression one
//it also verifies that the pmml model has a regression table with the same number of predictors of the model weights
// this verify that there is a model attached to the pmml object
// and the model is a regression one
// it also verifies that the pmml model has a regression table
// with the same number of predictors of the model weights
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
.getRegressionTables().get(0).getNumericPredictors().size() === svmModel.weights.size)

//manual checking
//linearRegressionModel.toPMML("/tmp/linearregression.xml")
//ridgeRegressionModel.toPMML("/tmp/ridgeregression.xml")
//lassoModel.toPMML("/tmp/lassoregression.xml")
//svmModel.toPMML("/tmp/linearsvm.xml")
// manual checking
// linearRegressionModel.toPMML("/tmp/linearregression.xml")
// ridgeRegressionModel.toPMML("/tmp/ridgeregression.xml")
// lassoModel.toPMML("/tmp/lassoregression.xml")
// svmModel.toPMML("/tmp/linearsvm.xml")

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,33 @@ class KMeansPMMLModelExportSuite extends FunSuite{

test("KMeansPMMLModelExport generate PMML format") {

//arrange model to test
// arrange model to test
val clusterCenters = Array(
Vectors.dense(1.0, 2.0, 6.0),
Vectors.dense(1.0, 3.0, 0.0),
Vectors.dense(1.0, 4.0, 6.0)
)
val kmeansModel = new KMeansModel(clusterCenters);

//act by exporting the model to the PMML format
// act by exporting the model to the PMML format
val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel)

//assert that the PMML format is as expected
// assert that the PMML format is as expected
assert(modelExport.isInstanceOf[PMMLModelExport])
val pmml = modelExport.asInstanceOf[PMMLModelExport].getPmml()
assert(pmml.getHeader().getDescription() === "k-means clustering")
//check that the number of fields match the single vector size
// check that the number of fields match the single vector size
assert(pmml.getDataDictionary().getNumberOfFields() === clusterCenters(0).size)
//this verify that there is a model attached to the pmml object and the model is a clustering one
//it also verifies that the pmml model has the same number of clusters of the spark model
assert(pmml.getModels().get(0).asInstanceOf[ClusteringModel].getNumberOfClusters() === clusterCenters.size)
// this verify that there is a model attached to the pmml object
// and the model is a clustering one
// it also verifies that the pmml model has the same number of clusters of the spark model
assert(pmml.getModels().get(0).asInstanceOf[ClusteringModel].getNumberOfClusters()
=== clusterCenters.size)

//manual checking
//kmeansModel.toPMML("/tmp/kmeans.xml")
//kmeansModel.toPMML(System.out)
//System.out.println(kmeansModel.toPMML())
// manual checking
// kmeansModel.toPMML("/tmp/kmeans.xml")
// kmeansModel.toPMML(System.out)
// System.out.println(kmeansModel.toPMML())

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,39 @@ class LogisticRegressionPMMLModelExportSuite extends FunSuite{

test("LogisticRegressionPMMLModelExport generate PMML format") {

//arrange models to test
// arrange models to test
val linearInput = LinearDataGenerator.generateLinearInput(
3.0, Array(10.0, 10.0), 1, 17)
val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label);
val logisticRegressionModel = new LogisticRegressionModel(
linearInput(0).features, linearInput(0).label);

//act by exporting the model to the PMML format
val logisticModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel)
//assert that the PMML format is as expected
// act by exporting the model to the PMML format
val logisticModelExport = PMMLModelExportFactory
.createPMMLModelExport(logisticRegressionModel)
// assert that the PMML format is as expected
assert(logisticModelExport.isInstanceOf[PMMLModelExport])
var pmml = logisticModelExport.asInstanceOf[PMMLModelExport].getPmml()
assert(pmml.getHeader().getDescription() === "logistic regression")
//check that the number of fields match the weights size
assert(pmml.getDataDictionary().getNumberOfFields() === logisticRegressionModel.weights.size + 1)
//this verify that there is a model attached to the pmml object and the model is a regression one
//it also verifies that the pmml model has a regression table (for target category 1) with the same number of predictors of the model weights
// check that the number of fields match the weights size
assert(
pmml.getDataDictionary().getNumberOfFields() === logisticRegressionModel.weights.size + 1)
// this verify that there is a model attached to the pmml object
// and the model is a regression one
// it also verifies that the pmml model has a regression table (for target category 1)
// with the same number of predictors of the model weights
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
.getRegressionTables().get(0).getTargetCategory() === "1")
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
.getRegressionTables().get(0).getNumericPredictors().size() === logisticRegressionModel.weights.size)
//verify if there is a second table with target category 0 and no predictors
.getRegressionTables().get(0).getNumericPredictors().size()
=== logisticRegressionModel.weights.size)
// verify if there is a second table with target category 0 and no predictors
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
.getRegressionTables().get(1).getTargetCategory() === "0")
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
.getRegressionTables().get(1).getNumericPredictors().size() === 0)

//manual checking
//logisticRegressionModel.toPMML("/tmp/logisticregression.xml")
// manual checking
// logisticRegressionModel.toPMML("/tmp/logisticregression.xml")

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,78 +31,84 @@ class PMMLModelExportFactorySuite extends FunSuite{

test("PMMLModelExportFactory create KMeansPMMLModelExport when passing a KMeansModel") {

//arrange
// arrange
val clusterCenters = Array(
Vectors.dense(1.0, 2.0, 6.0),
Vectors.dense(1.0, 3.0, 0.0),
Vectors.dense(1.0, 4.0, 6.0)
)
val kmeansModel = new KMeansModel(clusterCenters);

//act
// act
val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel)

//assert
// assert
assert(modelExport.isInstanceOf[KMeansPMMLModelExport])

}

test("PMMLModelExportFactory create GeneralizedLinearPMMLModelExport when passing a "
+"LinearRegressionModel, RidgeRegressionModel, LassoModel or SVMModel") {
+ "LinearRegressionModel, RidgeRegressionModel, LassoModel or SVMModel") {

//arrange
// arrange
val linearInput = LinearDataGenerator.generateLinearInput(
3.0, Array(10.0, 10.0), 1, 17)
val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label)
val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label)
val linearRegressionModel = new LinearRegressionModel(
linearInput(0).features, linearInput(0).label)
val ridgeRegressionModel = new RidgeRegressionModel(
linearInput(0).features, linearInput(0).label)
val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label)
val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label)

//act
val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel)
//assert
// act
val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel)
// assert
assert(linearModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport])

//act
val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel)
//assert
// act
val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel)
// assert
assert(ridgeModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport])

//act
// act
val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel)
//assert
// assert
assert(lassoModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport])

//act
// act
val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel)
//assert
// assert
assert(svmModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport])

}

test("PMMLModelExportFactory create LogisticRegressionPMMLModelExport when passing a LogisticRegressionModel") {
test("PMMLModelExportFactory create LogisticRegressionPMMLModelExport "
+ "when passing a LogisticRegressionModel") {

//arrange
// arrange
val linearInput = LinearDataGenerator.generateLinearInput(
3.0, Array(10.0, 10.0), 1, 17)
val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label);
val logisticRegressionModel = new LogisticRegressionModel(
linearInput(0).features, linearInput(0).label);

//act
val logisticRegressionModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel)
//assert
// act
val logisticRegressionModelExport = PMMLModelExportFactory
.createPMMLModelExport(logisticRegressionModel)
// assert
assert(logisticRegressionModelExport.isInstanceOf[LogisticRegressionPMMLModelExport])

}

test("PMMLModelExportFactory throw IllegalArgumentException when passing an unsupported model") {
test("PMMLModelExportFactory throw IllegalArgumentException "
+ "when passing an unsupported model") {

//arrange
// arrange
val invalidModel = new Object;

//assert
// assert
intercept[IllegalArgumentException] {
//act
PMMLModelExportFactory.createPMMLModelExport(invalidModel)
// act
PMMLModelExportFactory.createPMMLModelExport(invalidModel)
}

}
Expand Down

0 comments on commit e2ffae8

Please sign in to comment.