-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SPARKNLP-835: Introduce ProtectedParam
- Loading branch information
Showing
4 changed files
with
108 additions
and
15 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/scala/com/johnsnowlabs/nlp/HasProtectedParams.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.johnsnowlabs.nlp | ||
|
||
import org.apache.spark.ml.param.{Param, Params} | ||
|
||
/** Enables a class to protect a parameter, which means that it can only be set once. | ||
* | ||
* This trait will enable a implicit conversion from Param to ProtectedParam. In addition, the | ||
* new set for ProtectedParam will then check, whether or not the value was already set. If so, | ||
* then a warning will be output and the value will not be set again. | ||
*/ | ||
trait HasProtectedParams { | ||
this: Params => | ||
implicit class ProtectedParam[T](private val param: Param[T]) | ||
extends Param[T](param.parent, param.name, param.doc, param.isValid) { | ||
|
||
var isProtected = false | ||
|
||
/** Sets this parameter to be protected, which means that it can only be set once. | ||
* | ||
* Default values do not count as a set value and can be overridden. | ||
* | ||
* @return | ||
* This object | ||
*/ | ||
def setProtected(): this.type = { | ||
isProtected = true | ||
this | ||
} | ||
|
||
def toParam: Param[T] = this.asInstanceOf[Param[T]] | ||
} | ||
|
||
/** Sets the value for a protected Param. | ||
* | ||
* If the parameter was already set, it will not be set again. Default values do not count as a | ||
* set value and can be overridden. | ||
* | ||
* @param param | ||
* Protected parameter to set | ||
* @param value | ||
* Value for the parameter | ||
* @tparam T | ||
* Type of the parameter | ||
* @return | ||
* This object | ||
*/ | ||
def set[T](param: ProtectedParam[T], value: T): this.type = { | ||
if (param.isProtected && get(param).isDefined) | ||
println( | ||
s"Warning: The parameter ${param.name} is protected and can only be set once." + | ||
" For a pretrained model, this was done during the initialization process." + | ||
" If you are trying to train your own model, please check the documentation.") | ||
else | ||
set(param.toParam, value) | ||
this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/scala/com/johnsnowlabs/nlp/HasProtectedParamsTestSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.johnsnowlabs.nlp | ||
|
||
import com.johnsnowlabs.nlp.serialization.StructFeature | ||
import com.johnsnowlabs.tags.FastTest | ||
import com.johnsnowlabs.util.TestUtils.captureOutput | ||
import org.apache.spark.ml.param.Param | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
class HasProtectedParamsTestSpec extends AnyFlatSpec { | ||
class MockModel extends AnnotatorModel[MockModel] with HasProtectedParams { | ||
override val uid: String = "MockModel" | ||
override val outputAnnotatorType: AnnotatorType = AnnotatorType.DUMMY | ||
override val inputAnnotatorTypes: Array[AnnotatorType] = Array(AnnotatorType.DUMMY) | ||
|
||
val protectedParam = | ||
new Param[String](this, "MockString", "Mock protected Param").setProtected() | ||
def setProtectedParam(value: String): this.type = { | ||
set(protectedParam, value) | ||
} | ||
|
||
def getProtectedParam: String = { | ||
$(protectedParam) | ||
} | ||
|
||
} | ||
|
||
val model = new MockModel | ||
|
||
behavior of "HasProtectedParams" | ||
|
||
it should "set protected params only once" taggedAs FastTest in { | ||
model.setProtectedParam("first") | ||
|
||
assert(model.getProtectedParam == "first") | ||
|
||
val output = captureOutput { | ||
model.setProtectedParam("second") | ||
|
||
} | ||
assert(output.contains("is protected and can only be set once")) | ||
assert(model.getProtectedParam == "first") | ||
} | ||
} |