Skip to content

Commit e2ffae8

Browse files
committed
fixed scala style
1 parent b8823b0 commit e2ffae8

File tree

5 files changed

+115
-86
lines changed

5 files changed

+115
-86
lines changed

mllib/src/main/scala/org/apache/spark/mllib/clustering/KMeansModel.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import org.apache.spark.sql.Row
3535
/**
3636
* A clustering model for K-means. Each point belongs to the cluster with the closest center.
3737
*/
38-
class KMeansModel (val clusterCenters: Array[Vector]) extends Saveable with Serializable with PMMLExportable {
38+
class KMeansModel (
39+
val clusterCenters: Array[Vector]) extends Saveable with Serializable with PMMLExportable {
3940

4041
/** A Java-friendly constructor that takes an Iterable of Vectors. */
4142
def this(centers: java.lang.Iterable[Vector]) = this(centers.asScala.toArray)

mllib/src/test/scala/org/apache/spark/mllib/pmml/export/GeneralizedLinearPMMLModelExportSuite.scala

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,71 +29,85 @@ class GeneralizedLinearPMMLModelExportSuite extends FunSuite{
2929

3030
test("GeneralizedLinearPMMLModelExport generate PMML format") {
3131

32-
//arrange models to test
32+
// arrange models to test
3333
val linearInput = LinearDataGenerator.generateLinearInput(
3434
3.0, Array(10.0, 10.0), 1, 17)
35-
val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label);
36-
val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label);
35+
val linearRegressionModel = new LinearRegressionModel(
36+
linearInput(0).features, linearInput(0).label);
37+
val ridgeRegressionModel = new RidgeRegressionModel(
38+
linearInput(0).features, linearInput(0).label);
3739
val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label);
3840
val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label);
3941

40-
//act by exporting the model to the PMML format
41-
val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel)
42-
//assert that the PMML format is as expected
42+
// act by exporting the model to the PMML format
43+
val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel)
44+
// assert that the PMML format is as expected
4345
assert(linearModelExport.isInstanceOf[PMMLModelExport])
4446
var pmml = linearModelExport.asInstanceOf[PMMLModelExport].getPmml()
4547
assert(pmml.getHeader().getDescription() === "linear regression")
46-
//check that the number of fields match the weights size
47-
assert(pmml.getDataDictionary().getNumberOfFields() === linearRegressionModel.weights.size + 1)
48-
//this verify that there is a model attached to the pmml object and the model is a regression one
49-
//it also verifies that the pmml model has a regression table with the same number of predictors of the model weights
48+
// check that the number of fields match the weights size
49+
assert(pmml.getDataDictionary().getNumberOfFields()
50+
=== linearRegressionModel.weights.size + 1)
51+
// this verify that there is a model attached to the pmml object
52+
// and the model is a regression one
53+
// it also verifies that the pmml model has a regression table
54+
// with the same number of predictors of the model weights
5055
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
51-
.getRegressionTables().get(0).getNumericPredictors().size() === linearRegressionModel.weights.size)
56+
.getRegressionTables().get(0).getNumericPredictors().size()
57+
=== linearRegressionModel.weights.size)
5258

53-
//act
59+
// act
5460
val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel)
55-
//assert that the PMML format is as expected
61+
// assert that the PMML format is as expected
5662
assert(ridgeModelExport.isInstanceOf[PMMLModelExport])
5763
pmml = ridgeModelExport.asInstanceOf[PMMLModelExport].getPmml()
5864
assert(pmml.getHeader().getDescription() === "ridge regression")
59-
//check that the number of fields match the weights size
65+
// check that the number of fields match the weights size
6066
assert(pmml.getDataDictionary().getNumberOfFields() === ridgeRegressionModel.weights.size + 1)
61-
//this verify that there is a model attached to the pmml object and the model is a regression one
62-
//it also verifies that the pmml model has a regression table with the same number of predictors of the model weights
67+
// this verify that there is a model attached to the pmml object
68+
// and the model is a regression one
69+
// it also verifies that the pmml model has a regression table
70+
// with the same number of predictors of the model weights
6371
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
64-
.getRegressionTables().get(0).getNumericPredictors().size() === ridgeRegressionModel.weights.size)
72+
.getRegressionTables().get(0).getNumericPredictors().size()
73+
=== ridgeRegressionModel.weights.size)
6574

