Skip to content

Compute and print time elapsed #224

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

Merged
merged 6 commits into from
Nov 1, 2019
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Imports:
stats,
tidyr,
globals,
prettyunits,
vctrs (>= 0.2.0)
Roxygen: list(markdown = TRUE)
RoxygenNote: 6.1.99.9001
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# parsnip 0.0.3.9001

## New Features

* The time elapsed during model fitting is stored in the `$elapsed` slot of the
parsnip model object, and is printed when the model object is printed.

* Some default parameter ranges were updated for SVM, KNN, and MARS models.

## Fixes

* [A bug](https://github.com/tidymodels/parsnip/issues/208) was fixed related to using data descriptors and `fit_xy()`.

* A bug was fixed related to the column names generated by `multi_predict()`. The top-level tibble will always have a column named `.pred` and this list column contains tibbles across sub-models. The column names for these sub-model tibbles will have names consistent with `predict()` (which was previously incorrect). See [43c15db](https://github.com/tidymodels/parsnip/commit/43c15db377ea9ef27483ff209f6bd0e98cb830d2).
Expand Down
1 change: 1 addition & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ check_xy_interface <- function(x, y, cl, model) {
#' @export
print.model_fit <- function(x, ...) {
cat("parsnip model object\n\n")
cat("Fit in: ", prettyunits::pretty_sec(x$elapsed[["elapsed"]]))

if (inherits(x$fit, "try-error")) {
cat("Model fit failed with error:\n", x$fit, "\n")
Expand Down
32 changes: 20 additions & 12 deletions R/fit_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ form_form <-
spec = object
)

res$fit <- eval_mod(
fit_call,
capture = control$verbosity == 0,
catch = control$catch,
env = env,
...
elapsed <- system.time(
res$fit <- eval_mod(
fit_call,
capture = control$verbosity == 0,
catch = control$catch,
env = env,
...
)
)
res$preproc <- list(y_var = all.vars(env$formula[[2]]))
res$elapsed <- elapsed
res
}

Expand Down Expand Up @@ -107,19 +110,24 @@ xy_xy <- function(object, env, control, target = "none", ...) {

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

res$fit <- eval_mod(
fit_call,
capture = control$verbosity == 0,
catch = control$catch,
env = env,
...

elapsed <- system.time(
res$fit <- eval_mod(
fit_call,
capture = control$verbosity == 0,
catch = control$catch,
env = env,
...
)
)

if (is.vector(env$y)) {
y_name <- character(0)
} else {
y_name <- colnames(env$y)
}
res$preproc <- list(y_var = y_name)
res$elapsed <- elapsed
res
}

Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test_fit_interfaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,20 @@ test_that('unknown modes', {
)
})

test_that("elapsed time parsnip mods", {
lm1 <-
linear_reg() %>%
set_engine("lm") %>%
fit_xy(x = mtcars[, 2:4], y = mtcars$mpg)

lm2 <-
linear_reg() %>%
set_engine("lm") %>%
fit(mpg ~ ., data = mtcars)

expect_output(print(lm1), "Fit in:")
expect_output(print(lm2), "Fit in:")
expect_true(!is.null(lm1$elapsed))
expect_true(!is.null(lm2$elapsed))
})

1 change: 1 addition & 0 deletions tests/testthat/test_linear_reg_keras.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ test_that('model fitting', {
),
regexp = NA
)
fit1$elapsed <- fit2$elapsed
expect_equal(fit1, fit2)

expect_error(
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test_logistic_reg_keras.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ test_that('model fitting', {
),
regexp = NA
)
fit1$elapsed <- fit2$elapsed
expect_equal(fit1, fit2)

expect_error(
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test_multinom_reg_keras.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ test_that('model fitting', {
),
regexp = NA
)
fit1$elapsed <- fit2$elapsed
expect_equal(fit1, fit2)

expect_error(
Expand Down