Skip to content

Commit b109d5b

Browse files
export internal functions for tidyclust (#1181)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 30848e2 commit b109d5b

13 files changed

Lines changed: 161 additions & 55 deletions

NAMESPACE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ S3method(vec_restore,resample_results)
134134
S3method(vec_restore,tune_results)
135135
export(.catch_and_log)
136136
export(.check_grid)
137+
export(.check_param_objects)
137138
export(.config_key_from_metrics)
138139
export(.create_weight_mapping)
139140
export(.determine_pred_types)
@@ -153,13 +154,25 @@ export(.get_tune_outcome_names)
153154
export(.get_tune_parameter_names)
154155
export(.get_tune_parameters)
155156
export(.get_tune_workflow)
157+
export(.has_preprocessor)
158+
export(.has_preprocessor_formula)
159+
export(.has_preprocessor_recipe)
160+
export(.has_preprocessor_variables)
161+
export(.has_spec)
162+
export(.is_cataclysmic)
156163
export(.load_namespace)
157164
export(.loop_over_all_stages)
158165
export(.loop_over_all_stages2)
159166
export(.make_static)
167+
export(.needs_finalization)
160168
export(.par_fns)
169+
export(.set_workflow)
170+
export(.set_workflow_recipe)
171+
export(.set_workflow_spec)
161172
export(.stash_last_result)
173+
export(.update_model)
162174
export(.update_parallel_over)
175+
export(.update_recipe)
163176
export(.use_case_weights_with_yardstick)
164177
export(.validate_resample_weights)
165178
export(.weighted_sd)
@@ -251,6 +264,7 @@ export(min_grid.proportional_hazards)
251264
export(min_grid.rule_fit)
252265
export(mirai_installed)
253266
export(new_backend_options)
267+
export(new_bare_tibble)
254268
export(new_iteration_results)
255269
export(outcome_names)
256270
export(parameters)

R/checks.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ grid_msg <- "{.arg grid} should be a positive integer or a data frame."
153153
grid
154154
}
155155