66-
//act
75+
// act
6776
val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel)
68-
//assert that the PMML format is as expected
77+
// assert that the PMML format is as expected
6978
assert(lassoModelExport.isInstanceOf[PMMLModelExport])
7079
pmml = lassoModelExport.asInstanceOf[PMMLModelExport].getPmml()
7180
assert(pmml.getHeader().getDescription() === "lasso regression")
72-
//check that the number of fields match the weights size
81+
// check that the number of fields match the weights size
7382
assert(pmml.getDataDictionary().getNumberOfFields() === lassoModel.weights.size + 1)
74-
//this verify that there is a model attached to the pmml object and the model is a regression one
75-
//it also verifies that the pmml model has a regression table with the same number of predictors of the model weights
83+
// this verify that there is a model attached to the pmml object
84+
// and the model is a regression one
85+
// it also verifies that the pmml model has a regression table
86+
// with the same number of predictors of the model weights
7687
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
7788
.getRegressionTables().get(0).getNumericPredictors().size() === lassoModel.weights.size)
7889

79-
//act
90+
// act
8091
val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel)
81-
//assert that the PMML format is as expected
92+
// assert that the PMML format is as expected
8293
assert(svmModelExport.isInstanceOf[PMMLModelExport])
8394
pmml = svmModelExport.asInstanceOf[PMMLModelExport].getPmml()
84-
assert(pmml.getHeader().getDescription() === "linear SVM: if predicted value > 0, the outcome is positive, or negative otherwise")
85-
//check that the number of fields match the weights size
95+
assert(pmml.getHeader().getDescription()
96+
=== "linear SVM: if predicted value > 0, the outcome is positive, or negative otherwise")
97+
// check that the number of fields match the weights size
8698
assert(pmml.getDataDictionary().getNumberOfFields() === svmModel.weights.size + 1)
87-
//this verify that there is a model attached to the pmml object and the model is a regression one
88-
//it also verifies that the pmml model has a regression table with the same number of predictors of the model weights
99+
// this verify that there is a model attached to the pmml object
100+
// and the model is a regression one
101+
// it also verifies that the pmml model has a regression table
102+
// with the same number of predictors of the model weights
89103
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
90104
.getRegressionTables().get(0).getNumericPredictors().size() === svmModel.weights.size)
91105

92-
//manual checking
93-
//linearRegressionModel.toPMML("/tmp/linearregression.xml")
94-
//ridgeRegressionModel.toPMML("/tmp/ridgeregression.xml")
95-
//lassoModel.toPMML("/tmp/lassoregression.xml")
96-
//svmModel.toPMML("/tmp/linearsvm.xml")
106+
// manual checking
107+
// linearRegressionModel.toPMML("/tmp/linearregression.xml")
108+
// ridgeRegressionModel.toPMML("/tmp/ridgeregression.xml")
109+
// lassoModel.toPMML("/tmp/lassoregression.xml")
110+
// svmModel.toPMML("/tmp/linearsvm.xml")
97111

98112
}
99113

