Skip to content

Commit 7f08d40

Browse files
authored
Merge pull request #446 from cmu-delphi/check_name
drop `check_pname` in favor just just `check_name`
2 parents 93386d8 + 416e4c3 commit 7f08d40

9 files changed

+37
-62
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: epipredict
22
Title: Basic epidemiology forecasting methods
3-
Version: 0.1.12
3+
Version: 0.1.13
44
Authors@R: c(
55
person("Daniel J.", "McDonald", , "[email protected]", role = c("aut", "cre")),
66
person("Ryan", "Tibshirani", , "[email protected]", role = "aut"),

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
4242
- adjust default quantiles throughout so that they match.
4343
- force `layer_residual_quantiles()` to always include `0.5`.
4444
- Rename `recipes:::check_training_set()` to `recipes:::validate_training_data()`, as it changed in recipes 1.1.0.
45+
- A new column name duplicating an existing column name results in an error instead of a random name.
4546

4647
# epipredict 0.1
4748

R/layer_cdc_flatline_quantiles.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ slather.layer_cdc_flatline_quantiles <-
227227
) %>%
228228
select(all_of(c(avail_grps, ".pred_distn_all")))
229229

230-
# res <- check_pname(res, components$predictions, object)
231230
components$predictions <- left_join(
232231
components$predictions,
233232
res,

R/layer_point_from_distn.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ slather.layer_point_from_distn <-
9393
components$predictions$.pred <- dstn
9494
} else {
9595
dstn <- tibble(dstn = dstn)
96-
dstn <- check_pname(dstn, components$predictions, object)
96+
dstn <- check_name(
97+
dstn,
98+
components$predictions,
99+
object,
100+
newname = object$name
101+
)
97102
components$predictions <- mutate(components$predictions, !!!dstn)
98103
}
99104
components

R/layer_quantile_distn.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ slather.layer_quantile_distn <-
9696
dstn <- snap(dstn, truncate[1], truncate[2])
9797
}
9898
dstn <- tibble(dstn = dstn)
99-
dstn <- check_pname(dstn, components$predictions, object)
99+
dstn <- check_name(
100+
dstn,
101+
components$predictions,
102+
object,
103+
newname = object$name
104+
)
100105
components$predictions <- mutate(components$predictions, !!!dstn)
101106
components
102107
}

R/layer_residual_quantiles.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ slather.layer_residual_quantiles <-
139139

140140
estimate <- components$predictions$.pred
141141
res <- tibble(.pred_distn = r$dstn + estimate)
142-
res <- check_pname(res, components$predictions, object)
142+
res <- check_name(
143+
res,
144+
components$predictions,
145+
object,
146+
newname = object$name
147+
)
143148
components$predictions <- mutate(components$predictions, !!!res)
144149
components
145150
}

R/utils-misc.R

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,3 @@
1-
#' Check that newly created variable names don't overlap
2-
#'
3-
#' `check_pname` is to be used in a slather method to ensure that
4-
#' newly created variable names don't overlap with existing names.
5-
#' Throws an warning if check fails, and creates a random string.
6-
#' @param res A data frame or tibble of the newly created variables.
7-
#' @param preds An epi_df or tibble containing predictions.
8-
#' @param object A layer object passed to [slather()].
9-
#' @param newname A string of variable names if the object doesn't contain a
10-
#' $name element
11-
#'
12-
#' @keywords internal
13-
check_pname <- function(res, preds, object, newname = NULL) {
14-
if (is.null(newname)) newname <- object$name
15-
new_preds_names <- colnames(preds)
16-
intersection <- new_preds_names %in% newname
17-
if (any(intersection)) {
18-
newname <- rand_id(newname)
19-
rlang::warn(
20-
paste0(
21-
"Name collision occured in `",
22-
class(object)[1],
23-
"`. The following variable names already exists: ",
24-
paste0(new_preds_names[intersection], collapse = ", "),
25-
". Result instead has randomly generated string `",
26-
newname, "`."
27-
)
28-
)
29-
}
30-
names(res) <- newname
31-
res
32-
}
33-
341
# Copied from `epiprocess`:
352

363
#' "Format" a character vector of column/variable names for cli interpolation

man/check_pname.Rd

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/testthat/test-layer_residual_quantiles.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ test_that("Returns expected number or rows and columns", {
2828
expect_equal(unique(unnested$.pred_distn_quantile_level), c(.0275, 0.5, .8, .95))
2929
})
3030

31+
test_that("new name works correctly", {
32+
f <- frosting() %>%
33+
layer_predict() %>%
34+
layer_naomit(.pred) %>%
35+
layer_residual_quantiles(name = "foo")
36+
37+
wf1 <- wf %>% add_frosting(f)
38+
expect_equal(names(forecast(wf1)), c("geo_value", "time_value", ".pred", "foo"))
39+
40+
f <- frosting() %>%
41+
layer_predict() %>%
42+
layer_naomit(.pred) %>%
43+
layer_residual_quantiles(name = "geo_value")
44+
45+
wf1 <- wf %>% add_frosting(f)
46+
expect_error(forecast(wf1))
47+
})
3148

3249
test_that("Errors when used with a classifier", {
3350
tib <- tibble(

0 commit comments

Comments
 (0)