Skip to content

Commit 96591a1

Browse files
authored
Merge pull request #230 from cmu-delphi/enumerate-steps-layers-print
In printing of an `epi_workflow`, number steps and layers
2 parents 69120e0 + 0d1fc6d commit 96591a1

27 files changed

+267
-83
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ S3method(print,alist)
5252
S3method(print,arx_class)
5353
S3method(print,arx_fcast)
5454
S3method(print,canned_epipred)
55+
S3method(print,epi_recipe)
5556
S3method(print,epi_workflow)
5657
S3method(print,flat_fcast)
5758
S3method(print,flatline)
@@ -71,6 +72,7 @@ S3method(print,step_epi_ahead)
7172
S3method(print,step_epi_lag)
7273
S3method(print,step_growth_rate)
7374
S3method(print,step_lag_difference)
75+
S3method(print,step_naomit)
7476
S3method(print,step_population_scaling)
7577
S3method(print,step_training_window)
7678
S3method(quantile,dist_quantiles)

R/epi_recipe.R

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,132 @@ kill_levels <- function(x, keys) {
410410
for (i in which(names(x) %in% keys)) x[[i]] <- list(values = NA, ordered = NA)
411411
x
412412
}
413+
414+
#' @export
415+
print.epi_recipe <- function(x, form_width = 30, ...) {
416+
cli::cli_div(theme = list(.pkg = list("vec-trunc" = Inf, "vec-last" = ", ")))
417+
418+
cli::cli_h1("Epi Recipe")
419+
cli::cli_h3("Inputs")
420+
421+
tab <- table(x$var_info$role, useNA = "ifany")
422+
tab <- stats::setNames(tab, names(tab))
423+
names(tab)[is.na(names(tab))] <- "undeclared role"
424+
425+
roles <- c("outcome", "predictor", "case_weights", "undeclared role")
426+
427+
tab <- c(
428+
tab[names(tab) == roles[1]],
429+
tab[names(tab) == roles[2]],
430+
tab[names(tab) == roles[3]],
431+
sort(tab[!names(tab) %in% roles], TRUE),
432+
tab[names(tab) == roles[4]]
433+
)
434+
435+
cli::cli_text("Number of variables by role")
436+
437+
spaces_needed <- max(nchar(names(tab))) - nchar(names(tab)) +
438+
max(nchar(tab)) - nchar(tab)
439+
440+
cli::cli_verbatim(
441+
glue::glue("{names(tab)}: {strrep('\ua0', spaces_needed)}{tab}")
442+
)
443+
444+
if ("tr_info" %in% names(x)) {
445+
cli::cli_h3("Training information")
446+
nmiss <- x$tr_info$nrows - x$tr_info$ncomplete
447+
nrows <- x$tr_info$nrows
448+
449+
cli::cli_text(
450+
"Training data contained {nrows} data points and {cli::no(nmiss)} \\
451+
incomplete row{?s}."
452+
)
453+
}
454+
455+
if (!is.null(x$steps)) {
456+
cli::cli_h3("Operations")
457+
}
458+
459+
i = 1
460+
for (step in x$steps) {
461+
cat(paste0(i, ". "))
462+
print(step, form_width = form_width)
463+
i = i + 1
464+
}
465+
cli::cli_end()
466+
467+
invisible(x)
468+
}
469+
470+
# Currently only used in the workflow printing
471+
print_preprocessor_recipe <- function(x, ...) {
472+
473+
recipe <- workflows::extract_preprocessor(x)
474+
steps <- recipe$steps
475+
n_steps <- length(steps)
476+
if (n_steps == 1L) {
477+
step <- "Step"
478+
}
479+
else {
480+
step <- "Steps"
481+
}
482+
n_steps_msg <- glue::glue("{n_steps} Recipe {step}")
483+
cat_line(n_steps_msg)
484+
485+
if (n_steps == 0L) return(invisible(x))
486+
487+
cat_line("")
488+
489+
step_names <- map_chr(steps, workflows:::pull_step_name)
490+
491+
if (n_steps <= 10L) {
492+
cli::cli_ol(step_names)
493+
return(invisible(x))
494+
}
495+
496+
extra_steps <- n_steps - 10L
497+
step_names <- step_names[1:10]
498+
499+
if (extra_steps == 1L) {
500+
step <- "step"
501+
}
502+
else {
503+
step <- "steps"
504+
}
505+
506+
extra_dots <- "..."
507+
extra_msg <- glue::glue("and {extra_steps} more {step}.")
508+
509+
cli::cli_ol(step_names)
510+
cli::cli_bullets(c(extra_dots, extra_msg))
511+
invisible(x)
512+
}
513+
514+
print_preprocessor <- function(x) {
515+
516+
has_preprocessor_formula <- workflows:::has_preprocessor_formula(x)
517+
has_preprocessor_recipe <- workflows:::has_preprocessor_recipe(x)
518+
has_preprocessor_variables <- workflows:::has_preprocessor_variables(x)
519+
520+
no_preprocessor <- !has_preprocessor_formula && !has_preprocessor_recipe &&
521+
!has_preprocessor_variables
522+
523+
if (no_preprocessor) {
524+
return(invisible(x))
525+
}
526+
527+
cat_line("")
528+
header <- cli::rule("Preprocessor")
529+
cat_line(header)
530+
531+
if (has_preprocessor_formula) {
532+
workflows:::print_preprocessor_formula(x)
533+
}
534+
if (has_preprocessor_recipe) {
535+
print_preprocessor_recipe(x)
536+
}
537+
if (has_preprocessor_variables) {
538+
workflows:::print_preprocessor_variables(x)
539+
}
540+
invisible(x)
541+
}