mllib/src/test/scala/org/apache/spark/mllib/pmml/export/KMeansPMMLModelExportSuite.scala

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,33 @@ class KMeansPMMLModelExportSuite extends FunSuite{
2626

2727
test("KMeansPMMLModelExport generate PMML format") {
2828

29-
//arrange model to test
29+
// arrange model to test
3030
val clusterCenters = Array(
3131
Vectors.dense(1.0, 2.0, 6.0),
3232
Vectors.dense(1.0, 3.0, 0.0),
3333
Vectors.dense(1.0, 4.0, 6.0)
3434
)
3535
val kmeansModel = new KMeansModel(clusterCenters);
3636

37-
//act by exporting the model to the PMML format
37+
// act by exporting the model to the PMML format
3838
val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel)
3939

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

50-
//manual checking
51-
//kmeansModel.toPMML("/tmp/kmeans.xml")
52-
//kmeansModel.toPMML(System.out)
53-
//System.out.println(kmeansModel.toPMML())
52+
// manual checking
53+
// kmeansModel.toPMML("/tmp/kmeans.xml")
54+
// kmeansModel.toPMML(System.out)
55+
// System.out.println(kmeansModel.toPMML())
5456

5557
}
5658

mllib/src/test/scala/org/apache/spark/mllib/pmml/export/LogisticRegressionPMMLModelExportSuite.scala

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,39 @@ class LogisticRegressionPMMLModelExportSuite extends FunSuite{
2626

2727
test("LogisticRegressionPMMLModelExport generate PMML format") {
2828

29-
//arrange models to test
29+
// arrange models to test
3030
val linearInput = LinearDataGenerator.generateLinearInput(
3131
3.0, Array(10.0, 10.0), 1, 17)
32-
val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label);
32+
val logisticRegressionModel = new LogisticRegressionModel(
33+
linearInput(0).features, linearInput(0).label);
3334

34-
//act by exporting the model to the PMML format
35-
val logisticModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel)
36-
//assert that the PMML format is as expected
35+
// act by exporting the model to the PMML format
36+
val logisticModelExport = PMMLModelExportFactory
37+
.createPMMLModelExport(logisticRegressionModel)
38+
// assert that the PMML format is as expected
3739
assert(logisticModelExport.isInstanceOf[PMMLModelExport])
3840
var pmml = logisticModelExport.asInstanceOf[PMMLModelExport].getPmml()
3941
assert(pmml.getHeader().getDescription() === "logistic regression")
40-
//check that the number of fields match the weights size
41-
assert(pmml.getDataDictionary().getNumberOfFields() === logisticRegressionModel.weights.size + 1)
42-
//this verify that there is a model attached to the pmml object and the model is a regression one
43-
//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
42+
// check that the number of fields match the weights size
43+
assert(
44+
pmml.getDataDictionary().getNumberOfFields() === logisticRegressionModel.weights.size + 1)
45+
// this verify that there is a model attached to the pmml object
46+
// and the model is a regression one
47+
// it also verifies that the pmml model has a regression table (for target category 1)
48+
// with the same number of predictors of the model weights
4449
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
4550
.getRegressionTables().get(0).getTargetCategory() === "1")
4651
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
47-
.getRegressionTables().get(0).getNumericPredictors().size() === logisticRegressionModel.weights.size)
48-
//verify if there is a second table with target category 0 and no predictors
52+
.getRegressionTables().get(0).getNumericPredictors().size()
53+
=== logisticRegressionModel.weights.size)
54+
// verify if there is a second table with target category 0 and no predictors
4955
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
5056
.getRegressionTables().get(1).getTargetCategory() === "0")
5157
assert(pmml.getModels().get(0).asInstanceOf[RegressionModel]
5258
.getRegressionTables().get(1).getNumericPredictors().size() === 0)
5359

54-
//manual checking
55-
//logisticRegressionModel.toPMML("/tmp/logisticregression.xml")
60+
// manual checking
61+
// logisticRegressionModel.toPMML("/tmp/logisticregression.xml")
5662

5763
}
5864

mllib/src/test/scala/org/apache/spark/mllib/pmml/export/PMMLModelExportFactorySuite.scala

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,78 +31,84 @@ class PMMLModelExportFactorySuite extends FunSuite{
3131

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

34-
//arrange
34+
// arrange
3535
val clusterCenters = Array(
3636
Vectors.dense(1.0, 2.0, 6.0),
3737
Vectors.dense(1.0, 3.0, 0.0),
3838
Vectors.dense(1.0, 4.0, 6.0)
3939
)
4040
val kmeansModel = new KMeansModel(clusterCenters);
4141

42-
//act
42+
// act
4343
val modelExport = PMMLModelExportFactory.createPMMLModelExport(kmeansModel)
4444

45-
//assert
45+
// assert
4646
assert(modelExport.isInstanceOf[KMeansPMMLModelExport])
4747

4848
}
4949

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

