Skip to content

Commit

Permalink
revision
Browse files Browse the repository at this point in the history
  • Loading branch information
catalamarti committed Apr 11, 2024
1 parent c9ae297 commit aeec38b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 79 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
export(attrition)
export(cohortCodelist)
export(cohortCount)
export(conceptCohortSet)
export(conceptCohort)
export(generateIntersectCohortSet)
export(generateMatchedCohortSet)
export(getIdentifier)
Expand Down
73 changes: 26 additions & 47 deletions R/conceptCohortSet.R → R/conceptCohort.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@
#' @param conceptSet A conceptSet, can either be a list of concepts, a codelist
#' or a conceptSetExpression (TO DO).
#' @param name Name of the cohort in the cdm object.
#' @param verbose Whether to print intermediate progress messages.
#'
#' @export
#'
#' @return A cohort_table object.
#'
conceptCohortSet <- function(cdm,
conceptSet = NULL,
name = "cohort",
verbose = TRUE) {
conceptCohort <- function(cdm,
conceptSet,
name) {
# initial input validation
cdm <- validateCdm(cdm)
name <- validateName(name)
verbose <- validateVerbose(verbose)
if (length(conceptSet) == 0) {
if (verbose) {
cli::cli_inform(c("i" = "Empty codelist provided, returning empty cohort"))
}
cli::cli_inform(c("i" = "Empty codelist provided, returning empty cohort"))
cdm <- omopgenerics::emptyCohortTable(cdm = cdm, name = name)
return(cdm[[name]])
}
Expand All @@ -37,17 +32,15 @@ conceptCohortSet <- function(cdm,
dplyr::mutate("type" = "index event") |>
addDomains(cdm)

if (verbose) {
ud <- cohortCodelist |>
dplyr::group_by(.data$domain_id) |>
dplyr::tally() |>
dplyr::collect() |>
dplyr::filter(!.data$domain_id %in% domainsData$domain_id)
for (k in seq_len(nrow(ud))) {
cli::cli_inform(c(
"x" = "Domain {.strong {ud$domain_id[k]}} ({ud$n[k]} concept{?s}) excluded because it is not supported."
))
}
ud <- cohortCodelist |>
dplyr::group_by(.data$domain_id) |>
dplyr::tally() |>
dplyr::collect() |>
dplyr::filter(!.data$domain_id %in% domainsData$domain_id)
for (k in seq_len(nrow(ud))) {
cli::cli_inform(c(
"x" = "Domain {.strong {ud$domain_id[k]}} ({ud$n[k]} concept{?s}) excluded because it is not supported."
))
}

cohortCodelist <- cohortCodelist |>
Expand All @@ -66,18 +59,14 @@ conceptCohortSet <- function(cdm,
concept <- domainsData$concept[domainsData$domain_id == domain]
start <- domainsData$start[domainsData$domain_id == domain]
end <- domainsData$end[domainsData$domain_id == domain]
if (verbose) {
n <- cohortCodelist |>
dplyr::filter(.data$domain_id %in% .env$domain) |>
dplyr::tally() |>
dplyr::pull()
}
n <- cohortCodelist |>
dplyr::filter(.data$domain_id %in% .env$domain) |>
dplyr::tally() |>
dplyr::pull()
if (table %in% names(cdm)) {
if (verbose) {
cli::cli_inform(c(
"i" = "Subsetting table {.strong {table}} using {n} concept{?s} with domain: {.strong {domain}}."
))
}
cli::cli_inform(c(
"i" = "Subsetting table {.strong {table}} using {n} concept{?s} with domain: {.strong {domain}}."
))
cohorts[[k]] <- cdm[[table]] |>
dplyr::select(
"subject_id" = "person_id",
Expand All @@ -91,17 +80,15 @@ conceptCohortSet <- function(cdm,
dplyr::select("concept_id", "cohort_definition_id"),
by = "concept_id"
)
} else if (verbose) {
} else {
cli::cli_inform(c(
"x" = "Domain {.strong {domain}} ({n} concept{?s}) excluded because table {table} is not present in the cdm."
))
}
}

if (length(cohorts) == 0) {
if (verbose) {
cli::cli_inform(c("i" = "No table could be subsetted, returning empty cohort."))
}
cli::cli_inform(c("i" = "No table could be subsetted, returning empty cohort."))
cdm <- omopgenerics::emptyCohortTable(cdm = cdm, name = name)
cdm[[name]] <- cdm[[name]] |>
omopgenerics::newCohortTable(
Expand All @@ -112,9 +99,7 @@ conceptCohortSet <- function(cdm,
return(cdm[[name]])
}

if (verbose) {
cli::cli_inform(c("i" = "Subsetting tables."))
}
cli::cli_inform(c("i" = "Subsetting tables."))
cohort <- cohorts[[1]]
if (length(cohorts) > 1) {
for (k in 2:length(cohorts)) {
Expand All @@ -124,18 +109,14 @@ conceptCohortSet <- function(cdm,
cohort <- cohort |>
dplyr::compute(name = name, temporary = FALSE)

if (verbose) {
cli::cli_inform(c("i" = "Collapsing records."))
}
cli::cli_inform(c("i" = "Collapsing records."))
# assign to cdm so we keep class, to be removed when https://github.com/darwin-eu-dev/omopgenerics/issues/256
cdm[[name]] <- cohort |>
collapseGap(gap = 0)
cohort <- cdm[[name]] |>
dplyr::compute(name = name, temporary = FALSE)

if (verbose) {
cli::cli_inform(c("i" = "Creating cohort attributes."))
}
cli::cli_inform(c("i" = "Creating cohort attributes."))
cdm[[name]] <- cohort |>
omopgenerics::newCohortTable(
cohortSetRef = cohortSet,
Expand All @@ -145,9 +126,7 @@ conceptCohortSet <- function(cdm,
dplyr::collect()
)

if (verbose) {
cli::cli_inform(c("v" = "Cohort {.strong {name}} created."))
}
cli::cli_inform(c("v" = "Cohort {.strong {name}} created."))

return(cdm[[name]])
}
Expand Down
7 changes: 0 additions & 7 deletions R/validateFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,3 @@ validateName <- function(name) {
validateConceptSet <- function(conceptSet) {
omopgenerics::newCodelist(conceptSet)
}

validateVerbose <- function(verbose) {
if (!is.logical(verbose) | length(verbose) != 1 | is.na(verbose)) {
cli::cli_abort("verbose must be TRUE or FALSE.")
}
return(invisible(verbose))
}
10 changes: 4 additions & 6 deletions man/conceptCohortSet.Rd → man/conceptCohort.Rd

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

Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ test_that("expected errors and messages", {
)

# not a cdm reference
expect_error(conceptCohortSet(cdm = NULL))
expect_error(conceptCohort(cdm = NULL, name = "cohort", conceptSet = NULL))

# wrong naming
expect_error(conceptCohortSet(cdm = cdm, name = NA))
expect_error(conceptCohortSet(cdm = cdm, name = 1))
expect_error(conceptCohortSet(cdm = cdm, name = c("ass", "asdf")))
expect_error(conceptCohortSet(cdm = cdm, name = "AAAA"))
expect_no_error(x <- conceptCohortSet(cdm = cdm, name = "my_cohort"))
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_no_error(x <- conceptCohort(cdm = cdm, name = "my_cohort", conceptSet = NULL))
expect_true("my_cohort" == omopgenerics::tableName(x))

# empty cohort
expect_no_error(x <- conceptCohortSet(cdm = cdm, conceptSet = NULL))
expect_no_error(x <- conceptCohort(cdm = cdm, conceptSet = NULL, name = "cohort"))
expect_true(inherits(x, "cohort_table"))
expect_true(x |> dplyr::collect() |> nrow() == 0)

# not codelist
expect_error(x <- conceptCohortSet(cdm = cdm, conceptSet = 1))
expect_error(x <- conceptCohortSet(cdm = cdm, conceptSet = list(1)))
expect_error(x <- conceptCohort(cdm = cdm, conceptSet = 1, name = "cohort"))
expect_error(x <- conceptCohort(cdm = cdm, conceptSet = list(1), name = "cohort"))
expect_message(expect_message(
x <- conceptCohortSet(cdm = cdm, conceptSet = list(a = 1))
x <- conceptCohort(cdm = cdm, conceptSet = list(a = 1), name = "cohort")
))
expect_true(inherits(x, "cohort_table"))
expect_true(x |> dplyr::collect() |> nrow() == 0)
Expand All @@ -52,14 +52,9 @@ test_that("expected errors and messages", {
cohortCodelist(x, 1), omopgenerics::newCodelist(list())
))
expect_message(expect_message(
x <- conceptCohortSet(cdm = cdm, conceptSet = list(a = 2))
x <- conceptCohort(cdm = cdm, conceptSet = list(a = 2), name = "cohort")
))

# verbose
expect_error(x <- conceptCohortSet(cdm = cdm, verbose = "my_cohort"))
expect_error(x <- conceptCohortSet(cdm = cdm, verbose = c(T, F)))
expect_error(x <- conceptCohortSet(cdm = cdm, verbose = as.logical(NA)))

})

test_that("simple example", {
Expand Down Expand Up @@ -97,7 +92,7 @@ test_that("simple example", {
)
)

expect_no_error(cohort <- conceptCohortSet(cdm = cdm, conceptSet = list(a = 1)))
expect_no_error(cohort <- conceptCohort(cdm = cdm, conceptSet = list(a = 1), name = "cohort"))

expect_true(cohort |> dplyr::tally() |> dplyr::pull() == 4)
expect_true(cohortCount(cohort)$number_records == 4)
Expand Down Expand Up @@ -164,7 +159,7 @@ test_that("simple example duckdb", {

cdm <- CDMConnector::copyCdmTo(con = DBI::dbConnect(duckdb::duckdb()), cdm = cdm, schema = "main")

expect_no_error(cohort <- conceptCohortSet(cdm = cdm, conceptSet = list(a = 1)))
expect_no_error(cohort <- conceptCohort(cdm = cdm, conceptSet = list(a = 1), name = "cohort"))

expect_true(cohort |> dplyr::tally() |> dplyr::pull() == 4)
expect_true(cohortCount(cohort)$number_records == 4)
Expand Down

0 comments on commit aeec38b

Please sign in to comment.