Skip to content

Commit 7cf4115

Browse files
topepojuliasilge
andauthored
Updates to execution time collection (#614)
* system.time changes for #611 * Edits to documentation Co-authored-by: Julia Silge <[email protected]>
1 parent 3c88e50 commit 7cf4115

File tree

8 files changed

+82
-40
lines changed

8 files changed

+82
-40
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
## Other Changes
1616

1717
* When the xy interface is used and the underlying model expects to use a matrix, a better warning is issued when predictors contain non-numeric columns (including dates).
18+
19+
* The fit time is only calculated when the `verbosity` argument of `control_parsnip()` is 2L or greater. Also, the call to `system.time()` now uses `gcFirst = FALSE`. (#611)
1820

1921
# parsnip 0.1.7
2022

R/aaa.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ utils::globalVariables(
9090
"neighbors", ".submodels", "has_submodel", "max_neighbor", "max_penalty",
9191
"max_terms", "max_tree", "model", "name", "num_terms", "penalty", "trees",
9292
"sub_neighbors", ".pred_class", "x", "y", "predictor_indicators",
93-
"compute_intercept", "remove_intercept", "estimate", "term")
93+
"compute_intercept", "remove_intercept", "estimate", "term",
94+
"call_info", "component", "component_id", "func")
9495
)
9596

9697
# nocov end

R/control_parsnip.R

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
#' Control the fit function
22
#'
3-
#' Options can be passed to the [fit.model_spec()] function that control the output and
4-
#' computations
3+
#' Pass options to the [fit.model_spec()] function to control its
4+
#' output and computations
5+
#'
6+
#' @param verbosity An integer to control how verbose the output is. For a
7+
#' value of zero, no messages or output are shown when packages are loaded or
8+
#' when the model is fit. For a value of 1, package loading is quiet but model
9+
#' fits can produce output to the screen (depending on if they contain their
10+
#' own `verbose`-type argument). For a value of 2 or more, any output at all
11+
#' is displayed and the execution time of the fit is recorded and printed.
12+
#' @param catch A logical where a value of `TRUE` will evaluate the model
13+
#' inside of `try(, silent = TRUE)`. If the model fails, an object is still
14+
#' returned (without an error) that inherits the class "try-error".
15+
#' @return An S3 object with class "control_parsnip" that is a named list
16+
#' with the results of the function call
17+
#'
18+
#' @examples
19+
#' control_parsnip(verbosity = 2L)
520
#'
6-
#' @param verbosity An integer where a value of zero indicates
7-
#' that no messages or output should be shown when packages are
8-
#' loaded or when the model is fit. A value of 1 means that package
9-
#' loading is quiet but model fits can produce output to the screen
10-
#' (depending on if they contain their own `verbose`-type
11-
#' argument). A value of 2 or more indicates that any output should
12-
#' be seen.
13-
#' @param catch A logical where a value of `TRUE` will evaluate
14-
#' the model inside of `try(, silent = TRUE)`. If the model fails,
15-
#' an object is still returned (without an error) that inherits the
16-
#' class "try-error".
17-
#' @return An S3 object with class "fit_control" that is a named list with the
18-
#' results of the function call
1921
#' @details
2022
#' `fit_control()` is deprecated in favor of `control_parsnip()`.
2123
#' @export

R/fit.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ allow_sparse <- function(x) {
420420
#' @export
421421
print.model_fit <- function(x, ...) {
422422
cat("parsnip model object\n\n")
423-
cat("Fit time: ", prettyunits::pretty_sec(x$elapsed[["elapsed"]]), "\n")
423+
if (!is.na(x$elapsed[["elapsed"]])) {
424+
cat("Fit time: ", prettyunits::pretty_sec(x$elapsed[["elapsed"]]), "\n")
425+
}
424426

425427
if (inherits(x$fit, "try-error")) {
426428
cat("Model fit failed with error:\n", x$fit, "\n")

R/fit_helpers.R

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,27 @@ form_form <-
3838
spec = object
3939
)
4040

41-
elapsed <- system.time(
41+
if (control$verbosity > 1L) {
42+
elapsed <- system.time(
43+
res$fit <- eval_mod(
44+
fit_call,
45+
capture = control$verbosity == 0,
46+
catch = control$catch,
47+
env = env,
48+
...
49+
),
50+
gcFirst = FALSE
51+
)
52+
} else {
4253
res$fit <- eval_mod(
4354
fit_call,
4455
capture = control$verbosity == 0,
4556
catch = control$catch,
4657
env = env,
4758
...
4859
)
49-
)
60+
elapsed <- list(elapsed = NA_real_)
61+
}
5062
res$preproc <- list(y_var = all.vars(env$formula[[2]]))
5163
res$elapsed <- elapsed
5264
res
@@ -85,16 +97,27 @@ xy_xy <- function(object, env, control, target = "none", ...) {
8597

8698
res <- list(lvl = levels(env$y), spec = object)
8799

88-
89-
elapsed <- system.time(
100+
if (control$verbosity > 1L) {
101+
elapsed <- system.time(
102+
res$fit <- eval_mod(
103+
fit_call,
104+
capture = control$verbosity == 0,
105+
catch = control$catch,
106+
env = env,
107+
...
108+
),
109+
gcFirst = FALSE
110+
)
111+
} else {
90112
res$fit <- eval_mod(
91113
fit_call,
92114
capture = control$verbosity == 0,
93115
catch = control$catch,
94116
env = env,
95117
...
96118
)
97-
)
119+
elapsed <- list(elapsed = NA_real_)
120+
}
98121

99122
if (is.vector(env$y)) {
100123
y_name <- character(0)

man/control_parsnip.Rd

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

man/details_gen_additive_mod_mgcv.Rd

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

tests/testthat/test_fit_interfaces.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,27 @@ test_that("elapsed time parsnip mods", {
8484
lm1 <-
8585
linear_reg() %>%
8686
set_engine("lm") %>%
87-
fit_xy(x = mtcars[, 2:4], y = mtcars$mpg)
87+
fit_xy(x = mtcars[, 2:4], y = mtcars$mpg,
88+
control = control_parsnip(verbosity = 2L))
8889

8990
lm2 <-
9091
linear_reg() %>%
9192
set_engine("lm") %>%
92-
fit(mpg ~ ., data = mtcars)
93+
fit(mpg ~ ., data = mtcars,
94+
control = control_parsnip(verbosity = 2))
9395

9496
expect_output(print(lm1), "Fit time:")
9597
expect_output(print(lm2), "Fit time:")
9698
expect_true(!is.null(lm1$elapsed))
9799
expect_true(!is.null(lm2$elapsed))
100+
101+
lm3 <-
102+
linear_reg() %>%
103+
set_engine("lm") %>%
104+
fit_xy(x = mtcars[, 2:4], y = mtcars$mpg)
105+
106+
output3 <- capture.output(print(lm3))
107+
108+
expect_equal(sum(grepl("Fit time", output3)), 0)
98109
})
99110

0 commit comments

Comments
 (0)