Skip to content

Commit

Permalink
add support for ascen cdf values w/num out_type_id
Browse files Browse the repository at this point in the history
Ensuring a mix of numeric and character values are sorted correctly
involves creating a separate column for the numerics and using that as
the first sort key. This then allows the original method of validating
ascent to work.

Another solution is split the data frame into a list and then perform a
numeric sort if the column can be transformed, but the coercion will
probably have the equivalent amount of overhead.

This will fix #78
  • Loading branch information
zkamvar committed Jan 3, 2025
1 parent 353e8db commit 98c4252
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
10 changes: 9 additions & 1 deletion R/check_tbl_value_col_ascending.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ check_values_ascending <- function(tbl) {
group_cols <- names(tbl)[!names(tbl) %in% hubUtils::std_colnames]
tbl[["value"]] <- as.numeric(tbl[["value"]])

# group by all of the target columns
check_tbl <- dplyr::group_by(tbl, dplyr::across(dplyr::all_of(group_cols))) %>%
dplyr::arrange(.data$output_type_id, .by_group = TRUE) %>%
# FIX for <https://github.com/hubverse-org/hubValidations/issues/78>
# output_type_ids are grouped together and we want to make sure the numeric
# ids are sorted correctly. To do this, we need to create a separate column
# for numeric IDs and sort by that first and then the recorded value of
# output_type_id second. This way, we can ensure that numeric values are
# not sorted by character.
dplyr::mutate(num_id = suppressWarnings(as.numeric(.data$output_type_id))) %>%
dplyr::arrange(.data$num_id, .data$output_type_id, .by_group = TRUE) %>%
dplyr::summarise(non_asc = any(diff(.data[["value"]]) < 0))

if (!any(check_tbl$non_asc)) {
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-check_tbl_value_col_ascending.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,15 @@ test_that("check_tbl_value_col_ascending skips correctly", {
check_tbl_value_col_ascending(tbl, file_path)
)
})


test_that("(#78) check_tbl_value_col_ascending handle cdf char values", {

file_path <- test_path("testdata/files/2024-08-12-cdf-ascent.csv")
ex <- arrow::read_csv_arrow(file_path)

res <- hubValidations:::check_tbl_value_col_ascending(ex, file_path = "")
expect_s3_class(res, "check_success")
expect_null(res$error_tbl)
})

128 changes: 128 additions & 0 deletions tests/testthat/testdata/files/2024-08-12-cdf-ascent.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
location,reference_date,horizon,target_end_date,target,output_type,output_type_id,value
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.01,17
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.025,44
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.05,72
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.1,105
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.15,122
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.2,125
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.25,127
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.3,128
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.35,131
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.4,132
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.45,133
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.5,136
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.55,139
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.6,140
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.65,141
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.7,144
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.75,145
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.8,147
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.85,150
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.9,167
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.95,200
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.975,228
01,2023-01-14,0,2023-01-14,wk inc flu hosp,quantile,0.99,255
01,2023-01-14,0,2023-01-14,wk flu hosp rate category,pmf,low,0.220842781557067
01,2023-01-14,0,2023-01-14,wk flu hosp rate category,pmf,moderate,0.768398474282558
01,2023-01-14,0,2023-01-14,wk flu hosp rate category,pmf,high,0.0107282559276931
01,2023-01-14,0,2023-01-14,wk flu hosp rate category,pmf,very high,3.04882326815914e-5
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,0.25,0.00853380042747561
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,0.5,0.0135533534527697
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,0.75,0.0208413454592117
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,1,0.0299015976961877
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,1.25,0.0406341808051564
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,1.5,0.0548617841719505
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,1.75,0.0720793809659529
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,2,0.0929593384551091
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,2.25,0.111686153095421
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,2.5,0.236379680785012
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,2.75,0.560349384758665
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,3,0.864271648664744
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,3.25,0.89630163333021
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,3.5,0.918320726070205
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,3.75,0.937138052331475
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,4,0.952967021875378
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,4.25,0.96520141962482
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,4.5,0.974905656518415
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,4.75,0.983169904293088
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,5,0.989315411865382
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,5.25,0.99331123551173806
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,5.5,0.995919595802813
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,5.75,0.997577247537977
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,6,0.998600149380724
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,6.25,0.999213049267616
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,6.5,0.999569631735671
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,6.75,0.999771071520066
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,7,0.999881567193594
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,7.25,0.999940419028641
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,7.5,0.999970855156356
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,7.75,0.999986139027956
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,8,0.999993591354829
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,8.25,0.999997119649744
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,8.5,0.999998741655025
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,8.75,0.999999465680723
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,9,0.999999779493023
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,9.25,0.999999911561791
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,9.5,0.999999965530796
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,9.75,0.999999986945038
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,10,0.999999995195438
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,10.25,0.999999998281901
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,10.5,0.999999999403044
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,10.75,0.999999999798479
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,11,0.999999999933905
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,11.25,0.999999999978939
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,11.5,0.99999999999348
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,11.75,0.999999999998039
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,12,0.999999999999427
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,12.25,0.999999999999838
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,12.5,0.999999999999955
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,12.75,0.999999999999988
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,13,0.999999999999997
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,13.25,0.999999999999999
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,13.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,13.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,14,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,14.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,14.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,14.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,15,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,15.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,15.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,15.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,16,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,16.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,16.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,16.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,17,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,17.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,17.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,17.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,18,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,18.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,18.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,18.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,19,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,19.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,19.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,19.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,20,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,20.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,20.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,20.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,21,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,21.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,21.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,21.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,22,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,22.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,22.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,22.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,23,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,23.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,23.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,23.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,24,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,24.25,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,24.5,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,24.75,1
01,2023-01-14,0,2023-01-14,wk flu hosp rate,cdf,25,1

0 comments on commit 98c4252

Please sign in to comment.