Skip to content

Commit 3a6d11f

Browse files
authored
Merge pull request #224 from patr1ckm/time-elapsed
Compute and print time elapsed
2 parents 3fef9c3 + 582def3 commit 3a6d11f

File tree

8 files changed

+49
-12
lines changed

8 files changed

+49
-12
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Imports:
2828
stats,
2929
tidyr,
3030
globals,
31+
prettyunits,
3132
vctrs (>= 0.2.0)
3233
Roxygen: list(markdown = TRUE)
3334
RoxygenNote: 6.1.99.9001

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# parsnip 0.0.3.9001
22

3+
## New Features
4+
5+
* The time elapsed during model fitting is stored in the `$elapsed` slot of the
6+
parsnip model object, and is printed when the model object is printed.
7+
38
* Some default parameter ranges were updated for SVM, KNN, and MARS models.
49

10+
## Fixes
11+
512
* [A bug](https://github.com/tidymodels/parsnip/issues/208) was fixed related to using data descriptors and `fit_xy()`.
613

714
* 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).

R/fit.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ check_xy_interface <- function(x, y, cl, model) {
367367
#' @export
368368
print.model_fit <- function(x, ...) {
369369
cat("parsnip model object\n\n")
370+
cat("Fit in: ", prettyunits::pretty_sec(x$elapsed[["elapsed"]]))
370371

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

R/fit_helpers.R

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ form_form <-
5252
spec = object
5353
)
5454

55-
res$fit <- eval_mod(
56-
fit_call,
57-
capture = control$verbosity == 0,
58-
catch = control$catch,
59-
env = env,
60-
...
55+
elapsed <- system.time(
56+
res$fit <- eval_mod(
57+
fit_call,
58+
capture = control$verbosity == 0,
59+
catch = control$catch,
60+
env = env,
61+
...
62+
)
6163
)
6264
res$preproc <- list(y_var = all.vars(env$formula[[2]]))
65+
res$elapsed <- elapsed
6366
res
6467
}
6568

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

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

110-
res$fit <- eval_mod(
111-
fit_call,
112-
capture = control$verbosity == 0,
113-
catch = control$catch,
114-
env = env,
115-
...
113+
114+
elapsed <- system.time(
115+
res$fit <- eval_mod(
116+
fit_call,
117+
capture = control$verbosity == 0,
118+
catch = control$catch,
119+
env = env,
120+
...
121+
)
116122
)
123+
117124
if (is.vector(env$y)) {
118125
y_name <- character(0)
119126
} else {
120127
y_name <- colnames(env$y)
121128
}
122129
res$preproc <- list(y_var = y_name)
130+
res$elapsed <- elapsed
123131
res
124132
}
125133

tests/testthat/test_fit_interfaces.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,20 @@ test_that('unknown modes', {
7979
)
8080
})
8181

82+
test_that("elapsed time parsnip mods", {
83+
lm1 <-
84+
linear_reg() %>%
85+
set_engine("lm") %>%
86+
fit_xy(x = mtcars[, 2:4], y = mtcars$mpg)
87+
88+
lm2 <-
89+
linear_reg() %>%
90+
set_engine("lm") %>%
91+
fit(mpg ~ ., data = mtcars)
92+
93+
expect_output(print(lm1), "Fit in:")
94+
expect_output(print(lm2), "Fit in:")
95+
expect_true(!is.null(lm1$elapsed))
96+
expect_true(!is.null(lm2$elapsed))
97+
})
98+

tests/testthat/test_linear_reg_keras.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ test_that('model fitting', {
4949
),
5050
regexp = NA
5151
)
52+
fit1$elapsed <- fit2$elapsed
5253
expect_equal(fit1, fit2)
5354

5455
expect_error(

tests/testthat/test_logistic_reg_keras.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ test_that('model fitting', {
6565
),
6666
regexp = NA
6767
)
68+
fit1$elapsed <- fit2$elapsed
6869
expect_equal(fit1, fit2)
6970

7071
expect_error(

tests/testthat/test_multinom_reg_keras.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ test_that('model fitting', {
5858
),
5959
regexp = NA
6060
)
61+
fit1$elapsed <- fit2$elapsed
6162
expect_equal(fit1, fit2)
6263

6364
expect_error(

0 commit comments

Comments
 (0)