53-
//arrange
53+
// arrange
5454
val linearInput = LinearDataGenerator.generateLinearInput(
5555
3.0, Array(10.0, 10.0), 1, 17)
56-
val linearRegressionModel = new LinearRegressionModel(linearInput(0).features, linearInput(0).label)
57-
val ridgeRegressionModel = new RidgeRegressionModel(linearInput(0).features, linearInput(0).label)
56+
val linearRegressionModel = new LinearRegressionModel(
57+
linearInput(0).features, linearInput(0).label)
58+
val ridgeRegressionModel = new RidgeRegressionModel(
59+
linearInput(0).features, linearInput(0).label)
5860
val lassoModel = new LassoModel(linearInput(0).features, linearInput(0).label)
5961
val svmModel = new SVMModel(linearInput(0).features, linearInput(0).label)
6062

61-
//act
62-
val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel)
63-
//assert
63+
// act
64+
val linearModelExport = PMMLModelExportFactory.createPMMLModelExport(linearRegressionModel)
65+
// assert
6466
assert(linearModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport])
6567

66-
//act
67-
val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel)
68-
//assert
68+
// act
69+
val ridgeModelExport = PMMLModelExportFactory.createPMMLModelExport(ridgeRegressionModel)
70+
// assert
6971
assert(ridgeModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport])
7072

71-
//act
73+
// act
7274
val lassoModelExport = PMMLModelExportFactory.createPMMLModelExport(lassoModel)
73-
//assert
75+
// assert
7476
assert(lassoModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport])
7577

76-
//act
78+
// act
7779
val svmModelExport = PMMLModelExportFactory.createPMMLModelExport(svmModel)
78-
//assert
80+
// assert
7981
assert(svmModelExport.isInstanceOf[GeneralizedLinearPMMLModelExport])
8082

8183
}
8284

83-
test("PMMLModelExportFactory create LogisticRegressionPMMLModelExport when passing a LogisticRegressionModel") {
85+
test("PMMLModelExportFactory create LogisticRegressionPMMLModelExport "
86+
+ "when passing a LogisticRegressionModel") {
8487

85-
//arrange
88+
// arrange
8689
val linearInput = LinearDataGenerator.generateLinearInput(
8790
3.0, Array(10.0, 10.0), 1, 17)
88-
val logisticRegressionModel = new LogisticRegressionModel(linearInput(0).features, linearInput(0).label);
91+
val logisticRegressionModel = new LogisticRegressionModel(
92+
linearInput(0).features, linearInput(0).label);
8993

90-
//act
91-
val logisticRegressionModelExport = PMMLModelExportFactory.createPMMLModelExport(logisticRegressionModel)
92-
//assert
94+
// act
95+
val logisticRegressionModelExport = PMMLModelExportFactory
96+
.createPMMLModelExport(logisticRegressionModel)
97+
// assert
9398
assert(logisticRegressionModelExport.isInstanceOf[LogisticRegressionPMMLModelExport])
9499

95100
}
96101

97-
test("PMMLModelExportFactory throw IllegalArgumentException when passing an unsupported model") {
102+
test("PMMLModelExportFactory throw IllegalArgumentException "
103+
+ "when passing an unsupported model") {
98104

99-
//arrange
105+
// arrange
100106
val invalidModel = new Object;
101107

102-
//assert
108+
// assert
103109
intercept[IllegalArgumentException] {
104-
//act
105-
PMMLModelExportFactory.createPMMLModelExport(invalidModel)
110+
// act
111+
PMMLModelExportFactory.createPMMLModelExport(invalidModel)
106112
}
107113

108114
}

0 commit comments

Comments
 (0)