From e2ffae8987d5cfaaf8a94da1ab95f6e3ca3ba1b8 Mon Sep 17 00:00:00 2001 From: Vincenzo Selvaggio Date: Sun, 19 Apr 2015 17:06:39 +0100 Subject: [PATCH] fixed scala style --- .../spark/mllib/clustering/KMeansModel.scala | 3 +- ...eneralizedLinearPMMLModelExportSuite.scala | 80 +++++++++++-------- .../export/KMeansPMMLModelExportSuite.scala | 24 +++--- ...gisticRegressionPMMLModelExportSuite.scala | 32 +++++--- .../export/PMMLModelExportFactorySuite.scala | 62 +++++++------- 5 files changed, 115 insertions(+), 86 deletions(-) diff --git a/mllib/src/main/scala/org/apache/spark/mllib/clustering/KMeansModel.scala b/mllib/src/main/scala/org/apache/spark/mllib/clustering/KMeansModel.scala index 1dcc04f17444b..ba228b11fcec3 100644 --- a/mllib/src/main/scala/org/apache/spark/mllib/clustering/KMeansModel.scala +++ b/mllib/src/main/scala/org/apache/spark/mllib/clustering/KMeansModel.scala @@ -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) diff --git a/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/GeneralizedLinearPMMLModelExportSuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/GeneralizedLinearPMMLModelExportSuite.scala index 9b0c81b5b099f..b160b6881dc43 100644 --- a/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/GeneralizedLinearPMMLModelExportSuite.scala +++ b/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/GeneralizedLinearPMMLModelExportSuite.scala @@ -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") } diff --git a/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/KMeansPMMLModelExportSuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/KMeansPMMLModelExportSuite.scala index 00682b0f78190..83def5ace0cbc 100644 --- a/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/KMeansPMMLModelExportSuite.scala +++ b/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/KMeansPMMLModelExportSuite.scala @@ -26,7 +26,7 @@ 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), @@ -34,23 +34,25 @@ class KMeansPMMLModelExportSuite extends FunSuite{ ) 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()) } diff --git a/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/LogisticRegressionPMMLModelExportSuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/LogisticRegressionPMMLModelExportSuite.scala index b96194d47b882..ca5d8ca8b2f5b 100644 --- a/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/LogisticRegressionPMMLModelExportSuite.scala +++ b/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/LogisticRegressionPMMLModelExportSuite.scala @@ -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") } diff --git a/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/PMMLModelExportFactorySuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/PMMLModelExportFactorySuite.scala index 5b34e5a8329fb..a54cd247a120f 100644 --- a/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/PMMLModelExportFactorySuite.scala +++ b/mllib/src/test/scala/org/apache/spark/mllib/pmml/export/PMMLModelExportFactorySuite.scala @@ -31,7 +31,7 @@ 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), @@ -39,70 +39,76 @@ class PMMLModelExportFactorySuite extends FunSuite{ ) 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) } }