Skip to content

Commit

Permalink
Merge pull request #359 from OHDSI/max_age_inf
Browse files Browse the repository at this point in the history
allow max age to be Inf
  • Loading branch information
edward-burn authored Oct 18, 2024
2 parents fdb2280 + 8a42005 commit 9cca6d3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
7 changes: 4 additions & 3 deletions R/requireDemographics.R
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,11 @@ reqDemographicsCohortSet <- function(set,
.data$cohort_name
))
}

combinations <- combinations |>
dplyr::mutate(min_age = as.integer(sub("_.*", "", .data$age_range)), max_age = as.integer(sub(".*_", "", .data$age_range))) |>
dplyr::mutate(age_range = stringr::str_replace(.data$age_range, "_999", "_Inf")) |>
dplyr::mutate(min_age = as.integer(sub("_.*", "", .data$age_range)),
max_age = sub(".*_", "", .data$age_range)) |>
dplyr::mutate(max_age = stringr::str_replace(.data$max_age, "Inf", "999")) |>
dplyr::mutate(max_age = as.integer(.data$max_age)) |>
dplyr::select(!c("group_id"))

# new cohort set
Expand Down
10 changes: 6 additions & 4 deletions R/trimDemographics.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ trimDemographics <- function(cohort,
ids <- settings(cohort)$cohort_definition_id

# replace age Inf to avoid potential sql issues
for (j in seq_along(ageRange)) {
ageRange[[j]][is.infinite(ageRange[[j]])] <- as.integer(999)
}

# temp tables
tablePrefix <- omopgenerics::tmpPrefix()
tmpName <- omopgenerics::uniqueTableName(tablePrefix)
Expand Down Expand Up @@ -91,6 +87,7 @@ trimDemographics <- function(cohort,
minFutureObservation = minFutureObservation,
requirementInteractions = TRUE
)

# insert settings
cdm <- omopgenerics::insertTable(
cdm = cdm,
Expand Down Expand Up @@ -152,6 +149,11 @@ trimDemographics <- function(cohort,
}
if (!is.null(ageRange)) {
cli::cli_inform(c("Trim age"))

for (j in seq_along(ageRange)) {
ageRange[[j]][is.infinite(ageRange[[j]])] <- as.integer(999)
}

newCohort <- newCohort %>%
dplyr::mutate(!!!datesAgeRange(ageRange)) %>%
dplyr::mutate(
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-requireDemographics.R
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,25 @@ test_that("requireInteractions", {
)
PatientProfiles::mockDisconnect(cdm)
})

test_that("Inf age", {

testthat::skip_on_cran()
cdm_local <- omock::mockCdmReference() |>
omock::mockPerson(n = 3,seed = 1) |>
omock::mockObservationPeriod(seed = 1) |>
omock::mockCohort(numberCohorts = 3, seed = 4)
# to remove in new omock
cdm_local$person <- cdm_local$person |>
dplyr::mutate(dplyr::across(dplyr::ends_with("of_birth"), ~ as.numeric(.x)))
cdm <- cdm_local |> copyCdm()

expect_no_error(cdm$cohort1 <- cdm$cohort |>
requireDemographics(ageRange = c(0, Inf),
name = "cohort1"))
expect_no_error(cdm$cohort2 <-cdm$cohort |>
requireDemographics(ageRange = list(c(0, 17),
c(18,Inf)),
name = "cohort2"))

})

0 comments on commit 9cca6d3

Please sign in to comment.