## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  fig.width = 7,
  fig.height = 4.5,
  fig.alt = "ggplot2 chart showing biologically meaningful weather summaries."
)

## ----packages, message=FALSE, warning=FALSE-----------------------------------
library(windcut)
library(dplyr)
library(ggplot2)
library(tidyr)
library(cowplot)

## -----------------------------------------------------------------------------
data(window_pane_demo_data)

weather <- window_pane_demo_data$weather
assessments <- window_pane_demo_data$assessments

knitr::kable(head(weather))

## -----------------------------------------------------------------------------
weather <- weather |>
  derive_vpd(daily_mean_temp, daily_mean_rh, name = "daily_vpd") |>
  derive_leaf_wetness_from_rh(
    daily_mean_rh,
    threshold = 85,
    name = "daily_leaf_wetness_est"
  )

weather |>
  select(site_id, time, daily_mean_temp, daily_mean_rh, daily_vpd, daily_leaf_wetness_est) |>
  slice_head(n = 8) |>
  knitr::kable()

## -----------------------------------------------------------------------------
example_site <- assessments$site_id[1]
example_reference <- assessments %>%
  filter(site_id == example_site) %>%
  pull(assessment_time)

derived_plot_data <- weather %>%
  filter(site_id == example_site) %>%
  filter(time >= example_reference - 35 * 86400, time <= example_reference) %>%
  select(time, daily_mean_rh, daily_vpd, daily_leaf_wetness_est) %>%
  pivot_longer(
    cols = c(daily_mean_rh, daily_vpd, daily_leaf_wetness_est),
    names_to = "variable",
    values_to = "value"
  )

ggplot(derived_plot_data, aes(time, value)) +
  geom_vline(
    xintercept = example_reference,
    linetype = "dashed",
    color = "#20262e",
    linewidth = 0.7
  ) +
  geom_line(color = "#3f7d58", linewidth = 0.8) +
  facet_wrap(~ variable, scales = "free_y", ncol = 1) +
  labs(
    title = "Derived variables make the weather signal easier to inspect",
    subtitle = "The dashed line marks the disease assessment date",
    x = NULL,
    y = NULL
  ) +
  theme_half_open()

## -----------------------------------------------------------------------------
summary_statistics <- list(
  daily_mean_temp = list(
    mean = "mean",
    days_18_26 = count_between(18, 26),
    thermal_time_10 = degree_hours_above(10)
  ),
  daily_mean_rh = list(
    mean = "mean",
    humid_days = humid_hours(85),
    prop_humid = proportion_at_or_above(85)
  ),
  daily_vpd = list(
    mean = "mean",
    dry_days = count_above(1.2)
  ),
  daily_sum_rain = list(
    total = "sum",
    rainy_days = rainy_days(0),
    rain_events = rain_events(0.2)
  ),
  daily_leaf_wetness_est = list(
    wet_days = wet_days(0),
    max_wet_spell = max_consecutive_wet_hours(0)
  )
)

## -----------------------------------------------------------------------------
rule_plot_data <- weather %>%
  filter(site_id == example_site) %>%
  filter(time >= example_reference - 35 * 86400, time <= example_reference) %>%
  mutate(
    warm_18_26 = daily_mean_temp >= 18 & daily_mean_temp <= 26,
    humid = daily_mean_rh >= 85,
    wet_estimated = daily_leaf_wetness_est > 0
  )

ggplot(rule_plot_data, aes(time, daily_mean_temp)) +
  geom_vline(
    xintercept = example_reference,
    linetype = "dashed",
    color = "#20262e",
    linewidth = 0.7
  ) +
  geom_ribbon(
    aes(ymin = 18, ymax = 26),
    fill = "#6ea87d",
    alpha = 0.12
  ) +
  geom_line(color = "#20262e", linewidth = 0.8) +
  geom_point(
    data = rule_plot_data %>% filter(warm_18_26 & humid),
    color = "#c47f2c",
    size = 2.4
  ) +
  geom_point(
    data = rule_plot_data %>% filter(wet_estimated),
    aes(y = 17),
    color = "#2b6c4f",
    size = 2,
    alpha = 0.85
  ) +
  labs(
    title = "Biological rules can be inspected before feature generation",
    subtitle = "Orange points are warm-and-humid days; green points along the bottom are estimated wet days",
    x = NULL,
    y = "Daily mean temperature (deg C)"
  ) +
  theme_half_open()

## -----------------------------------------------------------------------------
windows <- make_windows(
  min_offset = -28,
  max_offset = 0,
  width = 7,
  slide_by = 7,
  reference_col = "assessment_time"
)

features <- window_pane(
  weather = weather,
  assessments = assessments,
  windows = windows,
  reference_col = "assessment_time",
  id_col = "site_id",
  response_col = "disease_intensity",
  unit = "days",
  weather_cols = c(
    "daily_mean_temp",
    "daily_mean_rh",
    "daily_vpd",
    "daily_sum_rain",
    "daily_leaf_wetness_est"
  ),
  statistics = summary_statistics
)

features %>%
  select(1:12) %>%
  slice_head(n = 6) %>%
  knitr::kable()

## -----------------------------------------------------------------------------
example_columns <- features |>
  select(
    site_id,
    contains("daily_mean_temp_days_18_26"),
    contains("daily_mean_temp_thermal_time_10"),
    contains("daily_mean_rh_humid_days"),
    contains("daily_sum_rain_rain_events"),
    contains("daily_leaf_wetness_est_max_wet_spell")
  ) |>
  select(1:11)

example_columns |>
  slice_head(n = 5) |>
  knitr::kable()

## -----------------------------------------------------------------------------
class_plot_data <- features |>
  filter(site_id == example_site) |>
  select(
    contains("daily_mean_temp_days_18_26"),
    contains("daily_mean_rh_humid_days"),
    contains("daily_sum_rain_rain_events"),
    contains("daily_leaf_wetness_est_max_wet_spell")
  ) |>
  pivot_longer(
    cols = everything(),
    names_to = "feature",
    values_to = "value"
  ) |>
  mutate(
    window = sub(".*_window_", "window_", feature),
    summary_class = case_when(
      grepl("days_18_26", feature) ~ "Temperature range count",
      grepl("humid_days", feature) ~ "Humidity exposure",
      grepl("rain_events", feature) ~ "Rain events",
      grepl("max_wet_spell", feature) ~ "Wet-spell duration",
      TRUE ~ "Summary"
    ),
    window = factor(window, levels = unique(window))
  )

ggplot(class_plot_data, aes(window, value, fill = summary_class)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~ summary_class, scales = "free_y", ncol = 2) +
  labs(
    title = "Different function classes create different biological summaries",
    subtitle = "Example values for one site across 7-day candidate windows",
    x = "Candidate window",
    y = "Summary value"
  ) +
  theme_half_open() +
  theme(axis.text.x = element_text(angle = 35, hjust = 1))

