Skip to content

Commit

Permalink
simple compactify test, rename function, fix warn
Browse files Browse the repository at this point in the history
  • Loading branch information
dsweber2 committed Jul 31, 2024
1 parent 0bc62cd commit cd33ab1
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 27 deletions.
12 changes: 7 additions & 5 deletions R/archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,13 @@ new_epi_archive <- function(
nrow_before_compactify <- nrow(data_table)
# Runs compactify on data frame
if (is.null(compactify) || compactify == TRUE) {
data_table <- compactify(data_table, key_vars, compactify_tol)
compactified <- apply_compactify(data_table, key_vars, compactify_tol)
} else{

Check warning on line 322 in R/archive.R

View workflow job for this annotation

GitHub Actions / lint

file=R/archive.R,line=322,col=9,[brace_linter] There should be a space before an opening curly brace.
compactified <- data_table
}
# Warns about redundant rows if the number of rows decreased, and we didn't
# explicitly say to compactify
if (is.null(compactify) && nrow(data_table) < nrow_before_compactify) {
if (is.null(compactify) && nrow(compactified) < nrow_before_compactify) {
elim <- removed_by_compactify(data_table, key_vars, compactify_tol)
warning_intro <- cli::format_inline(
"Found rows that appear redundant based on
Expand All @@ -345,7 +347,7 @@ new_epi_archive <- function(

structure(
list(
DT = data_table,
DT = compactified,
geo_type = geo_type,
time_type = time_type,
additional_metadata = additional_metadata,
Expand All @@ -364,7 +366,7 @@ new_epi_archive <- function(
#' changed, and so is kept.
#' @keywords internal
#' @importFrom dplyr filter
compactify <- function(df, keys, tolerance = .Machine$double.eps^.5) {
apply_compactify <- function(df, keys, tolerance = .Machine$double.eps^.5) {
df %>%
arrange(!!!keys) %>%
filter(if_any(
Expand All @@ -379,7 +381,7 @@ compactify <- function(df, keys, tolerance = .Machine$double.eps^.5) {
removed_by_compactify <- function(df, keys, tolerance) {
df %>%
arrange(!!!keys) %>%
filter(if_any(
filter(if_all(
c(everything(), -version),
~ is_locf(., tolerance)
)) # nolint: object_usage_linter
Expand Down
2 changes: 1 addition & 1 deletion R/revision_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ revision_summary <- function(epi_arch,
if (should_compactify) {
revision_behavior <- revision_behavior %>%
arrange(across(c(geo_value, time_value, all_of(keys), version))) %>% # need to sort before compactifying
compactify(c(keys, version), compactify_tol)
apply_compactify(c(keys, version), compactify_tol)
}
revision_behavior <-
revision_behavior %>%
Expand Down
15 changes: 15 additions & 0 deletions man/apply_compactify.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions man/compactify.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/revision_summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions tests/testthat/_snaps/revision-latency-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@
* 2 out of 19 (10.53%)
Fraction of epi_key+time_values with
No revisions:
* 1 out of 7 (14.29%)
* 2 out of 7 (28.57%)
Quick revisions (last revision within 3 days of the `time_value`):
* 3 out of 7 (42.86%)
* 4 out of 7 (57.14%)
Few revisions (At most 3 revisions for that `time_value`):
* 6 out of 7 (85.71%)
Fraction of revised epi_key+time_values which have:
Less than 0.1 spread in relative value:
* 3 out of 6 (50%)
* 2 out of 5 (40%)
Spread of more than 5.1 in actual value (when revised):
* 3 out of 6 (50%)
* 3 out of 5 (60%)
days until within 20% of the latest value:
Output
min median mean max
Expand All @@ -76,10 +76,10 @@
time_value geo_value n_revisions min_lag max_lag spread rel_spread
<date> <chr> <dbl> <drtn> <drtn> <dbl> <dbl>
1 2020-01-01 ak 6 2 days 19 days 101 0.990
2 2020-01-01 al 2 0 days 19 days 99 0.99
2 2020-01-01 al 1 0 days 19 days 99 0.99
3 2020-01-02 ak 1 4 days 5 days 9 0.09
4 2020-01-02 al 0 0 days 0 days 0 0
5 2020-01-03 ak 1 3 days 4 days 0 NaN
5 2020-01-03 ak 0 3 days 3 days 0 NaN
6 2020-01-03 al 1 1 days 2 days 3 0.75
7 2020-01-04 al 1 0 days 1 days 0 0
time_near_latest
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ test_that("as_epi_archive custom name mapping works correctly", {
)
})

dumb_ex <- data.frame(
geo_value = c("ca", "ca"),
time_value = as.Date(c("2020-01-01", "2020-01-01")),
value = c(1,1),

Check warning on line 54 in tests/testthat/test-archive.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-archive.R,line=54,col=15,[commas_linter] Commas should always have a space after.
version = as.Date(c("2020-01-01", "2020-01-02"))
)
test_that("new_epi_archive correctly detects and warns about compactification", {
expect_snapshot(res <- dumb_ex %>% as_epi_archive())
})

test_that("other_keys can only contain names of the data.frame columns", {
expect_error(as_epi_archive(archive_data, other_keys = "xyz", compactify = FALSE),
regexp = "`other_keys` must be contained in the column names of `x`."
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-revision-latency-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ dummy_ex <- tibble::tribble(
as_epi_archive(compactify = FALSE)

test_that("revision_summary works for a dummy dataset", {
dummy_ex %>%
revision_summary() %>%
print(n = 10, width = 300)
expect_snapshot(dummy_ex %>% revision_summary() %>% print(n = 10, width = 300))
expect_snapshot(dummy_ex %>% revision_summary(drop_nas = FALSE) %>% print(n = 10, width = 300))
})
Expand Down

0 comments on commit cd33ab1

Please sign in to comment.