R/epi_workflow.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ new_epi_workflow <- function(
218218
#' @export
219219
print.epi_workflow <- function(x, ...) {
220220
print_header(x)
221-
workflows:::print_preprocessor(x)
222-
# workflows:::print_case_weights(x)
221+
print_preprocessor(x)
222+
#workflows:::print_case_weights(x)
223223
workflows:::print_model(x)
224224
print_postprocessor(x)
225225
invisible(x)

R/frosting.R

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,12 @@ print.frosting <- function(x, form_width = 30, ...) {
294294
cli::cli_h1("Frosting")
295295

296296
if (!is.null(x$layers)) cli::cli_h3("Layers")
297-
for (layer in x$layers) print(layer, form_width = form_width)
297+
i = 1
298+
for (layer in x$layers){
299+
cat(paste0(i, ". "))
300+
print(layer, form_width = form_width)
301+
i = i + 1
302+
}
298303
cli::cli_end()
299304
invisible(x)
300305
}
@@ -316,7 +321,7 @@ print_frosting <- function(x, ...) {
316321
layer_names <- map_chr(layers, pull_layer_name)
317322

318323
if (n_layers <= 10L) {
319-
cli::cat_bullet(layer_names)
324+
cli::cli_ol(layer_names)
320325
return(invisible(x))
321326
}
322327

@@ -328,9 +333,8 @@ print_frosting <- function(x, ...) {
328333
extra_dots <- "..."
329334
extra_msg <- glue::glue("and {extra_layers} more {layer}.")
330335

331-
layer_names <- c(layer_names, extra_dots, extra_msg)
332-
333-
cli::cat_bullet(layer_names)
336+
cli::cli_ol(layer_names)
337+
cli::cli_bullets(c(extra_dots, extra_msg))
334338
invisible(x)
335339
}
336340

R/print_epi_step.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ print_epi_step <- function(
4444
)
4545
more_dots <- ifelse(first_line == length(elements), "", ", ...")
4646
cli::cli_bullets(
47-
c(`*` = "\n {title}: \\\n {.pkg {cli::cli_vec(elements[seq_len(first_line)])}}\\\n {more_dots} \\\n {conjunction} \\\n {.pkg {extra_text}} \\\n {vline_seperator} \\\n {.emph {trained_text}}\\\n {comma_seperator} \\\n {.emph {case_weights_text}}\n ")
48-
)
47+
c("\n {title}: \\\n {.pkg {cli::cli_vec(elements[seq_len(first_line)])}}\\\n {more_dots} \\\n {conjunction} \\\n {.pkg {extra_text}} \\\n {vline_seperator} \\\n {.emph {trained_text}}\\\n {comma_seperator} \\\n {.emph {case_weights_text}}\n "))
48+
4949
cli::cli_end(theme_div_id)
5050
invisible(NULL)
5151
}

R/print_layer.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ print_layer <- function(
2525
)
2626
more_dots <- ifelse(first_line == length(elements), "", ", ...")
2727
cli::cli_bullets(
28-
c(`*` = "\n {title}: \\\n {.pkg {elements[seq_len(first_line)]}}\\\n {more_dots} \\\n {conjunction} \\\n {.pkg {extra_text}}")
29-
)
28+
c("\n {title}: \\\n {.pkg {elements[seq_len(first_line)]}}\\\n {more_dots} \\\n {conjunction} \\\n {.pkg {extra_text}}"))
29+
3030
invisible(NULL)
3131
}

R/step_epi_naomit.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ step_epi_naomit <- function(recipe) {
1717
recipes::step_naomit(all_predictors(), skip = FALSE) %>%
1818
recipes::step_naomit(all_outcomes(), skip = TRUE)
1919
}
20+
21+
#' @export
22+
print.step_naomit <- # not exported from recipes package
23+
function(x, width = max(20, options()$width - 30), ...) {
24+
title <- "Removing rows with NA values in "
25+
print_step(x$columns, x$terms, x$trained, title, width)
26+
invisible(x)
27+
}

man/add_frosting.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/arx_fcast_epi_workflow.Rd

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/arx_forecaster.Rd

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_layer.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dist_quantiles.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/extrapolate_quantiles.Rd

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fit-epi_workflow.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/flatline.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/frosting.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/layer_add_forecast_date.Rd

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)