Skip to content

Commit 2d05865

Browse files
authored
Merge pull request #280 from cmu-delphi/277-recipe-printing-bug
277 recipe printing bug
2 parents 6be79f1 + d22efc5 commit 2d05865

8 files changed

+293
-208
lines changed

R/canned-epipred.R

+33-29
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,43 @@ print.alist <- function(x, ...) {
5858

5959
#' @export
6060
print.canned_epipred <- function(x, name, ...) {
61-
cat("\n")
62-
bullet <- "\u2022"
63-
header <- glue::glue("A basic forecaster of type {name}")
64-
header <- cli::rule(header, line = 2)
65-
cat_line(header)
66-
cat("\n")
67-
68-
date_created <- glue::glue(
69-
"This forecaster was fit on {format(x$metadata$forecast_created)}"
61+
d <- cli::cli_div(theme = list(rule = list("line-type" = "double")))
62+
cli::cli_rule("A basic forecaster of type {name}")
63+
cli::cli_end(d)
64+
cli::cli_text("")
65+
cli::cli_text(
66+
"This forecaster was fit on {.field {format(x$metadata$forecast_created)}}."
7067
)
71-
cat_line(date_created)
72-
cat("\n")
73-
74-
cat_line("Training data was an `epi_df` with")
75-
cat_line(glue::glue("\u2022 Geography: {x$metadata$training$geo_type},"))
76-
cat_line(glue::glue("{bullet} Time type: {x$metadata$training$time_type},"))
77-
cat_line(glue::glue("{bullet} Using data up-to-date as of: {format(x$metadata$training$as_of)}."))
68+
cli::cli_text("")
69+
cli::cli_text("Training data was an {.cls epi_df} with:")
70+
cli::cli_ul(c(
71+
"Geography: {.field {x$metadata$training$geo_type}},",
72+
"Time type: {.field {x$metadata$training$time_type}},",
73+
"Using data up-to-date as of: {.field {format(x$metadata$training$as_of)}}."
74+
))
75+
cli::cli_text("")
7876

79-
cat("\n")
80-
header <- cli::rule("Predictions")
81-
cat_line(header)
82-
cat("\n")
77+
cli::cli_rule("Predictions")
78+
cli::cli_text("")
8379

8480
n_geos <- dplyr::n_distinct(x$predictions$geo_value)
85-
fds <- unique(x$predictions$forecast_date)
86-
tds <- unique(x$predictions$target_date)
87-
88-
cat_line(
89-
glue::glue("A total of {nrow(x$predictions)} predictions are available for")
81+
fds <- cli::cli_vec(
82+
unique(x$predictions$forecast_date),
83+
list("vec-trunc" = 5)
84+
)
85+
tds <- cli::cli_vec(
86+
unique(x$predictions$target_date),
87+
list("vec-trunc" = 5)
9088
)
91-
cat_line(glue::glue("{bullet} {n_geos} unique geographic regions,"))
92-
cat_line(glue::glue("{bullet} At forecast dates: {fds},"))
93-
cat_line(glue::glue("{bullet} For target dates: {tds}."))
9489

95-
cat("\n")
90+
cli::cli_text(c(
91+
"A total of {.val {nrow(x$predictions)}} prediction{?s}",
92+
" {?is/are} available for"
93+
))
94+
cli::cli_ul(c(
95+
"{.val {n_geos}} unique geographic region{?s},",
96+
"At forecast date{?s}: {.val {fds}},",
97+
"For target date{?s}: {.val {tds}}."
98+
))
99+
cli::cli_text("")
96100
}

R/epi_recipe.R

+19-28
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,15 @@ adjust_epi_recipe.epi_recipe <- function(
431431
prep.epi_recipe <- function(
432432
x, training = NULL, fresh = FALSE, verbose = FALSE,
433433
retain = TRUE, log_changes = FALSE, strings_as_factors = TRUE, ...) {
434+
if (is.null(training)) {
435+
cli::cli_warn(c(
436+
"!" = "No training data was supplied to {.fn prep}.",
437+
"!" = "Unlike a {.cls recipe}, an {.cls epi_recipe} does not ",
438+
"!" = "store the full template data in the object.",
439+
"!" = "Please supply the training data to the {.fn prep} function,",
440+
"!" = "to avoid addtional warning messages."
441+
))
442+
}
434443
training <- recipes:::check_training_set(training, x, fresh)
435444
training <- epi_check_training_set(training, x)
436445
training <- dplyr::relocate(training, tidyselect::all_of(epi_keys(training)))
@@ -598,12 +607,12 @@ print.epi_recipe <- function(x, form_width = 30, ...) {
598607
cli::cli_h3("Operations")
599608
}
600609

601-
i <- 1
602-
for (step in x$steps) {
603-
cat(paste0(i, ". "))
604-
print(step, form_width = form_width)
605-
i <- i + 1
606-
}
610+
fmt <- cli::cli_fmt({
611+
for (step in x$steps) {
612+
print(step, form_width = form_width)
613+
}
614+
})
615+
cli::cli_ol(fmt)
607616
cli::cli_end()
608617

609618
invisible(x)
@@ -614,20 +623,12 @@ print_preprocessor_recipe <- function(x, ...) {
614623
recipe <- workflows::extract_preprocessor(x)
615624
steps <- recipe$steps
616625
n_steps <- length(steps)
617-
if (n_steps == 1L) {
618-
step <- "Step"
619-
} else {
620-
step <- "Steps"
621-
}
622-
n_steps_msg <- glue::glue("{n_steps} Recipe {step}")
623-
cat_line(n_steps_msg)
626+
cli::cli_text("{n_steps} Recipe step{?s}.")
624627

625628
if (n_steps == 0L) {
626629
return(invisible(x))
627630
}
628631

629-
cat_line("")
630-
631632
step_names <- map_chr(steps, workflows:::pull_step_name)
632633

633634
if (n_steps <= 10L) {
@@ -638,17 +639,8 @@ print_preprocessor_recipe <- function(x, ...) {
638639
extra_steps <- n_steps - 10L
639640
step_names <- step_names[1:10]
640641

641-
if (extra_steps == 1L) {
642-
step <- "step"
643-
} else {
644-
step <- "steps"
645-
}
646-
647-
extra_dots <- "..."
648-
extra_msg <- glue::glue("and {extra_steps} more {step}.")
649-
650642
cli::cli_ol(step_names)
651-
cli::cli_bullets(c(extra_dots, extra_msg))
643+
cli::cli_bullets("... and {extra_steps} more step{?s}.")
652644
invisible(x)
653645
}
654646

@@ -664,9 +656,8 @@ print_preprocessor <- function(x) {
664656
return(invisible(x))
665657
}
666658

667-
cat_line("")
668-
header <- cli::rule("Preprocessor")
669-
cat_line(header)
659+
cli::cli_rule("Preprocessor")
660+
cli::cli_text("")
670661

671662
if (has_preprocessor_formula) {
672663
workflows:::print_preprocessor_formula(x)

R/epi_workflow.R

+1-44
Original file line numberDiff line numberDiff line change
@@ -323,50 +323,7 @@ print.epi_workflow <- function(x, ...) {
323323
print_header(x)
324324
print_preprocessor(x)
325325
# workflows:::print_case_weights(x)
326-
workflows:::print_model(x)
326+
print_model(x)
327327
print_postprocessor(x)
328328
invisible(x)
329329
}
330-
331-
print_header <- function(x) {
332-
# same as in workflows but with a postprocessor
333-
trained <- ifelse(workflows::is_trained_workflow(x), " [trained]", "")
334-
335-
header <- glue::glue("Epi Workflow{trained}")
336-
header <- cli::rule(header, line = 2)
337-
cat_line(header)
338-
339-
preprocessor_msg <- cli::style_italic("Preprocessor:")
340-
341-
if (workflows:::has_preprocessor_formula(x)) {
342-
preprocessor <- "Formula"
343-
} else if (workflows:::has_preprocessor_recipe(x)) {
344-
preprocessor <- "Recipe"
345-
} else if (workflows:::has_preprocessor_variables(x)) {
346-
preprocessor <- "Variables"
347-
} else {
348-
preprocessor <- "None"
349-
}
350-
351-
preprocessor_msg <- glue::glue("{preprocessor_msg} {preprocessor}")
352-
cat_line(preprocessor_msg)
353-
354-
spec_msg <- cli::style_italic("Model:")
355-
356-
if (workflows:::has_spec(x)) {
357-
spec <- class(workflows::extract_spec_parsnip(x))[[1]]
358-
spec <- glue::glue("{spec}()")
359-
} else {
360-
spec <- "None"
361-
}
362-
363-
spec_msg <- glue::glue("{spec_msg} {spec}")
364-
cat_line(spec_msg)
365-
366-
postprocessor_msg <- cli::style_italic("Postprocessor:")
367-
postprocessor <- ifelse(has_postprocessor_frosting(x), "Frosting", "None")
368-
postprocessor_msg <- glue::glue("{postprocessor_msg} {postprocessor}")
369-
cat_line(postprocessor_msg)
370-
371-
invisible(x)
372-
}

R/frosting.R

+7-54
Original file line numberDiff line numberDiff line change
@@ -415,60 +415,13 @@ print.frosting <- function(x, form_width = 30, ...) {
415415
cli::cli_h1("Frosting")
416416

417417
if (!is.null(x$layers)) cli::cli_h3("Layers")
418-
i <- 1
419-
for (layer in x$layers) {
420-
cat(paste0(i, ". "))
421-
print(layer, form_width = form_width)
422-
i <- i + 1
423-
}
424-
cli::cli_end()
425-
invisible(x)
426-
}
427-
428-
# Currently only used in the workflow printing
429-
print_frosting <- function(x, ...) {
430-
layers <- x$layers
431-
n_layers <- length(layers)
432-
layer <- ifelse(n_layers == 1L, "Layer", "Layers")
433-
n_layers_msg <- glue::glue("{n_layers} Frosting {layer}")
434-
cat_line(n_layers_msg)
435-
436-
if (n_layers == 0L) {
437-
return(invisible(x))
438-
}
439-
440-
cat_line("")
441-
442-
layer_names <- map_chr(layers, pull_layer_name)
443-
444-
if (n_layers <= 10L) {
445-
cli::cli_ol(layer_names)
446-
return(invisible(x))
447-
}
448-
449-
extra_layers <- n_layers - 10L
450-
layer_names <- layer_names[1:10]
451-
452-
layer <- ifelse(extra_layers == 1L, "layer", "layers")
453-
454-
extra_dots <- "..."
455-
extra_msg <- glue::glue("and {extra_layers} more {layer}.")
456-
457-
cli::cli_ol(layer_names)
458-
cli::cli_bullets(c(extra_dots, extra_msg))
459-
invisible(x)
460-
}
461-
462-
print_postprocessor <- function(x) {
463-
if (!has_postprocessor_frosting(x)) {
464-
return(invisible(x))
465-
}
466-
467-
header <- cli::rule("Postprocessor")
468-
cat_line(header)
469-
470-
frost <- extract_frosting(x)
471-
print_frosting(frost)
472418

419+
fmt <- cli::cli_fmt({
420+
for (layer in x$layers) {
421+
print(layer, form_width = form_width)
422+
}
423+
})
424+
cli::cli_ol(fmt)
425+
cli::cli_end()
473426
invisible(x)
474427
}

0 commit comments

Comments
 (0)