From 0e42bdba7943bf0db22febc8da2b2e1f89aaae8c Mon Sep 17 00:00:00 2001 From: nmercadeb Date: Thu, 19 Dec 2024 10:51:30 +0000 Subject: [PATCH 1/3] working with omock dev --- CohortConstructor.Rproj | 1 + R/conceptCohort.R | 140 +++++++++--------- R/intersectCohorts.R | 8 +- R/requireConceptIntersect.R | 2 +- R/requireTableIntersect.R | 2 +- tests/testthat/test-addIndex.R | 3 +- tests/testthat/test-collapseCohorts.R | 8 +- tests/testthat/test-conceptCohort.R | 106 ++----------- tests/testthat/test-intersectCohorts.R | 2 +- tests/testthat/test-measurementCohort.R | 2 +- tests/testthat/test-requireConceptIntersect.R | 36 ++--- tests/testthat/test-stratifyCohorts.R | 2 +- tests/testthat/test-subsetCohorts.R | 2 +- tests/testthat/test-trimDemographics.R | 44 +++--- tests/testthat/test-unionCohorts.R | 4 +- 15 files changed, 140 insertions(+), 222 deletions(-) diff --git a/CohortConstructor.Rproj b/CohortConstructor.Rproj index 69fafd4b..bb27d76a 100644 --- a/CohortConstructor.Rproj +++ b/CohortConstructor.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: 27e959ec-87b2-4ff1-ac1d-3ea886296a0d RestoreWorkspace: No SaveWorkspace: No diff --git a/R/conceptCohort.R b/R/conceptCohort.R index fe7be4df..5d925c33 100644 --- a/R/conceptCohort.R +++ b/R/conceptCohort.R @@ -198,10 +198,10 @@ conceptCohort <- function(cdm, cdm[[name]] <- fulfillCohortReqs(cdm = cdm, name = name) if(overlap == "merge"){ - cli::cli_inform(c("i" = "Merging overlapping records.")) - cdm[[name]] <- cdm[[name]] |> - joinOverlap(name = name, gap = 0) |> - omopgenerics::recordCohortAttrition(reason = "Merge overlapping records") + cli::cli_inform(c("i" = "Merging overlapping records.")) + cdm[[name]] <- cdm[[name]] |> + joinOverlap(name = name, gap = 0) |> + omopgenerics::recordCohortAttrition(reason = "Merge overlapping records") } if(overlap == "extend"){ @@ -210,7 +210,7 @@ conceptCohort <- function(cdm, extendOverlap(name = name) |> omopgenerics::recordCohortAttrition(reason = "Add overlapping records") - # adding days might mean we no longer satisfy cohort requirements + # adding days might mean we no longer satisfy cohort requirements cli::cli_inform(c("i" = "Re-appplying cohort requirements.")) cdm[[name]] <- fulfillCohortReqs(cdm = cdm, name = name) } @@ -492,86 +492,82 @@ extendOverlap <- function(cohort, # Because once we add to a record this may cause a new overlap # will do a while loop until all overlaps are resolved while(hasOverlap(cohort)){ - cli::cli_inform("Recursively adding overlapping records") - workingTblNames <- paste0(omopgenerics::uniqueTableName(), "_", c(1:4)) - cohort <- cohort %>% + cli::cli_inform("Recursively adding overlapping records") + workingTblNames <- paste0(omopgenerics::uniqueTableName(), "_", c(1:4)) + cohort <- cohort %>% dplyr::mutate(record_id = dplyr::row_number()) |> dplyr::compute(temporary = FALSE, name = workingTblNames[1]) - # keep overlapping records - cohort_overlap <- cohort %>% - dplyr::inner_join(cohort, - by = c("cohort_definition_id", "subject_id"), - suffix = c("", "_overlap")) |> - dplyr::filter( - record_id != record_id_overlap, - cohort_start_date <= cohort_end_date_overlap & - cohort_end_date >= cohort_start_date_overlap - ) |> - dplyr::select("cohort_definition_id", "subject_id", - "cohort_start_date", "cohort_end_date", - "record_id") |> - dplyr::distinct() |> - dplyr::compute(temporary = FALSE, - name = workingTblNames[2]) - - cohort_no_overlap <- cohort |> - dplyr::anti_join(cohort_overlap |> - dplyr::select("record_id"), - by = "record_id") |> - dplyr::select(!"record_id") |> - dplyr::compute(temporary = FALSE, - name = workingTblNames[3]) - - cohort_overlap <- cohort_overlap %>% - dplyr::mutate(days = !!CDMConnector::datediff("cohort_start_date", - "cohort_end_date")) |> - dplyr::group_by(dplyr::pick("cohort_definition_id", - "subject_id")) |> - dplyr::summarise(cohort_start_date = min(.data$cohort_start_date, na.rm = TRUE), - days = as.integer(sum(.data$days))) %>% - dplyr:: ungroup() %>% - dplyr::mutate(cohort_end_date = as.Date( - !!CDMConnector::dateadd( - date = "cohort_start_date", - number = "days", - interval = "day" - ))) |> - dplyr::select(!"days") |> - dplyr::compute(temporary = FALSE, - name = workingTblNames[4]) - - cohort <- dplyr::union_all(cohort_overlap, - cohort_no_overlap) |> - dplyr::compute(name = name, temporary = FALSE) + # keep overlapping records + cohort_overlap <- cohort %>% + dplyr::inner_join(cohort, + by = c("cohort_definition_id", "subject_id"), + suffix = c("", "_overlap")) |> + dplyr::filter( + record_id != record_id_overlap, + cohort_start_date <= cohort_end_date_overlap & + cohort_end_date >= cohort_start_date_overlap + ) |> + dplyr::select("cohort_definition_id", "subject_id", + "cohort_start_date", "cohort_end_date", + "record_id") |> + dplyr::distinct() |> + dplyr::compute(temporary = FALSE, + name = workingTblNames[2]) - CDMConnector::dropTable(cdm = cdm, - name = workingTblNames) + cohort_no_overlap <- cohort |> + dplyr::anti_join(cohort_overlap |> + dplyr::select("record_id"), + by = "record_id") |> + dplyr::select(!"record_id") |> + dplyr::compute(temporary = FALSE, + name = workingTblNames[3]) + + cohort_overlap <- cohort_overlap %>% + dplyr::mutate(days = !!CDMConnector::datediff("cohort_start_date", + "cohort_end_date")) |> + dplyr::group_by(dplyr::pick("cohort_definition_id", + "subject_id")) |> + dplyr::summarise(cohort_start_date = min(.data$cohort_start_date, na.rm = TRUE), + days = as.integer(sum(.data$days, na.rm = TRUE))) %>% + dplyr:: ungroup() %>% + dplyr::mutate(cohort_end_date = as.Date( + !!CDMConnector::dateadd( + date = "cohort_start_date", + number = "days", + interval = "day" + ))) |> + dplyr::select(!"days") |> + dplyr::compute(temporary = FALSE, + name = workingTblNames[4]) + + cohort <- dplyr::union_all(cohort_overlap, cohort_no_overlap) |> + dplyr::compute(name = name, temporary = FALSE) + CDMConnector::dropTable(cdm = cdm, name = workingTblNames) } cohort - } hasOverlap <- function(cohort){ - overlaps <- cohort |> - dplyr::group_by(.data$cohort_definition_id, .data$subject_id) |> - dplyr::arrange(.data$cohort_start_date) |> - dplyr::mutate( - "next_cohort_start_date" = dplyr::lead(.data$cohort_start_date) - ) |> - dplyr::filter(.data$cohort_end_date >= .data$next_cohort_start_date) |> - dplyr::ungroup() |> - dplyr::tally() |> - dplyr::collect() + overlaps <- cohort |> + dplyr::group_by(.data$cohort_definition_id, .data$subject_id) |> + dplyr::arrange(.data$cohort_start_date) |> + dplyr::mutate( + "next_cohort_start_date" = dplyr::lead(.data$cohort_start_date) + ) |> + dplyr::filter(.data$cohort_end_date >= .data$next_cohort_start_date) |> + dplyr::ungroup() |> + dplyr::tally() |> + dplyr::collect() - if (overlaps$n > 0) { - cli::cli_inform(" - {overlaps$n} overlapping record{?s} found") + if (overlaps$n > 0) { + cli::cli_inform(" - {overlaps$n} overlapping record{?s} found") return(TRUE) - } else { + } else { return(FALSE) - } - } + +} diff --git a/R/intersectCohorts.R b/R/intersectCohorts.R index 484e31af..e6548868 100644 --- a/R/intersectCohorts.R +++ b/R/intersectCohorts.R @@ -342,10 +342,10 @@ joinOverlap <- function(cohort, cdm <- omopgenerics::cdmReference(cohort) start <- cohort |> - dplyr::select(by, "date" := !!startDate) |> + dplyr::select(dplyr::all_of(by), "date" := !!startDate) |> dplyr::mutate("date_id" = -1) end <- cohort |> - dplyr::select(by, "date" := !!endDate) |> + dplyr::select(dplyr::all_of(by), "date" := !!endDate) |> dplyr::mutate("date_id" = 1) if (gap > 0) { end <- end |> @@ -357,7 +357,7 @@ joinOverlap <- function(cohort, dplyr::compute(temporary = FALSE, name = workingTbl) x <- x |> - dplyr::group_by(dplyr::pick(by)) |> + dplyr::group_by(dplyr::pick(dplyr::all_of(by))) |> dplyr::arrange(.data$date, .data$date_id) |> dplyr::mutate( "cum_id" = cumsum(.data$date_id), @@ -410,7 +410,7 @@ joinAll <- function(cohort, } x <- cohort |> - dplyr::group_by(dplyr::across(by)) |> + dplyr::group_by(dplyr::across(dplyr::all_of(by))) |> dplyr::summarise( cohort_start_date = min(.data$cohort_start_date, na.rm = TRUE), diff --git a/R/requireConceptIntersect.R b/R/requireConceptIntersect.R index 5647c95c..c01f6f1a 100644 --- a/R/requireConceptIntersect.R +++ b/R/requireConceptIntersect.R @@ -95,7 +95,7 @@ requireConceptIntersect <- function(cohort, ) | (!.data$cohort_definition_id %in% .env$cohortId) ) |> - dplyr::select(cols) |> + dplyr::select(dplyr::all_of(cols)) |> dplyr::compute(name = subsetName, temporary = FALSE) # attrition reason diff --git a/R/requireTableIntersect.R b/R/requireTableIntersect.R index 743dc9fd..6dbdd9fb 100644 --- a/R/requireTableIntersect.R +++ b/R/requireTableIntersect.R @@ -90,7 +90,7 @@ requireTableIntersect <- function(cohort, ) | (!.data$cohort_definition_id %in% .env$cohortId) ) |> - dplyr::select(cols) |> + dplyr::select(dplyr::all_of(cols)) |> dplyr::compute(name = subsetName, temporary = FALSE) # attrition reason diff --git a/tests/testthat/test-addIndex.R b/tests/testthat/test-addIndex.R index 415954a9..0ef4de6d 100644 --- a/tests/testthat/test-addIndex.R +++ b/tests/testthat/test-addIndex.R @@ -1,4 +1,3 @@ - test_that("local tibble and duckdb test - will do nothing for these", { skip_on_cran() cdm <- omock::mockCdmReference() |> @@ -6,7 +5,7 @@ test_that("local tibble and duckdb test - will do nothing for these", { "cohort_definition_id" = 1, "subject_id" = c(1, 2, 3), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) expect_no_error(cdm$cohort <- cdm$cohort |> addCohortTableIndex()) diff --git a/tests/testthat/test-collapseCohorts.R b/tests/testthat/test-collapseCohorts.R index c3cd2c2b..f039dea7 100644 --- a/tests/testthat/test-collapseCohorts.R +++ b/tests/testthat/test-collapseCohorts.R @@ -5,7 +5,7 @@ test_that("simple example", { "cohort_definition_id" = 1L, "subject_id" = c(1L, 2L, 3L), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) cdm <- omopgenerics::insertTable( cdm = cdm, name = "concept", table = dplyr::tibble( @@ -70,7 +70,7 @@ test_that("simple example", { expect_no_error(newCohort <- cohort |> collapseCohorts(gap = 1, name = "my_cohort")) expect_identical(settings(newCohort), settings(cohort)) expect_identical(cohortCount(newCohort), dplyr::tibble( - "cohort_definition_id" = 1L, "number_records" = 4L, "number_subjects" = 2L + "cohort_definition_id" = 1L, "number_records" = 2L, "number_subjects" = 2L )) # expect_identical( # attrition(newCohort), @@ -112,7 +112,7 @@ test_that("out of observation", { "cohort_definition_id" = 1L, "subject_id" = c(1L, 2L, 3L), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) cdm <- omopgenerics::insertTable( cdm = cdm, name = "concept", table = dplyr::tibble( @@ -169,7 +169,7 @@ test_that("out of observation", { expect_no_error(newCohort <- cohort |> collapseCohorts(gap = 1, name = "my_cohort")) expect_identical(settings(newCohort), settings(cohort)) expect_identical(cohortCount(newCohort), dplyr::tibble( - "cohort_definition_id" = 1L, "number_records" = 4L, "number_subjects" = 2L + "cohort_definition_id" = 1L, "number_records" = 2L, "number_subjects" = 2L )) # expect_identical( # attrition(newCohort), diff --git a/tests/testthat/test-conceptCohort.R b/tests/testthat/test-conceptCohort.R index 12fa3a23..c26a2a73 100644 --- a/tests/testthat/test-conceptCohort.R +++ b/tests/testthat/test-conceptCohort.R @@ -23,7 +23,7 @@ test_that("expected errors and messages", { expect_error(conceptCohort(cdm = cdm, name = NA, conceptSet = NULL)) expect_error(conceptCohort(cdm = cdm, name = 1, conceptSet = NULL)) expect_error(conceptCohort(cdm = cdm, name = c("ass", "asdf"), conceptSet = NULL)) - expect_error(conceptCohort(cdm = cdm, name = "AAAA", conceptSet = NULL)) + expect_error(conceptCohort(cdm = cdm, name = "aaaa", conceptSet = NULL)) expect_error(conceptCohort(cdm = cdm, conceptSet = NULL, name = "cohort")) # empty cohort from empty concept @@ -69,7 +69,7 @@ test_that("simple example", { "cohort_definition_id" = 1L, "subject_id" = c(1L, 2L, 3L), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) cdm <- omopgenerics::insertTable( cdm = cdm, name = "concept", table = dplyr::tibble( @@ -136,8 +136,8 @@ test_that("simple example", { ) } - expect_true(cohort |> dplyr::tally() |> dplyr::pull() == 4) - expect_true(cohortCount(cohort)$number_records == 4) + expect_true(cohort |> dplyr::tally() |> dplyr::pull() == 2) + expect_true(cohortCount(cohort)$number_records == 2) expect_true(cohortCount(cohort)$number_subjects == 2) expect_identical( settings(cohort), @@ -150,14 +150,14 @@ test_that("simple example", { attrition(cohort) |> dplyr::as_tibble(), dplyr::tibble( "cohort_definition_id" = 1L, - "number_records" = c(9L, 9L, 9L, 4L), + "number_records" = c(9L, 9L, 4L, 2L), "number_subjects" = 2L, "reason_id" = 1:4L, "reason" = c( "Initial qualifying events", "Record start <= record end", "Record in observation", "Merge overlapping records" ), - "excluded_records" = c(0L, 0L, 0L, 5L), + "excluded_records" = c(0L, 0L, 5L, 2L), "excluded_subjects" = 0L ) ) @@ -173,9 +173,9 @@ test_that("simple example", { cohort, dplyr::tibble( "cohort_definition_id" = 1L, - "subject_id" = c(1L, 1L, 1L, 2L), - "cohort_start_date" = as.Date(c(0, 1500, 1800, 10), origin = "2020-01-01"), - "cohort_end_date" = as.Date(c(800, 1600, 1804, 2000), origin = "2020-01-01") + "subject_id" = c(1L, 2L), + "cohort_start_date" = as.Date(c(0, 10), origin = "2020-01-01"), + "cohort_end_date" = as.Date(c(800, 1461), origin = "2020-01-01") ) ) @@ -211,78 +211,6 @@ test_that("simple example", { PatientProfiles::mockDisconnect(cdm) }) -test_that("simple example duckdb", { - testthat::skip_on_cran() - cdm <- omock::mockCdmReference() |> - omock::mockCdmFromTables(tables = list("cohort" = dplyr::tibble( - "cohort_definition_id" = 1L, - "subject_id" = 1:3L, - "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") - ))) - cdm <- omopgenerics::insertTable( - cdm = cdm, name = "concept", table = dplyr::tibble( - "concept_id" = 1L, - "concept_name" = "my concept", - "domain_id" = "drUg", - "vocabulary_id" = NA, - "concept_class_id" = NA, - "concept_code" = NA, - "valid_start_date" = NA, - "valid_end_date" = NA - ) - ) - cdm <- omopgenerics::insertTable( - cdm = cdm, name = "drug_exposure", table = dplyr::tibble( - "drug_exposure_id" = 1:11L, - "person_id" = c(1, 1, 1, 1, 2, 2, 3, 1, 1, 1, 1) |> as.integer(), - "drug_concept_id" = c(1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1) |> as.integer(), - "drug_exposure_start_date" = c(0, 300, 1500, 750, 10, 800, 150, 1800, 1801, 1802, 1803), - "drug_exposure_end_date" = c(400, 800, 1600, 1550, 2000, 1000, 600, 1801, 1802, 1803, 1804), - "drug_type_concept_id" = 1L - ) |> - dplyr::mutate( - "drug_exposure_start_date" = as.Date(.data$drug_exposure_start_date, origin = "2020-01-01"), - "drug_exposure_end_date" = as.Date(.data$drug_exposure_end_date, origin = "2020-01-01") - ) - ) - - cdm <- cdm |> copyCdm() - - expect_no_error(cohort <- conceptCohort(cdm = cdm, conceptSet = list(a = 1L), name = "cohort")) - - expect_true(cohort |> dplyr::tally() |> dplyr::pull() == 4) - expect_true(cohortCount(cohort)$number_records == 4L) - expect_true(cohortCount(cohort)$number_subjects == 2L) - # expect_true(attrition(cohort) |> nrow() == 1) - expect_identical( - settings(cohort), - dplyr::tibble( - "cohort_definition_id" = 1L, "cohort_name" = "a", - "cdm_version" = attr(cdm, "cdm_version"), "vocabulary_version" = "mock" - ) - ) - expect_identical(cohortCodelist(cohort, 1), omopgenerics::newCodelist(list(a = 1L))) - cohort <- cohort |> - dplyr::collect() |> - dplyr::as_tibble() |> - dplyr::arrange(subject_id, cohort_start_date) - attr(cohort, "cohort_attrition") <- NULL - attr(cohort, "cohort_codelist") <- NULL - attr(cohort, "cohort_set") <- NULL - expect_equal( - cohort, - dplyr::tibble( - "cohort_definition_id" = 1L, - "subject_id" = c(1L, 1L, 1L, 2L), - "cohort_start_date" = as.Date(c(0, 1500, 1800, 10), origin = "2020-01-01"), - "cohort_end_date" = as.Date(c(800, 1600, 1804, 2000), origin = "2020-01-01") - ) - ) - - PatientProfiles::mockDisconnect(cdm) -}) - test_that("concepts from multiple cdm tables duckdb", { testthat::skip_on_cran() cdm <- omock::mockCdmReference() |> @@ -290,7 +218,7 @@ test_that("concepts from multiple cdm tables duckdb", { "cohort_definition_id" = 1L, "subject_id" = c(1L, 2L, 3L), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) |> omock::mockConditionOccurrence() |> omock::mockDrugExposure() @@ -320,7 +248,7 @@ test_that("excluded concepts in codelist", { "cohort_definition_id" = 1L, "subject_id" = c(1L, 2L, 3L), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) cdm <- omopgenerics::insertTable( cdm = cdm, name = "concept", table = dplyr::tibble( @@ -513,7 +441,7 @@ test_that("table not present in the cdm", { "cohort_definition_id" = 1L, "subject_id" = c(1L, 2L, 3L), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) cdm <- omopgenerics::insertTable( cdm = cdm, name = "concept", table = dplyr::tibble( @@ -547,9 +475,7 @@ test_that("table not present in the cdm", { expect_warning(cdm$conceptcohort <- conceptCohort(cdm, list(a = 1L, b = 1L, c = 1:2L, d = 2L), name = "conceptcohort")) expect_true(all(cdm$conceptcohort |> dplyr::pull(cohort_definition_id) |> unique() |> sort() == 1:3)) expect_true(all(cdm$conceptcohort |> dplyr::pull(cohort_start_date) |> sort() == - c("2020-01-01", "2020-01-01", "2020-01-01", "2020-01-11", "2020-01-11", - "2020-01-11", "2024-02-09", "2024-02-09", "2024-02-09", "2024-12-05", - "2024-12-05", "2024-12-05", "2024-12-08", "2024-12-08", "2024-12-08"))) + c("2020-01-01", "2020-01-01", "2020-01-01", "2020-01-11", "2020-01-11", "2020-01-11"))) PatientProfiles::mockDisconnect(cdm) }) @@ -561,7 +487,7 @@ test_that("cohort exit as event start date", { "cohort_definition_id" = 1L, "subject_id" = c(1L, 2L, 3L), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) cdm <- omopgenerics::insertTable( cdm = cdm, name = "concept", table = dplyr::tibble( @@ -633,7 +559,7 @@ test_that("use source field concepts", { "cohort_definition_id" = 1L, "subject_id" = c(1L, 2L, 3L), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) cdm <- omopgenerics::insertTable( cdm = cdm, name = "concept", table = dplyr::tibble( @@ -690,7 +616,7 @@ test_that("missing event end dates", { "cohort_definition_id" = 1L, "subject_id" = c(1L, 2L, 3L), "cohort_start_date" = as.Date("2020-01-01"), - "cohort_end_date" = as.Date("2029-12-31") + "cohort_end_date" = as.Date("2024-01-01") ))) cdm <- omopgenerics::insertTable( cdm = cdm, name = "concept", table = dplyr::tibble( diff --git a/tests/testthat/test-intersectCohorts.R b/tests/testthat/test-intersectCohorts.R index 2fb016f9..68abc997 100644 --- a/tests/testthat/test-intersectCohorts.R +++ b/tests/testthat/test-intersectCohorts.R @@ -470,7 +470,7 @@ test_that("codelist", { cdm <- cdm_local |> copyCdm() - cdm$cohort1 <- conceptCohort(cdm, conceptSet = list(c1 = c(1,3), c2 = c(2)), name = "cohort1") + cdm$cohort1 <- conceptCohort(cdm, conceptSet = list(c1 = c(1L,3L), c2 = c(2L)), name = "cohort1") # intersect concept generated cohort cdm$cohort2 <- intersectCohorts(cdm$cohort1, name = "cohort2") diff --git a/tests/testthat/test-measurementCohort.R b/tests/testthat/test-measurementCohort.R index e96b01c1..457fcd90 100644 --- a/tests/testthat/test-measurementCohort.R +++ b/tests/testthat/test-measurementCohort.R @@ -287,7 +287,7 @@ cdm <- omopgenerics::insertTable(cdm, "measurement", cohort_1 <- measurementCohort( cdm = cdm, name = "cohort", - conceptSet = list("normal_blood_pressure" = c(4326744)), + conceptSet = list("normal_blood_pressure" = c(4326744L)), valueAsNumber = list("8876" = c(70L, 120L)) ) expect_true(all(sort(cohort_1 |> diff --git a/tests/testthat/test-requireConceptIntersect.R b/tests/testthat/test-requireConceptIntersect.R index 391f1613..f9298eaa 100644 --- a/tests/testthat/test-requireConceptIntersect.R +++ b/tests/testthat/test-requireConceptIntersect.R @@ -30,7 +30,7 @@ test_that("require flag in concept", { start_cols <- colnames(cdm$cohort1) cdm$cohort3 <- requireConceptIntersect(cohort = cdm$cohort1, - conceptSet = list(a = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), name = "cohort3") expect_identical(colnames(cdm$cohort3), colnames(cdm$cohort1)) @@ -46,7 +46,7 @@ test_that("require flag in concept", { # cohort Id cdm$cohort4 <- requireConceptIntersect(cohort = cdm$cohort1, cohortId = 1, - conceptSet = list(a = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), name = "cohort4") expect_true(all(cdm$cohort4 |> dplyr::pull("subject_id") == @@ -59,7 +59,7 @@ test_that("require flag in concept", { "Initial qualifying events"))) # censor date cdm$cohort5 <- requireConceptIntersect(cohort = cdm$cohort1, - conceptSet = list(a = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), censorDate = "cohort_end_date", name = "cohort5") @@ -72,7 +72,7 @@ test_that("require flag in concept", { # name cdm$cohort1 <- requireConceptIntersect(cohort = cdm$cohort1, - conceptSet = list(a = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf)) expect_true(all(omopgenerics::attrition(cdm$cohort1)$reason == c("Initial qualifying events", @@ -139,11 +139,11 @@ test_that("requiring absence in another cohort", { cdm <- cdm_local |> copyCdm() cdm$cohort3_inclusion <- requireConceptIntersect(cohort = cdm$cohort1, - conceptSet = list(a = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), name = "cohort3_inclusion") cdm$cohort3_exclusion <- requireConceptIntersect(cohort = cdm$cohort1, - conceptSet = list(a = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), intersections = 0, name = "cohort3_exclusion") @@ -171,7 +171,7 @@ test_that("requiring absence in another cohort", { cdm$cohort3_exclusion_partial <- requireConceptIntersect( cohort = cdm$cohort1, cohortId = "cohort_1", - conceptSet = list(a = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), intersections = 0, name = "cohort3_exclusion_partial" @@ -224,7 +224,7 @@ test_that("different intersection count requirements", { # no intersections - people not in cohort2 expect_identical(sort(cdm$cohort1 |> requireConceptIntersect(intersections = c(0, 0), - conceptSet = list("a" = 1), + conceptSet = list("a" = 1L), window = c(-Inf, Inf), name = "cohort1_test") |> dplyr::pull("subject_id")), as.integer(c(4,5,6,7,8,9,10))) @@ -233,14 +233,14 @@ test_that("different intersection count requirements", { # only one intersection expect_identical(sort(cdm$cohort1 |> requireConceptIntersect(intersections = c(1, 1), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), name = "cohort1_test") |> dplyr::pull("subject_id")), c(1L)) expect_identical(sort(cdm$cohort1 |> requireConceptIntersect(intersections = c(1), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), name = "cohort1_test") |> dplyr::pull("subject_id")), c(1L)) @@ -248,14 +248,14 @@ test_that("different intersection count requirements", { # 2 intersections expect_identical(sort(cdm$cohort1 |> requireConceptIntersect(intersections = c(2, 2), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), name = "cohort1_test") |> dplyr::pull("subject_id")), c(2L)) expect_identical(sort(cdm$cohort1 |> requireConceptIntersect(intersections = c(2), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), name = "cohort1_test") |> dplyr::pull("subject_id")), c(2L)) @@ -264,7 +264,7 @@ test_that("different intersection count requirements", { # 2 or more intersections expect_identical(sort(cdm$cohort1 |> requireConceptIntersect(intersections = c(2, Inf), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), name = "cohort1_test") |> dplyr::pull("subject_id")), c(2L, 3L)) @@ -272,7 +272,7 @@ test_that("different intersection count requirements", { # 2 or 3 intersections expect_identical(sort(cdm$cohort1 |> requireConceptIntersect(intersections = c(2, 3), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf), name = "cohort1_test") |> dplyr::pull("subject_id")), c(2L, 3L)) @@ -282,19 +282,19 @@ test_that("different intersection count requirements", { # expected errors expect_error(requireConceptIntersect(cohort = cdm$cohort1, intersections = c(-10, 10), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf))) expect_error(requireConceptIntersect(cohort = cdm$cohort1, intersections = c(11, 10), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf))) expect_error(requireConceptIntersect(cohort = cdm$cohort1, intersections = c(Inf, Inf), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf))) expect_error(requireConceptIntersect(cohort = cdm$cohort1, intersections = c(1, 2, 3), - conceptSet = list("a" = 1), + conceptSet = list(a = 1L), window = c(-Inf, Inf))) PatientProfiles::mockDisconnect(cdm) diff --git a/tests/testthat/test-stratifyCohorts.R b/tests/testthat/test-stratifyCohorts.R index e14a3de6..6dfa8da4 100644 --- a/tests/testthat/test-stratifyCohorts.R +++ b/tests/testthat/test-stratifyCohorts.R @@ -15,7 +15,7 @@ test_that("simple stratification", { "observation_period_id" = 1:4, "person_id" = 1:4, "observation_period_start_date" = as.Date("2000-01-01"), - "observation_period_end_date" = as.Date("2030-01-01"), + "observation_period_end_date" = as.Date("2024-01-01"), "period_type_concept_id" = 0L ) ), diff --git a/tests/testthat/test-subsetCohorts.R b/tests/testthat/test-subsetCohorts.R index c918f587..427bc699 100644 --- a/tests/testthat/test-subsetCohorts.R +++ b/tests/testthat/test-subsetCohorts.R @@ -76,7 +76,7 @@ test_that("codelist works", { cdm <- cdm_local |> copyCdm() - cdm$cohort1 <- conceptCohort(cdm, conceptSet = list(c1 = c(1,3), c2 = c(2)), name = "cohort1") + cdm$cohort1 <- conceptCohort(cdm, conceptSet = list(c1 = c(1L,3L), c2 = c(2L)), name = "cohort1") # Subset 1 cohort cdm$cohort2 <- subsetCohorts(cdm$cohort1, 1, name = "cohort2") diff --git a/tests/testthat/test-trimDemographics.R b/tests/testthat/test-trimDemographics.R index 7587cedb..ab70e6f6 100644 --- a/tests/testthat/test-trimDemographics.R +++ b/tests/testthat/test-trimDemographics.R @@ -16,10 +16,10 @@ test_that("simple duckdb checks", { "observation_period_id" = as.integer(1:4), "person_id" = as.integer(c(1, 2, 2, 3)), "observation_period_start_date" = as.Date(c( - "1993-04-19", "2010-03-12", "2031-08-23", "2020-10-06" + "1993-04-19", "2010-03-12", "2017-08-23", "2020-10-06" )), "observation_period_end_date" = as.Date(c( - "2033-10-11", "2017-01-01", "2045-03-12", "2100-12-31" + "2023-10-11", "2017-01-01", "2023-03-12", "2024-01-01" )), "period_type_concept_id" = 0L ) @@ -30,10 +30,10 @@ test_that("simple duckdb checks", { "cohort_definition_id" = as.integer(c(1, 1, 1, 2)), "subject_id" = as.integer(c(1, 2, 3, 1)), "cohort_start_date" = as.Date(c( - "2032-01-19", "2039-11-12", "2036-03-16", "2003-12-15" + "2012-01-19", "2010-11-12", "2021-03-16", "2003-12-15" )), "cohort_end_date" = as.Date(c( - "2033-10-11", "2045-01-12", "2074-05-18", "2010-05-25" + "2023-10-11", "2015-01-12", "2024-01-01", "2010-05-25" )) ) ) @@ -68,18 +68,14 @@ test_that("simple duckdb checks", { expect_identical( x, dplyr::tibble( - "subject_id" = as.integer(c(1, 2, 3)), - "cohort_start_date" = as.Date(c( - "2033-04-19", "2040-01-15", "2045-08-20" - )), - "cohort_end_date" = as.Date(c( - "2033-10-11", "2045-01-12", "2065-08-19" - )) + "subject_id" = as.integer(c()), + "cohort_start_date" = as.Date(c()), + "cohort_end_date" = as.Date(c()) ) ) id <- settings(cdm$cohort2) |> dplyr::filter( - sex == "Both" & age_range == "40_59" & + sex == "Both" & age_range == "0_19" & min_prior_observation == 0 & min_future_observation == 365 & grepl("cohort_1", cohort_name) @@ -89,9 +85,9 @@ test_that("simple duckdb checks", { expect_identical( x, dplyr::tibble( - "subject_id" = c(2L, 3L), - "cohort_start_date" = as.Date(c("2040-01-15", "2045-08-20")), - "cohort_end_date" = as.Date(c("2045-01-12", "2065-08-19")) + "subject_id" = c(1L, 2L, 3L), + "cohort_start_date" = as.Date(c("2012-01-19", "2010-11-12", "2021-03-16")), + "cohort_end_date" = as.Date(c("2013-04-18", "2015-01-12", "2024-01-01")) ) ) @@ -156,9 +152,9 @@ test_that("simple duckdb checks", { expect_identical( x, dplyr::tibble( - "subject_id" = as.integer(c(1, 2, 3)), - "cohort_start_date" = as.Date(c("1993-04-19", "2010-03-12", "2020-10-06")), - "cohort_end_date" = as.Date(c("2013-04-18", "2017-01-01", "2025-08-19")) + "subject_id" = as.integer(c(1, 2, 2, 3)), + "cohort_start_date" = as.Date(c("1993-04-19", "2010-03-12", "2017-08-23", "2020-10-06")), + "cohort_end_date" = as.Date(c("2013-04-18", "2017-01-01", "2020-01-14", "2024-01-01")) ) ) id <- settings(cdm$obs1) |> @@ -172,9 +168,9 @@ test_that("simple duckdb checks", { expect_identical( x, dplyr::tibble( - "subject_id" = c(1L, 2L, 3L), - "cohort_start_date" = as.Date(c("1994-04-19", "2011-03-12", "2021-10-06")), - "cohort_end_date" = as.Date(c("2013-04-18", "2017-01-01", "2025-08-19")) + "subject_id" = c(1L, 2L, 2L, 3L), + "cohort_start_date" = as.Date(c("1994-04-19", "2011-03-12", "2018-08-23", "2021-10-06")), + "cohort_end_date" = as.Date(c("2013-04-18", "2017-01-01", "2020-01-14", "2024-01-01")) ) ) @@ -291,9 +287,9 @@ test_that("simple duckdb checks", { expect_identical( x, dplyr::tibble( - "subject_id" = c(1L, 2L, 3L), - "cohort_start_date" = as.Date(c("1993-04-19", "2010-03-12", "2020-10-06")), - "cohort_end_date" = as.Date(c("2013-04-18", "2017-01-01", "2025-08-19")) + "subject_id" = c(1L, 2L, 2L, 3L), + "cohort_start_date" = as.Date(c("1993-04-19", "2010-03-12", "2017-08-23", "2020-10-06")), + "cohort_end_date" = as.Date(c("2013-04-18", "2017-01-01", "2020-01-14", "2024-01-01")) ) ) id <- settings(cdm$obs_new) |> diff --git a/tests/testthat/test-unionCohorts.R b/tests/testthat/test-unionCohorts.R index 864bfbc0..8c7deb2f 100644 --- a/tests/testthat/test-unionCohorts.R +++ b/tests/testthat/test-unionCohorts.R @@ -61,7 +61,7 @@ test_that("unionCohorts works", { expect_identical(collectCohort(cdm$cohort3, 2), collectCohort(cdm$cohort4, 2)) # union 2 empty cohorts - cdm$cohort5 <- conceptCohort(cdm = cdm, conceptSet = list("a"= 1, "b" = 2), name = "cohort5") + cdm$cohort5 <- conceptCohort(cdm = cdm, conceptSet = list("a"= 1L, "b" = 2L), name = "cohort5") cdm$cohort6 <- cdm$cohort5 |> unionCohorts(name = "cohort6") expect_true(nrow(attrition(cdm$cohort6)) == 1) expect_true(attrition(cdm$cohort6)$number_records == 0) @@ -235,7 +235,7 @@ test_that("test codelist", { cdm <- cdm_local |> copyCdm() - cdm$cohort1 <- conceptCohort(cdm, conceptSet = list(c1 = c(1,3), c2 = c(2)), name = "cohort1") + cdm$cohort1 <- conceptCohort(cdm, conceptSet = list(c1 = c(1L,3L), c2 = c(2L)), name = "cohort1") # Union concept generated cohort cdm$cohort2 <- unionCohorts(cdm$cohort1, name = "cohort2") From 2443e25cee030848920b49cd586653195a4b4eee Mon Sep 17 00:00:00 2001 From: nmercadeb Date: Thu, 19 Dec 2024 11:00:03 +0000 Subject: [PATCH 2/3] clean warnings in tests --- R/requireCohortIntersect.R | 2 +- tests/testthat/test-requireConceptIntersect.R | 2 +- tests/testthat/test-requireDemographics.R | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/requireCohortIntersect.R b/R/requireCohortIntersect.R index ff5c68c6..510f20ca 100644 --- a/R/requireCohortIntersect.R +++ b/R/requireCohortIntersect.R @@ -105,7 +105,7 @@ requireCohortIntersect <- function(cohort, ) | (!.data$cohort_definition_id %in% .env$cohortId) ) |> - dplyr::select(cols) |> + dplyr::select(dplyr::all_of(cols)) |> dplyr::compute(name = subsetName, temporary = FALSE) # attrition reason diff --git a/tests/testthat/test-requireConceptIntersect.R b/tests/testthat/test-requireConceptIntersect.R index f9298eaa..0266069e 100644 --- a/tests/testthat/test-requireConceptIntersect.R +++ b/tests/testthat/test-requireConceptIntersect.R @@ -96,7 +96,7 @@ test_that("require flag in concept", { # only support one concept at the moment expect_error( requireConceptIntersect(cohort = cdm$cohort1, - conceptSet = list(a = 1, b = 2), + conceptSet = list(a = 1L, b = 2L), window = c(-Inf, Inf)) ) expect_error( diff --git a/tests/testthat/test-requireDemographics.R b/tests/testthat/test-requireDemographics.R index eebd4834..d884237d 100644 --- a/tests/testthat/test-requireDemographics.R +++ b/tests/testthat/test-requireDemographics.R @@ -436,7 +436,7 @@ test_that("codelist kept with >1 requirement", { cdm <- cdm_local |> copyCdm() - cdm$cohort1 <- conceptCohort(cdm = cdm, conceptSet = list(a = 1, b = 2), name = "cohort1") + cdm$cohort1 <- conceptCohort(cdm = cdm, conceptSet = list(a = 1L, b = 2L), name = "cohort1") cdm$cohort2 <- cdm$cohort1 |> requireDemographics(name = "cohort2", minPriorObservation = c(0,3), cohortId = 1) expect_equal(attr(cdm$cohort2, "cohort_codelist") |> dplyr::collect() |> dplyr::arrange(.data$cohort_definition_id), dplyr::tibble( From f1146a22670436cf2fec4c8e444b58b1bb36dde2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=BAria=20Mercad=C3=A9-Besora?= <61558739+nmercadeb@users.noreply.github.com> Date: Thu, 19 Dec 2024 11:28:25 +0000 Subject: [PATCH 3/3] Update DESCRIPTION --- DESCRIPTION | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index 0ce57e72..f5d2a0d1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -71,3 +71,5 @@ Depends: R (>= 4.1) URL: https://ohdsi.github.io/CohortConstructor/, https://github.com/OHDSI/CohortConstructor LazyData: true +Remotes: + ohdsi/omock