156-
needs_finalization <- function(x, nms = character(0)) {
156+
#' @export
157+
#' @keywords internal
158+
#' @rdname empty_ellipses
159+
.needs_finalization <- function(x, nms = character(0)) {
157160
# If an unknown engine-specific parameter, the object column is missing and
158161
# no need for finalization
159162
x <- x[!is.na(x$object), ]
@@ -187,7 +190,7 @@ check_parameters <- function(
187190
tune_recipe <- tune_param$id[tune_param$source == "recipe"]
188191
tune_recipe <- length(tune_recipe) > 0
189192

190-
if (needs_finalization(pset, grid_names)) {
193+
if (.needs_finalization(pset, grid_names)) {
191194
if (tune_recipe) {
192195
cli::cli_abort(
193196
c(
@@ -289,7 +292,10 @@ check_bayes_initial_size <- function(num_param, num_grid, race = FALSE) {
289292
invisible(NULL)
290293
}
291294

292-
check_param_objects <- function(pset) {
295+
#' @export
296+
#' @keywords internal
297+
#' @rdname empty_ellipses
298+
.check_param_objects <- function(pset) {
293299
params <- purrr::map_lgl(pset$object, inherits, "param")
294300

295301
if (!all(params)) {
@@ -318,11 +324,11 @@ check_workflow <- function(
318324
)
319325
}
320326

321-
if (!has_preprocessor(x)) {
327+
if (!.has_preprocessor(x)) {
322328
cli::cli_abort("A formula, recipe, or variables preprocessor is required.")
323329
}
324330

325-
if (!has_spec(x)) {
331+
if (!.has_spec(x)) {
326332
cli::cli_abort("A parsnip model is required.")
327333
}
328334

@@ -333,7 +339,7 @@ check_workflow <- function(
333339
pset <- hardhat::extract_parameter_set_dials(x)
334340
}
335341

336-
check_param_objects(pset)
342+
.check_param_objects(pset)
337343

338344
incompl <- dials::has_unknowns(pset$object)
339345

R/collect.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ estimate_tune_results <- function(x, ..., col_name = ".metrics") {
648648
id_names <- grep("^id", names(x), value = TRUE)
649649
group_cols <- .get_extra_col_names(x)
650650

651-
all_bad <- is_cataclysmic(x)
651+
all_bad <- .is_cataclysmic(x)
652652
if (all_bad) {
653653
cli::cli_abort(
654654
"All models failed. Run {.code show_notes(.Last.tune.result)} for more

R/finalize.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ finalize_workflow <- function(x, parameters) {
9292

9393
mod <- extract_spec_parsnip(x)
9494
mod <- finalize_model(mod, parameters)
95-
x <- set_workflow_spec(x, mod)
95+
x <- .set_workflow_spec(x, mod)
9696

97-
if (has_preprocessor_recipe(x)) {
97+
if (.has_preprocessor_recipe(x)) {
9898
rec <- extract_preprocessor(x)
9999
rec <- finalize_recipe(rec, parameters)
100-
x <- set_workflow_recipe(x, rec)
100+
x <- .set_workflow_recipe(x, rec)
101101
}
102102

103103
if (has_postprocessor(x)) {

R/grid_helpers.R

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# if (length(orig_rows) != nrow(x_vals)) {
99
# msg <- "Some assessment set rows are not available at prediction time."
1010
#
11-
# if (has_preprocessor_recipe(workflow)) {
11+
# if (.has_preprocessor_recipe(workflow)) {
1212
# msg <-
1313
# c(
1414
# msg,
@@ -129,28 +129,40 @@ finalize_workflow_preprocessor <- function(workflow, grid_preprocessor) {
129129
recipe <- extract_preprocessor(workflow)
130130
recipe <- merge(recipe, grid_preprocessor)$x[[1]]
131131

132-
workflow <- set_workflow_recipe(workflow, recipe)
132+
workflow <- .set_workflow_recipe(workflow, recipe)
133133

134134
workflow
135135
}
136136

137137
# ------------------------------------------------------------------------------
138138

139-
has_preprocessor <- function(workflow) {
140-
has_preprocessor_recipe(workflow) ||
141-
has_preprocessor_formula(workflow) ||
142-
has_preprocessor_variables(workflow)
139+
#' @export
140+
#' @keywords internal
141+
#' @rdname empty_ellipses
142+
.has_preprocessor <- function(workflow) {
143+
.has_preprocessor_recipe(workflow) ||
144+
.has_preprocessor_formula(workflow) ||
145+
.has_preprocessor_variables(workflow)
143146
}
144147

145-
has_preprocessor_recipe <- function(workflow) {
148+
#' @export
149+
#' @keywords internal
150+
#' @rdname empty_ellipses
151+
.has_preprocessor_recipe <- function(workflow) {
146152
"recipe" %in% names(workflow$pre$actions)
147153
}
148154

149-
has_preprocessor_formula <- function(workflow) {
155+
#' @export
156+
#' @keywords internal
157+
#' @rdname empty_ellipses
158+
.has_preprocessor_formula <- function(workflow) {
150159
"formula" %in% names(workflow$pre$actions)
151160
}
152161

153-
has_preprocessor_variables <- function(workflow) {
162+
#' @export
163+
#' @keywords internal
164+
#' @rdname empty_ellipses
165+
.has_preprocessor_variables <- function(workflow) {
154166
"variables" %in% names(workflow$pre$actions)
155167
}
156168

@@ -162,16 +174,25 @@ has_case_weights <- function(workflow) {
162174
"case_weights" %in% names(workflow$pre$actions)
163175
}
164176

165-
has_spec <- function(workflow) {
177+
#' @export
178+
#' @keywords internal
179+
#' @rdname empty_ellipses
180+
.has_spec <- function(workflow) {
166181
"model" %in% names(workflow$fit$actions)
167182
}
168183

169-
set_workflow_spec <- function(workflow, spec) {
184+
#' @export
185+
#' @keywords internal
186+
#' @rdname empty_ellipses
187+
.set_workflow_spec <- function(workflow, spec) {
170188
workflow$fit$actions$model$spec <- spec
171189
workflow
172190
}
173191

174-
set_workflow_recipe <- function(workflow, recipe) {
192+
#' @export
193+
#' @keywords internal
194+
#' @rdname empty_ellipses
195+
.set_workflow_recipe <- function(workflow, recipe) {
175196
workflow$pre$actions$recipe$recipe <- recipe
176197
workflow
177198
}

R/loop_over_all_stages-helpers.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ finalize_fit_pre <- function(wflow_current, grid, static) {
336336
if (length(pre_proc_id) > 0) {
337337
grid <- grid[, pre_proc_id]
338338
pre_proc <- finalize_recipe(pre_proc, grid)
339-
wflow_current <- set_workflow_recipe(wflow_current, pre_proc)
339+
wflow_current <- .set_workflow_recipe(wflow_current, pre_proc)
340340
}
341341
}
342342
workflows::.fit_pre(wflow_current, static$data$fit$data)
@@ -352,7 +352,7 @@ finalize_fit_model <- function(wflow_current, grid) {
352352
if (length(mod_id) > 0) {
353353
grid <- grid[, mod_id]
354354
mod_spec <- finalize_model(mod_spec, grid)
355-
wflow_current <- set_workflow_spec(wflow_current, mod_spec)
355+
wflow_current <- .set_workflow_spec(wflow_current, mod_spec)
356356
}
357357

358358
# .catch_and_log_fit()

R/merge.R

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,20 @@ merge.model_spec <- function(x, y, ...) {
7575
merger(x, y, ...)
7676
}
7777

78-
update_model <- function(grid, object, pset, step_id, nms, ...) {
78+
#' @export
79+
#' @keywords internal
80+
#' @rdname empty_ellipses
81+
.update_model <- function(
82+
grid,
83+
object,
84+
pset,
85+
step_id,
86+
nms,
87+
...,
88+
source = "model_spec"
89+
) {
7990
for (i in nms) {
80-
param_info <- pset |> dplyr::filter(id == i & source == "model_spec")
91+
param_info <- pset |> dplyr::filter(id == i & .data$source == .env$source)
8192
if (nrow(param_info) > 1) {
8293
cli::cli_abort("Cannot update; there are too many parameters.")
8394
}
@@ -94,7 +105,10 @@ update_model <- function(grid, object, pset, step_id, nms, ...) {
94105
object
95106
}
96107

97-
update_recipe <- function(grid, object, pset, step_id, nms, ...) {
108+
#' @export
109+
#' @keywords internal
110+
#' @rdname empty_ellipses
111+
.update_recipe <- function(grid, object, pset, step_id, nms, ...) {
98112
for (i in nms) {
99113
param_info <- pset |> dplyr::filter(id == i & source == "recipe")
100114
if (nrow(param_info) == 1) {
@@ -123,10 +137,10 @@ merger <- function(x, y, ...) {
123137
# We will deliberately allow `y` to lack some tunable parameters in `x`
124138

125139
if (inherits(x, "recipe")) {
126-
updater <- update_recipe
140+
updater <- .update_recipe
127141
step_ids <- purrr::map_chr(x$steps, "id")
128142
} else {
129-
updater <- update_model
143+
updater <- .update_model
130144
step_ids <- NULL
131145
}
132146

R/tune_bayes.R

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ tune_bayes_workflow <- function(
396396

397397
# Preempt `estimate_tune_results()` error and rely
398398
# on `on.exit()` condition to return preliminary results
399-
if (is_cataclysmic(unsummarized)) {
399+
if (.is_cataclysmic(unsummarized)) {
400400
return()
401401
}
402402

@@ -514,7 +514,7 @@ tune_bayes_workflow <- function(
514514

515515
check_time(start_time, control$time_limit)
516516

517-
all_bad <- is_cataclysmic(tmp_res)
517+
all_bad <- .is_cataclysmic(tmp_res)
518518
if (!inherits(tmp_res, "try-error") & !all_bad) {
519519
tmp_res[[".metrics"]] <- purrr::map(
520520
tmp_res[[".metrics"]],
@@ -574,7 +574,7 @@ tune_bayes_workflow <- function(
574574
check_time(start_time, control$time_limit)
575575
}
576576

577-
workflow_output <- set_workflow(object, control)
577+
workflow_output <- .set_workflow(object, control)
578578

579579
# Reset `on.exit()` hook
580580
on.exit()
@@ -597,7 +597,7 @@ tune_bayes_workflow <- function(
597597
}
598598

599599
create_initial_set <- function(param, n = NULL, checks) {
600-
check_param_objects(param)
600+
.check_param_objects(param)
601601
if (is.null(n)) {
602602
n <- nrow(param) + 1
603603
}
@@ -799,7 +799,7 @@ more_results <- function(
799799
type = "danger"
800800
)
801801
} else {
802-
all_bad <- is_cataclysmic(tmp_res)
802+
all_bad <- .is_cataclysmic(tmp_res)
803803
if (all_bad) {
804804
p_chr <- glue::glue_collapse(
805805
p_chr,
@@ -822,7 +822,10 @@ more_results <- function(
822822
}
823823

824824

825-
is_cataclysmic <- function(x) {
825+
#' @export
826+
#' @keywords internal
827+
#' @rdname empty_ellipses
828+
.is_cataclysmic <- function(x) {
826829
is_err <- purrr::map_lgl(x$.metrics, inherits, c("simpleError", "error"))
827830
if (any(!is_err)) {
828831
is_good <- purrr::map_lgl(

R/tune_grid.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,14 @@ tune_grid_workflow <- function(
394394

395395
y_name <- outcome_names(resamples)
396396

397-
if (is_cataclysmic(resamples)) {
397+
if (.is_cataclysmic(resamples)) {
398398
cli::cli_warn(
399399
"All models failed. Run {.code show_notes(.Last.tune.result)} for more
400400
information."
401401
)
402402
}
403403

404-
workflow <- set_workflow(workflow, control)
404+
workflow <- .set_workflow(workflow, control)
405405

406406
new_tune_results(
407407
x = resamples,
@@ -441,7 +441,10 @@ pull_rset_attributes <- function(x) {
441441

442442
# ------------------------------------------------------------------------------
443443

444-
set_workflow <- function(workflow, control) {
444+
#' @export
445+
#' @keywords internal
446+
#' @rdname empty_ellipses
447+
.set_workflow <- function(workflow, control) {
445448
if (control$save_workflow) {
446449
if (!is.null(workflow)) {
447450
w_size <- utils::object.size(workflow)

R/utils.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ should_run_examples <- function(suggests = NULL) {
7676

7777
# new_tibble() currently doesn't strip attributes
7878
# https://github.com/tidyverse/tibble/pull/769
79+
#' @export
80+
#' @keywords internal
81+
#' @rdname empty_ellipses
7982
new_bare_tibble <- function(x, ..., class = character()) {
8083
x <- vctrs::new_data_frame(x)
8184
tibble::new_tibble(x, nrow = nrow(x), ..., class = class)

0 commit comments

Comments
 (0)