-
Notifications
You must be signed in to change notification settings - Fork 24
APIs for determining original predictor columns #215
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
Open
topepo
wants to merge
7
commits into
main
Choose a base branch
from
input-names
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dc90367
Create standalone-input-names.R
07f30f8
unit tests
c93a3be
Apply suggestions from code review
topepo 3421147
changes based on reviewer feedback
topepo e2d83a3
renamed file
topepo 1180fde
fix/change dates
af22e77
Merge branch 'main' into input-names
topepo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # --- | ||
| # repo: tidymodels/workflows | ||
| # file: standalone-input-names.R | ||
| # last-updated: 2024-01-21 | ||
| # license: https://unlicense.org | ||
| # --- | ||
|
|
||
| # secret gist at: https://gist.github.com/topepo/17d51cafcd0ac8dff0552198d6aeadbf | ||
|
|
||
| # This file provides a portable set of helper functions for determining the | ||
| # names of the predictor columns used as inputs into a workflow. | ||
|
|
||
| # ## Changelog | ||
| # 2024-01-21 | ||
| # * First version | ||
|
|
||
| # ------------------------------------------------------------------------------ | ||
|
|
||
| check_workflow_fit <- function(x) { | ||
| if (!x$trained) { | ||
| stop("The workflow should be trainined.") | ||
topepo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| invisible(NULL) | ||
| } | ||
|
|
||
| check_recipe_fit <- function(x) { | ||
| is_trained <- vapply(x$steps, function(x) x$trained, logical(1)) | ||
| if (!all(is_trained)) { | ||
| stop("All recipe steps should be trainined.") | ||
topepo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
topepo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| invisible(NULL) | ||
| } | ||
|
|
||
| blueprint_ptype <- function(x) { | ||
| names(x$pre$mold$blueprint$ptypes$predictors) | ||
| } | ||
|
|
||
| .get_input_predictors_workflow <- function(x, ...) { | ||
| check_workflow_fit(x) | ||
| # We can get the columns that are inputs to the recipe but some of these may | ||
| # not be predictors. We'll interrogate the recipe and pull out the current | ||
| # predictor names from the original input | ||
| if ("recipe" %in% names(x$pre$actions)) { | ||
| mold <- x$pre$mold | ||
| rec <- mold$blueprint$recipe | ||
| res <- .get_input_predictors_recipe(rec) | ||
| } else { | ||
| res <- blueprint_ptype(x) | ||
| } | ||
| sort(unique(res)) | ||
| } | ||
|
|
||
| is_predictor_role <- function(x) { | ||
| vapply(x$role, function(x) any(x == "predictor"), logical(1)) | ||
| } | ||
|
|
||
| .get_input_predictors_recipe <- function(x, ...) { | ||
topepo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| check_recipe_fit(x) | ||
| var_info <- x$last_term_info | ||
|
|
||
| keep_rows <- var_info$source == "original" & is_predictor_role(var_info) | ||
| var_info <- var_info[keep_rows,] | ||
| var_info$variable | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # get recipe input column names | ||
|
|
||
| Code | ||
| workflows:::.get_input_predictors_workflow(workflow) | ||
| Condition | ||
| Error in `check_workflow_fit()`: | ||
| ! The workflow should be trainined. | ||
|
|
||
| --- | ||
|
|
||
| Code | ||
| workflows:::.get_input_predictors_recipe(rec_with_id) | ||
| Condition | ||
| Error in `check_recipe_fit()`: | ||
| ! All recipe steps should be trainined. | ||
|
|
||
| # get formula input column names | ||
|
|
||
| Code | ||
| workflows:::.get_input_predictors_workflow(workflow) | ||
| Condition | ||
| Error in `check_workflow_fit()`: | ||
| ! The workflow should be trainined. | ||
|
|
||
| # get predictor input column names | ||
|
|
||
| Code | ||
| workflows:::.get_input_predictors_workflow(workflow) | ||
| Condition | ||
| Error in `check_workflow_fit()`: | ||
| ! The workflow should be trainined. | ||
|
|
topepo marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| test_that("get recipe input column names", { | ||
| skip_if_not_installed("modeldata") | ||
| skip_if_not_installed("recipes") | ||
|
|
||
| library(recipes) | ||
|
|
||
| data(cells, package = "modeldata") | ||
|
|
||
| cells <- cells[, 1:10] | ||
| pred_names <- sort(names(cells)[3:10]) | ||
|
|
||
| rec_with_id <- | ||
| recipes::recipe(class ~ ., cells) %>% | ||
| update_role(case, new_role = "destination") %>% | ||
| step_rm(angle_ch_1) %>% | ||
| step_pca(all_predictors()) | ||
|
|
||
| workflow <- workflow() | ||
| workflow <- add_recipe(workflow, rec_with_id) | ||
| workflow <- add_model(workflow, parsnip::logistic_reg()) | ||
| workflow_fit <- fit(workflow, cells) | ||
|
|
||
| expect_snapshot( | ||
| workflows:::.get_input_predictors_workflow(workflow), | ||
| error = TRUE | ||
| ) | ||
| expect_equal( | ||
| workflows:::.get_input_predictors_workflow(workflow_fit), | ||
| pred_names | ||
| ) | ||
| expect_snapshot( | ||
| workflows:::.get_input_predictors_recipe(rec_with_id), | ||
| error = TRUE | ||
| ) | ||
|
|
||
| }) | ||
|
|
||
| test_that("get formula input column names", { | ||
| skip_if_not_installed("modeldata") | ||
|
|
||
| data(Chicago, package = "modeldata") | ||
|
|
||
| Chicago <- Chicago[, c("ridership", "date", "Austin")] | ||
| pred_names <- sort(c("date", "Austin")) | ||
|
|
||
| workflow <- workflow() | ||
| workflow <- add_formula(workflow, ridership ~ .) | ||
| workflow <- add_model(workflow, parsnip::linear_reg()) | ||
| workflow_fit <- fit(workflow, Chicago) | ||
|
|
||
| expect_snapshot( | ||
| workflows:::.get_input_predictors_workflow(workflow), | ||
| error = TRUE | ||
| ) | ||
| expect_equal( | ||
| workflows:::.get_input_predictors_workflow(workflow_fit), | ||
| pred_names | ||
| ) | ||
|
|
||
| }) | ||
|
|
||
|
|
||
| test_that("get predictor input column names", { | ||
| skip_if_not_installed("modeldata") | ||
|
|
||
| data(Chicago, package = "modeldata") | ||
|
|
||
| Chicago <- Chicago[, c("ridership", "date", "Austin")] | ||
| pred_names <- sort(c("date", "Austin")) | ||
|
|
||
| workflow <- workflow() | ||
| workflow <- | ||
| add_variables(workflow, | ||
| outcomes = c(ridership), | ||
| predictors = c(tidyselect::everything())) | ||
| workflow <- add_model(workflow, parsnip::linear_reg()) | ||
| workflow_fit <- fit(workflow, Chicago) | ||
|
|
||
| expect_snapshot( | ||
| workflows:::.get_input_predictors_workflow(workflow), | ||
| error = TRUE | ||
| ) | ||
| expect_equal( | ||
| workflows:::.get_input_predictors_workflow(workflow_fit), | ||
| pred_names | ||
| ) | ||
|
|
||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.