From b4a6036eca748fff8af629033939e37342c2d333 Mon Sep 17 00:00:00 2001 From: mb706 Date: Sun, 25 Jun 2017 17:42:10 +0200 Subject: [PATCH 01/11] Fix printing of discretevectorparam with length 0 default --- R/convertDiscrete.R | 4 ++-- tests/testthat/test_Param.R | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/R/convertDiscrete.R b/R/convertDiscrete.R index 9c134582..dd66e426 100644 --- a/R/convertDiscrete.R +++ b/R/convertDiscrete.R @@ -53,7 +53,7 @@ discreteValueToName = function(par, x) { return(NA_character_) assertClass(par, "Param") assertChoice(par$type, c("discrete", "discretevector")) - if (par$type == "discretevector" && length(x) != par$len) + if (par$type == "discretevector" && !is.na(par$len) && length(x) != par$len) stopf("Length of x must be %i!", par$len) ns = names(par$values) getIndex = function(values, v) { @@ -65,6 +65,6 @@ discreteValueToName = function(par, x) { if (par$type == "discrete") { ns[getIndex(par$values, x)] } else if (par$type == "discretevector") { - ns[sapply(x, getIndex, values = par$values)] + sapply(x, function(x) ns[getIndex(x, values = par$values)]) } } diff --git a/tests/testthat/test_Param.R b/tests/testthat/test_Param.R index a7293f63..ec9d27a8 100644 --- a/tests/testthat/test_Param.R +++ b/tests/testthat/test_Param.R @@ -279,6 +279,9 @@ test_that("param print works", { expect_output(print(p), "99") p = makeUntypedParam(id = "x", default = c(99, 99)) expect_output(print(p), "untyped") + p = makeDiscreteVectorLearnerParam("test", + default = list(), values = c("a", "b", "c"), len = NA) + expect_output(print(p), "discretevector") }) test_that("normal (not learner) vec param cannot have NA lengths", { From a72a17fd4d1e60c4d106c701d7fe75880080fb69 Mon Sep 17 00:00:00 2001 From: mb706 Date: Sun, 25 Jun 2017 17:50:06 +0200 Subject: [PATCH 02/11] make 'tunable' uniform --- R/aParam.R | 3 +-- R/makeParamFuns.R | 14 ++++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/R/aParam.R b/R/aParam.R index fe38553c..78c4889d 100644 --- a/R/aParam.R +++ b/R/aParam.R @@ -77,8 +77,7 @@ #' Defining a parameter to be not-tunable allows to mark arguments like, e.g., \dQuote{verbose} or #' other purely technical stuff, and allows them to be excluded from later automatic optimization #' procedures that would try to consider all available parameters. -#' Default is \code{TRUE} (except for \code{untyped}, \code{function}, \code{character} and -#' \code{characterVector}) which means it is tunable. +#' Default is \code{TRUE}, which means it is tunable. #' @param special.vals [\code{list()}]\cr #' A list of special values the parameter can except which are outside of the defined range. #' Default is an empty list. diff --git a/R/makeParamFuns.R b/R/makeParamFuns.R index d65c51b0..82928260 100644 --- a/R/makeParamFuns.R +++ b/R/makeParamFuns.R @@ -76,10 +76,11 @@ makeDiscreteVectorParam = function(id, len, values, default, requires = NULL, #' @rdname Param #' @export -makeFunctionParam = function(id, default = default, requires = NULL, special.vals = list()) { +makeFunctionParam = function(id, default = default, requires = NULL, tunable = TRUE, + special.vals = list()) { makeParam(id = id, type = "function", learner.param = FALSE, values = NULL, default = default, trafo = NULL, - requires = requires, tunable = FALSE, special.vals = special.vals) + requires = requires, tunable = tunable, special.vals = special.vals) } #FIXME: what happens if NA is later used for untyped params? because we might interpret this as @@ -94,19 +95,20 @@ makeUntypedParam = function(id, default, requires = NULL, tunable = TRUE, specia #' @rdname Param #' @export -makeCharacterParam = function(id, default, requires = NULL, special.vals = list()) { +makeCharacterParam = function(id, default, requires = NULL, tunable = TRUE, + special.vals = list()) { makeParam(id = id, type = "character", learner.param = FALSE, default = default, trafo = NULL, - requires = requires, tunable = FALSE, special.vals = special.vals) + requires = requires, tunable = tunable, special.vals = special.vals) } #' @rdname Param #' @export makeCharacterVectorParam = function(id, len, cnames = NULL, default, - requires = NULL, special.vals = list()) { + requires = NULL, tunable = TRUE, special.vals = list()) { makeParam(id = id, type = "charactervector", learner.param = FALSE, len = len, cnames = cnames, default = default, - trafo = NULL, requires = requires, tunable = FALSE, special.vals = special.vals) + trafo = NULL, requires = requires, tunable = tunable, special.vals = special.vals) } From dcdb6f98ae5373f9cc5a085f4f696872d590fc9d Mon Sep 17 00:00:00 2001 From: mb706 Date: Sun, 25 Jun 2017 17:56:06 +0200 Subject: [PATCH 03/11] Adding makeCharacterLearnerParam --- R/makeLearnerParamFuns.R | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/R/makeLearnerParamFuns.R b/R/makeLearnerParamFuns.R index 73800533..9c92d338 100644 --- a/R/makeLearnerParamFuns.R +++ b/R/makeLearnerParamFuns.R @@ -64,7 +64,7 @@ makeLogicalLearnerParam = function(id, default, when = "train", requires = NULL, makeLogicalVectorLearnerParam = function(id, len = as.integer(NA), default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) { values = list("TRUE" = TRUE, "FALSE" = FALSE) - makeParam(id = id, type = "logicalvector", learner.param = TRUE, len = len,values = values, + makeParam(id = id, type = "logicalvector", learner.param = TRUE, len = len, values = values, default = default, requires = requires, tunable = tunable, special.vals = special.vals, when = when) } @@ -83,3 +83,21 @@ makeFunctionLearnerParam = function(id, default, when = "train", requires = NULL } +#' @rdname LearnerParam +#' @export +makeCharacterParam = function(id, default, when = "train", requires = NULL, tunable = TRUE, + special.vals = list()) { + makeParam(id = id, type = "character", learner.param = TRUE, + default = default, requires = requires, tunable = tunable, special.vals = special.vals, when = when) +} + +#' @rdname LearnerParam +#' @export +makeCharacterVectorParam = function(id, len = as.integer(NA), default, when = "train", + requires = NULL, tunable = TRUE, special.vals = list()) { + + makeParam(id = id, type = "charactervector", learner.param = TRUE, len = len, + default = default, requires = requires, tunable = tunable, special.vals = special.vals, when = when) +} + + From 50b17a243f66436fe593f2645cbf1d06e1769471 Mon Sep 17 00:00:00 2001 From: mb706 Date: Sun, 25 Jun 2017 18:29:02 +0200 Subject: [PATCH 04/11] roxygenize --- man/LearnerParam.Rd | 16 ++++++++++++++-- man/Param.Rd | 10 +++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/man/LearnerParam.Rd b/man/LearnerParam.Rd index ee103df7..5a1a9c94 100644 --- a/man/LearnerParam.Rd +++ b/man/LearnerParam.Rd @@ -12,6 +12,8 @@ \alias{makeLogicalVectorLearnerParam} \alias{makeUntypedLearnerParam} \alias{makeFunctionLearnerParam} +\alias{makeCharacterParam} +\alias{makeCharacterVectorParam} \title{Create a description object for a parameter of a machine learning algorithm.} \usage{ makeNumericLearnerParam(id, lower = -Inf, upper = Inf, allow.inf = FALSE, @@ -49,6 +51,12 @@ makeUntypedLearnerParam(id, default, when = "train", requires = NULL, makeFunctionLearnerParam(id, default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) + +makeCharacterParam(id, default, requires = NULL, tunable = TRUE, + special.vals = list()) + +makeCharacterVectorParam(id, len, cnames = NULL, default, requires = NULL, + tunable = TRUE, special.vals = list()) } \arguments{ \item{id}{[\code{character(1)}]\cr @@ -94,8 +102,7 @@ Is this parameter tunable? Defining a parameter to be not-tunable allows to mark arguments like, e.g., \dQuote{verbose} or other purely technical stuff, and allows them to be excluded from later automatic optimization procedures that would try to consider all available parameters. -Default is \code{TRUE} (except for \code{untyped}, \code{function}, \code{character} and -\code{characterVector}) which means it is tunable.} +Default is \code{TRUE}, which means it is tunable.} \item{special.vals}{[\code{list()}]\cr A list of special values the parameter can except which are outside of the defined range. @@ -111,6 +118,11 @@ you are also allowed to pass a list of quite \dQuote{complex} R objects, which are used as discrete choices. If you do the latter, the elements must be uniquely named, so that the names can be used as internal representations for the choice.} + +\item{cnames}{[\code{character}]\cr +Component names for vector params (except discrete). +Every function in this package that creates vector values for such a param, will name +that vector with \code{cnames}.} } \value{ [\code{\link{LearnerParam}}]. diff --git a/man/Param.Rd b/man/Param.Rd index ab407a64..56582546 100644 --- a/man/Param.Rd +++ b/man/Param.Rd @@ -43,16 +43,17 @@ makeDiscreteParam(id, values, trafo = NULL, default, requires = NULL, makeDiscreteVectorParam(id, len, values, default, requires = NULL, tunable = TRUE, special.vals = list()) -makeFunctionParam(id, default = default, requires = NULL, +makeFunctionParam(id, default = default, requires = NULL, tunable = TRUE, special.vals = list()) makeUntypedParam(id, default, requires = NULL, tunable = TRUE, special.vals = list()) -makeCharacterParam(id, default, requires = NULL, special.vals = list()) +makeCharacterParam(id, default, requires = NULL, tunable = TRUE, + special.vals = list()) makeCharacterVectorParam(id, len, cnames = NULL, default, requires = NULL, - special.vals = list()) + tunable = TRUE, special.vals = list()) } \arguments{ \item{id}{[\code{character(1)}]\cr @@ -100,8 +101,7 @@ Is this parameter tunable? Defining a parameter to be not-tunable allows to mark arguments like, e.g., \dQuote{verbose} or other purely technical stuff, and allows them to be excluded from later automatic optimization procedures that would try to consider all available parameters. -Default is \code{TRUE} (except for \code{untyped}, \code{function}, \code{character} and -\code{characterVector}) which means it is tunable.} +Default is \code{TRUE}, which means it is tunable.} \item{special.vals}{[\code{list()}]\cr A list of special values the parameter can except which are outside of the defined range. From 8096d7176e7dbff3d7e074716d5bb7fa3c10fd43 Mon Sep 17 00:00:00 2001 From: mb706 Date: Sun, 25 Jun 2017 18:34:37 +0200 Subject: [PATCH 05/11] Forgot name change when copy-pasting --- NAMESPACE | 2 ++ R/makeLearnerParamFuns.R | 4 ++-- man/LearnerParam.Rd | 18 +++++++----------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 821b2ec2..7ae2b85c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -136,6 +136,8 @@ export(isRequiresOk) export(isSpecialValue) export(isVector) export(isVectorTypeString) +export(makeCharacterLearnerParam) +export(makeCharacterLearnerVectorParam) export(makeCharacterParam) export(makeCharacterVectorParam) export(makeDiscreteLearnerParam) diff --git a/R/makeLearnerParamFuns.R b/R/makeLearnerParamFuns.R index 9c92d338..e7e7b252 100644 --- a/R/makeLearnerParamFuns.R +++ b/R/makeLearnerParamFuns.R @@ -85,7 +85,7 @@ makeFunctionLearnerParam = function(id, default, when = "train", requires = NULL #' @rdname LearnerParam #' @export -makeCharacterParam = function(id, default, when = "train", requires = NULL, tunable = TRUE, +makeCharacterLearnerParam = function(id, default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) { makeParam(id = id, type = "character", learner.param = TRUE, default = default, requires = requires, tunable = tunable, special.vals = special.vals, when = when) @@ -93,7 +93,7 @@ makeCharacterParam = function(id, default, when = "train", requires = NULL, tuna #' @rdname LearnerParam #' @export -makeCharacterVectorParam = function(id, len = as.integer(NA), default, when = "train", +makeCharacterLearnerVectorParam = function(id, len = as.integer(NA), default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) { makeParam(id = id, type = "charactervector", learner.param = TRUE, len = len, diff --git a/man/LearnerParam.Rd b/man/LearnerParam.Rd index 5a1a9c94..42f356c8 100644 --- a/man/LearnerParam.Rd +++ b/man/LearnerParam.Rd @@ -12,8 +12,8 @@ \alias{makeLogicalVectorLearnerParam} \alias{makeUntypedLearnerParam} \alias{makeFunctionLearnerParam} -\alias{makeCharacterParam} -\alias{makeCharacterVectorParam} +\alias{makeCharacterLearnerParam} +\alias{makeCharacterLearnerVectorParam} \title{Create a description object for a parameter of a machine learning algorithm.} \usage{ makeNumericLearnerParam(id, lower = -Inf, upper = Inf, allow.inf = FALSE, @@ -52,11 +52,12 @@ makeUntypedLearnerParam(id, default, when = "train", requires = NULL, makeFunctionLearnerParam(id, default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) -makeCharacterParam(id, default, requires = NULL, tunable = TRUE, - special.vals = list()) - -makeCharacterVectorParam(id, len, cnames = NULL, default, requires = NULL, +makeCharacterLearnerParam(id, default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) + +makeCharacterLearnerVectorParam(id, len = as.integer(NA), default, + when = "train", requires = NULL, tunable = TRUE, + special.vals = list()) } \arguments{ \item{id}{[\code{character(1)}]\cr @@ -118,11 +119,6 @@ you are also allowed to pass a list of quite \dQuote{complex} R objects, which are used as discrete choices. If you do the latter, the elements must be uniquely named, so that the names can be used as internal representations for the choice.} - -\item{cnames}{[\code{character}]\cr -Component names for vector params (except discrete). -Every function in this package that creates vector values for such a param, will name -that vector with \code{cnames}.} } \value{ [\code{\link{LearnerParam}}]. From b98b9277192741e2f4ef4b4c46f44ab902adc0bc Mon Sep 17 00:00:00 2001 From: Jakob Richter Date: Mon, 26 Jun 2017 11:39:49 +0200 Subject: [PATCH 06/11] Use isTRUE instead of is.na && ... --- R/convertDiscrete.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/convertDiscrete.R b/R/convertDiscrete.R index dd66e426..42d1fa0a 100644 --- a/R/convertDiscrete.R +++ b/R/convertDiscrete.R @@ -53,7 +53,7 @@ discreteValueToName = function(par, x) { return(NA_character_) assertClass(par, "Param") assertChoice(par$type, c("discrete", "discretevector")) - if (par$type == "discretevector" && !is.na(par$len) && length(x) != par$len) + if (par$type == "discretevector" && isTRUE(length(x) != par$len)) stopf("Length of x must be %i!", par$len) ns = names(par$values) getIndex = function(values, v) { From 3b26d9b51eb4ac12d30a8f6f41c62d175504b731 Mon Sep 17 00:00:00 2001 From: Jakob Richter Date: Mon, 26 Jun 2017 11:52:01 +0200 Subject: [PATCH 07/11] Use vcapply instead of sapply --- R/convertDiscrete.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/convertDiscrete.R b/R/convertDiscrete.R index 42d1fa0a..73fe3807 100644 --- a/R/convertDiscrete.R +++ b/R/convertDiscrete.R @@ -65,6 +65,6 @@ discreteValueToName = function(par, x) { if (par$type == "discrete") { ns[getIndex(par$values, x)] } else if (par$type == "discretevector") { - sapply(x, function(x) ns[getIndex(x, values = par$values)]) + vcapply(x, function(app x) ns[getIndex(x, values = par$values)]) } } From c19ca1fd08b7cf3a16431e11ceb9fa23514bdb1e Mon Sep 17 00:00:00 2001 From: Jakob Richter Date: Mon, 26 Jun 2017 11:59:57 +0200 Subject: [PATCH 08/11] typo --- R/convertDiscrete.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/convertDiscrete.R b/R/convertDiscrete.R index 73fe3807..c8bccd40 100644 --- a/R/convertDiscrete.R +++ b/R/convertDiscrete.R @@ -65,6 +65,6 @@ discreteValueToName = function(par, x) { if (par$type == "discrete") { ns[getIndex(par$values, x)] } else if (par$type == "discretevector") { - vcapply(x, function(app x) ns[getIndex(x, values = par$values)]) + vcapply(x, function(x) ns[getIndex(x, values = par$values)]) } } From e94c9eed3d0ed3a5d2722505ded2732cbac195e1 Mon Sep 17 00:00:00 2001 From: Jakob Richter Date: Mon, 26 Jun 2017 12:18:54 +0200 Subject: [PATCH 09/11] add tests, fix name to makeCharacterVectorLearnerParam --- R/makeLearnerParamFuns.R | 2 +- tests/testthat/test_LearnerParam.R | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/R/makeLearnerParamFuns.R b/R/makeLearnerParamFuns.R index e7e7b252..ed216081 100644 --- a/R/makeLearnerParamFuns.R +++ b/R/makeLearnerParamFuns.R @@ -93,7 +93,7 @@ makeCharacterLearnerParam = function(id, default, when = "train", requires = NUL #' @rdname LearnerParam #' @export -makeCharacterLearnerVectorParam = function(id, len = as.integer(NA), default, when = "train", +makeCharacterVectorLearnerParam = function(id, len = as.integer(NA), default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) { makeParam(id = id, type = "charactervector", learner.param = TRUE, len = len, diff --git a/tests/testthat/test_LearnerParam.R b/tests/testthat/test_LearnerParam.R index 3ad450e2..a5299117 100644 --- a/tests/testthat/test_LearnerParam.R +++ b/tests/testthat/test_LearnerParam.R @@ -70,6 +70,31 @@ test_that("disc vec", { p = makeDiscreteVectorLearnerParam(id = "x", len = NA_integer_, values = list("a", "b"), default = list("a", "b", "a")) }) +test_that("function", { + p = makeFunctionLearnerParam("f1") + expect_equal(p$id, "x") + expect_true(!isFeasible(p, "a")) + expect_true(isFeasible(p, identity)) + expect_true(isFeasible(p, function(x) x^2)) + # defaults + p = makeFunctionLearnerParam(id = "f2", default = sin) + expect_equal(getDefaults(p), sin) +}) + +test_that("character", { + p = makeCharacterLearnerParam("c") + expect_equal(p$id, "c") + expect_true(isFeasible(p, "a")) + expect_true(!isFeasible(p, "ab")) +}) + +test_that("character vec", { + p = makeCharacterVectorLearnerParam("cv") + expect_equal(p$id, "cv") + expect_true(isFeasible(p, "a")) + expect_true(isFeasible(p, c("a","b"))) + expect_true(!isFeasible(p, "ab")) +}) test_that("untyped", { p = makeUntypedLearnerParam("x") From a3d2c6a291cc51c040413f2077f585d9a4dda85a Mon Sep 17 00:00:00 2001 From: Jakob Richter Date: Mon, 26 Jun 2017 12:53:48 +0200 Subject: [PATCH 10/11] fix test, NAMESPACE --- NAMESPACE | 2 +- man/LearnerParam.Rd | 4 ++-- tests/testthat/test_LearnerParam.R | 11 ++++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 7ae2b85c..6c7a1f9c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -137,8 +137,8 @@ export(isSpecialValue) export(isVector) export(isVectorTypeString) export(makeCharacterLearnerParam) -export(makeCharacterLearnerVectorParam) export(makeCharacterParam) +export(makeCharacterVectorLearnerParam) export(makeCharacterVectorParam) export(makeDiscreteLearnerParam) export(makeDiscreteParam) diff --git a/man/LearnerParam.Rd b/man/LearnerParam.Rd index 42f356c8..709a15b6 100644 --- a/man/LearnerParam.Rd +++ b/man/LearnerParam.Rd @@ -13,7 +13,7 @@ \alias{makeUntypedLearnerParam} \alias{makeFunctionLearnerParam} \alias{makeCharacterLearnerParam} -\alias{makeCharacterLearnerVectorParam} +\alias{makeCharacterVectorLearnerParam} \title{Create a description object for a parameter of a machine learning algorithm.} \usage{ makeNumericLearnerParam(id, lower = -Inf, upper = Inf, allow.inf = FALSE, @@ -55,7 +55,7 @@ makeFunctionLearnerParam(id, default, when = "train", requires = NULL, makeCharacterLearnerParam(id, default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) -makeCharacterLearnerVectorParam(id, len = as.integer(NA), default, +makeCharacterVectorLearnerParam(id, len = as.integer(NA), default, when = "train", requires = NULL, tunable = TRUE, special.vals = list()) } diff --git a/tests/testthat/test_LearnerParam.R b/tests/testthat/test_LearnerParam.R index a5299117..e4186272 100644 --- a/tests/testthat/test_LearnerParam.R +++ b/tests/testthat/test_LearnerParam.R @@ -72,7 +72,7 @@ test_that("disc vec", { test_that("function", { p = makeFunctionLearnerParam("f1") - expect_equal(p$id, "x") + expect_equal(p$id, "f1") expect_true(!isFeasible(p, "a")) expect_true(isFeasible(p, identity)) expect_true(isFeasible(p, function(x) x^2)) @@ -84,16 +84,17 @@ test_that("function", { test_that("character", { p = makeCharacterLearnerParam("c") expect_equal(p$id, "c") - expect_true(isFeasible(p, "a")) - expect_true(!isFeasible(p, "ab")) + expect_true(isFeasible(p, "ab")) + expect_true(!isFeasible(p, c("a","b"))) + expect_true(!isFeasible(p, 1)) }) test_that("character vec", { p = makeCharacterVectorLearnerParam("cv") expect_equal(p$id, "cv") expect_true(isFeasible(p, "a")) - expect_true(isFeasible(p, c("a","b"))) - expect_true(!isFeasible(p, "ab")) + expect_true(isFeasible(p, c("a","b","a"))) + expect_true(!isFeasible(p, 1:3)) }) test_that("untyped", { From 6e755905bab322c0cdd881143a0ead1859c67cc9 Mon Sep 17 00:00:00 2001 From: mb706 Date: Mon, 24 Jul 2017 22:32:55 -0400 Subject: [PATCH 11/11] Print 'list()' as default value for empty list --- R/paramValueToString.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/paramValueToString.R b/R/paramValueToString.R index 4e67c919..ea755c0c 100644 --- a/R/paramValueToString.R +++ b/R/paramValueToString.R @@ -53,8 +53,12 @@ paramValueToString.Param = function(par, x, show.missing.values = FALSE, num.for else return("") } - if (isDiscrete(par, include.logical = FALSE)) + if (isDiscrete(par, include.logical = FALSE)) { x = discreteValueToName(par, x) + if (length(x) == 0) { + x = "list()" + } + } s = convertToShortString(x, num.format = num.format) }