Skip to content

Commit

Permalink
style closes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
joethorley committed Jan 23, 2025
1 parent 6d225d3 commit 22c1e4b
Show file tree
Hide file tree
Showing 13 changed files with 1,103 additions and 1,097 deletions.
110 changes: 59 additions & 51 deletions R/classify-time-series-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
#' The data is processed by:
#'
#' 1. Classifying the temperature values based on their values
#' (questionable_min, questionable_max, erroneous_min, erroneous_max).
#'
#' (questionable_min, questionable_max, erroneous_min, erroneous_max).
#'
#' 2. The
#' rate of change between adjacent values is calculate and values are
#' classified based on the rate parameters (questionable_rate,
#' erroneous_rate).
#'
#' erroneous_rate).
#'
#' 3. Adjacent values to questionable/erroneous are coded as
#' questionable/erroneous.
#'
#' questionable/erroneous.
#'
#' 4. A buffer is applied that any value within the
#' buffer is classified as questionable/erroneous based on the buffer
#' parameters (questionable_buffer, erroneous_buffer).
#'
#' parameters (questionable_buffer, erroneous_buffer).
#'
#' 5. Reasonable values
#' identified between two questionable/erroneous values are coded as
#' questionable/erroneous based on the gap hour difference allowed
Expand Down Expand Up @@ -72,28 +72,29 @@ classify_time_series_data <- function(data,
erroneous_rate = erroneous_rate,
questionable_buffer = questionable_buffer,
erroneous_buffer = erroneous_buffer,
gap_range = gap_range)

gap_range = gap_range
)

data <- data |>
duckplyr::rename(.date_time = date_time, .value = value) |>
duckplyr::arrange(.data$.date_time) |>
duckplyr::mutate(status_id = rep(NA_integer_, nrow(data))) |>
set_status_id()

missing_rows <- data |>
duckplyr::filter(is.na(.data$.value))

lookup <- c(".date_time", ".value") |>
rlang::set_names(c(date_time, value))
if(identical(nrow(missing_rows), nrow(data))) {

if (identical(nrow(missing_rows), nrow(data))) {
data <- data |>
duckplyr::rename(duckplyr::all_of(lookup))
return(data)
return(data)
}

tz <- attr(data$.date_time, "tzone")

data <- data |>
duckplyr::filter(!is.na(.data$.value)) |>
duckplyr::mutate(
Expand All @@ -110,37 +111,43 @@ classify_time_series_data <- function(data,
TRUE ~ 1L
),
status_id = pmax(
.data$status_id,
duckplyr::lag(.data$status_id),
.data$status_id,
duckplyr::lag(.data$status_id),
duckplyr::lead(.data$status_id),
na.rm = TRUE
)
)
) |>
duckplyr::select(
!".rate"
)

questionable_range <- data |>
duckplyr::filter(.data$status_id == 2L) |>
duckplyr::mutate(.start_date_time = .data$.date_time - questionable_buffer * 3600,
.end_date_time = .data$.date_time + questionable_buffer * 3600,
.keep = "none")

duckplyr::mutate(
.start_date_time = .data$.date_time - questionable_buffer * 3600,
.end_date_time = .data$.date_time + questionable_buffer * 3600,
.keep = "none"
)

erroneous_range <- data |>
duckplyr::filter(.data$status_id == 3L) |>
duckplyr::mutate(.start_date_time = .data$.date_time - erroneous_buffer * 3600,
.end_date_time = .data$.date_time + erroneous_buffer * 3600,
.keep = "none")

duckplyr::mutate(
.start_date_time = .data$.date_time - erroneous_buffer * 3600,
.end_date_time = .data$.date_time + erroneous_buffer * 3600,
.keep = "none"
)

