Skip to content

Commit

Permalink
Fix #136 (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonysena authored May 1, 2024
1 parent 0c0e487 commit a3ad15a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
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)

0 comments on commit a3ad15a

Please sign in to comment.