Skip to content

Commit ce3fc8c

Browse files
authored
Merge pull request #237 from cmu-delphi/ndefries/int-addl-capacity
Parse int fields as double so large values don't get set to NA
2 parents ff68c45 + a25797f commit ce3fc8c

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

R/model.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,19 @@ parse_value <- function(info, value, disable_date_parsing = FALSE) {
156156
} else if (info$type == "bool") {
157157
return(as.logical(value))
158158
} else if (info$type == "int") {
159-
return(as.integer(value))
159+
# Int doesn't have enough capacity to store some weekly `pub_wiki` values.
160+
value <- as.double(value)
161+
if (any(value != round(value))) {
162+
cli::cli_warn(
163+
c(
164+
"Values in {info$name} were expected to be integers but contain a decimal component",
165+
"i" = "Decimal components are returned as-is"
166+
),
167+
class = "epidatr__int_nonzero_decimal_digits"
168+
)
169+
}
170+
171+
return(value)
160172
} else if (info$type == "float") {
161173
return(as.double(value))
162174
} else if (info$type == "categorical") {

man/covidcast_epidata.Rd

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

tests/testthat/test-model.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ test_that("parse_data_frame warns when df contains fields not listed in meta", {
100100
expect_no_warning(parse_data_frame(epidata_call, mock_df))
101101
})
102102

103+
test_that("parse_data_frame warns when df contains int values with decimal component", {
104+
epidata_call <- pub_flusurv(
105+
locations = "ca",
106+
epiweeks = 202001,
107+
fetch_args = fetch_args_list(dry_run = TRUE)
108+
)
109+
# see generate_test_data.R
110+
mock_df <- as.data.frame(readr::read_rds(testthat::test_path("data/flusurv-epiweeks.rds")))
111+
112+
# Int fields are returned as double
113+
result <- parse_data_frame(epidata_call, mock_df)
114+
expect_type(result$lag, "double")
115+
116+
# Replace int fields with decimal
117+
mock_df$lag <- 4.3
118+
119+
# Warning when int values have a decimal component
120+
expect_warning(
121+
parse_data_frame(epidata_call, mock_df),
122+
class = "epidatr__int_nonzero_decimal_digits"
123+
)
124+
})
125+
103126
test_that("parse_api_date accepts str and int input", {
104127
expect_identical(parse_api_date("20200101"), as.Date("2020-01-01"))
105128
expect_identical(parse_api_date(20200101), as.Date("2020-01-01"))

0 commit comments

Comments
 (0)