gap <- data |>
duckplyr::filter(.data$status_id != 1L) |>
duckplyr::mutate(.status_id = pmax(.data$status_id, duckplyr::lead(.data$status_id), na.rm = TRUE),
.status_id = 2L, # TODO 2L, pmin or pmax?? - pmax
.start_date_time = .data$.date_time,
.end_date_time = duckplyr::lead(.data$.date_time),
.keep = "none") |>
duckplyr::mutate(
.status_id = pmax(.data$status_id, duckplyr::lead(.data$status_id), na.rm = TRUE),
.status_id = 2L, # TODO 2L, pmin or pmax?? - pmax
.start_date_time = .data$.date_time,
.end_date_time = duckplyr::lead(.data$.date_time),
.keep = "none"
) |>
duckplyr::filter(.data$.end_date_time - .data$.start_date_time <= gap_range * 3600)

data <- data |>
dplyr::left_join(questionable_range, by = dplyr::join_by(closest(x$.date_time >= y$.start_date_time))) |>
duckplyr::mutate(status_id = duckplyr::if_else(.data$status_id == 1L & .data$.date_time <= .data$.end_date_time, 2L, .data$status_id, .data$status_id)) |>
Expand Down Expand Up @@ -172,47 +179,47 @@ check_time_series_args <- function(data,
questionable_buffer = 1,
erroneous_buffer = 1,
gap_range = 5) {

chk::chk_data(data)
chk::chk_unused(...)
chk::chk_string(date_time)
chk::chk_string(value)

values <- list(
as.POSIXct("2021-05-07 08:00:00"),
c(1, NA_real_)) |>
as.POSIXct("2021-05-07 08:00:00"),
c(1, NA_real_)
) |>
rlang::set_names(c(date_time, value))

chk::check_data(data, values = values)
chk::chk_unique(data[[date_time]], x_name = paste0("`data$", date_time, "`"))

chk::chk_not_subset(colnames(data), reserved_colnames())

chk::chk_number(questionable_min)
chk::chk_number(questionable_max)
chk::chk_gt(questionable_max, questionable_min)

chk::chk_number(erroneous_min)
chk::chk_number(erroneous_max)
chk::chk_gt(erroneous_max, erroneous_min)

chk::chk_gte(erroneous_max, questionable_max)
chk::chk_lte(erroneous_min, questionable_min)

chk::chk_number(questionable_rate)
chk::chk_gt(questionable_rate)

chk::chk_number(erroneous_rate)
chk::chk_gt(erroneous_rate)

chk::chk_gte(erroneous_rate, questionable_rate)

chk::chk_number(questionable_buffer)
chk::chk_gte(questionable_buffer)

chk::chk_number(erroneous_buffer)
chk::chk_gte(erroneous_buffer)

