Skip to content

Commit

Permalink
Merge pull request #277 from tidymodels/more-error-calls
Browse files Browse the repository at this point in the history
Sending the call to `check_data_frame_or_matrix()`
  • Loading branch information
hfrick authored Jan 23, 2025
2 parents b1fe750 + 5e47856 commit 7aaa32f
Show file tree
Hide file tree
Showing 22 changed files with 195 additions and 93 deletions.
37 changes: 22 additions & 15 deletions R/blueprint-formula-default.R
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ mold_formula_default_process_predictors <- function(blueprint, data, ..., call =
original_names <- get_all_predictors(formula, data, call = call)
data <- data[original_names]

ptype <- extract_ptype(data)
ptype <- extract_ptype(data, call = call)

if (identical(blueprint$indicators, "traditional") ||
identical(blueprint$indicators, "one_hot")) {
Expand Down Expand Up @@ -566,18 +566,20 @@ mold_formula_default_process_predictors <- function(blueprint, data, ..., call =
data <- mask_factorish_in_data(data, factorish_names)
}

framed <- model_frame(formula, data)
framed <- model_frame(formula, data, call = call)
offset <- extract_offset(framed$terms, framed$data, call = call)

if (identical(blueprint$indicators, "one_hot")) {
predictors <- model_matrix_one_hot(
terms = framed$terms,
data = framed$data
data = framed$data,
call = call
)
} else {
predictors <- model_matrix(
terms = framed$terms,
data = framed$data
data = framed$data,
call = call
)
}

Expand Down Expand Up @@ -609,14 +611,14 @@ mold_formula_default_process_outcomes <- function(blueprint, data, ..., call = c
original_names <- get_all_outcomes(formula, data, call = call)
data <- data[original_names]

ptype <- extract_ptype(data)
ptype <- extract_ptype(data, call = call)

formula <- get_outcomes_formula(formula)

# used on the `~ LHS` formula
check_no_interactions(formula, error_call = call)

framed <- model_frame(formula, data)
framed <- model_frame(formula, data, call = call)

outcomes <- flatten_embedded_columns(framed$data)

Expand Down Expand Up @@ -670,7 +672,7 @@ run_forge.default_formula_blueprint <- function(blueprint,

forge_formula_default_clean <- function(blueprint, new_data, outcomes, ..., call = caller_env()) {
check_dots_empty0(...)
check_data_frame_or_matrix(new_data)
check_data_frame_or_matrix(new_data, call = call)
new_data <- coerce_to_tibble(new_data)
check_unique_column_names(new_data)
check_bool(outcomes)
Expand All @@ -691,13 +693,14 @@ forge_formula_default_clean <- function(blueprint, new_data, outcomes, ..., call
predictors <- scream(
predictors,
predictors_ptype,
allow_novel_levels = blueprint$allow_novel_levels
allow_novel_levels = blueprint$allow_novel_levels,
call = call
)

if (outcomes) {
outcomes <- shrink(new_data, blueprint$ptypes$outcomes, call = call)
# Never allow novel levels for outcomes
outcomes <- scream(outcomes, blueprint$ptypes$outcomes)
outcomes <- scream(outcomes, blueprint$ptypes$outcomes, call = call)
} else {
outcomes <- NULL
}
Expand All @@ -722,7 +725,8 @@ forge_formula_default_process <- function(blueprint, predictors, outcomes, extra

processed <- forge_formula_default_process_outcomes(
blueprint = blueprint,
outcomes = outcomes
outcomes = outcomes,
call = call
)

blueprint <- processed$blueprint
Expand All @@ -749,17 +753,19 @@ forge_formula_default_process_predictors <- function(blueprint, predictors, ...,
predictors <- mask_factorish_in_data(predictors, factorish_names)
}

framed <- model_frame(terms, predictors)
framed <- model_frame(terms, predictors, call = call)

if (identical(blueprint$indicators, "one_hot")) {
data <- model_matrix_one_hot(
terms = framed$terms,
data = framed$data
data = framed$data,
call = call
)
} else {
data <- model_matrix(
terms = framed$terms,
data = framed$data
data = framed$data,
call = call
)
}

Expand All @@ -780,7 +786,8 @@ forge_formula_default_process_predictors <- function(blueprint, predictors, ...,
)
}

forge_formula_default_process_outcomes <- function(blueprint, outcomes) {
forge_formula_default_process_outcomes <- function(blueprint, outcomes, ..., call = caller_env()) {
check_dots_empty0(...)

# no outcomes to process
if (is.null(outcomes)) {
Expand All @@ -794,7 +801,7 @@ forge_formula_default_process_outcomes <- function(blueprint, outcomes) {
terms <- blueprint$terms$outcomes
terms <- alter_terms_environment(terms)

framed <- model_frame(terms, outcomes)
framed <- model_frame(terms, outcomes, call = call)

# Because model.matrix() does this for the RHS and we want
# to be consistent even though we are only going through
Expand Down
43 changes: 26 additions & 17 deletions R/blueprint-recipe-default.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ mold_recipe_default_process <- function(blueprint, data, ..., call = caller_env(
predictors_ptype <- processed$ptype
predictors_extras <- processed$extras

processed <- mold_recipe_default_process_outcomes(blueprint = blueprint, data = data)
processed <- mold_recipe_default_process_outcomes(blueprint = blueprint, data = data, call = call)

blueprint <- processed$blueprint
outcomes <- processed$data
outcomes_ptype <- processed$ptype
outcomes_extras <- processed$extras

processed <- mold_recipe_default_process_extras(blueprint, data)
processed <- mold_recipe_default_process_extras(blueprint, data, call = call)

blueprint <- processed$blueprint
extras <- processed$extras
Expand All @@ -277,11 +277,11 @@ mold_recipe_default_process_predictors <- function(blueprint, data, ..., call =

predictors <- recipes::juice(blueprint$recipe, all_predictors())

predictors <- maybe_add_intercept_column(predictors, blueprint$intercept)
predictors <- maybe_add_intercept_column(predictors, blueprint$intercept, call = call)

predictors <- recompose(predictors, composition = blueprint$composition, call = call)

ptype <- get_original_predictor_ptype(blueprint$recipe, data)
ptype <- get_original_predictor_ptype(blueprint$recipe, data, call = call)

new_mold_process_terms(
blueprint = blueprint,
Expand All @@ -290,12 +290,14 @@ mold_recipe_default_process_predictors <- function(blueprint, data, ..., call =
)
}

mold_recipe_default_process_outcomes <- function(blueprint, data) {
mold_recipe_default_process_outcomes <- function(blueprint, data, ..., call = caller_env()) {
check_dots_empty0(...)

all_outcomes <- recipes::all_outcomes

outcomes <- recipes::juice(blueprint$recipe, all_outcomes())

ptype <- get_original_outcome_ptype(blueprint$recipe, data)
ptype <- get_original_outcome_ptype(blueprint$recipe, data, call = call)

new_mold_process_terms(
blueprint = blueprint,
Expand All @@ -304,7 +306,8 @@ mold_recipe_default_process_outcomes <- function(blueprint, data) {
)
}

mold_recipe_default_process_extras <- function(blueprint, data) {
mold_recipe_default_process_extras <- function(blueprint, data, ..., call = caller_env()) {
check_dots_empty0(...)

# Capture original non standard role columns that exist in `data` and are also
# required by the `recipe$requirements$bake` requirement. These columns are
Expand All @@ -315,7 +318,7 @@ mold_recipe_default_process_extras <- function(blueprint, data) {
)

if (!is.null(original_extra_role_cols)) {
original_extra_role_ptypes <- lapply(original_extra_role_cols, extract_ptype)
original_extra_role_ptypes <- lapply(original_extra_role_cols, extract_ptype, call = call)

blueprint <- update_blueprint0(
blueprint,
Expand Down Expand Up @@ -376,7 +379,7 @@ run_forge.default_recipe_blueprint <- function(blueprint,

forge_recipe_default_clean <- function(blueprint, new_data, outcomes, ..., call = caller_env()) {
check_dots_empty0(...)
check_data_frame_or_matrix(new_data)
check_data_frame_or_matrix(new_data, call = call)
new_data <- coerce_to_tibble(new_data)
check_unique_column_names(new_data)
check_bool(outcomes)
Expand All @@ -386,13 +389,14 @@ forge_recipe_default_clean <- function(blueprint, new_data, outcomes, ..., call
predictors <- scream(
predictors,
blueprint$ptypes$predictors,
allow_novel_levels = blueprint$allow_novel_levels
allow_novel_levels = blueprint$allow_novel_levels,
call = call
)

if (outcomes) {
outcomes <- shrink(new_data, blueprint$ptypes$outcomes, call = call)
# Never allow novel levels for outcomes
outcomes <- scream(outcomes, blueprint$ptypes$outcomes)
outcomes <- scream(outcomes, blueprint$ptypes$outcomes, call = call)
} else {
outcomes <- NULL
}
Expand Down Expand Up @@ -420,7 +424,8 @@ forge_recipe_default_clean_extras <- function(blueprint, new_data, ..., call = c
extra_role_cols,
blueprint$extra_role_ptypes,
scream,
allow_novel_levels = blueprint$allow_novel_levels
allow_novel_levels = blueprint$allow_novel_levels,
call = call
)

extras <- list(roles = extra_role_cols)
Expand Down Expand Up @@ -500,7 +505,7 @@ forge_recipe_default_process <- function(blueprint, predictors, outcomes, extras
forge_recipe_default_process_predictors <- function(blueprint, predictors, ..., call = caller_env()) {
check_dots_empty0(...)

predictors <- maybe_add_intercept_column(predictors, blueprint$intercept)
predictors <- maybe_add_intercept_column(predictors, blueprint$intercept, call = call)

predictors <- recompose(predictors, composition = blueprint$composition, call = call)

Expand Down Expand Up @@ -553,7 +558,9 @@ forge_recipe_default_process_extras <- function(extras,

# ------------------------------------------------------------------------------

get_original_predictor_ptype <- function(rec, data) {
get_original_predictor_ptype <- function(rec, data, ..., call = caller_env()) {
check_dots_empty0(...)

roles <- rec$var_info$role
roles <- chr_explicit_na(roles)

Expand All @@ -562,18 +569,20 @@ get_original_predictor_ptype <- function(rec, data) {

data <- data[original_names]

extract_ptype(data)
extract_ptype(data, call = call)
}

get_original_outcome_ptype <- function(rec, data) {
get_original_outcome_ptype <- function(rec, data, ..., call = caller_env()) {
check_dots_empty0(...)

roles <- rec$var_info$role
roles <- chr_explicit_na(roles)

original_names <- rec$var_info$variable[roles == "outcome"]

data <- data[original_names]

extract_ptype(data)
extract_ptype(data, call = call)
}

get_extra_role_columns_original <- function(rec, data) {
Expand Down
34 changes: 20 additions & 14 deletions R/blueprint-xy-default.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ refresh_blueprint.default_xy_blueprint <- function(blueprint) {
run_mold.default_xy_blueprint <- function(blueprint, ..., x, y, call = caller_env()) {
check_dots_empty0(...)

cleaned <- mold_xy_default_clean(blueprint = blueprint, x = x, y = y)
cleaned <- mold_xy_default_clean(blueprint = blueprint, x = x, y = y, call = call)

blueprint <- cleaned$blueprint
x <- cleaned$x
Expand All @@ -192,8 +192,10 @@ run_mold.default_xy_blueprint <- function(blueprint, ..., x, y, call = caller_en
# ------------------------------------------------------------------------------
# mold - xy - clean

mold_xy_default_clean <- function(blueprint, x, y) {
cleaned <- mold_xy_default_clean_predictors(blueprint, x)
mold_xy_default_clean <- function(blueprint, x, y, ..., call = caller_env()) {
check_dots_empty0(...)

cleaned <- mold_xy_default_clean_predictors(blueprint, x, call = call)

blueprint <- cleaned$blueprint
x <- cleaned$x
Expand All @@ -211,8 +213,9 @@ mold_xy_default_clean <- function(blueprint, x, y) {
new_mold_clean_xy(blueprint, x, y)
}

mold_xy_default_clean_predictors <- function(blueprint, x) {
check_data_frame_or_matrix(x)
mold_xy_default_clean_predictors <- function(blueprint, x, ..., call = caller_env()) {
check_dots_empty0(...)
check_data_frame_or_matrix(x, call = call)
x <- coerce_to_tibble(x)
list(blueprint = blueprint, x = x)
}
Expand All @@ -235,7 +238,7 @@ mold_xy_default_process <- function(blueprint, x, y, ..., call = caller_env()) {
predictors_ptype <- processed$ptype
predictors_extras <- processed$extras

processed <- mold_xy_default_process_outcomes(blueprint, y)
processed <- mold_xy_default_process_outcomes(blueprint, y, call = call)

blueprint <- processed$blueprint
outcomes <- processed$data
Expand All @@ -254,9 +257,9 @@ mold_xy_default_process_predictors <- function(blueprint, x, ..., call = caller_
check_dots_empty0(...)

# Important! Collect ptype before adding intercept!
ptype <- extract_ptype(x)
ptype <- extract_ptype(x, call = call)

x <- maybe_add_intercept_column(x, blueprint$intercept)
x <- maybe_add_intercept_column(x, blueprint$intercept, call = call)

x <- recompose(x, composition = blueprint$composition, call = call)

Expand All @@ -267,8 +270,10 @@ mold_xy_default_process_predictors <- function(blueprint, x, ..., call = caller_
)
}

mold_xy_default_process_outcomes <- function(blueprint, y) {
ptype <- extract_ptype(y)
mold_xy_default_process_outcomes <- function(blueprint, y, ..., call = caller_env()) {
check_dots_empty0(...)

ptype <- extract_ptype(y, call = call)

new_mold_process_terms(
blueprint = blueprint,
Expand Down Expand Up @@ -314,7 +319,7 @@ run_forge.default_xy_blueprint <- function(blueprint,

forge_xy_default_clean <- function(blueprint, new_data, outcomes, ..., call = caller_env()) {
check_dots_empty0(...)
check_data_frame_or_matrix(new_data)
check_data_frame_or_matrix(new_data, call = call)
new_data <- coerce_to_tibble(new_data)
check_unique_column_names(new_data)
check_bool(outcomes)
Expand All @@ -324,13 +329,14 @@ forge_xy_default_clean <- function(blueprint, new_data, outcomes, ..., call = ca
predictors <- scream(
predictors,
blueprint$ptypes$predictors,
allow_novel_levels = blueprint$allow_novel_levels
allow_novel_levels = blueprint$allow_novel_levels,
call = call
)

if (outcomes) {
outcomes <- shrink(new_data, blueprint$ptypes$outcomes, call = call)
# Never allow novel levels for outcomes
outcomes <- scream(outcomes, blueprint$ptypes$outcomes)
outcomes <- scream(outcomes, blueprint$ptypes$outcomes, call = call)
} else {
outcomes <- NULL
}
Expand Down Expand Up @@ -366,7 +372,7 @@ forge_xy_default_process <- function(blueprint, predictors, outcomes, extras, ..
forge_xy_default_process_predictors <- function(blueprint, predictors, ..., call = caller_env()) {
check_dots_empty0(...)

predictors <- maybe_add_intercept_column(predictors, blueprint$intercept)
predictors <- maybe_add_intercept_column(predictors, blueprint$intercept, call = call)

predictors <- recompose(predictors, composition = blueprint$composition, call = call)

Expand Down
9 changes: 6 additions & 3 deletions R/classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#' `get_data_classes()` extracts the classes from the original training data.
#'
#' @param data A data frame or matrix.
#'
#' @inheritParams validate_column_names
#'
#' @return
#'
Expand All @@ -24,8 +26,9 @@
#'
#' get_data_classes(data)
#' @export
get_data_classes <- function(data) {
data <- extract_ptype(data)
check_unique_column_names(data)
get_data_classes <- function(data, ..., call = current_env()) {
check_dots_empty0(...)
data <- extract_ptype(data, call = call)
check_unique_column_names(data, call = call)
lapply(data, class)
}
Loading

0 comments on commit 7aaa32f

Please sign in to comment.