Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 40e8778

Browse files
committedMar 10, 2025·
tests(epi_slide_opt): more + failing test for epi_slide_opt.epi_archive()
1 parent 0435460 commit 40e8778

File tree

1 file changed

+75
-17
lines changed

1 file changed

+75
-17
lines changed
 

‎tests/testthat/test-epi_slide_opt_archive.R

+75-17
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test_that("epi_slide_opt_archive_one_epikey works as expected", {
3535
),
3636
tibble(
3737
version = 13, time_value = 8:10, value = c(9, 9, 10),
38-
slide_value = frollmean(c(6, 7, 9, 9, 10), 3, algo = "exact")[-(1:2)]
38+
slide_value = frollmean(c(6, 7, 9, 9, 10), 3, algo = "exact")[-(1:2)]
3939
),
4040
tibble(
4141
version = 14, time_value = 11:13, value = c(NA, 12, 13), slide_value = rep(NA_real_, 3L)
@@ -89,7 +89,6 @@ test_that("epi_slide_opt.epi_archive is not confused by unique(DT$version) unsor
8989
})
9090

9191
test_that("epi_slide_opt.epi_archive is not confused by unique(DT$time_value) unsorted", {
92-
9392
start_date <- as.Date("2020-01-01")
9493
tibble(
9594
geo_value = c(1, 1, 2, 2),
@@ -109,26 +108,85 @@ test_that("epi_slide_opt.epi_archive is not confused by unique(DT$time_value) un
109108
) %>%
110109
as_epi_archive()
111110
)
112-
113111
})
114112

115-
test_that("epi_slide_opt.epi_archive is equivalent to epix_slide reconversion on example data", {
113+
test_that("epi_slide_opt.epi_archive gives expected results on example data", {
114+
# vs. built-in case_rate_7d_av column:
115+
116+
case_death_rate_archive_time <- system.time(
117+
case_death_rate_archive_result <- case_death_rate_archive %>%
118+
{
119+
as_tibble(as.data.frame(.$DT))
120+
} %>%
121+
select(-case_rate_7d_av) %>%
122+
as_epi_archive() %>%
123+
epi_slide_opt(case_rate, frollmean, algo = "exact", .window_size = 7, .suffix = "_{.n}d_av")
124+
)
125+
126+
case_death_rate_archive_expected <- case_death_rate_archive %>%
127+
{
128+
as_tibble(as.data.frame(.$DT))
129+
} %>%
130+
relocate(case_rate_7d_av, .after = last_col()) %>%
131+
as_epi_archive() # ensure compact
132+
133+
expect_equal(case_death_rate_archive_result, case_death_rate_archive_expected)
134+
135+
tbl_diff2(
136+
case_death_rate_archive_expected$DT[geo_value == "ak" & time_value <= as.Date("2020-03-16")] %>%
137+
as.data.frame() %>% as_tibble(),
138+
case_death_rate_archive_result$DT[geo_value == "ak" & time_value <= as.Date("2020-03-16")] %>%
139+
as.data.frame() %>% as_tibble(),
140+
c("geo_value", "time_value", "version")
141+
)
142+
143+
case_death_rate_archive_result$DT[geo_value == "ak" & time_value <= as.Date("2020-03-16")]
144+
145+
# case_death_rate_archive_result %>%
146+
case_death_rate_archive_expected %>%
147+
epix_as_of(as.Date("2020-11-01")) %>%
148+
filter(geo_value == "ak", time_value <= as.Date("2020-03-16")) %>%
149+
# filter(between(time_value, as.Date("2020-03-09"), as.Date("2020-03-15"))) %>%
150+
mutate(d = c(NA, diff(case_rate_7d_av))) %>%
151+
{}
116152

117-
case_death_rate_archive %>%
118-
epi_slide_opt(case_rate, frollmean, .window_size = 7
119-
# , algo = "exact"
120-
) %>%
121-
.$DT %>%
122-
as.data.frame() %>%
123-
as_tibble() %>%
124-
filter(!approx_equal(case_rate_7dav, case_rate_7d_av, 1e-6, TRUE)) %>%
125-
dplyr::transmute(version, geo_value, time_value, case_rate_7dav, case_rate_7d_av,
126-
abs_diff = abs(case_rate_7dav - case_rate_7d_av)) %>%
127-
{}
153+
# vs. computing via epix_slide:
128154

129-
# TODO finish tests on example data sets
155+
mini_case_death_rate_archive <- case_death_rate_archive %>%
156+
{
157+
as_tibble(as.data.frame(.$DT))
158+
} %>%
159+
filter(geo_value %in% head(unique(geo_value), 4L)) %>%
160+
as_epi_archive()
130161

131-
})
162+
mini_case_death_rate_archive_time_opt <- system.time(
163+
mini_case_death_rate_archive_result <- mini_case_death_rate_archive %>%
164+
epi_slide_opt(case_rate, frollmean, .window_size = 7)
165+
)
166+
167+
mini_case_death_rate_archive_time_gen <- system.time(
168+
mini_case_death_rate_archive_expected <- mini_case_death_rate_archive %>%
169+
epix_slide(~ .x %>% epi_slide_opt(case_rate, frollmean, .window_size = 7)) %>%
170+
select(names(mini_case_death_rate_archive$DT), everything()) %>%
171+
as_epi_archive()
172+
)
173+
174+
expect_equal(mini_case_death_rate_archive_result, mini_case_death_rate_archive_expected)
175+
176+
archive_cases_dv_subset_time_opt <- system.time(
177+
archive_cases_dv_subset_result <- archive_cases_dv_subset %>%
178+
epi_slide_opt(percent_cli, frollmean, .window_size = 7)
179+
)
180+
181+
archive_cases_dv_subset_time_gen <- system.time(
182+
archive_cases_dv_subset_expected <- archive_cases_dv_subset %>%
183+
epix_slide(~ .x %>% epi_slide_opt(percent_cli, frollmean, .window_size = 7)) %>%
184+
select(geo_value, time_value, version, everything()) %>%
185+
as_epi_archive()
186+
)
187+
188+
expect_equal(archive_cases_dv_subset_result, archive_cases_dv_subset_expected)
189+
})
132190

133191

134192
# TODO grouped behavior checks

0 commit comments

Comments
 (0)
Please sign in to comment.