Skip to content

Commit

Permalink
Merge pull request #752 from mlr-org/s3params_compat
Browse files Browse the repository at this point in the history
check with new paradox
  • Loading branch information
mb706 authored Feb 28, 2024
2 parents 044762e + 9c60738 commit ca8123e
Show file tree
Hide file tree
Showing 73 changed files with 491 additions and 398 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/dev-cmd-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# dev cmd check workflow of the mlr3 ecosystem v0.1.0
# https://github.com/mlr-org/actions
on:
workflow_dispatch:
push:
branches:
- master
pull_request:
branches:
- master

name: dev-check

jobs:
check-package:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.dev-package }}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

strategy:
fail-fast: false
matrix:
config:
- {os: ubuntu-latest, r: 'release', dev-package: "mlr-org/bbotk', 'mlr-org/mlr3learners', 'mlr-org/paradox"}

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- name: Install dev versions
run: pak::pkg_install(c('${{ matrix.config.dev-package }}'))
shell: Rscript {0}

- uses: r-lib/actions/check-r-package@v2
29 changes: 14 additions & 15 deletions R/LearnerAvg.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@
LearnerClassifAvg = R6Class("LearnerClassifAvg", inherit = LearnerClassif,
public = list(
initialize = function(id = "classif.avg") {
ps = ParamSet$new(params = list(
ParamUty$new("measure", custom_check = check_class_or_character("MeasureClassif", mlr_measures), tags = "train"),
ParamUty$new("optimizer", custom_check = check_optimizer, tags = "train"),
ParamUty$new("log_level", tags = "train",
ps = ps(
measure = p_uty(custom_check = check_class_or_character("MeasureClassif", mlr_measures), tags = "train"),
optimizer = p_uty(custom_check = check_optimizer, tags = "train"),
log_level = p_uty(tags = "train",
function(x) check_string(x) %check||% check_integerish(x))
))
)
ps$values = list(measure = "classif.ce", optimizer = "nloptr", log_level = "warn")
super$initialize(
id = id,
Expand Down Expand Up @@ -132,12 +132,12 @@ LearnerClassifAvg = R6Class("LearnerClassifAvg", inherit = LearnerClassif,
LearnerRegrAvg = R6Class("LearnerRegrAvg", inherit = LearnerRegr,
public = list(
initialize = function(id = "regr.avg") {
ps = ParamSet$new(params = list(
ParamUty$new("measure", custom_check = check_class_or_character("MeasureRegr", mlr_measures), tags = "train"),
ParamUty$new("optimizer", custom_check = check_optimizer, tags = "train"),
ParamUty$new("log_level", tags = "train",
ps = ps(
measure = p_uty(custom_check = check_class_or_character("MeasureRegr", mlr_measures), tags = "train"),
optimizer = p_uty(custom_check = check_optimizer, tags = "train"),
log_level = p_uty(tags = "train",
function(x) check_string(x) %check||% check_integerish(x))
))
)
ps$values = list(measure = "regr.mse", optimizer = "nloptr", log_level = "warn")
super$initialize(
id = id,
Expand Down Expand Up @@ -185,10 +185,9 @@ optimize_weights_learneravg = function(self, task, n_weights, data) {
}

pars = self$param_set$get_values(tags = "train")
ps = ParamSet$new(params = imap(data, function(x, n) {
if (is.numeric(n)) n = paste0("w.", n)
ParamDbl$new(id = n, lower = 0, upper = 1)
}))
pl = rep(list(p_dbl(0, 1)), length(data))
names(pl) = names(data) %??% paste0("w.", seq_along(data))
ps = do.call(ps, pl)
optimizer = pars$optimizer
if (inherits(optimizer, "character")) {
optimizer = bbotk::opt(optimizer)
Expand All @@ -198,7 +197,7 @@ optimize_weights_learneravg = function(self, task, n_weights, data) {
}
measure = pars$measure
if (is.character(measure)) measure = msr(measure)
codomain = ParamSet$new(list(ParamDbl$new(id = measure$id, tags = ifelse(measure$minimize, "minimize", "maximize"))))
codomain = do.call(paradox::ps, structure(list(p_dbl(tags = ifelse(measure$minimize, "minimize", "maximize"))), names = measure$id))
objfun = bbotk::ObjectiveRFun$new(
fun = function(xs) learneravg_objfun(xs, task = task, measure = measure, avg_weight_fun = self$weighted_average_prediction, data = data),
domain = ps, codomain = codomain
Expand Down
8 changes: 4 additions & 4 deletions R/PipeOp.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#'
#' @section Construction:
#' ```
#' PipeOp$new(id, param_set = ParamSet$new(), param_vals = list(), input, output, packages = character(0), tags = character(0))
#' PipeOp$new(id, param_set = ps(), param_vals = list(), input, output, packages = character(0), tags = character(0))
#' ```
#'
#' * `id` :: `character(1)`\cr
Expand Down Expand Up @@ -236,7 +236,7 @@ PipeOp = R6Class("PipeOp",
.result = NULL,
tags = NULL,

initialize = function(id, param_set = ParamSet$new(), param_vals = list(), input, output, packages = character(0), tags = "abstract") {
initialize = function(id, param_set = ps(), param_vals = list(), input, output, packages = character(0), tags = "abstract") {
if (inherits(param_set, "ParamSet")) {
private$.param_set = assert_param_set(param_set)
private$.param_set_source = NULL
Expand Down Expand Up @@ -338,7 +338,7 @@ PipeOp = R6Class("PipeOp",
id = function(val) {
if (!missing(val)) {
private$.id = val
if (!is.null(private$.param_set)) {
if (paradox_info$is_old && !is.null(private$.param_set)) {
# private$.param_set may be NULL if it is constructed dynamically by active binding
private$.param_set$set_id = val
}
Expand All @@ -353,7 +353,7 @@ PipeOp = R6Class("PipeOp",
} else {
private$.param_set = sourcelist[[1]]
}
if (!is.null(self$id)) {
if (paradox_info$is_old && !is.null(self$id)) {
private$.param_set$set_id = self$id
}
}
Expand Down
12 changes: 6 additions & 6 deletions R/PipeOpBoxCox.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ PipeOpBoxCox = R6Class("PipeOpBoxCox",
inherit = PipeOpTaskPreproc,
public = list(
initialize = function(id = "boxcox", param_vals = list()) {
ps = ParamSet$new(params = list(
ParamLgl$new("standardize", default = TRUE, tags = c("train", "boxcox")),
ParamDbl$new("eps", default = 0.001, lower = 0, tags = c("train", "boxcox")),
ParamDbl$new("lower", tags = c("train", "boxcox")),
ParamDbl$new("upper", tags = c("train", "boxcox"))
))
ps = ps(
standardize = p_lgl(default = TRUE, tags = c("train", "boxcox")),
eps = p_dbl(default = 0.001, lower = 0, tags = c("train", "boxcox")),
lower = p_dbl(tags = c("train", "boxcox")),
upper = p_dbl(tags = c("train", "boxcox"))
)
super$initialize(id, param_set = ps, param_vals = param_vals,
packages = "bestNormalize", feature_types = c("numeric", "integer"))
}
Expand Down
10 changes: 5 additions & 5 deletions R/PipeOpBranch.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#' * `options` :: `numeric(1)` | `character`\cr
#' If `options` is an integer number, it determines the number of
#' output channels / options that are created, named `output1`...`output<n>`. The
#' `$selection` parameter will then be a [`ParamInt`].
#' `$selection` parameter will then be an integer.
#' If `options` is a `character`, it determines the names of channels directly.
#' The `$selection` parameter will then be a [`ParamFct`].
#' The `$selection` parameter will then be factorial.
#' * `id` :: `character(1)`\cr
#' Identifier of resulting object, default `"branch"`.
#' * `param_vals` :: named `list`\cr
Expand Down Expand Up @@ -90,14 +90,14 @@ PipeOpBranch = R6Class("PipeOpBranch",
)
if (is.numeric(options)) {
options = round(options)
param = ParamInt$new("selection", lower = 1L, upper = options, tags = c("train", "predict", "required"))
param = p_int(lower = 1L, upper = options, tags = c("train", "predict", "required"))
options = rep_suffix("output", options)
initval = 1
} else {
param = ParamFct$new("selection", levels = options, tags = c("train", "predict", "required"))
param = p_fct(options, tags = c("train", "predict", "required"))
initval = options[1]
}
ps = ParamSet$new(params = list(param))
ps = ps(selection = param)
ps$values$selection = initval
super$initialize(id, ps, param_vals,
input = data.table(name = "input", train = "*", predict = "*"),
Expand Down
6 changes: 3 additions & 3 deletions R/PipeOpChunk.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ PipeOpChunk = R6Class("PipeOpChunk",
public = list(
initialize = function(outnum, id = "chunk", param_vals = list()) {
outnum = assert_int(outnum, lower = 1L)
ps = ParamSet$new(params = list(
ParamLgl$new("shuffle", tags = "train")
))
ps = ps(
shuffle = p_lgl(tags = "train")
)
ps$values = list(shuffle = TRUE)
super$initialize(id,
param_set = ps, param_vals = param_vals,
Expand Down
14 changes: 6 additions & 8 deletions R/PipeOpClassBalancing.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ PipeOpClassBalancing = R6Class("PipeOpClassBalancing",

public = list(
initialize = function(id = "classbalancing", param_vals = list()) {
ps = ParamSet$new(params = list(
ParamDbl$new("ratio", lower = 0, upper = Inf, tags = "train"),
ParamFct$new("reference",
levels = c("all", "major", "minor", "nonmajor", "nonminor", "one"), tags = "train"),
ParamFct$new("adjust",
levels = c("all", "major", "minor", "nonmajor", "nonminor", "upsample", "downsample"), tags = "train"),
ParamLgl$new("shuffle", tags = "train")
))
ps = ps(
ratio = p_dbl(lower = 0, upper = Inf, tags = "train"),
reference = p_fct(c("all", "major", "minor", "nonmajor", "nonminor", "one"), tags = "train"),
adjust = p_fct(c("all", "major", "minor", "nonmajor", "nonminor", "upsample", "downsample"), tags = "train"),
shuffle = p_lgl(tags = "train")
)
ps$values = list(ratio = 1, reference = "all", adjust = "all", shuffle = TRUE)
super$initialize(id, param_set = ps, param_vals = param_vals, can_subset_cols = FALSE, task_type = "TaskClassif", tags = "imbalanced data")
}
Expand Down
6 changes: 3 additions & 3 deletions R/PipeOpClassWeights.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ PipeOpClassWeights = R6Class("PipeOpClassWeights",

public = list(
initialize = function(id = "classweights", param_vals = list()) {
ps = ParamSet$new(params = list(
ParamDbl$new("minor_weight", lower = 0, upper = Inf, tags = "train")
))
ps = ps(
minor_weight = p_dbl(lower = 0, upper = Inf, tags = "train")
)
ps$values = list(minor_weight = 1)
super$initialize(id, param_set = ps, param_vals = param_vals, can_subset_cols = FALSE, task_type = "TaskClassif", tags = "imbalanced data")
}
Expand Down
6 changes: 3 additions & 3 deletions R/PipeOpColApply.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ PipeOpColApply = R6Class("PipeOpColApply",
inherit = PipeOpTaskPreprocSimple,
public = list(
initialize = function(id = "colapply", param_vals = list()) {
ps = ParamSet$new(params = list(
ParamUty$new("applicator", custom_check = check_function, tags = c("train", "predict"))
))
ps = ps(
applicator = p_uty(custom_check = check_function, tags = c("train", "predict"))
)
ps$values = list(applicator = identity)
super$initialize(id, ps, param_vals = param_vals)
}
Expand Down
6 changes: 3 additions & 3 deletions R/PipeOpColRoles.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ PipeOpColRoles = R6Class("PipeOpColRoles",
inherit = PipeOpTaskPreprocSimple,
public = list(
initialize = function(id = "colroles", param_vals = list()) {
ps = ParamSet$new(params = list(
ps = ps(
# named list, each entry with a vector of roles
ParamUty$new("new_role", tags = c("train", "predict"), custom_check = function(x) {
new_role = p_uty(tags = c("train", "predict"), custom_check = function(x) {
first_check = check_list(x, types = "character", any.missing = FALSE, min.len = 1L, names = "named")
# return the error directly if this failed
if (is.character(first_check)) {
Expand All @@ -69,7 +69,7 @@ PipeOpColRoles = R6Class("PipeOpColRoles",
all_col_roles = unique(unlist(mlr3::mlr_reflections$task_col_roles))
check_subset(unlist(x), all_col_roles[all_col_roles != "target"])
})
))
)
super$initialize(id, param_set = ps, param_vals = param_vals, can_subset_cols = FALSE)
}
),
Expand Down
8 changes: 4 additions & 4 deletions R/PipeOpCollapseFactors.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ PipeOpCollapseFactors = R6Class("PipeOpCollapseFactors",
inherit = PipeOpTaskPreprocSimple,
public = list(
initialize = function(id = "collapsefactors", param_vals = list()) {
ps = ParamSet$new(params = list(
ParamDbl$new("no_collapse_above_prevalence", 0, 1, tags = c("train", "predict")),
ParamInt$new("target_level_count", 2, tags = c("train", "predict"))
))
ps = ps(
no_collapse_above_prevalence = p_dbl(0, 1, tags = c("train", "predict")),
target_level_count = p_int(2, tags = c("train", "predict"))
)
ps$values = list(no_collapse_above_prevalence = 1, target_level_count = 2)
super$initialize(id, param_set = ps, param_vals = param_vals, feature_types = c("factor", "ordered"))
}
Expand Down
28 changes: 14 additions & 14 deletions R/PipeOpDateFeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,20 @@ PipeOpDateFeatures = R6Class("PipeOpDateFeatures",
inherit = PipeOpTaskPreprocSimple,
public = list(
initialize = function(id = "datefeatures", param_vals = list()) {
ps = ParamSet$new(params = list(
ParamLgl$new("keep_date_var", tags = c("train", "predict", "required")),
ParamLgl$new("cyclic", tags = c("train", "predict", "required")),
ParamLgl$new("year", tags = c("train", "predict", "datepart", "required")),
ParamLgl$new("month", tags = c("train", "predict", "datepart", "required")),
ParamLgl$new("week_of_year", tags = c("train", "predict", "datepart", "required")),
ParamLgl$new("day_of_year", tags = c("train", "predict", "datepart", "required")),
ParamLgl$new("day_of_month", tags = c("train", "predict", "datepart", "required")),
ParamLgl$new("day_of_week", tags = c("train", "predict", "datepart", "required")),
ParamLgl$new("hour", tags = c("train", "predict", "datepart", "required")),
ParamLgl$new("minute", tags = c("train", "predict", "datepart", "required")),
ParamLgl$new("second", tags = c("train", "predict", "datepart", "required")),
ParamLgl$new("is_day", tags = c("train", "predict", "datepart", "required"))
))
ps = ps(
keep_date_var = p_lgl(tags = c("train", "predict", "required")),
cyclic = p_lgl(tags = c("train", "predict", "required")),
year = p_lgl(tags = c("train", "predict", "datepart", "required")),
month = p_lgl(tags = c("train", "predict", "datepart", "required")),
week_of_year = p_lgl(tags = c("train", "predict", "datepart", "required")),
day_of_year = p_lgl(tags = c("train", "predict", "datepart", "required")),
day_of_month = p_lgl(tags = c("train", "predict", "datepart", "required")),
day_of_week = p_lgl(tags = c("train", "predict", "datepart", "required")),
hour = p_lgl(tags = c("train", "predict", "datepart", "required")),
minute = p_lgl(tags = c("train", "predict", "datepart", "required")),
second = p_lgl(tags = c("train", "predict", "datepart", "required")),
is_day = p_lgl(tags = c("train", "predict", "datepart", "required"))
)
ps$values = list(keep_date_var = FALSE, cyclic = FALSE, year = TRUE,
month = TRUE, week_of_year = TRUE, day_of_year = TRUE, day_of_month = TRUE,
day_of_week = TRUE, hour = TRUE, minute = TRUE, second = TRUE, is_day = TRUE)
Expand Down
6 changes: 3 additions & 3 deletions R/PipeOpEncode.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ PipeOpEncode = R6Class("PipeOpEncode",
inherit = PipeOpTaskPreprocSimple,
public = list(
initialize = function(id = "encode", param_vals = list()) {
ps = ParamSet$new(params = list(
ParamFct$new("method", levels = c("one-hot", "treatment", "helmert", "poly", "sum"), tags = c("train", "predict"))
))
ps = ps(
method = p_fct(levels = c("one-hot", "treatment", "helmert", "poly", "sum"), tags = c("train", "predict"))
)
ps$values = list(method = "one-hot")
super$initialize(id, param_set = ps, param_vals = param_vals, packages = "stats", tags = "encode", feature_types = c("factor", "ordered"))
}
Expand Down
8 changes: 4 additions & 4 deletions R/PipeOpEncodeImpact.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ PipeOpEncodeImpact = R6Class("PipeOpEncodeImpact",
inherit = PipeOpTaskPreprocSimple,
public = list(
initialize = function(id = "encodeimpact", param_vals = list()) {
ps = ParamSet$new(params = list(
ParamDbl$new("smoothing", 0, Inf, tags = c("train", "required")),
ParamLgl$new("impute_zero", tags = c("train", "required"))
))
ps = ps(
smoothing = p_dbl(0, Inf, tags = c("train", "required")),
impute_zero = p_lgl(tags = c("train", "required"))
)
ps$values = list(smoothing = 1e-4, impute_zero = FALSE)
super$initialize(id, param_set = ps, param_vals = param_vals, tags = "encode", feature_types = c("factor", "ordered"))
}
Expand Down
6 changes: 3 additions & 3 deletions R/PipeOpEncodeLmer.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ PipeOpEncodeLmer = R6Class("PipeOpEncodeLmer",
inherit = PipeOpTaskPreprocSimple,
public = list(
initialize = function(id = "encodelmer", param_vals = list()) {
ps = ParamSet$new(params = list(
ParamLgl$new("fast_optim", tags = c("train", "required"))
))
ps = ps(
fast_optim = p_lgl(tags = c("train", "required"))
)
ps$values = list(fast_optim = TRUE)
super$initialize(id, param_set = ps, param_vals = param_vals, packages = c("lme4", "nloptr"), tags = "encode", feature_types = c("factor", "ordered"))
}
Expand Down
11 changes: 8 additions & 3 deletions R/PipeOpEnsemble.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' @section Construction:
#' Note: This object is typically constructed via a derived class, e.g. [`PipeOpClassifAvg`] or [`PipeOpRegrAvg`].
#' ```
#' PipeOpEnsemble$new(innum = 0, collect_multiplicity = FALSE, id, param_set = ParamSet$new(), param_vals = list(), packages = character(0), prediction_type = "Prediction")
#' PipeOpEnsemble$new(innum = 0, collect_multiplicity = FALSE, id, param_set = ps(), param_vals = list(), packages = character(0), prediction_type = "Prediction")
#' ```
#'
#' * `innum` :: `numeric(1)`\cr
Expand Down Expand Up @@ -82,9 +82,14 @@
PipeOpEnsemble = R6Class("PipeOpEnsemble",
inherit = PipeOp,
public = list(
initialize = function(innum = 0, collect_multiplicity = FALSE, id, param_set = ParamSet$new(), param_vals = list(), packages = character(0), prediction_type = "Prediction", tags = NULL) {
initialize = function(innum = 0, collect_multiplicity = FALSE, id, param_set = ps(), param_vals = list(), packages = character(0), prediction_type = "Prediction", tags = NULL) {
assert_integerish(innum, lower = 0)
param_set$add(ParamUty$new("weights", custom_check = check_weights(innum), tags = "predict"))
if (paradox_info$is_old) {
paux = ps(weights = p_uty(check_weights(innum), tags = "predict"))
param_set$add(paux$params$weights)
} else {
param_set = c(param_set, ps(weights = p_uty(check_weights(innum), tags = "predict")))
}
param_set$values$weights = 1
inname = if (innum) rep_suffix("input", innum) else "..."
intype = c("NULL", prediction_type)
Expand Down
Loading

0 comments on commit ca8123e

Please sign in to comment.