Github user jkbradley commented on a diff in the pull request:

    https://github.com/apache/spark/pull/3099#discussion_r20118197
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/param/params.scala ---
    @@ -0,0 +1,305 @@
    +/*
    + * 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
    +
    +import java.lang.reflect.Modifier
    +
    +import scala.annotation.varargs
    +import scala.collection.mutable
    +
    +import org.apache.spark.ml.Identifiable
    +
    +/**
    + * A param with self-contained documentation and optionally default value. 
Primitive-typed param
    + * should use the specialized versions, which are more friendly to Java 
users.
    + *
    + * @param parent parent object
    + * @param name param name
    + * @param doc documentation
    + * @tparam T param value type
    + */
    +class Param[T] (
    +    val parent: Params,
    +    val name: String,
    +    val doc: String,
    +    val default: Option[T] = None) extends Serializable {
    +
    +  /**
    +   * Creates a param pair with the given value (for Java).
    +   */
    +  def w(value: T): ParamPair[T] = this -> value
    +
    +  /**
    +   * Creates a param pair with the given value (for Scala).
    +   */
    +  def ->(value: T): ParamPair[T] = ParamPair(this, value)
    +
    +  override def toString: String = {
    +    if (default.isDefined) {
    +      s"$name: $doc (default: ${default.get})"
    +    } else {
    +      s"$name: $doc"
    +    }
    +  }
    +}
    +
    +// specialize primitive-typed params because Java doesn't recognize 
scala.Double, scala.Int, ...
    +
    +/** Specialized version of [[Param[Double]]] for Java. */
    +class DoubleParam(parent: Params, name: String, doc: String, default: 
Option[Double] = None)
    +    extends Param[Double](parent, name, doc, default) {
    +  override def w(value: Double): ParamPair[Double] = super.w(value)
    +}
    +
    +/** Specialized version of [[Param[Int]]] for Java. */
    +class IntParam(parent: Params, name: String, doc: String, default: 
Option[Int] = None)
    +    extends Param[Int](parent, name, doc, default) {
    +  override def w(value: Int): ParamPair[Int] = super.w(value)
    +}
    +
    +/** Specialized version of [[Param[Float]]] for Java. */
    +class FloatParam(parent: Params, name: String, doc: String, default: 
Option[Float] = None)
    +    extends Param[Float](parent, name, doc, default) {
    +  override def w(value: Float): ParamPair[Float] = super.w(value)
    +}
    +
    +/** Specialized version of [[Param[Long]]] for Java. */
    +class LongParam(parent: Params, name: String, doc: String, default: 
Option[Long] = None)
    +    extends Param[Long](parent, name, doc, default) {
    +  override def w(value: Long): ParamPair[Long] = super.w(value)
    +}
    +
    +/** Specialized version of [[Param[Boolean]]] for Java. */
    +class BooleanParam(parent: Params, name: String, doc: String, default: 
Option[Boolean] = None)
    +    extends Param[Boolean](parent, name, doc, default) {
    +  override def w(value: Boolean): ParamPair[Boolean] = super.w(value)
    +}
    +
    +/**
    + * A param amd its value.
    + */
    +case class ParamPair[T](param: Param[T], value: T)
    +
    +/**
    + * Trait for components that take parameters. This also provides an 
internal param map to store
    + * parameter values attached to the instance.
    + */
    +trait Params extends Identifiable with Serializable {
    +
    +  /** Returns all params. */
    +  def params: Array[Param[_]] = {
    +    val methods = this.getClass.getMethods
    +    methods.filter { m =>
    +        Modifier.isPublic(m.getModifiers) &&
    +          classOf[Param[_]].isAssignableFrom(m.getReturnType) &&
    +          m.getParameterTypes.isEmpty
    +      }.sortBy(_.getName)
    +      .map(m => m.invoke(this).asInstanceOf[Param[_]])
    +  }
    +
    +  /**
    +   * Validates parameter values stored internally plus the input parameter 
map.
    +   * Raises an exception if any parameter is invalid.
    +   */
    +  def validate(paramMap: ParamMap): Unit = {}
    +
    +  /**
    +   * Validates parameter values stored internally.
    +   * Raise an exception if any parameter value is invalid.
    +   */
    +  def validate(): Unit = validate(ParamMap.empty)
    +
    +  /**
    +   * Returns the documentation of all params.
    +   */
    +  def explainParams(): String = params.mkString("\n")
    +
    +  /** Checks whether a param is explicitly set. */
    +  def isSet(param: Param[_]): Boolean = {
    +    require(param.parent.eq(this))
    +    paramMap.contains(param)
    +  }
    +
    +  /** Gets a param by its name. */
    +  private[ml] def getParam(paramName: String): Param[Any] = {
    +    val m = this.getClass.getMethod(paramName)
    +    assert(Modifier.isPublic(m.getModifiers) &&
    +      classOf[Param[_]].isAssignableFrom(m.getReturnType))
    +    m.invoke(this).asInstanceOf[Param[Any]]
    +  }
    +
    +  /**
    +   * Internal param map.
    +   */
    +  protected val paramMap: ParamMap = ParamMap.empty
    +
    +  /**
    +   * Sets a parameter in the own parameter map.
    --- End diff --
    
    "the own" --> "the internal"


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to