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

sort ids via vctrs, pass sorting to pull_*() helpers #730

Merged
merged 2 commits into from
Oct 6, 2023
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

* `last_fit()` will now error when supplied a fitted workflow. (#678)

* Fixes bug where `.notes` entries were sorted in the wrong order in tuning results for resampling schemes with IDs that aren't already in alphabetical order. (#728)

* A method for rsample's `int_pctl()` function that will compute percentile confidence intervals on performance metrics for objects produced by `fit_resamples()`, `tune_*()`, and `last_fit()`.

* Fixes bug where `.config` entries in the `.extracts` column in `tune_bayes()` output didn't align with the entries they ought to in the `.metrics` and `.predictions` columns (#715).
Expand Down
12 changes: 8 additions & 4 deletions R/grid_code_paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ tune_grid_loop <- function(resamples,
rng
)

resamples <- pull_metrics(resamples, results, control)
resamples <- pull_notes(resamples, results, control)
resamples <- pull_extracts(resamples, results, control)
resamples <- pull_predictions(resamples, results, control)
# carry out arranging by id before extracting each element of results (#728)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this sorting happened independently for each of the pull_*() functions that call pulley(). For those that don't call pulley(), the ordering is wrong.

resample_ids <- grep("^id", names(resamples), value = TRUE)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the pattern used by pulley().

id_order <- vctrs::vec_order(resamples[resample_ids])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't suspect this is any different than passing those columns to arrange() (some locale technicalities maybe?), but worth looking into.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrange() uses vec_order_radix() under the hood. but I don't think it should matter much here


resamples <- pull_metrics(resamples, results, control, order = id_order)
resamples <- pull_notes(resamples, results, control, order = id_order)
resamples <- pull_extracts(resamples, results, control, order = id_order)
resamples <- pull_predictions(resamples, results, control, order = id_order)
resamples <- pull_all_outcome_names(resamples, results)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this function doesn't gain an order argument, as its output doesn't end up in tuning results.


resamples
Expand Down
24 changes: 14 additions & 10 deletions R/pull.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extract_details <- function(object, extractor) {

# Grab the new results, make sure that they align row-wise with the rsample
# object and then bind columns
pulley <- function(resamples, res, col) {
pulley <- function(resamples, res, col, order) {
if (all(purrr::map_lgl(res, inherits, "simpleError"))) {
res <-
resamples %>%
Expand All @@ -22,7 +22,9 @@ pulley <- function(resamples, res, col) {
all_null <- all(purrr::map_lgl(res, is.null))

id_cols <- grep("^id", names(resamples), value = TRUE)
resamples <- dplyr::arrange(resamples, !!!syms(id_cols))

resamples <- vctrs::vec_slice(resamples, order)

pulled_vals <- purrr::map(res, ~ .x[[col]]) %>% purrr::list_rbind()

if (nrow(pulled_vals) == 0) {
Expand Down Expand Up @@ -65,22 +67,22 @@ maybe_repair <- function(x) {
}


pull_metrics <- function(resamples, res, control) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remainder of the changes in this file just newly pass order along to pulley().

out <- pulley(resamples, res, ".metrics")
pull_metrics <- function(resamples, res, control, order) {
out <- pulley(resamples, res, ".metrics", order = order)
out$.metrics <- maybe_repair(out$.metrics)
out
}

pull_extracts <- function(resamples, res, control) {
pull_extracts <- function(resamples, res, control, order) {
if (!is.null(control$extract)) {
resamples <- pulley(resamples, res, ".extracts")
resamples <- pulley(resamples, res, ".extracts", order = order)
}
resamples
}

pull_predictions <- function(resamples, res, control) {
pull_predictions <- function(resamples, res, control, order) {
if (control$save_pred) {
resamples <- pulley(resamples, res, ".predictions")
resamples <- pulley(resamples, res, ".predictions", order = order)
resamples$.predictions <- maybe_repair(resamples$.predictions)
}
resamples
Expand Down Expand Up @@ -126,8 +128,10 @@ ensure_tibble <- function(x) {
res
}

pull_notes <- function(resamples, res, control) {
resamples$.notes <- purrr::map(res, ~ purrr::pluck(.x, ".notes"))
pull_notes <- function(resamples, res, control, order) {
notes <- purrr::map(res, ~ purrr::pluck(.x, ".notes"))
resamples$.notes <- notes[order]

resamples
}

Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-notes.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,19 @@ test_that("showing notes", {
expect_snapshot(show_notes(fit_lr))

})

test_that("notes are sorted in the correct order", {
# set `apparent = TRUE` so that resamples aren't in alphabetical order by id
mt_boots <- bootstraps(mtcars, 3, apparent = TRUE)

# induce the size zero yardstick error in Bootstrap1
mt_boots$splits[[1]]$out_id <- numeric(0)

suppressMessages({
mt_res <- fit_resamples(linear_reg(), mpg ~ ., mt_boots)
})

boots_1_loc <- which(mt_res$id == "Bootstrap1")
boots_1_notes <- mt_res$.notes[[boots_1_loc]]
expect_equal(nrow(boots_1_notes), 1)
})