Skip to content

Commit

Permalink
issue #328
Browse files Browse the repository at this point in the history
  • Loading branch information
nmercadeb committed Oct 15, 2024
1 parent a06cbd2 commit be78163
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 14 deletions.
8 changes: 8 additions & 0 deletions R/documentationHelper.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,11 @@ NULL
#' @keywords internal
NULL

#' Helper for consistent documentation of `keepOriginalCohorts`.
#'
#' @param keepOriginalCohorts If TRUE the original cohorts will be return
#' together with the new ones. If FALSE only the new cohort will be returned.
#'
#' @name keepOriginalCohortsDoc
#' @keywords internal
NULL
4 changes: 1 addition & 3 deletions R/intersectCohorts.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
#' @inheritParams cohortIdSubsetDoc
#' @inheritParams gapDoc
#' @inheritParams nameDoc
#' @inheritParams keepOriginalCohortsDoc
#' @param returnNonOverlappingCohorts Whether the generated cohorts are mutually
#' exclusive or not.
#' @param keepOriginalCohorts If TRUE the original cohorts and the newly
#' created intersection cohort will be returned. If FALSE only the new cohort
#' will be returned.
#'
#' @export
#'
Expand Down
12 changes: 10 additions & 2 deletions R/matchCohorts.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @inheritParams cohortDoc
#' @inheritParams cohortIdSubsetDoc
#' @inheritParams nameDoc
#' @inheritParams keepOriginalCohortsDoc
#' @param matchSex Whether to match in sex.
#' @param matchYearOfBirth Whether to match in year of birth.
#' @param ratio Number of allowed matches per individual in the target cohort.
Expand Down Expand Up @@ -34,6 +35,7 @@ matchCohorts <- function(cohort,
matchSex = TRUE,
matchYearOfBirth = TRUE,
ratio = 1,
keepOriginalCohorts = FALSE,
name = tableName(cohort)) {
cli::cli_inform("Starting matching")

Expand Down Expand Up @@ -64,6 +66,10 @@ matchCohorts <- function(cohort,
tablePrefix <- omopgenerics::tmpPrefix()
target <- omopgenerics::uniqueTableName(tablePrefix)
control <- omopgenerics::uniqueTableName(tablePrefix)
if (keepOriginalCohorts) {
keep <- omopgenerics::uniqueTableName(tablePrefix)
cdm[[keep]] <- subsetCohorts(cohort, cohortId, name = keep)
}

if (cohort |> settings() |> nrow() == 0) {
cdm[[name]] <- cohort |>
Expand Down Expand Up @@ -166,8 +172,10 @@ matchCohorts <- function(cohort,
)

# Bind both cohorts
cli::cli_inform(c("Binding both cohorts"))
cdm <- omopgenerics::bind(cdm[[target]], cdm[[control]], name = name)
cli::cli_inform(c("Binding cohorts"))
cohorts <- list(cdm[[target]], cdm[[control]])
if (keepOriginalCohorts) cohorts <- c(list(cdm[[keep]]), cohorts)
cdm <- do.call(omopgenerics::bind, c(cohorts, "name" = name))

# drop tmp tables
omopgenerics::dropTable(cdm = cdm, name = dplyr::starts_with(tablePrefix))
Expand Down
4 changes: 1 addition & 3 deletions R/unionCohorts.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
#' @inheritParams cohortIdSubsetDoc
#' @inheritParams gapDoc
#' @inheritParams nameDoc
#' @inheritParams keepOriginalCohortsDoc
#' @param cohortName Name of the returned cohort. If NULL, the cohort name will
#' be created by collapsing the individual cohort names, separated by "_".
#' @param keepOriginalCohorts If TRUE the original cohorts and the newly
#' created union cohort will be returned. If FALSE only the new cohort will be
#' returned.
#'
#' @export
#'
Expand Down
5 changes: 2 additions & 3 deletions man/intersectCohorts.Rd

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

13 changes: 13 additions & 0 deletions man/keepOriginalCohortsDoc.Rd

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

4 changes: 4 additions & 0 deletions man/matchCohorts.Rd

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

5 changes: 2 additions & 3 deletions man/unionCohorts.Rd

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

33 changes: 33 additions & 0 deletions tests/testthat/test-matchCohorts.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,36 @@ test_that("test exactMatchingCohort with a ratio bigger than 1", {

PatientProfiles::mockDisconnect(cdm)
})

test_that("keepOriginalCohorts works" , {
cdm <- mockCohortConstructor()
cohort <- cdm$cohort2 |> matchCohorts(cohortId = 1, keepOriginalCohorts = TRUE, name = "new_cohort")
expect_equal(
settings(cohort),
dplyr::tibble(
cohort_definition_id = as.integer(1:3),
cohort_name = c("cohort_1", "cohort_1_matched", "matched_to_cohort_1"),
target_table_name = c(NA, rep("cohort2", 2)),
target_cohort_id = c(NA, 1L, 1L),
target_cohort_name = c(NA, "cohort_1_matched", "cohort_1_matched"),
match_sex = c(NA, rep(TRUE, 2)),
match_year_of_birth = c(NA, rep(TRUE, 2)),
match_status = c(NA, "target", "control")
)
)
cohort <- cdm$cohort2 |> matchCohorts(keepOriginalCohorts = TRUE)
expect_equal(
settings(cohort),
dplyr::tibble(
cohort_definition_id = as.integer(1:6),
cohort_name = c("cohort_1", "cohort_2", "cohort_1_matched", "cohort_2_matched", "matched_to_cohort_1", "matched_to_cohort_2"),
target_table_name = c(NA, NA, rep("cohort2", 4)),
target_cohort_id = c(NA, NA, 1L, 2L, 1L, 2L),
target_cohort_name = c(NA, NA, "cohort_1_matched", "cohort_2_matched", "cohort_1_matched", "cohort_2_matched"),
match_sex = c(NA, NA, rep(TRUE, 4)),
match_year_of_birth = c(NA, NA, rep(TRUE, 4)),
match_status = c(NA, NA, "target", "target", "control", "control")
)
)
PatientProfiles::mockDisconnect(cdm)
})

0 comments on commit be78163

Please sign in to comment.