chk::chk_number(gap_range)
chk::chk_gte(gap_range)
}
Expand All @@ -224,7 +231,8 @@ set_status_id <- function(data) {
.data$status_id == 3L ~ "erroneous",
.data$status_id == 2L ~ "questionable",
.data$status_id == 1L ~ "reasonable",
TRUE ~ NA_character_),
TRUE ~ NA_character_
),
status_id = ordered(
.data$status_id,
levels = c("reasonable", "questionable", "erroneous"),
Expand Down
13 changes: 6 additions & 7 deletions R/date-atus.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Calculate Date of Accumulated Thermal Units (ATUs)
#'
#' A wrapper on [gsdd::date_atus()] to calculats the date on which a
#'
#' A wrapper on [gsdd::date_atus()] to calculats the date on which a
#' specified number of Accumulated Thermal Units (ATUs) are exceeded.
#'
#' @inheritParams gsdd::date_atus
Expand All @@ -12,11 +12,10 @@
date_atus <- function(
x,
atus = 600,
start_date = as.Date("1972-03-01")
) {

start_date = as.Date("1972-03-01")) {
gsdd::date_atus(
x,
x,
atus = atus,
start_date = start_date)
start_date = start_date
)
}
14 changes: 7 additions & 7 deletions R/gdd.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Calculate Growing Degree Days (GDD)
#'
#'
#' A wrapper on [gsdd::gdd()] to get the Growing Degree Days up to a date for
#' the longest growing season.
#'
Expand All @@ -13,15 +13,15 @@ gdd <- function(
x,
end_date = as.Date("1972-09-30"),
min_length = 60,
msgs = TRUE
) {
msgs = TRUE) {
chk_whole_number(min_length)
chk_range(min_length, c(14, 213))

gsdd::gdd(
x,
x,
end_date = end_date,
min_length = min_length,
min_length = min_length,
pick = "longest",
msgs = msgs)
msgs = msgs
)
}
14 changes: 7 additions & 7 deletions R/gsdd-cf.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Calculate Growing Season Degree Days (GSDD)
#'
#'
#' Soft-deprecated for [gsdd::gsdd_vctr()].
#'
#' @inheritParams gsdd::gsdd_vctr
Expand All @@ -14,15 +14,15 @@ gsdd_cf <- function(
x,
ignore_truncation = FALSE,
min_length = 120,
msgs = TRUE
) {
msgs = TRUE) {
lifecycle::deprecate_soft("0.1.2", "gsdd_cf()", with = "gsdd::gsdd_vctr()")
chk_whole_number(min_length)
chk_range(min_length, c(14, 274))

gsdd::gsdd_vctr(
x,
x,
ignore_truncation = ignore_truncation,
min_length = min_length,
msgs = msgs)
min_length = min_length,
msgs = msgs
)
}
14 changes: 7 additions & 7 deletions R/gsdd.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' A wrapper on [gsdd::gsdd()] to get the Growing Season Degree Days for
#' the longest growing season.
#'
#'
#' @inheritParams gsdd::gsdd
#' @seealso [gsdd::gsdd()], [gsdd()] and [gss()].
#' @export
Expand All @@ -12,14 +12,14 @@
gsdd <- function(
x,
min_length = 120,
msgs = TRUE
) {
msgs = TRUE) {
chk_whole_number(min_length)
chk_range(min_length, c(14, 274))

gsdd::gsdd(
x,
min_length = min_length,
x,
min_length = min_length,
pick = "longest",
msgs = msgs)
msgs = msgs
)
}
16 changes: 8 additions & 8 deletions R/gss-plot.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Plot Growing Seasons (GSS)
#'
#' A wrapper on [gsdd::gss_plot()] to by default
#'
#' A wrapper on [gsdd::gss_plot()] to by default
#' plot all Growing Seasons ignoring truncation.
#' For more information see [gsdd::gss_plot()].
#'
Expand All @@ -18,18 +18,18 @@ gss_plot <- function(
latex = FALSE,
nrow = NULL,
ncol = NULL,
msgs = TRUE
) {
msgs = TRUE) {
chk_whole_number(min_length)
chk_range(min_length, c(14, 274))

gsdd::gss_plot(
x,
min_length = min_length,
x,
min_length = min_length,
ignore_truncation = ignore_truncation,
pick = pick,
latex = latex,
nrow = nrow,
ncol = ncol,
msgs = msgs)
msgs = msgs
)
}
16 changes: 8 additions & 8 deletions R/gss.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Calculate Growing Seasons (GSS)
#'
#' A wrapper on [gsdd::gss()] to by default
#'
#' A wrapper on [gsdd::gss()] to by default
#' get all Growing Seasons ignoring truncation.
#' For more information see [gsdd::gss()].
#'
Expand All @@ -15,15 +15,15 @@ gss <- function(
min_length = 120,
ignore_truncation = TRUE,
pick = "all",
msgs = TRUE
) {
msgs = TRUE) {
chk_whole_number(min_length)
chk_range(min_length, c(14, 274))

gsdd::gss(
x,
min_length = min_length,
x,
min_length = min_length,
ignore_truncation = ignore_truncation,
pick = pick,
msgs = msgs)
msgs = msgs
)
}
Loading

0 comments on commit 22c1e4b

Please sign in to comment.