diff --git a/.github/workflows/dev-cmd-check.yml b/.github/workflows/dev-cmd-check.yml index 89638dd04..a95932ab5 100644 --- a/.github/workflows/dev-cmd-check.yml +++ b/.github/workflows/dev-cmd-check.yml @@ -31,7 +31,6 @@ jobs: fail-fast: false matrix: config: - - {os: ubuntu-latest, r: 'release', dev-package: 'mlr-org/mlr3misc'} - {os: ubuntu-latest, r: 'release', dev-package: 'mlr-org/paradox'} steps: diff --git a/.lintr b/.lintr index 443133305..26acc0eaf 100644 --- a/.lintr +++ b/.lintr @@ -3,7 +3,6 @@ linters: linters_with_defaults( # the following setup changes/removes certain linters assignment_linter = NULL, # do not force using <- for assignments object_name_linter = object_name_linter(c("snake_case", "CamelCase")), # only allow snake case and camel case object names - cyclocomp_linter = NULL, # do not check function complexity commented_code_linter = NULL, # allow code in comments line_length_linter = line_length_linter(180L), indentation_linter(indent = 2L, hanging_indent_style = "never") diff --git a/DESCRIPTION b/DESCRIPTION index f5b6c9196..6704a1f60 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -74,7 +74,8 @@ Suggests: rpart, testthat (>= 3.2.0) Remotes: - mlr-org/mlr3misc + mlr-org/mlr3misc@common_baseclass, + mlr-org/mlr3data@common_baseclass Encoding: UTF-8 Config/testthat/edition: 3 Config/testthat/parallel: false diff --git a/R/Learner.R b/R/Learner.R index 2db4358ff..06f57bc48 100644 --- a/R/Learner.R +++ b/R/Learner.R @@ -162,13 +162,8 @@ #' @template seealso_learner #' @export Learner = R6Class("Learner", + inherit = Mlr3Component, public = list( - #' @template field_id - id = NULL, - - #' @template field_label - label = NA_character_, - #' @field state (`NULL` | named `list()`)\cr #' Current (internal) state of the learner. #' Contains all information gathered during `train()` and `predict()`. @@ -184,14 +179,6 @@ Learner = R6Class("Learner", #' A complete list of candidate feature types, grouped by task type, is stored in [`mlr_reflections$task_feature_types`][mlr_reflections]. feature_types = NULL, - #' @field properties (`character()`)\cr - #' Stores a set of properties/capabilities the learner has. - #' A complete list of candidate properties, grouped by task type, is stored in [`mlr_reflections$learner_properties`][mlr_reflections]. - properties = NULL, - - #' @template field_packages - packages = NULL, - #' @template field_predict_sets predict_sets = "test", @@ -216,42 +203,35 @@ Learner = R6Class("Learner", #' \url{https://mlr3book.mlr-org.com/chapters/chapter10/advanced_technical_aspects_of_mlr3.html#sec-error-handling} timeout = c(train = Inf, predict = Inf), - #' @template field_man - man = NULL, - #' @description #' Creates a new instance of this [R6][R6::R6Class] class. #' #' Note that this object is typically constructed via a derived classes, e.g. [LearnerClassif] or [LearnerRegr]. - initialize = function(id, task_type, param_set = ps(), predict_types = character(), feature_types = character(), - properties = character(), packages = character(), label = NA_character_, man = NA_character_) { + initialize = function(id, task_type, param_set = ps(), predict_types = character(0), feature_types = character(0), + properties = character(0), packages = character(0), additional_configuration = character(0), label, man) { + + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Learner construction and will be removed in the future.") + } + + super$initialize(dict_entry = id, dict_shortaccess = "lrn", + param_set = param_set, packages = packages, properties = properties, + additional_configuration = c("predict_sets", "parallel_predict", "timeout", "use_weights", "predict_type", "selected_features_impute", + if ("validate" %in% names(self)) "validate", additional_configuration) + ) - self$id = assert_string(id, min.chars = 1L) - self$label = assert_string(label, na.ok = TRUE) self$task_type = assert_choice(task_type, mlr_reflections$task_types$type) self$feature_types = assert_ordered_set(feature_types, mlr_reflections$task_feature_types, .var.name = "feature_types") private$.predict_types = assert_ordered_set(predict_types, names(mlr_reflections$learner_predict_types[[task_type]]), empty.ok = FALSE, .var.name = "predict_types") private$.predict_type = predict_types[1L] - self$properties = sort(assert_subset(properties, mlr_reflections$learner_properties[[task_type]])) - self$packages = union("mlr3", assert_character(packages, any.missing = FALSE, min.chars = 1L)) - self$man = assert_string(man, na.ok = TRUE) + assert_subset(properties, mlr_reflections$learner_properties[[task_type]]) if ("weights" %in% self$properties) { self$use_weights = "use" } else { self$use_weights = "error" } - private$.param_set = param_set - - check_packages_installed(packages, msg = sprintf("Package '%%s' required but not installed for Learner '%s'", id)) - }, - - #' @description - #' Helper for print outputs. - #' @param ... (ignored). - format = function(...) { - sprintf("<%s:%s>", class(self)[1L], self$id) }, #' @description @@ -295,12 +275,6 @@ Learner = R6Class("Learner", } }, - #' @description - #' Opens the corresponding help page referenced by field `$man`. - help = function() { - open_help(self$man) - }, - #' @description #' Train the learner on a set of observations of the provided `task`. #' Mutates the learner by reference, i.e. stores the model alongside other information in field `$state`. @@ -623,54 +597,6 @@ Learner = R6Class("Learner", return(invisible(self)) }, - #' @description - #' Sets parameter values and fields of the learner. - #' All arguments whose names match the name of a parameter of the [paradox::ParamSet] are set as parameters. - #' All remaining arguments are assumed to be regular fields. - #' - #' @param ... (named `any`)\cr - #' Named arguments to set parameter values and fields. - #' @param .values (named `any`)\cr - #' Named list of parameter values and fields. - #' @examples - #' learner = lrn("classif.rpart") - #' learner$configure(minsplit = 3, parallel_predict = FALSE) - #' learner$configure(.values = list(cp = 0.005)) - configure = function(..., .values = list()) { - dots = list(...) - assert_list(dots, names = "unique") - assert_list(.values, names = "unique") - assert_disjunct(names(dots), names(.values)) - new_values = insert_named(dots, .values) - - # set params in ParamSet - if (length(new_values)) { - param_ids = self$param_set$ids() - ii = names(new_values) %in% param_ids - if (any(ii)) { - self$param_set$values = insert_named(self$param_set$values, new_values[ii]) - new_values = new_values[!ii] - } - } else { - param_ids = character() - } - - # remaining args go into fields - if (length(new_values)) { - ndots = names(new_values) - for (i in seq_along(new_values)) { - nn = ndots[[i]] - if (!exists(nn, envir = self, inherits = FALSE)) { - stopf("Cannot set argument '%s' for '%s' (not a parameter, not a field).%s", - nn, class(self)[1L], did_you_mean(nn, c(param_ids, setdiff(names(self), ".__enclos_env__")))) # nolint - } - self[[nn]] = new_values[[i]] - } - } - - return(invisible(self)) - }, - #' @description #' Returns the features selected by the model. #' The field `selected_features_impute` controls the behavior if the learner does not support feature selection. @@ -757,23 +683,6 @@ Learner = R6Class("Learner", get_log_condition(self$state, "error") }, - #' @field hash (`character(1)`)\cr - #' Hash (unique identifier) for this object. - #' The hash is calculated based on the learner id, the parameter settings, the predict type, the fallback hash, the parallel predict setting, the validate setting, and the predict sets. - hash = function(rhs) { - assert_ro_binding(rhs) - calculate_hash(class(self), self$id, self$param_set$values, private$.predict_type, - self$fallback$hash, self$parallel_predict, get0("validate", self), self$predict_sets, private$.use_weights) - }, - - #' @field phash (`character(1)`)\cr - #' Hash (unique identifier) for this partial object, excluding some components which are varied systematically during tuning (parameter values). - phash = function(rhs) { - assert_ro_binding(rhs) - calculate_hash(class(self), self$id, private$.predict_type, - self$fallback$hash, self$parallel_predict, get0("validate", self), private$.use_weights) - }, - #' @field predict_type (`character(1)`)\cr #' Stores the currently active predict type, e.g. `"response"`. #' Must be an element of `$predict_types`. @@ -791,14 +700,6 @@ Learner = R6Class("Learner", private$.predict_type = rhs }, - #' @template field_param_set - param_set = function(rhs) { - if (!missing(rhs) && !identical(rhs, private$.param_set)) { - stopf("param_set is read-only.") - } - private$.param_set - }, - #' @field fallback ([Learner])\cr #' Returns the fallback learner set with `$encapsulate()`. fallback = function(rhs) { @@ -851,7 +752,6 @@ Learner = R6Class("Learner", .fallback = NULL, .predict_type = NULL, .predict_types = NULL, - .param_set = NULL, .hotstart_stack = NULL, .selected_features_impute = "error", @@ -871,10 +771,12 @@ Learner = R6Class("Learner", } }, + .additional_phash_input = function() { + list(private$.predict_type, self$fallback$hash, self$parallel_predict, get0("validate", self), self$predict_sets, private$.use_weights, private$.when) + }, + deep_clone = function(name, value) { switch(name, - .param_set = value$clone(deep = TRUE), - .fallback = if (is.null(value)) NULL else value$clone(deep = TRUE), state = { if (!is.null(value$train_task)) { value$train_task = value$train_task$clone(deep = TRUE) @@ -882,7 +784,7 @@ Learner = R6Class("Learner", value$log = copy(value$log) value }, - value + super$deep_clone(name, value) ) } ) diff --git a/R/LearnerClassif.R b/R/LearnerClassif.R index 2a37eeb2b..3b12944fd 100644 --- a/R/LearnerClassif.R +++ b/R/LearnerClassif.R @@ -48,10 +48,16 @@ LearnerClassif = R6Class("LearnerClassif", inherit = Learner, public = list( #' @description #' Creates a new instance of this [R6][R6::R6Class] class. - initialize = function(id, param_set = ps(), predict_types = "response", feature_types = character(), properties = character(), packages = character(), label = NA_character_, man = NA_character_) { + initialize = function(id, param_set = ps(), predict_types = "response", + feature_types = character(), properties = character(), packages = character(), + additional_configuration = character(0), label, man + ) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Learner construction and will be removed in the future.") + } + super$initialize(id = id, task_type = "classif", param_set = param_set, predict_types = predict_types, - feature_types = feature_types, properties = properties, packages = packages, - label = label, man = man) + feature_types = feature_types, properties = properties, packages = packages, additional_configuration = additional_configuration) if (getOption("mlr3.prob_as_default", FALSE) && "prob" %in% self$predict_types) { self$predict_type = "prob" diff --git a/R/LearnerClassifDebug.R b/R/LearnerClassifDebug.R index c91e37d49..f976d5eb3 100644 --- a/R/LearnerClassifDebug.R +++ b/R/LearnerClassifDebug.R @@ -88,9 +88,7 @@ LearnerClassifDebug = R6Class("LearnerClassifDebug", inherit = LearnerClassif, param_set = param_set, feature_types = c("logical", "integer", "numeric", "character", "factor", "ordered"), predict_types = c("response", "prob"), - properties = c("twoclass", "multiclass", "missings", "hotstart_forward", "validation", "internal_tuning", "marshal", "weights"), - man = "mlr3::mlr_learners_classif.debug", - label = "Debug Learner for Classification" + properties = c("twoclass", "multiclass", "missings", "hotstart_forward", "validation", "internal_tuning", "marshal", "weights") ) }, #' @description diff --git a/R/LearnerClassifFeatureless.R b/R/LearnerClassifFeatureless.R index 4395c367d..fff2c20ed 100644 --- a/R/LearnerClassifFeatureless.R +++ b/R/LearnerClassifFeatureless.R @@ -43,9 +43,7 @@ LearnerClassifFeatureless = R6Class("LearnerClassifFeatureless", inherit = Learn feature_types = mlr_reflections$task_feature_types, predict_types = c("response", "prob"), param_set = ps, - properties = c("featureless", "twoclass", "multiclass", "missings", "importance", "selected_features", "weights"), - label = "Featureless Classification Learner", - man = "mlr3::mlr_learners_classif.featureless", + properties = c("featureless", "twoclass", "multiclass", "missings", "importance", "selected_features", "weights") ) }, diff --git a/R/LearnerClassifRpart.R b/R/LearnerClassifRpart.R index 42bc13916..05d9d6c0b 100644 --- a/R/LearnerClassifRpart.R +++ b/R/LearnerClassifRpart.R @@ -44,9 +44,7 @@ LearnerClassifRpart = R6Class("LearnerClassifRpart", inherit = LearnerClassif, feature_types = c("logical", "integer", "numeric", "factor", "ordered"), predict_types = c("response", "prob"), param_set = ps, - properties = c("twoclass", "multiclass", "weights", "missings", "importance", "selected_features"), - label = "Classification Tree", - man = "mlr3::mlr_learners_classif.rpart" + properties = c("twoclass", "multiclass", "weights", "missings", "importance", "selected_features") ) }, diff --git a/R/LearnerRegr.R b/R/LearnerRegr.R index 651648c42..67c9ac0f4 100644 --- a/R/LearnerRegr.R +++ b/R/LearnerRegr.R @@ -12,9 +12,9 @@ #' - `"se"`: Predicts the standard error for each value of response for each observation in the test set. #' - `"distr"`: Probability distribution as `VectorDistribution` object (requires package `distr6`, available via #' repository \url{https://raphaels1.r-universe.dev}). -#' - `"quantiles"`: Predicts quantile estimates for each observation in the test set. -#' Set `$quantiles` to specify the quantiles to predict and `$quantile_response` to specify the response quantile. -#' See mlr3book [section](https://mlr3book.mlr-org.com/chapters/chapter13/beyond_regression_and_classification.html#sec-quantile-regression) on quantile regression for more details. +#' - `"quantiles"`: Predicts quantile estimates for each observation in the test set. +#' Set `$quantiles` to specify the quantiles to predict and `$quantile_response` to specify the response quantile. +#' See mlr3book [section](https://mlr3book.mlr-org.com/chapters/chapter13/beyond_regression_and_classification.html#sec-quantile-regression) on quantile regression for more details. #' #' Predefined learners can be found in the [dictionary][mlr3misc::Dictionary] [mlr_learners]. #' Essential regression learners can be found in this dictionary after loading \CRANpkg{mlr3learners}. @@ -44,10 +44,17 @@ LearnerRegr = R6Class("LearnerRegr", inherit = Learner, public = list( #' @description #' Creates a new instance of this [R6][R6::R6Class] class. - initialize = function(id, task_type = "regr", param_set = ps(), predict_types = "response", feature_types = character(), properties = character(), packages = character(), label = NA_character_, man = NA_character_) { + initialize = function(dict_entry, id = dict_entry, task_type = "regr", + param_set = ps(), predict_types = "response", feature_types = character(), properties = character(), packages = character(), + additional_configuration = character(0), label, man + ) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Learner construction and will be removed in the future.") + } + super$initialize(id = id, task_type = task_type, param_set = param_set, feature_types = feature_types, predict_types = predict_types, properties = properties, packages = packages, - label = label, man = man) + additional_configuration = c("quantiles", "quantile_response", additional_configuration)) }, #' @description @@ -136,7 +143,7 @@ LearnerRegr = R6Class("LearnerRegr", inherit = Learner, if ("quantiles" %nin% self$predict_types) { stopf("Learner does not support predicting quantiles") } - private$.quantiles = assert_numeric(rhs, lower = 0, upper = 1, any.missing = FALSE, min.len = 1L, sorted = TRUE, .var.name = "quantiles") + private$.quantiles = assert_numeric(rhs, lower = 0, upper = 1, any.missing = FALSE, min.len = 1L, sorted = TRUE, .var.name = "quantiles", null.ok = TRUE) if (length(private$.quantiles) == 1) { private$.quantile_response = private$.quantiles @@ -154,7 +161,7 @@ LearnerRegr = R6Class("LearnerRegr", inherit = Learner, stopf("Learner does not support predicting quantiles") } - private$.quantile_response = assert_number(rhs, lower = 0, upper = 1, .var.name = "response") + private$.quantile_response = assert_number(rhs, lower = 0, upper = 1, .var.name = "response", null.ok = TRUE) private$.quantiles = sort(union(private$.quantiles, private$.quantile_response)) } ), diff --git a/R/LearnerRegrDebug.R b/R/LearnerRegrDebug.R index e84eb8ac7..231f4990f 100644 --- a/R/LearnerRegrDebug.R +++ b/R/LearnerRegrDebug.R @@ -53,9 +53,7 @@ LearnerRegrDebug = R6Class("LearnerRegrDebug", inherit = LearnerRegr, x = p_dbl(0, 1, tags = "train") ), properties = c("missings", "weights"), - packages = "stats", - man = "mlr3::mlr_learners_regr.debug", - label = "Debug Learner for Regression" + packages = "stats" ) }, diff --git a/R/LearnerRegrFeatureless.R b/R/LearnerRegrFeatureless.R index e8523de17..be9c89064 100644 --- a/R/LearnerRegrFeatureless.R +++ b/R/LearnerRegrFeatureless.R @@ -43,9 +43,7 @@ LearnerRegrFeatureless = R6Class("LearnerRegrFeatureless", inherit = LearnerRegr predict_types = c("response", "se", "quantiles"), param_set = ps, properties = c("featureless", "missings", "importance", "selected_features", "weights"), - packages = "stats", - label = "Featureless Regression Learner", - man = "mlr3::mlr_learners_regr.featureless" + packages = "stats" ) }, diff --git a/R/LearnerRegrRpart.R b/R/LearnerRegrRpart.R index 243008fe3..233dade63 100644 --- a/R/LearnerRegrRpart.R +++ b/R/LearnerRegrRpart.R @@ -44,9 +44,7 @@ LearnerRegrRpart = R6Class("LearnerRegrRpart", inherit = LearnerRegr, predict_types = "response", packages = "rpart", param_set = ps, - properties = c("weights", "missings", "importance", "selected_features"), - label = "Regression Tree", - man = "mlr3::mlr_learners_regr.rpart" + properties = c("weights", "missings", "importance", "selected_features") ) }, diff --git a/R/Measure.R b/R/Measure.R index 7a3fa3d27..6ea0117f8 100644 --- a/R/Measure.R +++ b/R/Measure.R @@ -78,19 +78,11 @@ #' @template seealso_measure #' @export Measure = R6Class("Measure", + inherit = Mlr3Component, public = list( - #' @template field_id - id = NULL, - - #' @template field_label - label = NULL, - #' @template field_task_type task_type = NULL, - #' @template field_param_set - param_set = NULL, - #' @field obs_loss (`function()` | `NULL`) #' Function to calculate the observation-wise loss. obs_loss = NULL, @@ -128,12 +120,6 @@ Measure = R6Class("Measure", #' If `TRUE`, good predictions correspond to small values of performance scores. minimize = NULL, - #' @template field_packages - packages = NULL, - - #' @template field_man - man = NULL, - #' @description #' Creates a new instance of this [R6][R6::R6Class] class. #' @@ -141,12 +127,18 @@ Measure = R6Class("Measure", initialize = function(id, task_type = NA, param_set = ps(), range = c(-Inf, Inf), minimize = NA, average = "macro", aggregator = NULL, obs_loss = NULL, properties = character(), predict_type = "response", predict_sets = "test", task_properties = character(), packages = character(), - label = NA_character_, man = NA_character_, trafo = NULL) { + additional_configuration = character(0), label, man, trafo = NULL, dict_entry = id) { + + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Measure construction and will be removed in the future.") + } + + super$initialize(id = id, dict_entry = dict_entry, dict_shortaccess = "msr", + param_set = param_set, packages = packages, properties = properties, + additional_configuration = c("check_prerequisites", "predict_sets", "average", "aggregator", "use_weights", + "obs_loss", "trafo", additional_configuration)) - self$id = assert_string(id, min.chars = 1L) - self$label = assert_string(label, na.ok = TRUE) self$task_type = task_type - self$param_set = assert_param_set(param_set) self$range = assert_range(range) self$minimize = assert_flag(minimize, na.ok = TRUE) self$average = average @@ -163,11 +155,9 @@ Measure = R6Class("Measure", assert_choice(predict_type, names(mlr_reflections$learner_predict_types[[task_type]])) assert_subset(task_properties, mlr_reflections$task_properties[[task_type]]) } else { - assert_subset(properties, unique(unlist(mlr_reflections$measure_properties, use.names = FALSE))) + assert_subset(properties, unlist(mlr_reflections$measure_properties, use.names = FALSE)) } - self$properties = unique(properties) - if ("weights" %in% properties) { self$use_weights = "use" } else if ("requires_no_prediction" %in% properties) { @@ -177,19 +167,10 @@ Measure = R6Class("Measure", } self$predict_type = predict_type - self$predict_sets = predict_sets + self$predict_sets = unique(assert_subset(predict_sets, + mlr_reflections$predict_sets, empty.ok = "requires_no_prediction" %chin% properties + )) self$task_properties = task_properties - self$packages = union("mlr3", assert_character(packages, any.missing = FALSE, min.chars = 1L)) - self$man = assert_string(man, na.ok = TRUE) - - check_packages_installed(packages, msg = sprintf("Package '%%s' required but not installed for Measure '%s'", id)) - }, - - #' @description - #' Helper for print outputs. - #' @param ... (ignored). - format = function(...) { - sprintf("<%s:%s>", class(self)[1L], self$id) }, #' @description @@ -212,12 +193,6 @@ Measure = R6Class("Measure", }) }, - #' @description - #' Opens the corresponding help page referenced by field `$man`. - help = function() { - open_help(self$man) - }, - #' @description #' Takes a [Prediction] (or a list of [Prediction] objects named with valid `predict_sets`) #' and calculates a numeric score. @@ -323,28 +298,6 @@ Measure = R6Class("Measure", private$.predict_sets }, - #' @field hash (`character(1)`)\cr - #' Hash (unique identifier) for this object. - #' The hash is calculated based on the id, the parameter settings, predict sets and the `$score`, `$average`, `$aggregator`, `$obs_loss`, `$trafo` method. - #' Measure can define additional fields to be included in the hash by setting the field `$.extra_hash`. - hash = function(rhs) { - assert_ro_binding(rhs) - calculate_hash(class(self), self$id, self$param_set$values, private$.score, - private$.average, private$.aggregator, self$obs_loss, self$trafo, - self$predict_sets, mget(private$.extra_hash, envir = self), private$.use_weights) - }, - - #' @field properties (`character()`)\cr - #' Properties of this measure. - properties = function(rhs) { - if (!missing(rhs)) { - props = if (is.na(self$task_type)) unique(unlist(mlr_reflections$measure_properties, use.names = FALSE)) else mlr_reflections$measure_properties[[self$task_type]] - private$.properties = assert_subset(rhs, props) - } else { - private$.properties - } - }, - #' @field average (`character(1)`)\cr #' Method for aggregation: #' @@ -404,14 +357,16 @@ Measure = R6Class("Measure", ), private = list( - .properties = character(), .predict_sets = NULL, - .extra_hash = character(), .average = NULL, .aggregator = NULL, .use_weights = NULL, .score = function(prediction, task, weights, ...) { stop("abstract method") + }, + .additional_phash_input = function() { + list(private$.average, private$.aggregator, self$obs_loss, self$trafo, + self$predict_sets, private$.use_weights) } ) ) diff --git a/R/MeasureAIC.R b/R/MeasureAIC.R index e0a988ebc..e7b238c9a 100644 --- a/R/MeasureAIC.R +++ b/R/MeasureAIC.R @@ -30,9 +30,7 @@ MeasureAIC = R6Class("MeasureAIC", predict_sets = NULL, properties = c("na_score", "requires_learner", "requires_model", "requires_no_prediction"), predict_type = NA_character_, - minimize = TRUE, - label = "Akaike Information Criterion", - man = "mlr3::mlr_measures_aic" + minimize = TRUE ) } ), diff --git a/R/MeasureBIC.R b/R/MeasureBIC.R index b3d48f205..dd57a80f1 100644 --- a/R/MeasureBIC.R +++ b/R/MeasureBIC.R @@ -28,9 +28,7 @@ MeasureBIC = R6Class("MeasureBIC", properties = c("na_score", "requires_learner", "requires_model", "requires_no_prediction"), predict_sets = NULL, predict_type = NA_character_, - minimize = TRUE, - label = "Bayesian Information Criterion", - man = "mlr3::mlr_measures_bic" + minimize = TRUE ) } ), diff --git a/R/MeasureClassif.R b/R/MeasureClassif.R index fb8adfdc0..ac4561879 100644 --- a/R/MeasureClassif.R +++ b/R/MeasureClassif.R @@ -33,10 +33,14 @@ MeasureClassif = R6Class("MeasureClassif", #' @description #' Creates a new instance of this [R6][R6::R6Class] class. initialize = function(id, param_set = ps(), range, minimize = NA, average = "macro", aggregator = NULL, properties = character(), predict_type = "response", - predict_sets = "test", task_properties = character(), packages = character(), label = NA_character_, man = NA_character_) { + predict_sets = "test", task_properties = character(), packages = character(), additional_configuration = character(0), label, man) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Measure construction and will be removed in the future.") + } + super$initialize(id, task_type = "classif", param_set = param_set, range = range, minimize = minimize, average = average, aggregator = aggregator, properties = properties, predict_type = predict_type, predict_sets = predict_sets, - task_properties = task_properties, packages = packages, label = label, man = man) + task_properties = task_properties, packages = packages, additional_configuration = additional_configuration) } ) ) diff --git a/R/MeasureClassifCosts.R b/R/MeasureClassifCosts.R index 38c59f290..baa4a8605 100644 --- a/R/MeasureClassifCosts.R +++ b/R/MeasureClassifCosts.R @@ -53,8 +53,7 @@ MeasureClassifCosts = R6Class("MeasureClassifCosts", range = c(-Inf, Inf), minimize = TRUE, properties = "weights", - label = "Cost-sensitive Classification", - man = "mlr3::mlr_measures_classif.costs" + additional_configuration = "costs" ) } ), @@ -66,6 +65,10 @@ MeasureClassifCosts = R6Class("MeasureClassifCosts", if (missing(rhs)) { return(private$.costs) } + if (is.null(rhs)) { # need to accept NULL for resetting to initial value + private$.costs = NULL + return(NULL) + } assert_matrix(rhs, mode = "numeric", any.missing = FALSE, col.names = "unique", row.names = "unique") assert_set_equal(rownames(rhs), colnames(rhs)) @@ -112,7 +115,9 @@ MeasureClassifCosts = R6Class("MeasureClassifCosts", perf }, - .extra_hash = "costs" + .additional_phash_input = function() { + c(list(self$costs), super$.additional_phash_input()) + } ) ) diff --git a/R/MeasureDebug.R b/R/MeasureDebug.R index 8e02c0a72..3316d02b1 100644 --- a/R/MeasureDebug.R +++ b/R/MeasureDebug.R @@ -33,9 +33,7 @@ MeasureDebugClassif = R6Class("MeasureDebugClassif", param_set = param_set, predict_type = "response", range = c(0, Inf), - properties = "na_score", - label = "Debug Classification Measure", - man = "mlr3::mlr_measures_debug_classif" + properties = "na_score" ) } ), diff --git a/R/MeasureElapsedTime.R b/R/MeasureElapsedTime.R index 8f2c0b53f..c12967faf 100644 --- a/R/MeasureElapsedTime.R +++ b/R/MeasureElapsedTime.R @@ -36,17 +36,15 @@ MeasureElapsedTime = R6Class("MeasureElapsedTime", #' @param stages (`character()`)\cr #' Subset of `("train", "predict")`. #' The runtime of provided stages will be summed. - initialize = function(id = "elapsed_time", stages) { + initialize = function(stages) { super$initialize( - id = id, + id = "elapsed_time", task_type = NA_character_, predict_sets = NULL, predict_type = NA_character_, range = c(0, Inf), minimize = TRUE, - properties = c("requires_learner", "requires_no_prediction"), - label = "Elapsed Time", - man = "mlr3::mlr_measures_elapsed_time" + properties = c("requires_learner", "requires_no_prediction") ) self$stages = assert_subset(stages, c("train", "predict"), empty.ok = FALSE) } @@ -57,11 +55,14 @@ MeasureElapsedTime = R6Class("MeasureElapsedTime", sum(unlist(learner$state[sprintf("%s_time", self$stages)], use.names = FALSE)) }, - .extra_hash = "stages" + .additional_phash_input = function() { + c(list(self$stages), super$.additional_phash_input()) + } ) ) #' @include mlr_measures.R -mlr_measures$add("time_train", function() MeasureElapsedTime$new(id = "time_train", stages = "train")) -mlr_measures$add("time_predict", function() MeasureElapsedTime$new(id = "time_predict", stages = "predict")) -mlr_measures$add("time_both", function() MeasureElapsedTime$new(id = "time_both", stages = c("train", "predict"))) +mlr_measures$add("time_train", function() MeasureElapsedTime$new(stages = "train")$configure(id = "time_train")) +mlr_measures$add("time_predict", function() MeasureElapsedTime$new(stages = "predict")$configure(id = "time_predict")) +mlr_measures$add("time_both", function() MeasureElapsedTime$new(stages = c("train", "predict"))$configure(id = "time_both")) +mlr_measures$add("elapsed_time", MeasureElapsedTime, .prototype_args = list(stages = "train")) diff --git a/R/MeasureInternalValidScore.R b/R/MeasureInternalValidScore.R index 13a934efa..f42aac8ee 100644 --- a/R/MeasureInternalValidScore.R +++ b/R/MeasureInternalValidScore.R @@ -32,23 +32,34 @@ MeasureInternalValidScore = R6Class("MeasureInternalValidScore", private$.select = assert_string(select, null.ok = TRUE) super$initialize( id = select %??% "internal_valid_score", + dict_entry = "internal_valid_score", task_type = NA_character_, properties = c("na_score", "requires_learner", "requires_no_prediction"), predict_sets = NULL, predict_type = NA_character_, range = c(-Inf, Inf), - minimize = assert_flag(minimize, na.ok = TRUE), - label = "Internal Validation Score", - man = "mlr3::mlr_measures_internal_valid_score" + minimize = assert_flag(minimize, na.ok = TRUE) ) } ), + active = list( + select = function(rhs) { + if (!missing(rhs)) { + private$.select = assert_string(rhs, null.ok = TRUE) + } + private$.select + } + ), + private = list( .select = NULL, .score = function(prediction, learner, ...) { x = get0("internal_valid_scores", learner) x[[private$.select %??% 1]] %??% NA_real_ + }, + .additional_phash_input = function() { + c(list(private$.select), super$.additional_phash_input()) } ) ) diff --git a/R/MeasureOOBError.R b/R/MeasureOOBError.R index 511ae83f8..c1b1cdd4d 100644 --- a/R/MeasureOOBError.R +++ b/R/MeasureOOBError.R @@ -26,9 +26,7 @@ MeasureOOBError = R6Class("MeasureOOBError", predict_sets = NULL, predict_type = NA_character_, range = c(-Inf, Inf), - minimize = TRUE, - label = "Out-of-bag Error", - man = "mlr3::mlr_measures_oob_error" + minimize = TRUE ) } ), diff --git a/R/MeasureRegr.R b/R/MeasureRegr.R index 8d98964b5..3f284dcb1 100644 --- a/R/MeasureRegr.R +++ b/R/MeasureRegr.R @@ -33,10 +33,15 @@ MeasureRegr = R6Class("MeasureRegr", #' @description #' Creates a new instance of this [R6][R6::R6Class] class. initialize = function(id, param_set = ps(), range, minimize = NA, average = "macro", aggregator = NULL, properties = character(), predict_type = "response", - predict_sets = "test", task_properties = character(), packages = character(), label = NA_character_, man = NA_character_) { + predict_sets = "test", task_properties = character(), packages = character(), additional_configuration = character(0), label, man) { + + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Measure construction and will be removed in the future.") + } + super$initialize(id, task_type = "regr", param_set = param_set, range = range, minimize = minimize, average = average, aggregator = aggregator, properties = properties, predict_type = predict_type, predict_sets = predict_sets, - task_properties = task_properties, packages = packages, label = label, man = man) + task_properties = task_properties, packages = packages, additional_configuration = additional_configuration) } ) ) diff --git a/R/MeasureRegrPinball.R b/R/MeasureRegrPinball.R index 2e454a8b9..6a912bed6 100644 --- a/R/MeasureRegrPinball.R +++ b/R/MeasureRegrPinball.R @@ -16,10 +16,13 @@ #' } #' where \eqn{q} is the quantile and \eqn{w_i} are normalized sample weights. #' -#' @param alpha `numeric(1)`\cr -#' The quantile to compute the pinball loss. -#' Must be one of the quantiles that the Learner was trained on. -# +#' @section Parameters +#' +#' * `alpha` (`numeric(1)`)\cr +#' The quantile to compute the pinball loss. +#' Must be one of the quantiles that the Learner was trained on. +#' Initialized to `0.5`. +#' #' @templateVar id regr.pinball #' @template measure #' @@ -28,18 +31,16 @@ MeasureRegrPinball = R6Class("MeasureRegrPinball", inherit = MeasureRegr, public = list( - #' @description - #' Creates a new instance of this [R6][R6::R6Class] class. - initialize = function(alpha = 0.5) { - param_set = ps(alpha = p_dbl(lower = 0, upper = 1)) - param_set$set_values(alpha = alpha) + #' @description + #' Creates a new instance of this [R6][R6::R6Class] class. + initialize = function() { + param_set = ps(alpha = p_dbl(lower = 0, upper = 1, init = 0.5)) super$initialize( id = "regr.pinball", param_set = param_set, predict_type = "quantiles", minimize = TRUE, - range = c(-Inf, Inf), - man = "mlr3::mlr_measures_regr.pinball" + range = c(-Inf, Inf) ) } ), diff --git a/R/MeasureRegrRQR.R b/R/MeasureRegrRQR.R index 012fd43a4..812e37ce4 100644 --- a/R/MeasureRegrRQR.R +++ b/R/MeasureRegrRQR.R @@ -20,14 +20,16 @@ #' #' This measure is undefined for constant \eqn{t}. #' +#' @section Parameters +#' * `alpha` (`numeric(1)`)\cr +#' The quantile for which to compute the measure. +#' Must be one of the quantiles that the Learner was trained on. +#' Initialized to `0.5`. +#' #' @param pred_set_mean `logical(1)`\cr #' If `TRUE`, the mean of the true values is calculated on the prediction set. #' If `FALSE`, the mean of the true values is calculated on the training set. #' -#' @param alpha `numeric(1)`\cr -#' The quantile for which to compute the measure. -#' Must be one of the quantiles that the Learner was trained on. -# #' @references #' `r format_bib("koenker_1999")` #' @@ -36,26 +38,36 @@ #' #' @template seealso_measure #' @export -MeasureRegrRQR = R6Class("MeasureRQR", +MeasureRegrRQR = R6Class("MeasureRegrRQR", inherit = MeasureRegr, public = list( - #' @description - #' Creates a new instance of this [R6][R6::R6Class] class. - initialize = function(alpha = 0.5, pred_set_mean = TRUE) { - private$.pred_set_mean = assert_flag(pred_set_mean) - param_set = ps(alpha = p_dbl(lower = 0, upper = 1)) - param_set$set_values(alpha = alpha) + #' @description + #' Creates a new instance of this [R6][R6::R6Class] class. + initialize = function(pred_set_mean = TRUE) { + private$.pred_set_mean = assert_flag(pred_set_mean) + param_set = ps(alpha = p_dbl(lower = 0, upper = 1, init = 0.5)) + + super$initialize( + id = "regr.rqr", + param_set = param_set, + properties = c(if (!private$.pred_set_mean) c("requires_task", "requires_train_set") else character(0)), + predict_type = "quantiles", + minimize = FALSE, + range = c(-Inf, 1) + ) + } + ), - super$initialize( - id = "regr.rqr", - param_set = param_set, - properties = c(if (!private$.pred_set_mean) c("requires_task", "requires_train_set")), - predict_type = "quantiles", - minimize = FALSE, - range = c(-Inf, 1), - man = "mlr3::mlr_measures_regr.rqr" - ) - } + active = list( + #' @field pred_set_mean (`logical(1)`)\cr + #' If `TRUE`, the mean of the true values is calculated on the prediction set. + #' If `FALSE`, the mean of the true values is calculated on the training set. + pred_set_mean = function(rhs) { + if (!missing(rhs)) { + private$.pred_set_mean = assert_flag(rhs) + } + private$.pred_set_mean + } ), private = list( @@ -89,8 +101,12 @@ MeasureRegrRQR = R6Class("MeasureRQR", ) ) - 1 - (numerator / denominator) - } + 1 - (numerator / denominator) + }, + + .additional_phash_input = function() { + c(list(self$pred_set_mean), super$.additional_phash_input()) + } ) ) diff --git a/R/MeasureRegrRSQ.R b/R/MeasureRegrRSQ.R index a6b1e704f..b7651a5ec 100644 --- a/R/MeasureRegrRSQ.R +++ b/R/MeasureRegrRSQ.R @@ -31,7 +31,7 @@ #' #' @template seealso_measure #' @export -MeasureRegrRSQ = R6Class("MeasureRSQ", +MeasureRegrRSQ = R6Class("MeasureRegrRSQ", inherit = MeasureRegr, public = list( #' @description @@ -46,12 +46,23 @@ MeasureRegrRSQ = R6Class("MeasureRSQ", properties = c(if (!private$.pred_set_mean) c("requires_task", "requires_train_set"), "weights"), predict_type = "response", minimize = FALSE, - range = c(-Inf, 1), - man = "mlr3::mlr_measures_regr.rsq" + range = c(-Inf, 1) ) } ), + active = list( + #' @field pred_set_mean (`logical(1)`)\cr + #' If `TRUE`, the mean of the true values is calculated on the prediction set. + #' If `FALSE`, the mean of the true values is calculated on the training set. + pred_set_mean = function(rhs) { + if (!missing(rhs)) { + private$.pred_set_mean = assert_flag(rhs) + } + private$.pred_set_mean + } + ), + private = list( # this is not included in the paramset as this flag influences properties of the learner # so this flag should not be "dynamic state" @@ -72,6 +83,10 @@ MeasureRegrRSQ = R6Class("MeasureRSQ", } 1 - sum(weights * (prediction$truth - prediction$response)^2) / sum(weights * (prediction$truth - mu)^2) } + }, + + .additional_phash_input = function() { + c(list(self$pred_set_mean), super$.additional_phash_input()) } ) ) diff --git a/R/MeasureSelectedFeatures.R b/R/MeasureSelectedFeatures.R index d53ead467..91e7933f7 100644 --- a/R/MeasureSelectedFeatures.R +++ b/R/MeasureSelectedFeatures.R @@ -41,9 +41,7 @@ MeasureSelectedFeatures = R6Class("MeasureSelectedFeatures", predict_sets = NULL, predict_type = NA_character_, range = c(0, Inf), - minimize = TRUE, - label = "Absolute or Relative Frequency of Selected Features", - man = "mlr3::mlr_measures_selected_features" + minimize = TRUE ) } ), diff --git a/R/MeasureSimilarity.R b/R/MeasureSimilarity.R index 254532529..c91584062 100644 --- a/R/MeasureSimilarity.R +++ b/R/MeasureSimilarity.R @@ -46,10 +46,14 @@ MeasureSimilarity = R6Class("MeasureSimilarity", #' @description #' Creates a new instance of this [R6][R6::R6Class] class. initialize = function(id, param_set = ps(), range, minimize = NA, average = "macro", aggregator = NULL, properties = character(), predict_type = NA_character_, - task_properties = character(), packages = character(), label = NA_character_, man = NA_character_) { + task_properties = character(), packages = character(), label, man) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Measure construction and will be removed in the future.") + } + super$initialize(id, task_type = NA_character_, param_set = param_set, range = range, minimize = minimize, average = "custom", aggregator = aggregator, properties = c("requires_model", "requires_no_prediction", properties), predict_type = predict_type, predict_sets = NULL, - task_properties = task_properties, packages = packages, label = label, man = man) + task_properties = task_properties, packages = packages) } ), diff --git a/R/MeasureSimple.R b/R/MeasureSimple.R index 2c1cead39..1ea7d3c32 100644 --- a/R/MeasureSimple.R +++ b/R/MeasureSimple.R @@ -16,10 +16,9 @@ MeasureBinarySimple = R6Class("MeasureBinarySimple", properties = if (weights) "weights" else character(), predict_type = info$predict_type, task_properties = "twoclass", - packages = "mlr3measures", - label = info$title, - man = paste0("mlr3::mlr_measures_classif.", name) + packages = "mlr3measures" ) + private$.man = paste0("mlr3::mlr_measures_classif.", name) self$fun = get(name, envir = asNamespace("mlr3measures"), mode = "function") if (!is.na(info$obs_loss)) { @@ -41,7 +40,9 @@ MeasureBinarySimple = R6Class("MeasureBinarySimple", ) }, - .extra_hash = c("fun", "na_value") + .additional_phash_input = function() { + c(list(self$fun, self$na_value), super$.additional_phash_input()) + } ) ) @@ -62,10 +63,10 @@ MeasureClassifSimple = R6Class("MeasureClassifSimple", minimize = info$minimize, properties = if (weights) "weights" else character(), predict_type = info$predict_type, - packages = "mlr3measures", - label = info$title, - man = paste0("mlr3::mlr_measures_classif.", name) + packages = "mlr3measures" ) + private$.man = paste0("mlr3::mlr_measures_classif.", name) + self$fun = get(name, envir = asNamespace("mlr3measures"), mode = "function") if (!is.na(info$obs_loss)) { self$obs_loss = get(info$obs_loss, envir = asNamespace("mlr3measures"), mode = "function") @@ -82,7 +83,9 @@ MeasureClassifSimple = R6Class("MeasureClassifSimple", na_value = self$na_value, sample_weights = weights) }, - .extra_hash = c("fun", "na_value") + .additional_phash_input = function() { + c(list(self$fun, self$na_value), super$.additional_phash_input()) + } ) ) @@ -111,10 +114,10 @@ MeasureRegrSimple = R6Class("MeasureRegrSimple", minimize = info$minimize, properties = if (weights) "weights" else character(), predict_type = info$predict_type, - packages = "mlr3measures", - label = info$title, - man = paste0("mlr3::mlr_measures_regr.", name) + packages = "mlr3measures" ) + private$.man = paste0("mlr3::mlr_measures_regr.", name) + self$fun = get(name, envir = asNamespace("mlr3measures"), mode = "function") if (!is.na(info$obs_loss)) { self$obs_loss = get(info$obs_loss, envir = asNamespace("mlr3measures"), mode = "function") @@ -131,7 +134,9 @@ MeasureRegrSimple = R6Class("MeasureRegrSimple", na_value = self$na_value, sample_weights = weights) }, - .extra_hash = c("fun", "na_value") + .additional_phash_input = function() { + c(list(self$fun, self$na_value), super$.additional_phash_input()) + } ) ) @@ -166,15 +171,16 @@ MeasureSimilaritySimple = R6Class("MeasureSimilaritySimple", average = "custom", aggregator = agg, predict_type = NA_character_, - packages = "mlr3measures", - label = info$title, - man = paste0("mlr3::mlr_measures_sim.", name), + packages = "mlr3measures" ) + private$.man = paste0("mlr3::mlr_measures_sim.", name) } ), private = list( - .extra_hash = c("fun", "na_value") + .additional_phash_input = function() { + c(list(self$fun, self$na_value), super$.additional_phash_input()) + } ) ) diff --git a/R/Resampling.R b/R/Resampling.R index 29867ed06..e9fc76d51 100644 --- a/R/Resampling.R +++ b/R/Resampling.R @@ -90,13 +90,8 @@ #' r$instantiate(task) #' prop.table(table(task$truth(r$train_set(1)))) # roughly same proportion Resampling = R6Class("Resampling", + inherit = Mlr3Component, public = list( - #' @template field_label - label = NULL, - - #' @template field_param_set - param_set = NULL, - #' @field instance (any)\cr #' During `instantiate()`, the instance is stored in this slot in an arbitrary format. #' Note that if a grouping variable is present in the [Task], a [Resampling] may operate on the @@ -125,9 +120,6 @@ Resampling = R6Class("Resampling", #' Only used internally. duplicated_ids = NULL, - #' @template field_man - man = NULL, - #' @description #' Creates a new instance of this [R6][R6::R6Class] class. #' @@ -135,19 +127,15 @@ Resampling = R6Class("Resampling", #' Set to `TRUE` if this resampling strategy may have duplicated row ids in a single training set or test set. #' #' Note that this object is typically constructed via a derived classes, e.g. [ResamplingCV] or [ResamplingHoldout]. - initialize = function(id, param_set = ps(), duplicated_ids = FALSE, label = NA_character_, man = NA_character_) { - private$.id = assert_string(id, min.chars = 1L) - self$label = assert_string(label, na.ok = TRUE) - self$param_set = assert_param_set(param_set) - self$duplicated_ids = assert_flag(duplicated_ids) - self$man = assert_string(man, na.ok = TRUE) - }, + initialize = function(id, param_set = ps(), duplicated_ids = FALSE, additional_configuration = character(0), label, man) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Resampling construction and will be removed in the future.") + } - #' @description - #' Helper for print outputs. - #' @param ... (ignored). - format = function(...) { - sprintf("<%s>", class(self)[1L]) + super$initialize(dict_entry = id, dict_shortaccess = "rsmp", param_set = param_set, + additional_configuration = additional_configuration) + + self$duplicated_ids = assert_flag(duplicated_ids) }, #' @description @@ -156,19 +144,13 @@ Resampling = R6Class("Resampling", print = function(...) { msg_h = if (is.null(self$label) || is.na(self$label)) "" else paste0(": ", self$label) cat_cli({ - cli_h1("{.cls {class(self)[1L]}} {msg_h}") + cli_h1("{.cls {class(self)[1L]}} ({self$id}){msg_h}") cli_li("Iterations: {.val {self$iters}}") cli_li("Instantiated: {.val {self$is_instantiated}}") cli_li("Parameters: {as_short_string(self$param_set$values, 1000L)}") }) }, - #' @description - #' Opens the corresponding help page referenced by field `$man`. - help = function() { - open_help(self$man) - }, - #' @description #' Materializes fixed training and test splits for a given task and stores them in `r$instance` #' in an arbitrary format. @@ -186,7 +168,6 @@ Resampling = R6Class("Resampling", #' resampling$instantiate(task) instantiate = function(task) { task = assert_task(as_task(task)) - private$.hash = NULL self$instance = private$.get_instance(task) self$task_hash = task$hash self$task_row_hash = task$row_hash @@ -224,45 +205,16 @@ Resampling = R6Class("Resampling", ), active = list( - #' @template field_id - id = function(rhs) { - if (missing(rhs)) { - return(private$.id) - } - - private$.hash = NULL - private$.id = assert_string(rhs, min.chars = 1L) - }, - #' @field is_instantiated (`logical(1)`)\cr #' Is `TRUE` if the resampling has been instantiated. is_instantiated = function(rhs) { assert_ro_binding(rhs) !is.null(self$instance) - }, - - #' @field hash (`character(1)`)\cr - #' Hash (unique identifier) for this object. - #' If the object has not been instantiated yet, `NA_character_` is returned. - #' The hash is calculated based on the class name, the id, the parameter set, and the instance. - hash = function(rhs) { - assert_ro_binding(rhs) - if (!self$is_instantiated) { - return(NA_character_) - } - - if (is.null(private$.hash)) { - private$.hash = calculate_hash(list(class(self), self$id, self$param_set$values, self$instance)) - } - - private$.hash } ), private = list( .primary_iters = NULL, - .id = NULL, - .hash = NULL, .groups = NULL, .get_instance = function(task) { @@ -295,7 +247,9 @@ Resampling = R6Class("Resampling", } private$.groups[list(ids), on = "group", allow.cartesian = TRUE][[1L]] - } + }, + + .additional_phash_input = function() list(self$instance) ) ) diff --git a/R/ResamplingBootstrap.R b/R/ResamplingBootstrap.R index 54667059d..2ad7ec684 100644 --- a/R/ResamplingBootstrap.R +++ b/R/ResamplingBootstrap.R @@ -51,8 +51,7 @@ ResamplingBootstrap = R6Class("ResamplingBootstrap", inherit = Resampling, ) ps$set_values(ratio = 1, repeats = 30L) - super$initialize(id = "bootstrap", param_set = ps, duplicated_ids = TRUE, - label = "Bootstrap", man = "mlr3::mlr_resamplings_bootstrap") + super$initialize(id = "bootstrap", param_set = ps, duplicated_ids = TRUE) } ), diff --git a/R/ResamplingCV.R b/R/ResamplingCV.R index 34a0e60fc..0c33b953a 100644 --- a/R/ResamplingCV.R +++ b/R/ResamplingCV.R @@ -46,8 +46,7 @@ ResamplingCV = R6Class("ResamplingCV", inherit = Resampling, ) ps$set_values(folds = 10L) - super$initialize(id = "cv", param_set = ps, - label = "Cross-Validation", man = "mlr3::mlr_resamplings_cv") + super$initialize(id = "cv", param_set = ps) } ), @@ -83,8 +82,7 @@ ResamplingCV = R6Class("ResamplingCV", inherit = Resampling, deep_clone = function(name, value) { switch(name, "instance" = copy(value), - "param_set" = value$clone(deep = TRUE), - value + super$deep_clone(name, value) ) } ) diff --git a/R/ResamplingCustom.R b/R/ResamplingCustom.R index 73265425a..26ea554eb 100644 --- a/R/ResamplingCustom.R +++ b/R/ResamplingCustom.R @@ -29,8 +29,7 @@ ResamplingCustom = R6Class("ResamplingCustom", inherit = Resampling, #' @description #' Creates a new instance of this [R6][R6::R6Class] class. initialize = function() { - super$initialize(id = "custom", duplicated_ids = TRUE, - label = "Custom Splits", man = "mlr3::mlr_resamplings_custom") + super$initialize(id = "custom", duplicated_ids = TRUE) }, #' @description diff --git a/R/ResamplingCustomCV.R b/R/ResamplingCustomCV.R index f5f6ca286..3e1b82402 100644 --- a/R/ResamplingCustomCV.R +++ b/R/ResamplingCustomCV.R @@ -40,8 +40,7 @@ ResamplingCustomCV = R6Class("ResamplingCustomCV", inherit = Resampling, #' @description #' Creates a new instance of this [R6][R6::R6Class] class. initialize = function() { - super$initialize(id = "custom_cv", duplicated_ids = FALSE, - label = "Custom Split Cross-Validation", man = "mlr3::mlr_resamplings_custom_cv") + super$initialize(id = "custom_cv", duplicated_ids = FALSE) }, #' @description diff --git a/R/ResamplingHoldout.R b/R/ResamplingHoldout.R index 0c47a12e9..26e05d727 100644 --- a/R/ResamplingHoldout.R +++ b/R/ResamplingHoldout.R @@ -47,8 +47,7 @@ ResamplingHoldout = R6Class("ResamplingHoldout", inherit = Resampling, ) ps$set_values(ratio = 2 / 3) - super$initialize(id = "holdout", param_set = ps, - label = "Holdout", man = "mlr3::mlr_resamplings_holdout") + super$initialize(id = "holdout", param_set = ps) } ), active = list( diff --git a/R/ResamplingInsample.R b/R/ResamplingInsample.R index e45e8cc81..ce4900c40 100644 --- a/R/ResamplingInsample.R +++ b/R/ResamplingInsample.R @@ -30,8 +30,7 @@ ResamplingInsample = R6Class("ResamplingInsample", inherit = Resampling, #' @description #' Creates a new instance of this [R6][R6::R6Class] class. initialize = function() { - super$initialize(id = "insample", - label = "Insample Resampling", man = "mlr3::mlr_resamplings_insample") + super$initialize(id = "insample") } ), active = list( diff --git a/R/ResamplingLOO.R b/R/ResamplingLOO.R index 741a1dce1..29735a1e2 100644 --- a/R/ResamplingLOO.R +++ b/R/ResamplingLOO.R @@ -49,7 +49,7 @@ ResamplingLOO = R6Class("ResamplingLOO", inherit = Resampling, #' @description #' Creates a new instance of this [R6][R6::R6Class] class. initialize = function() { - super$initialize(id = "loo", label = "Leave-One-Out", man = "mlr3::mlr_resamplings_loo") + super$initialize(id = "loo") } ), diff --git a/R/ResamplingRepeatedCV.R b/R/ResamplingRepeatedCV.R index bbf3b492c..7992daf2c 100644 --- a/R/ResamplingRepeatedCV.R +++ b/R/ResamplingRepeatedCV.R @@ -57,8 +57,7 @@ ResamplingRepeatedCV = R6Class("ResamplingRepeatedCV", inherit = Resampling, repeats = p_int(1L) ) ps$set_values(repeats = 10L, folds = 10L) - super$initialize(id = "repeated_cv", param_set = ps, - label = "Repeated Cross-Validation", man = "mlr3::mlr_resamplings_repeated_cv") + super$initialize(id = "repeated_cv", param_set = ps) }, #' @description @@ -126,8 +125,7 @@ ResamplingRepeatedCV = R6Class("ResamplingRepeatedCV", inherit = Resampling, deep_clone = function(name, value) { switch(name, "instance" = copy(value), - "param_set" = value$clone(deep = TRUE), - value + super$deep_clone(name, value) ) } ) diff --git a/R/ResamplingSubsampling.R b/R/ResamplingSubsampling.R index beeae7f1b..562b427b9 100644 --- a/R/ResamplingSubsampling.R +++ b/R/ResamplingSubsampling.R @@ -50,8 +50,7 @@ ResamplingSubsampling = R6Class("ResamplingSubsampling", inherit = Resampling, ) ps$set_values(repeats = 30L, ratio = 2 / 3) - super$initialize(id = "subsampling", param_set = ps, - label = "Subsampling", man = "mlr3::mlr_resamplings_subsampling") + super$initialize(id = "subsampling", param_set = ps) } ), diff --git a/R/Task.R b/R/Task.R index 96312d03f..2f4814adf 100644 --- a/R/Task.R +++ b/R/Task.R @@ -75,10 +75,8 @@ #' task$cbind(data.frame(foo = 1:344)) #' head(task) Task = R6Class("Task", + inherit = Mlr3Component, public = list( - #' @template field_label - label = NA_character_, - #' @template field_task_type task_type = NULL, @@ -100,9 +98,6 @@ Task = R6Class("Task", #' in this table. col_info = NULL, - #' @template field_man - man = NA_character_, - #' @field extra_args (named `list()`)\cr #' Additional arguments set during construction. #' Required for [convert_task()]. @@ -116,9 +111,16 @@ Task = R6Class("Task", #' Creates a new instance of this [R6][R6::R6Class] class. #' #' Note that this object is typically constructed via a derived classes, e.g. [TaskClassif] or [TaskRegr]. - initialize = function(id, task_type, backend, label = NA_character_, extra_args = list()) { - private$.id = assert_string(id, min.chars = 1L) - self$label = assert_string(label, na.ok = TRUE) + initialize = function(id, task_type, backend, extra_args = list(), label, man) { + + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Task construction and will be removed in the future.") + } + + super$initialize(dict_entry = id, dict_shortaccess = "tsk", + param_set = ps(), packages = character(0), properties = character(0), + representable = FALSE) + self$task_type = assert_choice(task_type, mlr_reflections$task_types$type) if (!inherits(backend, "DataBackend")) { self$backend = as_data_backend(backend) @@ -198,19 +200,6 @@ Task = R6Class("Task", invisible(self) }, - #' @description - #' Opens the corresponding help page referenced by field `$man`. - help = function() { - open_help(self$man) - }, - - #' @description - #' Helper for print outputs. - #' @param ... (ignored). - format = function(...) { - sprintf("<%s:%s>", class(self)[1L], self$id) - }, - #' @description #' Printer. #' @param ... (ignored). @@ -890,16 +879,6 @@ Task = R6Class("Task", ), active = list( - #' @template field_id - id = function(rhs) { - if (missing(rhs)) { - return(private$.id) - } - - private$.hash = NULL - private$.id = assert_string(rhs, min.chars = 1L) - }, - #' @field internal_valid_task (`Task` or `integer()` or `NULL`)\cr #' Optional validation task that can, e.g., be used for early stopping with learners such as XGBoost. #' See also the `$validate` field of [`Learner`]. @@ -915,7 +894,6 @@ Task = R6Class("Task", private$.internal_valid_task = NULL return(invisible(private$.internal_valid_task)) } - private$.hash = NULL if (test_integerish(rhs)) { train_ids = setdiff(self$row_ids, rhs) @@ -950,18 +928,6 @@ Task = R6Class("Task", invisible(private$.internal_valid_task) }, - #' @field hash (`character(1)`)\cr - #' Hash (unique identifier) for this object. - #' The hash is calculated based on the complete task object and `$row_ids`. - #' If an internal validation task is set, the hash is recalculated. - hash = function(rhs) { - if (is.null(private$.hash)) { - private$.hash = task_hash(self, self$row_ids, ignore_internal_valid_task = FALSE) - } - - private$.hash - }, - #' @field row_hash (`character(1)`)\cr #' Hash (unique identifier) calculated based on the row ids. row_hash = function(rhs) { @@ -1329,19 +1295,30 @@ Task = R6Class("Task", row_ids_backend = function(rhs) { assert_ro_binding(rhs) self$backend$rownames + }, + + hash = function(rhs) { + if (!missing(rhs)) { + stop("'hash' is read-only") + } + task_hash(self, self$row_ids, ignore_internal_valid_task = FALSE) } ), private = list( .internal_valid_task = NULL, - .id = NULL, - .properties = NULL, .col_roles = NULL, .row_roles = NULL, - .hash = NULL, .col_hashes = NULL, .characteristics = NULL, .row_hash = NULL, + .hash = NULL, # TODO: not used any more? + + .additional_phash_input = function() { + list(self$backend$hash, self$col_info, self$row_ids, self$col_roles, + private$.properties, self$internal_valid_task$hash, self$characteristics + ) + }, deep_clone = function(name, value) { # NB: DataBackends are never copied! @@ -1350,7 +1327,7 @@ Task = R6Class("Task", } else if (name == ".internal_valid_task" && !is.null(value)) { value$clone(deep = TRUE) } else { - value + super$deep_clone(name, value) } } ) diff --git a/R/TaskClassif.R b/R/TaskClassif.R index 674b8934d..181301c47 100644 --- a/R/TaskClassif.R +++ b/R/TaskClassif.R @@ -46,11 +46,15 @@ TaskClassif = R6Class("TaskClassif", #' positive class, and the second element is the negative class. #' @template param_label #' @template param_extra_args - initialize = function(id, backend, target, positive = NULL, label = NA_character_, extra_args = list()) { + initialize = function(id, backend, target, positive = NULL, label, man, extra_args = list()) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Task construction and will be removed in the future.") + } + assert_string(target) super$initialize( id = id, task_type = "classif", backend = backend, - target = target, label = label, extra_args = extra_args) + target = target, extra_args = extra_args) update_classif_property(self, private) diff --git a/R/TaskClassif_breast_cancer.R b/R/TaskClassif_breast_cancer.R index b561b9da8..22e37f73d 100644 --- a/R/TaskClassif_breast_cancer.R +++ b/R/TaskClassif_breast_cancer.R @@ -27,9 +27,8 @@ load_task_breast_cancer = function(id = "breast_cancer") { } b = as_data_backend(remove_named(tab[stats::complete.cases(tab), ], "id")) - task = TaskClassif$new(id, b, target = "class", positive = "malignant", - label = "Wisconsin Breast Cancer") - b$hash = task$man = "mlr3::mlr_tasks_breast_cancer" + task = TaskClassif$new(id, b, target = "class", positive = "malignant") + task$override_info(man = "mlr3::mlr_tasks_breast_cancer", hash = "mlr3::mlr_tasks_breast_cancer") task } diff --git a/R/TaskClassif_german_credit.R b/R/TaskClassif_german_credit.R index 69da9a091..549befa0d 100644 --- a/R/TaskClassif_german_credit.R +++ b/R/TaskClassif_german_credit.R @@ -42,9 +42,8 @@ NULL load_task_german_credit = function(id = "german_credit") { b = as_data_backend(readRDS(system.file("extdata", "german_credit.rds", package = "mlr3"))) - task = TaskClassif$new(id, b, target = "credit_risk", positive = "good", - label = "German Credit") - b$hash = task$man = "mlr3::mlr_tasks_german_credit" + task = TaskClassif$new(id, b, target = "credit_risk", positive = "good") + task$override_info(man = "mlr3::mlr_tasks_german_credit", hash = "mlr3::mlr_tasks_german_credit") task } diff --git a/R/TaskClassif_iris.R b/R/TaskClassif_iris.R index 526b07e43..1dd93544a 100644 --- a/R/TaskClassif_iris.R +++ b/R/TaskClassif_iris.R @@ -18,10 +18,10 @@ #' @template seealso_task NULL -load_task_iris = function(id = "iris") { +load_task_iris = function() { b = as_data_backend(load_dataset("iris", "datasets")) - task = TaskClassif$new(id, b, target = "Species", label = "Iris Flowers") - b$hash = task$man = "mlr3::mlr_tasks_iris" + task = TaskClassif$new(id = "iris", b, target = "Species") + task$override_info(man = "mlr3::mlr_tasks_iris", hash = "mlr3::mlr_tasks_iris") task } diff --git a/R/TaskClassif_penguins.R b/R/TaskClassif_penguins.R index aadff48a3..b8692356c 100644 --- a/R/TaskClassif_penguins.R +++ b/R/TaskClassif_penguins.R @@ -33,9 +33,8 @@ load_task_penguins = function() { ) b = as_data_backend(penguins) - task = TaskClassif$new("penguins", b, target = "species", - label = "Palmer Penguins") - b$hash = task$man = "mlr3::mlr_tasks_penguins" + task = TaskClassif$new("penguins", b, target = "species") + task$override_info(man = "mlr3::mlr_tasks_penguins", hash = "mlr3::mlr_tasks_penguins") task } diff --git a/R/TaskClassif_pima.R b/R/TaskClassif_pima.R index b1a4955ef..cdbbc8057 100644 --- a/R/TaskClassif_pima.R +++ b/R/TaskClassif_pima.R @@ -16,9 +16,8 @@ NULL load_task_pima = function(id = "pima") { b = as_data_backend(load_dataset("PimaIndiansDiabetes2", "mlbench")) - task = TaskClassif$new(id, b, target = "diabetes", positive = "pos", - label = "Pima Indian Diabetes") - b$hash = task$man = "mlr3::mlr_tasks_pima" + task = TaskClassif$new(id, b, target = "diabetes", positive = "pos") + task$override_info(man = "mlr3::mlr_tasks_pima", hash = "mlr3::mlr_tasks_pima") task } diff --git a/R/TaskClassif_sonar.R b/R/TaskClassif_sonar.R index bf4baf142..e3fef97f6 100644 --- a/R/TaskClassif_sonar.R +++ b/R/TaskClassif_sonar.R @@ -16,9 +16,8 @@ NULL load_task_sonar = function(id = "sonar") { b = as_data_backend(load_dataset("Sonar", "mlbench")) - task = TaskClassif$new(id, b, target = "Class", positive = "M", - label = "Sonar: Mines vs. Rocks") - b$hash = task$man = "mlr3::mlr_tasks_sonar" + task = TaskClassif$new(id, b, target = "Class", positive = "M") + task$override_info(man = "mlr3::mlr_tasks_sonar", hash = "mlr3::mlr_tasks_sonar") task } diff --git a/R/TaskClassif_spam.R b/R/TaskClassif_spam.R index 83424f79d..e5fd2af27 100644 --- a/R/TaskClassif_spam.R +++ b/R/TaskClassif_spam.R @@ -32,9 +32,8 @@ NULL load_task_spam = function(id = "spam") { b = as_data_backend(readRDS(system.file("extdata", "spam.rds", package = "mlr3"))) - task = TaskClassif$new(id, b, target = "type", positive = "spam", - label = "HP Spam Detection") - b$hash = task$man = "mlr3::mlr_tasks_spam" + task = TaskClassif$new(id, b, target = "type", positive = "spam") + task$override_info(man = "mlr3::mlr_tasks_spam", hash = "mlr3::mlr_tasks_spam") task } diff --git a/R/TaskClassif_wine.R b/R/TaskClassif_wine.R index 21c4a7cba..8f4faceaf 100644 --- a/R/TaskClassif_wine.R +++ b/R/TaskClassif_wine.R @@ -27,9 +27,8 @@ NULL load_task_wine = function(id = "wine") { b = as_data_backend(readRDS(system.file("extdata", "wine.rds", package = "mlr3"))) - task = TaskClassif$new(id, b, target = "type", - label = "Wine Regions") - b$hash = task$man = "mlr3::mlr_tasks_wine" + task = TaskClassif$new(id, b, target = "type") + task$override_info(man = "mlr3::mlr_tasks_wine", hash = "mlr3::mlr_tasks_wine") task } diff --git a/R/TaskClassif_zoo.R b/R/TaskClassif_zoo.R index bc5596f2b..e95b10e33 100644 --- a/R/TaskClassif_zoo.R +++ b/R/TaskClassif_zoo.R @@ -16,10 +16,10 @@ NULL load_task_zoo = function(id = "zoo") { b = as_data_backend(load_dataset("Zoo", "mlbench", keep_rownames = TRUE), keep_rownames = "animal") - task = TaskClassif$new(id, b, target = "type", label = "Zoo Animals") - b$hash = task$man = "mlr3::mlr_tasks_zoo" + task = TaskClassif$new(id, b, target = "type") task$col_roles$name = "animal" task$col_roles$feature = setdiff(task$col_roles$feature, "animal") + task$override_info(man = "mlr3::mlr_tasks_zoo", hash = "mlr3::mlr_tasks_zoo") task } diff --git a/R/TaskGenerator.R b/R/TaskGenerator.R index b66347ccf..945d854e7 100644 --- a/R/TaskGenerator.R +++ b/R/TaskGenerator.R @@ -17,43 +17,23 @@ #' @template seealso_task_generator #' @export TaskGenerator = R6Class("TaskGenerator", + inherit = Mlr3Component, public = list( - #' @template field_id - id = NULL, - - #' @template field_label - label = NULL, - #' @template field_task_type task_type = NULL, - #' @template field_param_set - param_set = NULL, - - #' @template field_packages - packages = NULL, - - #' @template field_man - man = NULL, - #' @description #' Creates a new instance of this [R6][R6::R6Class] class. - initialize = function(id, task_type, packages = character(), param_set = ps(), label = NA_character_, man = NA_character_) { - self$id = assert_string(id, min.chars = 1L) - self$param_set = assert_param_set(param_set) - self$packages = union("mlr3", assert_character(packages, any.missing = FALSE, min.chars = 1L)) - self$task_type = assert_choice(task_type, mlr_reflections$task_types$type) - self$label = assert_string(label, na.ok = TRUE) - self$man = assert_string(man, na.ok = TRUE) + initialize = function(id, task_type, packages = character(0), param_set = ps(), additional_configuration = character(0), label, man) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for TaskGenerator construction and will be removed in the future.") + } - check_packages_installed(packages, msg = sprintf("Package '%%s' required but not installed for TaskGenerator '%s'", id)) - }, + super$initialize(dict_entry = id, dict_shortaccess = "tgen", + param_set = param_set, packages = packages, additional_configuration = additional_configuration + ) - #' @description - #' Helper for print outputs. - #' @param ... (ignored). - format = function(...) { - sprintf("<%s:%s>", class(self)[1L], self$id) + self$task_type = assert_choice(task_type, mlr_reflections$task_types$type) }, #' @description diff --git a/R/TaskGenerator2DNormals.R b/R/TaskGenerator2DNormals.R index 8b17e7719..b2e2c1614 100644 --- a/R/TaskGenerator2DNormals.R +++ b/R/TaskGenerator2DNormals.R @@ -29,8 +29,7 @@ TaskGenerator2DNormals = R6Class("TaskGenerator2DNormals", sd = p_dbl(0L) ) - super$initialize(id = "2dnormals", "classif", "mlbench", ps, - label = "2D Normals Classification", man = "mlr3::mlr_task_generators_2dnormals") + super$initialize(id = "2dnormals", "classif", "mlbench", ps) }, #' @description diff --git a/R/TaskGeneratorCassini.R b/R/TaskGeneratorCassini.R index 7902f4930..d885fcc57 100644 --- a/R/TaskGeneratorCassini.R +++ b/R/TaskGeneratorCassini.R @@ -29,8 +29,7 @@ TaskGeneratorCassini = R6Class("TaskGeneratorCassini", relsize3 = p_int(1L, default = 1L) ) - super$initialize(id = "cassini", "classif", "mlbench", ps, - label = "Cassini Classification", man = "mlr3::mlr_task_generators_cassini") + super$initialize(id = "cassini", "classif", "mlbench", ps) }, #' @description diff --git a/R/TaskGeneratorCircle.R b/R/TaskGeneratorCircle.R index 0081afb42..2581745f2 100644 --- a/R/TaskGeneratorCircle.R +++ b/R/TaskGeneratorCircle.R @@ -28,8 +28,7 @@ TaskGeneratorCircle = R6Class("TaskGeneratorCircle", d = p_int(2L, default = 2L) ) - super$initialize(id = "circle", "classif", "mlbench", ps, - label = "Circle Classification", man = "mlr3::mlr_task_generators_circle") + super$initialize(id = "circle", "classif", "mlbench", ps) }, #' @description diff --git a/R/TaskGeneratorFriedman1.R b/R/TaskGeneratorFriedman1.R index 9e6389372..370665249 100644 --- a/R/TaskGeneratorFriedman1.R +++ b/R/TaskGeneratorFriedman1.R @@ -25,8 +25,7 @@ TaskGeneratorFriedman1 = R6Class("TaskGeneratorFriedman1", sd = p_dbl(0L, default = 1) ) - super$initialize(id = "friedman1", "regr", "mlbench", ps, - label = "Friedman Regression", man = "mlr3::mlr_task_generators_friedman1") + super$initialize(id = "friedman1", "regr", "mlbench", ps) } ), diff --git a/R/TaskGeneratorMoons.R b/R/TaskGeneratorMoons.R index 7a1b7be7a..93dc855fb 100644 --- a/R/TaskGeneratorMoons.R +++ b/R/TaskGeneratorMoons.R @@ -29,8 +29,7 @@ TaskGeneratorMoons = R6Class("TaskGeneratorMoons", ) ps$set_values(sigma = 1) - super$initialize(id = "moons", task_type = "classif", param_set = ps, - label = "Moons Classification", man = "mlr3::mlr_task_generators_moons") + super$initialize(id = "moons", task_type = "classif", param_set = ps) }, #' @description diff --git a/R/TaskGeneratorPeak.R b/R/TaskGeneratorPeak.R index c680fb937..a1a2673ea 100644 --- a/R/TaskGeneratorPeak.R +++ b/R/TaskGeneratorPeak.R @@ -30,9 +30,7 @@ TaskGeneratorPeak = R6Class( id = "peak", "regr", "mlbench", - ps, - label = "Peak Regression", - man = "mlr3::mlr_task_generators_peak" + ps ) } ), diff --git a/R/TaskGeneratorSimplex.R b/R/TaskGeneratorSimplex.R index 0e01a6b7f..521d492e6 100644 --- a/R/TaskGeneratorSimplex.R +++ b/R/TaskGeneratorSimplex.R @@ -33,8 +33,7 @@ TaskGeneratorSimplex = R6Class("TaskGeneratorSimplex", sides = p_int(1L, default = 1L) ) - super$initialize(id = "simplex", "classif", "mlbench", ps, - label = "Simplex Classification", man = "mlr3::mlr_task_generators_simplex") + super$initialize(id = "simplex", "classif", "mlbench", ps) }, #' @description diff --git a/R/TaskGeneratorSmiley.R b/R/TaskGeneratorSmiley.R index 164a8d7d9..86e74a63b 100644 --- a/R/TaskGeneratorSmiley.R +++ b/R/TaskGeneratorSmiley.R @@ -28,8 +28,7 @@ TaskGeneratorSmiley = R6Class("TaskGeneratorSmiley", sd2 = p_dbl(0L) ) - super$initialize(id = "smiley", "classif", "mlbench", ps, - label = "Smiley Classification", man = "mlr3::mlr_task_generators_smiley") + super$initialize(id = "smiley", "classif", "mlbench", ps) }, #' @description diff --git a/R/TaskGeneratorSpirals.R b/R/TaskGeneratorSpirals.R index 38246562c..9be5d6945 100644 --- a/R/TaskGeneratorSpirals.R +++ b/R/TaskGeneratorSpirals.R @@ -28,8 +28,7 @@ TaskGeneratorSpirals = R6Class("TaskGeneratorSpirals", sd = p_dbl(0, default = 0) ) - super$initialize(id = "spirals", "classif", "mlbench", ps, - label = "Spiral Classification", man = "mlr3::mlr_task_generators_spirals") + super$initialize(id = "spirals", "classif", "mlbench", ps) }, #' @description diff --git a/R/TaskGeneratorXor.R b/R/TaskGeneratorXor.R index 9534eb310..af7dc6092 100644 --- a/R/TaskGeneratorXor.R +++ b/R/TaskGeneratorXor.R @@ -27,8 +27,7 @@ TaskGeneratorXor = R6Class("TaskGeneratorXor", d = p_int(1L, default = 1L) ) - super$initialize(id = "xor", "classif", "mlbench", ps, - label = "XOR Classification", man = "mlr3::mlr_task_generators_xor") + super$initialize(id = "xor", "classif", "mlbench", ps) }, #' @description diff --git a/R/TaskRegr.R b/R/TaskRegr.R index de823a250..209afd9e8 100644 --- a/R/TaskRegr.R +++ b/R/TaskRegr.R @@ -32,11 +32,15 @@ TaskRegr = R6Class("TaskRegr", #' @template param_target #' @template param_label #' @template param_extra_args - initialize = function(id, backend, target, label = NA_character_, extra_args = list()) { + initialize = function(id, backend, target, label, man, extra_args = list()) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Task construction and will be removed in the future.") + } + assert_string(target) super$initialize( id = id, task_type = "regr", backend = backend, - target = target, label = label, extra_args = extra_args) + target = target, extra_args = extra_args) type = fget_key(self$col_info, i = target, j = "type", key = "id") if (type %nin% c("integer", "numeric")) { diff --git a/R/TaskRegr_california_housing.R b/R/TaskRegr_california_housing.R index 24caa0319..ffcd09e46 100644 --- a/R/TaskRegr_california_housing.R +++ b/R/TaskRegr_california_housing.R @@ -26,8 +26,8 @@ NULL load_task_california_housing = function(id = "california_housing") { b = as_data_backend(readRDS(system.file("extdata", "california_housing.rds", package = "mlr3"))) - task = mlr3::TaskRegr$new(id, b, target = "median_house_value", label = "California House Value") - b$hash = task$man = "mlr3::mlr_tasks_california_housing" + task = mlr3::TaskRegr$new(id, b, target = "median_house_value") + task$override_info(man = "mlr3::mlr_tasks_california_housing", hash = "mlr3::mlr_tasks_california_housing") task } diff --git a/R/TaskRegr_mtcars.R b/R/TaskRegr_mtcars.R index ed961368f..e2de88ed0 100644 --- a/R/TaskRegr_mtcars.R +++ b/R/TaskRegr_mtcars.R @@ -23,10 +23,10 @@ NULL load_task_mtcars = function(id = "mtcars") { b = as_data_backend(load_dataset("mtcars", "datasets", keep_rownames = TRUE), keep_rownames = "model") - task = TaskRegr$new(id, b, target = "mpg", label = "Motor Trends") - b$hash = task$man = "mlr3::mlr_tasks_mtcars" + task = TaskRegr$new(id, b, target = "mpg") task$col_roles$name = "model" task$col_roles$feature = setdiff(task$col_roles$feature, "model") + task$override_info(man = "mlr3::mlr_tasks_mtcars", hash = "mlr3::mlr_tasks_mtcars") task } diff --git a/R/TaskSupervised.R b/R/TaskSupervised.R index 4a8130745..e5f1d5378 100644 --- a/R/TaskSupervised.R +++ b/R/TaskSupervised.R @@ -29,8 +29,12 @@ TaskSupervised = R6Class("TaskSupervised", inherit = Task, #' #' @param target (`character(1)`)\cr #' Name of the target column. - initialize = function(id, task_type, backend, target, label = NA_character_, extra_args = list()) { - super$initialize(id = id, task_type = task_type, backend = backend, label = label, extra_args = extra_args) + initialize = function(id, task_type, backend, target, extra_args = list(), label, man) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Task construction and will be removed in the future.") + } + + super$initialize(id = id, task_type = task_type, backend = backend, extra_args = extra_args) assert_subset(target, self$col_roles$feature) self$col_roles$target = target self$col_roles$feature = setdiff(self$col_roles$feature, target) diff --git a/R/TaskUnsupervised.R b/R/TaskUnsupervised.R index bbd22b2a0..3ad556acf 100644 --- a/R/TaskUnsupervised.R +++ b/R/TaskUnsupervised.R @@ -21,8 +21,12 @@ TaskUnsupervised = R6Class("TaskUnsupervised", public = list( #' @description #' Creates a new instance of this [R6][R6::R6Class] class. - initialize = function(id, task_type = "unsupervised", backend, label = NA_character_, extra_args = list()) { - super$initialize(id = id, task_type = task_type, backend = backend, label = label, extra_args = extra_args) + initialize = function(id, task_type = "unsupervised", backend, label, man, extra_args = list()) { + if (!missing(label) || !missing(man)) { + mlr3component_deprecation_msg("label and man are deprecated for Task construction and will be removed in the future.") + } + + super$initialize(id = id, task_type = task_type, backend = backend, extra_args = extra_args) } ) ) diff --git a/R/as_task_classif.R b/R/as_task_classif.R index 638d62f6b..39920feae 100644 --- a/R/as_task_classif.R +++ b/R/as_task_classif.R @@ -35,9 +35,13 @@ as_task_classif.TaskClassif = function(x, clone = FALSE, ...) { # nolint #' Level of the positive class. See [TaskClassif]. #' @template param_label #' @export -as_task_classif.data.frame = function(x, target, id = deparse1(substitute(x)), positive = NULL, label = NA_character_, ...) { # nolint +as_task_classif.data.frame = function(x, target, id = deparse1(substitute(x)), positive = NULL, label, ...) { # nolint force(id) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_classif and will be removed in the future.") + } + assert_data_frame(x, min.rows = 1L, min.cols = 1L, col.names = "unique") assert_choice(target, names(x)) @@ -51,28 +55,36 @@ as_task_classif.data.frame = function(x, target, id = deparse1(substitute(x)), p x[[target]] = as.factor(y) } - TaskClassif$new(id = id, backend = x, target = target, positive = positive, label = label) + TaskClassif$new(id = id, backend = x, target = target, positive = positive) } #' @rdname as_task_classif #' @export -as_task_classif.matrix = function(x, target, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint +as_task_classif.matrix = function(x, target, id = deparse1(substitute(x)), label, ...) { # nolint force(id) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_classif and will be removed in the future.") + } + assert_matrix(x, col.names = "unique", min.rows = 1L, min.cols = 1L) assert_choice(target, colnames(x)) - as_task_classif(as.data.table(x), target = target, id = id, label = label, ...) + as_task_classif(as.data.table(x), target = target, id = id, ...) } #' @rdname as_task_classif #' @export -as_task_classif.DataBackend = function(x, target, id = deparse1(substitute(x)), positive = NULL, label = NA_character_, ...) { # nolint +as_task_classif.DataBackend = function(x, target, id = deparse1(substitute(x)), positive = NULL, label, ...) { # nolint force(id) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_classif and will be removed in the future.") + } + assert_choice(target, x$colnames) - TaskClassif$new(id = id, backend = x, target = target, positive = positive, label = label, ...) + TaskClassif$new(id = id, backend = x, target = target, positive = positive, ...) } #' @rdname as_task_classif @@ -86,9 +98,13 @@ as_task_classif.TaskRegr = function(x, target, drop_original_target = FALSE, dro #' @param data (`data.frame()`)\cr #' Data frame containing all columns referenced in formula `x`. #' @export -as_task_classif.formula = function(x, data, id = deparse1(substitute(data)), positive = NULL, label = NA_character_, ...) { # nolint +as_task_classif.formula = function(x, data, id = deparse1(substitute(data)), positive = NULL, label, ...) { # nolint force(id) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_classif and will be removed in the future.") + } + assert_data_frame(data) assert_subset(all.vars(x), c(names(data), "."), .var.name = "formula") @@ -100,5 +116,5 @@ as_task_classif.formula = function(x, data, id = deparse1(substitute(data)), pos setattr(tab, "na.action", NULL) target = all.vars(x)[1L] - as_task_classif(tab, target = target, id = id, positive = positive, label = label, ...) + as_task_classif(tab, target = target, id = id, positive = positive, ...) } diff --git a/R/as_task_regr.R b/R/as_task_regr.R index d6950a404..c4bece071 100644 --- a/R/as_task_regr.R +++ b/R/as_task_regr.R @@ -33,9 +33,13 @@ as_task_regr.TaskRegr = function(x, clone = FALSE, ...) { # nolint #' Defaults to the (deparsed and substituted) name of the data argument. #' @template param_label #' @export -as_task_regr.data.frame = function(x, target, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint +as_task_regr.data.frame = function(x, target, id = deparse1(substitute(x)), label, ...) { # nolint force(id) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_regr and will be removed in the future.") + } + assert_data_frame(x, min.rows = 1L, min.cols = 1L, col.names = "unique") assert_choice(target, names(x)) @@ -44,28 +48,36 @@ as_task_regr.data.frame = function(x, target, id = deparse1(substitute(x)), labe warningf("Detected columns with unsupported Inf values in data: %s", str_collapse(names(ii))) } - TaskRegr$new(id = id, backend = x, target = target, label = label) + TaskRegr$new(id = id, backend = x, target = target) } #' @rdname as_task_regr #' @export -as_task_regr.matrix = function(x, target, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint +as_task_regr.matrix = function(x, target, id = deparse1(substitute(x)), label, ...) { # nolint force(id) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_regr and will be removed in the future.") + } + assert_matrix(x, mode = "numeric") assert_choice(target, colnames(x)) - as_task_regr(as.data.table(x), target = target, id = id, label = label, ...) + as_task_regr(as.data.table(x), target = target, id = id, ...) } #' @rdname as_task_regr #' @export -as_task_regr.DataBackend = function(x, target, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint +as_task_regr.DataBackend = function(x, target, id = deparse1(substitute(x)), label, ...) { # nolint force(id) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_regr and will be removed in the future.") + } + assert_choice(target, x$colnames) - TaskRegr$new(id = id, backend = x, target = target, label = label, ...) + TaskRegr$new(id = id, backend = x, target = target, ...) } #' @rdname as_task_regr @@ -79,9 +91,13 @@ as_task_regr.TaskClassif = function(x, target, drop_original_target = FALSE, dro #' @param data (`data.frame()`)\cr #' Data frame containing all columns referenced in formula `x`. #' @export -as_task_regr.formula = function(x, data, id = deparse1(substitute(data)), label = NA_character_, ...) { # nolint +as_task_regr.formula = function(x, data, id = deparse1(substitute(data)), label, ...) { # nolint force(id) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_regr and will be removed in the future.") + } + assert_data_frame(data) assert_subset(all.vars(x), c(names(data), "."), .var.name = "formula") @@ -93,5 +109,5 @@ as_task_regr.formula = function(x, data, id = deparse1(substitute(data)), label setattr(tab, "na.action", NULL) target = all.vars(x)[1L] - as_task_regr(tab, target = target, id = id, label = label, ...) + as_task_regr(tab, target = target, id = id, ...) } diff --git a/R/as_task_unsupervised.R b/R/as_task_unsupervised.R index d68c4cf13..a325bfd47 100644 --- a/R/as_task_unsupervised.R +++ b/R/as_task_unsupervised.R @@ -22,23 +22,31 @@ as_task_unsupervised.Task = function(x, clone = FALSE, ...) { # nolint #' Defaults to the (deparsed and substituted) name of the data argument. #' @template param_label #' @export -as_task_unsupervised.data.frame = function(x, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint +as_task_unsupervised.data.frame = function(x, id = deparse1(substitute(x)), label, ...) { # nolint force(id) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_unsupervised and will be removed in the future.") + } + ii = which(map_lgl(keep(x, is.double), anyInfinite)) if (length(ii)) { warningf("Detected columns with unsupported Inf values in data: %s", str_collapse(names(ii))) } - TaskUnsupervised$new(id = id, backend = x, label = label) + TaskUnsupervised$new(id = id, backend = x) } #' @rdname as_task_unsupervised #' @export -as_task_unsupervised.DataBackend = function(x, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint + as_task_unsupervised.DataBackend = function(x, id = deparse1(substitute(x)), label, ...) { # nolint force(id) - TaskUnsupervised$new(id = id, backend = x, label = label) + if (!missing(label)) { + mlr3component_deprecation_msg("label is deprecated for as_task_unsupervised and will be removed in the future.") + } + + TaskUnsupervised$new(id = id, backend = x) } #' @rdname as_task_unsupervised diff --git a/R/task_converters.R b/R/task_converters.R index c184f8d4c..8a5431c66 100644 --- a/R/task_converters.R +++ b/R/task_converters.R @@ -29,7 +29,7 @@ convert_task = function(intask, target = NULL, new_type = NULL, drop_original_ta constructor = get(fget_key(mlr_reflections$task_types, new_type, "task", key = "type")[[1L]]) common_args = intersect(names(intask$extra_args), names(formals(constructor$public_methods$initialize))) newtask = invoke(constructor$new, id = intask$id, backend = intask$backend, - target = target, label = intask$label, .args = intask$extra_args[common_args]) + target = target, .args = intask$extra_args[common_args]) newtask$extra_args = intask$extra_args # copy row_roles / col_roles / properties diff --git a/inst/testthat/helper_expectations.R b/inst/testthat/helper_expectations.R index c11909c62..a33ba0932 100644 --- a/inst/testthat/helper_expectations.R +++ b/inst/testthat/helper_expectations.R @@ -3,7 +3,7 @@ expect_man_exists = function(man) { if (!is.na(man)) { parts = strsplit(man, "::", fixed = TRUE)[[1L]] matches = help.search(parts[2L], package = parts[1L], ignore.case = FALSE) - checkmate::expect_data_frame(matches$matches, min.rows = 1L, info = "man page lookup") + checkmate::expect_data_frame(matches$matches, min.rows = 1L, info = sprintf("man page lookup for %s", man)) } } diff --git a/man/Learner.Rd b/man/Learner.Rd index 3794ad0a5..c7fe8ebbe 100644 --- a/man/Learner.Rd +++ b/man/Learner.Rd @@ -211,14 +211,6 @@ learner$reset() learner = lrn("classif.rpart") fallback = lrn("classif.featureless") learner$encapsulate("try", fallback = fallback) - -## ------------------------------------------------ -## Method `Learner$configure` -## ------------------------------------------------ - -learner = lrn("classif.rpart") -learner$configure(minsplit = 3, parallel_predict = FALSE) -learner$configure(.values = list(cp = 0.005)) } \seealso{ \itemize{ @@ -251,17 +243,12 @@ Other Learner: \code{\link{mlr_learners_regr.rpart}} } \concept{Learner} +\section{Super class}{ +\code{\link[mlr3misc:Mlr3Component]{mlr3misc::Mlr3Component}} -> \code{Learner} +} \section{Public fields}{ \if{html}{\out{
}} \describe{ -\item{\code{id}}{(\code{character(1)})\cr -Identifier of the object. -Used in tables, plot and text output.} - -\item{\code{label}}{(\code{character(1)})\cr -Label for this object. -Can be used in tables, plot and text output instead of the ID.} - \item{\code{state}}{(\code{NULL} | named \code{list()})\cr Current (internal) state of the learner. Contains all information gathered during \code{train()} and \code{predict()}. @@ -278,14 +265,6 @@ see \code{\link[=mlr_reflections]{mlr_reflections$task_types$type}}.} Stores the feature types the learner can handle, e.g. \code{"logical"}, \code{"numeric"}, or \code{"factor"}. A complete list of candidate feature types, grouped by task type, is stored in \code{\link[=mlr_reflections]{mlr_reflections$task_feature_types}}.} -\item{\code{properties}}{(\code{character()})\cr -Stores a set of properties/capabilities the learner has. -A complete list of candidate properties, grouped by task type, is stored in \code{\link[=mlr_reflections]{mlr_reflections$learner_properties}}.} - -\item{\code{packages}}{(\code{character(1)})\cr -Set of required packages. -These packages are loaded, but not attached.} - \item{\code{predict_sets}}{(\code{character()})\cr During \code{\link[=resample]{resample()}}/\code{\link[=benchmark]{benchmark()}}, a \link{Learner} can predict on multiple sets. Per default, a learner only predicts observations in the test set (\code{predict_sets == "test"}). @@ -315,10 +294,6 @@ This works differently for different encapsulation methods, see Default is \code{c(train = Inf, predict = Inf)}. Also see the section on error handling the mlr3book: \url{https://mlr3book.mlr-org.com/chapters/chapter10/advanced_technical_aspects_of_mlr3.html#sec-error-handling}} - -\item{\code{man}}{(\code{character(1)})\cr -String in the format \verb{[pkg]::[topic]} pointing to a manual page for this object. -Defaults to \code{NA}, but can be set by child classes.} } \if{html}{\out{
}} } @@ -367,22 +342,12 @@ Logged warnings as vector.} \item{\code{errors}}{(\code{character()})\cr Logged errors as vector.} -\item{\code{hash}}{(\code{character(1)})\cr -Hash (unique identifier) for this object. -The hash is calculated based on the learner id, the parameter settings, the predict type, the fallback hash, the parallel predict setting, the validate setting, and the predict sets.} - -\item{\code{phash}}{(\code{character(1)})\cr -Hash (unique identifier) for this partial object, excluding some components which are varied systematically during tuning (parameter values).} - \item{\code{predict_type}}{(\code{character(1)})\cr Stores the currently active predict type, e.g. \code{"response"}. Must be an element of \verb{$predict_types}. A few learners already use the predict type during training. So there is no guarantee that changing the predict type after training will have any effect or does not lead to errors.} -\item{\code{param_set}}{(\link[paradox:ParamSet]{paradox::ParamSet})\cr -Set of hyperparameters.} - \item{\code{fallback}}{(\link{Learner})\cr Returns the fallback learner set with \verb{$encapsulate()}.} @@ -408,20 +373,28 @@ This field is read-only.} \subsection{Public methods}{ \itemize{ \item \href{#method-Learner-new}{\code{Learner$new()}} -\item \href{#method-Learner-format}{\code{Learner$format()}} \item \href{#method-Learner-print}{\code{Learner$print()}} -\item \href{#method-Learner-help}{\code{Learner$help()}} \item \href{#method-Learner-train}{\code{Learner$train()}} \item \href{#method-Learner-predict}{\code{Learner$predict()}} \item \href{#method-Learner-predict_newdata}{\code{Learner$predict_newdata()}} \item \href{#method-Learner-reset}{\code{Learner$reset()}} \item \href{#method-Learner-base_learner}{\code{Learner$base_learner()}} \item \href{#method-Learner-encapsulate}{\code{Learner$encapsulate()}} -\item \href{#method-Learner-configure}{\code{Learner$configure()}} \item \href{#method-Learner-selected_features}{\code{Learner$selected_features()}} \item \href{#method-Learner-clone}{\code{Learner$clone()}} } } +\if{html}{\out{ +
Inherited methods + +
+}} \if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-Learner-new}{}}} @@ -434,12 +407,12 @@ Note that this object is typically constructed via a derived classes, e.g. \link id, task_type, param_set = ps(), - predict_types = character(), - feature_types = character(), - properties = character(), - packages = character(), - label = NA_character_, - man = NA_character_ + predict_types = character(0), + feature_types = character(0), + properties = character(0), + packages = character(0), + label, + man )}\if{html}{\out{}} } @@ -498,23 +471,6 @@ The referenced help package can be opened via method \verb{$help()}.} } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-Learner-format}{}}} -\subsection{Method \code{format()}}{ -Helper for print outputs. -\subsection{Usage}{ -\if{html}{\out{
}}\preformatted{Learner$format(...)}\if{html}{\out{
}} -} - -\subsection{Arguments}{ -\if{html}{\out{
}} -\describe{ -\item{\code{...}}{(ignored).} -} -\if{html}{\out{
}} -} -} -\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-Learner-print}{}}} \subsection{Method \code{print()}}{ @@ -530,16 +486,6 @@ Printer. } \if{html}{\out{}} } -} -\if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-Learner-help}{}}} -\subsection{Method \code{help()}}{ -Opens the corresponding help page referenced by field \verb{$man}. -\subsection{Usage}{ -\if{html}{\out{
}}\preformatted{Learner$help()}\if{html}{\out{
}} -} - } \if{html}{\out{
}} \if{html}{\out{}} @@ -782,39 +728,6 @@ learner$encapsulate("try", fallback = fallback) } -} -\if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-Learner-configure}{}}} -\subsection{Method \code{configure()}}{ -Sets parameter values and fields of the learner. -All arguments whose names match the name of a parameter of the \link[paradox:ParamSet]{paradox::ParamSet} are set as parameters. -All remaining arguments are assumed to be regular fields. -\subsection{Usage}{ -\if{html}{\out{
}}\preformatted{Learner$configure(..., .values = list())}\if{html}{\out{
}} -} - -\subsection{Arguments}{ -\if{html}{\out{
}} -\describe{ -\item{\code{...}}{(named \code{any})\cr -Named arguments to set parameter values and fields.} - -\item{\code{.values}}{(named \code{any})\cr -Named list of parameter values and fields.} -} -\if{html}{\out{
}} -} -\subsection{Examples}{ -\if{html}{\out{
}} -\preformatted{learner = lrn("classif.rpart") -learner$configure(minsplit = 3, parallel_predict = FALSE) -learner$configure(.values = list(cp = 0.005)) -} -\if{html}{\out{
}} - -} - } \if{html}{\out{
}} \if{html}{\out{}} diff --git a/man/LearnerClassif.Rd b/man/LearnerClassif.Rd index f107621e6..55f500c31 100644 --- a/man/LearnerClassif.Rd +++ b/man/LearnerClassif.Rd @@ -71,8 +71,8 @@ Other Learner: \code{\link{mlr_learners_regr.rpart}} } \concept{Learner} -\section{Super class}{ -\code{\link[mlr3:Learner]{mlr3::Learner}} -> \code{LearnerClassif} +\section{Super classes}{ +\code{\link[mlr3misc:Mlr3Component]{mlr3misc::Mlr3Component}} -> \code{\link[mlr3:Learner]{mlr3::Learner}} -> \code{LearnerClassif} } \section{Methods}{ \subsection{Public methods}{ @@ -85,11 +85,13 @@ Other Learner: \if{html}{\out{
Inherited methods