Skip to content

Commit

Permalink
whattheflux.quiet=TRUE should not silence warnings (#24)
Browse files Browse the repository at this point in the history
* Check for missing metadata dates and times
* whattheflux.quiet=TRUE should not silence warnings
  • Loading branch information
bpbond authored Feb 17, 2024
1 parent 182bd76 commit 711563f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
11 changes: 10 additions & 1 deletion R/metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ wtf_metadata_match <- function(data_timestamps,
stopifnot(length(start_dates) == length(dead_bands))
stopifnot(length(start_dates) == length(obs_lengths))

# The metadata dates and times shouldn't be empty
if(any(is.na(start_dates))) {
warning("One or more metadata dates are missing")
}
if(any(is.na(start_times))) {
warning("One or more metadata times are missing")
}

# Convert things to POSIXct and check validity
if(is.character(data_timestamps)) data_timestamps <- ymd_hms(data_timestamps)
stopifnot(is.POSIXct(data_timestamps))

Expand All @@ -114,7 +123,7 @@ wtf_metadata_match <- function(data_timestamps,
# single machine that generated the data
ord <- order(start_timestamps)
overlaps <- head(stop_timestamps[ord], -1) >= tail(start_timestamps[ord], -1)
if(any(overlaps)) {
if(any(overlaps, na.rm = TRUE)) {
stop("start_timestamps overlaps: ",
paste(which(overlaps) + 1, collapse = ", "))
}
Expand Down
6 changes: 3 additions & 3 deletions R/models.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ wtf_fit_models <- function(time, conc) {
# Basic linear model
try(mod <- lm(conc ~ time))
if(!exists("mod")) {
wtf_warning("Could not fit linear model")
warning("Could not fit linear model")
return(NULL)
}

Expand All @@ -48,7 +48,7 @@ wtf_fit_models <- function(time, conc) {
coefficients(robust)[2]
},
error = function(e) {
wtf_warning("Could not fit robust linear model")
warning("Could not fit robust linear model")
NA_real_
})

Expand All @@ -58,7 +58,7 @@ wtf_fit_models <- function(time, conc) {
summary(poly)$r.squared
},
error = function(e) {
wtf_warning("Could not fit polynomial model")
warning("Could not fit polynomial model")
NA_real_
})

Expand Down
7 changes: 0 additions & 7 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,3 @@ wtf_message <- function(...) {
}
message(...)
}

wtf_warning <- function(...) {
if (getOption("whattheflux.quiet", default = FALSE)) {
return()
}
warning(...)
}
10 changes: 10 additions & 0 deletions tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ test_that("wtf_metadata_match works", {
expect_silent(x <- wtf_metadata_match(d_t, s_d, s_t, db, ol))
expect_identical(x, c(1, 1, 2, NA_real_))

# Warns on missing metadata dates
suppressMessages({
s_d[1] <- NA
expect_warning(wtf_metadata_match(d_t, s_d, s_t, db, ol),
regexp = "dates are missing")
})
# We also warn on missing times, but checking that requires a nested
# expect_warning here, because of hms() behavior, which crashes
# on GitHub Actions

# Gives a message if there are unmatched metadata entries
s_d <- c("2024-01-01", "2024-01-01", "2024-01-10")
s_t <- c("13:00:00", "13:05:00", "13:10:00")
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ test_that("quiet functionality works", {
# Check that the wtf_message and wtf_warning functions respect quiet option
withr::local_options(whattheflux.quiet = TRUE)
expect_no_message(wtf_message("hi"))
expect_no_warning(wtf_warning("hi"))

withr::local_options(whattheflux.quiet = FALSE)
expect_message(wtf_message("hi"), regexp = "hi")
expect_warning(wtf_warning("hi"), regexp = "hi")
})

0 comments on commit 711563f

Please sign in to comment.