Skip to content

Commit

Permalink
Merge pull request #114 from OHDSI/mockCohort
Browse files Browse the repository at this point in the history
fixes recoredPerson = 1 behaviour
  • Loading branch information
catalamarti committed Sep 13, 2024
2 parents 4da15d0 + 9e1f10d commit 441312b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
27 changes: 19 additions & 8 deletions R/mockCohort.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mockCohort <- function(cdm,
numberCohorts = 1,
cohortName = paste0("cohort_", seq_len(numberCohorts)),
recordPerson = 1,
seed = 1) {
seed = NULL) {
# initial checks
checkInput(
cdm = cdm,
Expand Down Expand Up @@ -85,7 +85,7 @@ mockCohort <- function(cdm,
numberRows <-
recordPerson * (cdm$person |> dplyr::tally() |> dplyr::pull()) |> round()

numberRows <- numberRows * 1.2
numberRows <- (numberRows * 1.2) |> round()
rows_to_keep <- sum(numberRows / 1.2)


Expand Down Expand Up @@ -146,18 +146,29 @@ mockCohort <- function(cdm,
dplyr::ungroup() |>
dplyr::select(-"next_observation") |>
stats::na.omit() |>
dplyr::distinct() |>
dplyr::slice(1:rows_to_keep)



dplyr::distinct()

#correct cohort count
if(nrow(cohort) > 0) {
cohort_id <- cohort |>
dplyr::pull("cohort_definition_id") |>
unique() |> as.integer()

numberRows <- (numberRows / 1.2) |> round()

cohort <- purrr::map(
cohort_id,
\(x) cohort |>
dplyr::filter(.data$cohort_definition_id == x) |>
dplyr::slice(1:numberRows[x])
) |> dplyr::bind_rows()
}
# generate cohort set table

cohortName <- snakecase::to_snake_case(cohortName)

cohortSetTable <- dplyr::tibble(cohort_definition_id = cohortId,
cohort_name = cohortName)

# create class

cdm <-
Expand Down
2 changes: 1 addition & 1 deletion man/mockCohort.Rd

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

20 changes: 19 additions & 1 deletion tests/testthat/test-mockCohort.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test_that("mock cohort simple test", {
omock::mockObservationPeriod() |>
omock::mockCohort(recordPerson = 2)

testthat::expect_true(cdm$cohort |> dplyr::tally() == 200)
expect_true(cdm$cohort |> dplyr::tally() == 200)

expect_no_error(mockCdmReference() |>
mockPerson(nPerson = 100) |>
Expand All @@ -35,3 +35,21 @@ test_that("mock cohort simple test", {


})

test_that("cohort count", {
cdm <- omock::emptyCdmReference(cdmName = "mock") |>
omock::mockPerson(nPerson = 100) |>
omock::mockObservationPeriod() |>
omock::mockCohort(recordPerson = 1,
numberCohorts = 3,
seed = 1)

expect_true(all(
cdm$cohort |>
dplyr::group_by(cohort_definition_id) |>
dplyr::tally() |>
dplyr::pull(n) == c(100, 100, 100)
))

})

0 comments on commit 441312b

Please sign in to comment.