Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R] Gracefully handle deprecated parameters #10563

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions R-package/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ NVL <- function(x, val) {

# Merges booster params with whatever is provided in ...
# plus runs some checks
check.booster.params <- function(params, ...) {
# Note: deprecated params in deprecated_params will be excluded from the
# final list of merged params
check.booster.params <- function(params, deprecated_params, ...) {
if (!identical(class(params), "list"))
stop("params must be a list")

Expand All @@ -50,6 +52,7 @@ check.booster.params <- function(params, ...) {
if (length(intersect(names(params),
names(dot_params))) > 0)
stop("Same parameters in 'params' and in the call are not allowed. Please check your 'params' list.")
dot_params <- within(dot_params, rm(list = deprecated_params)) # Exclude deprecated params
params <- c(params, dot_params)

# providing a parameter multiple times makes sense only for 'eval_metric'
Expand Down Expand Up @@ -499,6 +502,7 @@ colnames(depr_par_lut) <- c('old', 'new')
# Checks the dot-parameters for deprecated names
# (including partial matching), gives a deprecation warning,
# and sets new parameters to the old parameters' values within its parent frame.
# Also returns the list of old parameters that were passed in.
# WARNING: has side-effects
check.deprecation <- function(..., env = parent.frame()) {
pars <- list(...)
Expand All @@ -511,6 +515,8 @@ check.deprecation <- function(..., env = parent.frame()) {
idx_lut <- all_match[idx_pars]
# which of idx_lut were the exact matches?
ex_match <- depr_par_lut[idx_lut, 1] %in% names(pars)
# List of deprecated parameters, to be returned
ret_deprecated_pars <- character(length(idx_pars))
for (i in seq_along(idx_pars)) {
pars_par <- names(pars)[idx_pars[i]]
old_par <- depr_par_lut[idx_lut[i], 1]
Expand All @@ -519,8 +525,10 @@ check.deprecation <- function(..., env = parent.frame()) {
warning("'", pars_par, "' was partially matched to '", old_par, "'")
}
.Deprecated(new_par, old = old_par, package = 'xgboost')
ret_deprecated_pars[i] <- old_par
if (new_par != 'NULL') {
eval(parse(text = paste(new_par, '<-', pars[[pars_par]])), envir = env)
assign(new_par, pars[[pars_par]], envir = env)
}
}
return(ret_deprecated_pars)
}
4 changes: 2 additions & 2 deletions R-package/R/xgb.cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ xgb.cv <- function(params = list(), data, nrounds, nfold,
verbose = TRUE, print_every_n = 1L,
early_stopping_rounds = NULL, maximize = NULL, callbacks = list(), ...) {

check.deprecation(...)
deprecated_params <- check.deprecation(...)
stopifnot(inherits(data, "xgb.DMatrix"))
if (inherits(data, "xgb.DMatrix") && .Call(XGCheckNullPtr_R, data)) {
stop("'data' is an invalid 'xgb.DMatrix' object. Must be constructed again.")
}

params <- check.booster.params(params, ...)
params <- check.booster.params(params, deprecated_params, ...)
# TODO: should we deprecate the redundant 'metrics' parameter?
for (m in metrics)
params <- c(params, list("eval_metric" = m))
Expand Down
4 changes: 2 additions & 2 deletions R-package/R/xgb.train.R
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ xgb.train <- function(params = list(), data, nrounds, evals = list(),
save_period = NULL, save_name = "xgboost.model",
xgb_model = NULL, callbacks = list(), ...) {

check.deprecation(...)
deprecated_params <- check.deprecation(...)

params <- check.booster.params(params, ...)
params <- check.booster.params(params, deprecated_params, ...)

check.custom.obj()
check.custom.eval()
Expand Down
3 changes: 2 additions & 1 deletion R-package/R/xgboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ xgboost <- function(data = NULL, label = NULL, missing = NA, weight = NULL,
early_stopping_rounds = NULL, maximize = NULL,
save_period = NULL, save_name = "xgboost.model",
xgb_model = NULL, callbacks = list(), ...) {
merged <- check.booster.params(params, ...)
deprecated_params <- check.deprecation(...)
merged <- check.booster.params(params, deprecated_params, ...)
dtrain <- xgb.get.DMatrix(
data = data,
label = label,
Expand Down
Loading