Skip to content

Commit

Permalink
move ParamGridBuilder test to ParamGridBuilderSuite
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxr committed Nov 9, 2014
1 parent e246f29 commit c7f6921
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 15 deletions.
16 changes: 1 addition & 15 deletions mllib/src/test/scala/org/apache/spark/ml/param/ParamsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,7 @@ import org.scalatest.FunSuite

class ParamsSuite extends FunSuite {

class Solver extends Params {
val maxIter = new IntParam(this, "maxIter", "max number of iterations", Some(100))
def setMaxIter(value: Int): this.type = { set(maxIter, value); this }
def getMaxIter: Int = get(maxIter)
val inputCol = new Param[String](this, "inputCol", "input column name")
def setInputCol(value: String): this.type = { set(inputCol, value); this }
def getInputCol: String = get(inputCol)
override def validate(paramMap: ParamMap) = {
val m = this.paramMap ++ paramMap
assert(m(maxIter) >= 0)
assert(m.contains(inputCol))
}
}

val solver = new Solver()
val solver = new TestParams()
import solver.{maxIter, inputCol}

test("param") {
Expand Down
36 changes: 36 additions & 0 deletions mllib/src/test/scala/org/apache/spark/ml/param/TestParams.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.spark.ml.param

/** A subclass of Params for testing. */
class TestParams extends Params {

val maxIter = new IntParam(this, "maxIter", "max number of iterations", Some(100))
def setMaxIter(value: Int): this.type = { set(maxIter, value); this }
def getMaxIter: Int = get(maxIter)

val inputCol = new Param[String](this, "inputCol", "input column name")
def setInputCol(value: String): this.type = { set(inputCol, value); this }
def getInputCol: String = get(inputCol)

override def validate(paramMap: ParamMap) = {
val m = this.paramMap ++ paramMap
assert(m(maxIter) >= 0)
assert(m.contains(inputCol))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.spark.ml.tuning

import scala.collection.mutable

import org.scalatest.FunSuite

import org.apache.spark.ml.param.{ParamMap, TestParams}

class ParamGridBuilderSuite extends FunSuite {

val solver = new TestParams()
import solver.{inputCol, maxIter}

test("param grid builder") {
def validateGrid(maps: Array[ParamMap], expected: mutable.Set[(Int, String)]): Unit = {
assert(maps.size === expected.size)
maps.foreach { m =>
val tuple = (m(maxIter), m(inputCol))
assert(expected.contains(tuple))
expected.remove(tuple)
}
assert(expected.isEmpty)
}

val maps0 = new ParamGridBuilder()
.baseOn(maxIter -> 10)
.addGrid(inputCol, Array("input0", "input1"))
.build()
val expected0 = mutable.Set(
(10, "input0"),
(10, "input1"))
validateGrid(maps0, expected0)

val maps1 = new ParamGridBuilder()
.baseOn(ParamMap(maxIter -> 5, inputCol -> "input")) // will be overwritten
.addGrid(maxIter, Array(10, 20))
.addGrid(inputCol, Array("input0", "input1"))
.build()
val expected1 = mutable.Set(
(10, "input0"),
(20, "input0"),
(10, "input1"),
(20, "input1"))
validateGrid(maps1, expected1)
}
}

0 comments on commit c7f6921

Please sign in to comment.