From 1140d81b81fe67c49c93f3392cb641e26870b52d Mon Sep 17 00:00:00 2001 From: nmercadeb Date: Wed, 16 Oct 2024 14:02:35 +0100 Subject: [PATCH 1/6] test union and collapse --- R/unionCohorts.R | 4 ++- tests/testthat/test-collapseCohorts.R | 1 + tests/testthat/test-intersectCohorts.R | 34 +++++++++++++++++++++ tests/testthat/test-unionCohorts.R | 42 +++++++++++++++++++++++++- 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/R/unionCohorts.R b/R/unionCohorts.R index 4264114e..970c7eaf 100644 --- a/R/unionCohorts.R +++ b/R/unionCohorts.R @@ -63,9 +63,11 @@ unionCohorts <- function(cohort, tmpTable <- omopgenerics::uniqueTableName() unionedCohort <- cohort |> dplyr::filter(.data$cohort_definition_id %in% .env$cohortId) |> + PatientProfiles::addObservationPeriodId(name = tmpTable) |> joinOverlap(name = tmpTable, - by = "subject_id", + by = c("observation_period_id", "subject_id"), gap = gap) |> + dplyr::select(!"observation_period_id") |> dplyr::mutate(cohort_definition_id = 1L) |> dplyr::relocate(dplyr::all_of(omopgenerics::cohortColumns("cohort"))) |> dplyr::compute(name = tmpTable, temporary = FALSE) diff --git a/tests/testthat/test-collapseCohorts.R b/tests/testthat/test-collapseCohorts.R index 6d57edcd..6bf612ea 100644 --- a/tests/testthat/test-collapseCohorts.R +++ b/tests/testthat/test-collapseCohorts.R @@ -307,5 +307,6 @@ test_that("multiple observation periods", { expect_true(nrow(cdm$cohort_1 |> dplyr::collect()) == 2) + PatientProfiles::mockDisconnect(cdm) }) diff --git a/tests/testthat/test-intersectCohorts.R b/tests/testthat/test-intersectCohorts.R index c7a7a0d7..b099ae7f 100644 --- a/tests/testthat/test-intersectCohorts.R +++ b/tests/testthat/test-intersectCohorts.R @@ -591,3 +591,37 @@ test_that("records combined for gap must be in the same observation period", { PatientProfiles::mockDisconnect(cdm) }) + +test_that("multiple observation periods", { + cdm_local <- omock::mockCdmReference() |> + omock::mockPerson(n = 4, seed = 1) + cdm_local$observation_period <- dplyr::tibble( + "observation_period_id" = as.integer(1:7), + "person_id" = as.integer(c(1, 1, 1, 2, 2, 3, 4)), + "observation_period_start_date" = as.Date(c( + "2000-01-01", "2001-01-01", "2003-01-01", "2001-01-01", "2002-01-01", + "2000-01-01", "2000-01-01" + )), + "observation_period_end_date" =as.Date(c( + "2000-12-20", "2002-01-01", "2005-01-01", "2001-12-31", "2004-01-01", + "2004-01-01", "2003-01-01" + )), + "period_type_concept_id" = NA_integer_ + ) + cdm_local$cohort <- dplyr::tibble( + "cohort_definition_id" = as.integer(c(1, 1, 1, 1, 2, 2)), + "subject_id" = as.integer(c(1, 1, 1, 2, 2, 1)), + "cohort_start_date" = as.Date(c( + "2000-01-01", "2000-12-01", "2001-01-01", "2001-01-01", "2002-01-01", "2003-01-01" + )), + "cohort_end_date" =as.Date(c( + "2000-05-20", "2000-12-20", "2001-04-01", "2001-12-30", "2003-01-01", "2004-01-01" + )) + ) + cdm <- cdm_local |> copyCdm() + cdm$cohort <- cdm$cohort |> omopgenerics::newCohortTable() + cdm$cohort <- cdm$cohort |> intersectCohorts(gap = 9999) + expect_true(cdm$cohort |> dplyr::tally() |> dplyr::pull(n) == 0) + + PatientProfiles::mockDisconnect(cdm) +}) diff --git a/tests/testthat/test-unionCohorts.R b/tests/testthat/test-unionCohorts.R index 1d92588b..30039a59 100644 --- a/tests/testthat/test-unionCohorts.R +++ b/tests/testthat/test-unionCohorts.R @@ -4,7 +4,7 @@ test_that("unionCohorts works", { omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort1"), numberCohorts = 4, seed = 1) cdm <- cdm_local |> copyCdm() - # simple example + # simple example cdm$cohort2 <- unionCohorts(cdm$cohort1, name = "cohort2") expect_true(all( cdm$cohort2 %>% dplyr::pull("cohort_start_date") %>% sort() == @@ -297,3 +297,43 @@ test_that("keep original cohorts", { PatientProfiles::mockDisconnect(cdm) }) + +test_that("multiple observation periods", { + cdm_local <- omock::mockCdmReference() |> + omock::mockPerson(n = 4, seed = 1) + cdm_local$observation_period <- dplyr::tibble( + "observation_period_id" = as.integer(1:7), + "person_id" = as.integer(c(1, 1, 1, 2, 2, 3, 4)), + "observation_period_start_date" = as.Date(c( + "2000-01-01", "2001-01-01", "2003-01-01", "2001-01-01", "2002-01-01", + "2000-01-01", "2000-01-01" + )), + "observation_period_end_date" =as.Date(c( + "2000-12-20", "2002-01-01", "2005-01-01", "2001-12-31", "2004-01-01", + "2004-01-01", "2003-01-01" + )), + "period_type_concept_id" = NA_integer_ + ) + cdm_local$cohort <- dplyr::tibble( + "cohort_definition_id" = as.integer(c(1, 1, 1, 1, 2, 2)), + "subject_id" = as.integer(c(1, 1, 1, 2, 2, 1)), + "cohort_start_date" = as.Date(c( + "2000-01-01", "2000-12-01", "2001-01-01", "2001-01-01", "2002-01-01", "2003-01-01" + )), + "cohort_end_date" =as.Date(c( + "2000-05-20", "2000-12-20", "2001-04-01", "2001-12-30", "2003-01-01", "2004-01-01" + )) + ) + cdm <- cdm_local |> copyCdm() + cdm$cohort <- cdm$cohort |> omopgenerics::newCohortTable() |> unionCohorts(gap = 99999) + expect_equal( + collectCohort(cdm$cohort, 1), + dplyr::tibble( + "subject_id" = as.integer(c(1, 1, 1, 2, 2)), + "cohort_start_date" = as.Date(c("2000-01-01", "2001-01-01", "2003-01-01", "2001-01-01", "2002-01-01")), + "cohort_end_date" = as.Date(c("2000-12-20", "2001-04-01", "2004-01-01", "2001-12-30", "2003-01-01")) + ) + ) + + PatientProfiles::mockDisconnect(cdm) +}) From 81c56f806bf6814a37e02a4cc3e9561408b96f36 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: Wed, 16 Oct 2024 15:11:19 +0100 Subject: [PATCH 2/6] test intersect --- R/intersectCohorts.R | 8 ++++++-- tests/testthat/test-intersectCohorts.R | 23 +++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/R/intersectCohorts.R b/R/intersectCohorts.R index 94fd92e3..57e01de6 100644 --- a/R/intersectCohorts.R +++ b/R/intersectCohorts.R @@ -131,8 +131,12 @@ intersectCohorts <- function(cohort, dplyr::compute(name = tblName, temporary = FALSE) if (cohortOut |> dplyr::tally() |> dplyr::pull("n") > 0) { cohortOut <- cohortOut %>% - dplyr::compute(name = tblName, temporary = FALSE) |> - joinOverlap(name = tblName, gap = gap) + PatientProfiles::addObservationPeriodId(name = tblName) |> + joinOverlap( + name = tblName, gap = gap, + by = c("observation_period_id", "cohort_definition_id", "subject_id") + ) |> + dplyr::select(!"observation_period_id") } # attributes diff --git a/tests/testthat/test-intersectCohorts.R b/tests/testthat/test-intersectCohorts.R index b099ae7f..85f55559 100644 --- a/tests/testthat/test-intersectCohorts.R +++ b/tests/testthat/test-intersectCohorts.R @@ -608,20 +608,27 @@ test_that("multiple observation periods", { )), "period_type_concept_id" = NA_integer_ ) - cdm_local$cohort <- dplyr::tibble( - "cohort_definition_id" = as.integer(c(1, 1, 1, 1, 2, 2)), - "subject_id" = as.integer(c(1, 1, 1, 2, 2, 1)), + cdm_local$cohort1 <- dplyr::tibble( + "cohort_definition_id" = as.integer(c(1, 2, 1, 2)), + "subject_id" = as.integer(c(1, 1, 1, 1)), "cohort_start_date" = as.Date(c( - "2000-01-01", "2000-12-01", "2001-01-01", "2001-01-01", "2002-01-01", "2003-01-01" + "2000-01-01", "2000-12-01", "2001-01-01", "2001-01-01" )), "cohort_end_date" =as.Date(c( - "2000-05-20", "2000-12-20", "2001-04-01", "2001-12-30", "2003-01-01", "2004-01-01" + "2000-12-20", "2000-12-20", "2001-04-01", "2001-12-30" )) ) cdm <- cdm_local |> copyCdm() - cdm$cohort <- cdm$cohort |> omopgenerics::newCohortTable() - cdm$cohort <- cdm$cohort |> intersectCohorts(gap = 9999) - expect_true(cdm$cohort |> dplyr::tally() |> dplyr::pull(n) == 0) + cdm$cohort1 <- cdm$cohort1 |> omopgenerics::newCohortTable() + cdm$cohort1 <- cdm$cohort1 |> intersectCohorts(gap = 9999) + expect_equal( + collectCohort(cdm$cohort1, 1), + dplyr::tibble( + "subject_id" = as.integer(c(1, 1)), + "cohort_start_date" = as.Date(c("2001-01-01", "2001-12-30")), + "cohort_end_date" = as.Date(c("2000-01-01", "2000-12-20")) + ) + ) PatientProfiles::mockDisconnect(cdm) }) From 592b1e993c9a857e861f094e6eaeadef8884a87b 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: Wed, 16 Oct 2024 15:17:38 +0100 Subject: [PATCH 3/6] checks --- tests/testthat/test-intersectCohorts.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-intersectCohorts.R b/tests/testthat/test-intersectCohorts.R index 85f55559..1428d54b 100644 --- a/tests/testthat/test-intersectCohorts.R +++ b/tests/testthat/test-intersectCohorts.R @@ -625,8 +625,8 @@ test_that("multiple observation periods", { collectCohort(cdm$cohort1, 1), dplyr::tibble( "subject_id" = as.integer(c(1, 1)), - "cohort_start_date" = as.Date(c("2001-01-01", "2001-12-30")), - "cohort_end_date" = as.Date(c("2000-01-01", "2000-12-20")) + "cohort_start_date" = as.Date(c("2000-01-01", "2001-01-01")), + "cohort_end_date" = as.Date(c("2000-12-20", "2001-12-30")) ) ) From 319240370c15ab007fe5e5ea5ec51bfaf300cbde 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: Wed, 16 Oct 2024 17:24:05 +0100 Subject: [PATCH 4/6] Update test-intersectCohorts.R --- tests/testthat/test-intersectCohorts.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testthat/test-intersectCohorts.R b/tests/testthat/test-intersectCohorts.R index 1428d54b..53de670b 100644 --- a/tests/testthat/test-intersectCohorts.R +++ b/tests/testthat/test-intersectCohorts.R @@ -457,8 +457,6 @@ test_that("codelist", { "drug_exposure_start_date" = as.Date(.data$drug_exposure_start_date, origin = "2010-01-01"), "drug_exposure_end_date" = as.Date(.data$drug_exposure_end_date, origin = "2010-01-01") ) - cdm_local$observation_period <- cdm_local$observation_period|> - dplyr::mutate(observation_period_start_date = as.Date("1990-01-01"), observation_period_end_date = as.Date("2020-01-01")) cdm <- cdm_local |> copyCdm() From 412bffaebc45ab3db42e149893a1c000cdf4b10c 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: Wed, 16 Oct 2024 17:24:30 +0100 Subject: [PATCH 5/6] Update test-intersectCohorts.R --- tests/testthat/test-intersectCohorts.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-intersectCohorts.R b/tests/testthat/test-intersectCohorts.R index 53de670b..cacda5f4 100644 --- a/tests/testthat/test-intersectCohorts.R +++ b/tests/testthat/test-intersectCohorts.R @@ -434,7 +434,7 @@ test_that("codelist", { cdm_local <- omock::mockCdmReference() |> omock::mockPerson(n = 3) |> omock::mockObservationPeriod() |> - omock::mockCohort() + omock::mockCohort(seed = 1) cdm_local$concept <- dplyr::tibble( "concept_id" = c(1, 2, 3), "concept_name" = c("my concept 1", "my concept 2", "my concept 3"), @@ -457,6 +457,8 @@ test_that("codelist", { "drug_exposure_start_date" = as.Date(.data$drug_exposure_start_date, origin = "2010-01-01"), "drug_exposure_end_date" = as.Date(.data$drug_exposure_end_date, origin = "2010-01-01") ) + cdm_local$observation_period <- cdm_local$observation_period |> + dplyr::mutate(observation_period_start_date = as.Date("1990-01-01"), observation_period_end_date = as.Date("2020-01-01")) cdm <- cdm_local |> copyCdm() From 167fbc2c72a249ecb4d95082198197fc1fdc463f 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: Wed, 16 Oct 2024 20:48:44 +0100 Subject: [PATCH 6/6] checks --- R/mockCohortConstructor.R | 2 +- tests/testthat/test-intersectCohorts.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/mockCohortConstructor.R b/R/mockCohortConstructor.R index 3a9e1a40..f62b425e 100644 --- a/R/mockCohortConstructor.R +++ b/R/mockCohortConstructor.R @@ -56,7 +56,7 @@ mockCohortConstructor <- function(nPerson = 10, } if (!is.null(conceptIdClass) && !is.null(conceptId)) { - cdm <- cdm |> omock::mockConcepts(conceptSet = conceptId, domain = conceptIdClass) + cdm <- cdm |> omock::mockConcepts(conceptSet = conceptId, domain = conceptIdClass, seed = seed) } if (drugExposure) { diff --git a/tests/testthat/test-intersectCohorts.R b/tests/testthat/test-intersectCohorts.R index cacda5f4..dd57cd3f 100644 --- a/tests/testthat/test-intersectCohorts.R +++ b/tests/testthat/test-intersectCohorts.R @@ -458,7 +458,7 @@ test_that("codelist", { "drug_exposure_end_date" = as.Date(.data$drug_exposure_end_date, origin = "2010-01-01") ) cdm_local$observation_period <- cdm_local$observation_period |> - dplyr::mutate(observation_period_start_date = as.Date("1990-01-01"), observation_period_end_date = as.Date("2020-01-01")) + dplyr::mutate(observation_period_start_date = as.Date("1980-01-01"), observation_period_end_date = as.Date("2020-01-01")) cdm <- cdm_local |> copyCdm()