Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getCohortCounts() if cohortIds is not specified, but cohortDefinitionSet is #139

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ License: Apache License
VignetteBuilder: knitr
URL: https://ohdsi.github.io/CohortGenerator/, https://github.com/OHDSI/CohortGenerator
BugReports: https://github.com/OHDSI/CohortGenerator/issues
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Encoding: UTF-8
Language: en-US
11 changes: 10 additions & 1 deletion R/CohortCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
#' @template CohortTable
#'
#' @param cohortIds The cohort Id(s) used to reference the cohort in the cohort
#' table. If left empty, all cohorts in the table will be included.
#' table. If left empty and no `cohortDefinitionSet` argument is
#' specified, all cohorts in the table will be included. If
#' you specify the `cohortIds` AND `cohortDefinitionSet`, the counts will
#' reflect the `cohortIds` from the `cohortDefinitionSet`.
#'
#' @template CohortDefinitionSet
#'
Expand Down Expand Up @@ -67,6 +70,12 @@ getCohortCounts <- function(connectionDetails = NULL,
delta <- Sys.time() - start
ParallelLogger::logInfo(paste("Counting cohorts took", signif(delta, 3), attr(delta, "units")))
if (!is.null(cohortDefinitionSet)) {
# If the user has NOT specified a list of cohortIds
# to use to filter the cohortDefinitionSet, then
# extract the unique cohortIds
if (length(cohortIds) == 0) {
cohortIds <- cohortDefinitionSet$cohortId
}
counts <- merge(
x = counts,
y = cohortDefinitionSet[cohortDefinitionSet$cohortId %in% cohortIds, ],
Expand Down
5 changes: 4 additions & 1 deletion man/getCohortCounts.Rd

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

26 changes: 26 additions & 0 deletions tests/testthat/test-CohortCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,31 @@ test_that("Call getCohortCounts with a cohortDefinitionSet returns 0 counts for
expect_true(testCohortCounts[testCohortCounts$cohortId == 100, "cohortSubjects"] == 0)
})

test_that("Call getCohortCounts with no cohortId specified and cohortDefinitionSet returns 0 counts for cohortId not in cohort table", {
cohortDefinitionSet <- getCohortDefinitionSet(
settingsFileName = "testdata/id/Cohorts.csv",
jsonFolder = "testdata/id/cohorts",
sqlFolder = "testdata/id/sql/sql_server",
packageName = "CohortGenerator",
verbose = TRUE
)

cohortDefinitionSet <- rbind(
cohortDefinitionSet,
cohortDefinitionSet[1, ] |> transform(atlasId = 100, cohortId = 100, cohortName = "not in cohort table", logicDescription = "not in cohort table")
)

testCohortCounts <- getCohortCounts(
connectionDetails = connectionDetails,
cohortDatabaseSchema = "main",
cohortTable = "cohort",
cohortDefinitionSet = cohortDefinitionSet
)

expect_true(nrow(testCohortCounts) == 4)
expect_true(testCohortCounts[testCohortCounts$cohortId == 100, "cohortEntries"] == 0)
expect_true(testCohortCounts[testCohortCounts$cohortId == 100, "cohortSubjects"] == 0)
})

# Cleanup ------
rm(cohortCounts)
Loading