Skip to content

Commit

Permalink
Merge pull request #350 from OHDSI/patient_profiles
Browse files Browse the repository at this point in the history
Use `name` in PatientProfiles add functions
  • Loading branch information
edward-burn authored Oct 16, 2024
2 parents bf37049 + c7c3db1 commit 8159619
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions R/collapseCohorts.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ collapseCohorts <- function(cohort,
}
if (gap == Inf) {
newCohort <- newCohort |>
PatientProfiles::addObservationPeriodId() |>
PatientProfiles::addObservationPeriodId(name = tmpNewCohort) |>
joinAll(by = c(
"cohort_definition_id",
"subject_id",
Expand All @@ -52,7 +52,7 @@ collapseCohorts <- function(cohort,
dplyr::select(!"observation_period_id")
} else if (gap > 0) {
newCohort <- newCohort |>
PatientProfiles::addObservationPeriodId() |>
PatientProfiles::addObservationPeriodId(name = tmpNewCohort) |>
joinOverlap(
name = tmpNewCohort,
gap = gap,
Expand Down
14 changes: 9 additions & 5 deletions R/exitAtColumnDate.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ exitAtColumnDate <- function(cohort,
cli::cli_abort("All cohort records must have at least one non-empty date in the `dateColumns`")
}

tmpName <- omopgenerics::uniqueTableName()
tmpPrefix <- omopgenerics::tmpPrefix()
tmpName <- omopgenerics::uniqueTableName(prefix = tmpPrefix)

if (all(ids %in% cohortId)) {
newCohort <- cohort |>
Expand Down Expand Up @@ -186,7 +187,7 @@ exitAtColumnDate <- function(cohort,
dplyr::compute(name = tmpName, temporary = FALSE)

# checks with informative errors
validateNewCohort(newCohort, cdm, tmpName)
validateNewCohort(newCohort, cdm, tmpPrefix)

if (any(!ids %in% cohortId)) {
dateColumns <- dateColumns[!dateColumns %in% c("cohort_end_date", "cohort_start_date")]
Expand All @@ -210,7 +211,7 @@ exitAtColumnDate <- function(cohort,
dplyr::compute(name = name, temporary = FALSE) |>
omopgenerics::newCohortTable(.softValidation = TRUE)

cdm <- omopgenerics::dropTable(cdm, name = dplyr::starts_with(tmpName))
cdm <- omopgenerics::dropTable(cdm, name = dplyr::starts_with(tmpPrefix))

return(newCohort)
}
Expand All @@ -230,8 +231,11 @@ validateNewCohort <- function(newCohort, cdm, tmpName) {
}
## Out of observation
checkObservation <- newCohort |>
PatientProfiles::addFutureObservation(futureObservationName = "observation_end_0123456789",
futureObservationType = "date") |>
PatientProfiles::addFutureObservation(
futureObservationName = "observation_end_0123456789",
futureObservationType = "date",
name = omopgenerics::uniqueTableName(prefix = tmpName)
) |>
dplyr::filter(.data$cohort_end_date > .data$observation_end_0123456789) |>
dplyr::tally() |>
dplyr::pull("n")
Expand Down
4 changes: 2 additions & 2 deletions R/exitAtDate.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exitAtObservationEnd <- function(cohort,

# create new cohort
newCohort <- cohort |>
PatientProfiles::addFutureObservation(futureObservationType = "date") |>
PatientProfiles::addFutureObservation(futureObservationType = "date", name = name) |>
# exit at observation end
dplyr::mutate(
"cohort_end_date" = dplyr::if_else(
Expand Down Expand Up @@ -93,7 +93,7 @@ exitAtDeath <- function(cohort,

# create new cohort
newCohort <- cohort |>
PatientProfiles::addDeathDate() |>
PatientProfiles::addDeathDate(name = name) |>
# exit
dplyr::mutate(
"cohort_end_date" = dplyr::if_else(
Expand Down
3 changes: 2 additions & 1 deletion R/padCohortEnd.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ padCohortEnd <- function(cohort,
cohort <- cohort |>
PatientProfiles::addFutureObservationQuery(
futureObservationType = "date",
futureObservationName = futureObsCol)
futureObservationName = futureObsCol
)
}

if(length(cohortId) < length(ids)) {
Expand Down
9 changes: 7 additions & 2 deletions R/requireCohortIntersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ requireCohortIntersect <- function(cohort,
dplyr::filter(.data$cohort_definition_id == .env$targetCohortId) %>%
dplyr::pull("cohort_name")

subsetName <- omopgenerics::uniqueTableName()
subsetCohort <- cohort %>%
dplyr::select(dplyr::all_of(.env$cols)) %>%
PatientProfiles::addCohortIntersectCount(
Expand All @@ -91,7 +92,8 @@ requireCohortIntersect <- function(cohort,
targetEndDate = targetEndDate,
window = window,
censorDate = censorDate,
nameStyle = "intersect_cohort"
nameStyle = "intersect_cohort",
name = name
)

subsetCohort <- subsetCohort %>%
Expand All @@ -103,7 +105,8 @@ requireCohortIntersect <- function(cohort,
) |
(!.data$cohort_definition_id %in% .env$cohortId)
) %>%
dplyr::select(cols)
dplyr::select(cols) %>%
dplyr::compute(name = subsetName, temporary = FALSE)

# attrition reason
if (all(intersections == 0)) {
Expand Down Expand Up @@ -134,5 +137,7 @@ requireCohortIntersect <- function(cohort,
omopgenerics::newCohortTable(.softValidation = TRUE) %>%
omopgenerics::recordCohortAttrition(reason = reason, cohortId = cohortId)

omopgenerics::dropTable(cdm = cdm, name = subsetName)

return(x)
}
9 changes: 7 additions & 2 deletions R/requireConceptIntersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ requireConceptIntersect <- function(cohort,
if (length(conceptSet) == 0) {
cli::cli_inform(c("i" = "Empty codelist provided, returning input cohort"))
} else {
subsetName <- omopgenerics::uniqueTableName()
subsetCohort <- cohort %>%
dplyr::select(dplyr::all_of(.env$cols)) %>%
PatientProfiles::addConceptIntersectCount(
Expand All @@ -81,7 +82,8 @@ requireConceptIntersect <- function(cohort,
targetEndDate = targetEndDate,
window = window,
censorDate = censorDate,
nameStyle = "intersect_concept"
nameStyle = "intersect_concept",
name = subsetName
)

subsetCohort <- subsetCohort %>%
Expand All @@ -93,7 +95,8 @@ requireConceptIntersect <- function(cohort,
) |
(!.data$cohort_definition_id %in% .env$cohortId)
) %>%
dplyr::select(cols)
dplyr::select(cols) %>%
dplyr::compute(name = subsetName, temporary = FALSE)

# attrition reason
if (all(intersections == 0)) {
Expand Down Expand Up @@ -123,6 +126,8 @@ requireConceptIntersect <- function(cohort,
dplyr::compute(name = name, temporary = FALSE) %>%
omopgenerics::newCohortTable(.softValidation = TRUE) %>%
omopgenerics::recordCohortAttrition(reason = reason, cohortId = cohortId)

omopgenerics::dropTable(cdm = cdm, name = subsetName)
}

return(cohort)
Expand Down
12 changes: 9 additions & 3 deletions R/requireDeathFlag.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,23 @@ requireDeathFlag <- function(cohort,
window_start <- window[[1]][1]
window_end <- window[[1]][2]

subsetName <- omopgenerics::uniqueTableName()
subsetCohort <- cohort %>%
dplyr::select(dplyr::all_of(.env$cols)) %>%
PatientProfiles::addDeathFlag(
indexDate = indexDate,
censorDate = censorDate,
window = window,
deathFlagName = "death"
deathFlagName = "death",
name = subsetName
)

if (isFALSE(negate)) {
subsetCohort <- subsetCohort %>%
dplyr::filter(.data$death == 1 |
(!.data$cohort_definition_id %in% cohortId)) %>%
dplyr::select(!"death")
dplyr::select(!"death") %>%
dplyr::compute(name = subsetName, temporary = FALSE)
# attrition reason
reason <- glue::glue("Death between {window_start} & ",
"{window_end} days relative to {indexDate}")
Expand All @@ -80,7 +83,8 @@ requireDeathFlag <- function(cohort,
subsetCohort <- subsetCohort %>%
dplyr::filter(.data$death != 1 |
(!.data$cohort_definition_id %in% cohortId)) %>%
dplyr::select(!"death")
dplyr::select(!"death") %>%
dplyr::compute(name = subsetName, temporary = FALSE)
# attrition reason
reason <- glue::glue("Alive between {window_start} & ",
"{window_end} days relative to {indexDate}")
Expand All @@ -96,5 +100,7 @@ requireDeathFlag <- function(cohort,
omopgenerics::newCohortTable(.softValidation = TRUE) %>%
omopgenerics::recordCohortAttrition(reason = reason, cohortId = cohortId)

omopgenerics::dropTable(cdm = cdm, name = subsetName)

return(x)
}
6 changes: 3 additions & 3 deletions R/requireDemographics.R
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ demographicsFilter <- function(cohort,
age = reqAge,
sex = reqSex,
priorObservation = reqPriorObservation,
futureObservation = reqFutureObservation
) |>
dplyr::compute(name = workingName, temporary = FALSE)
futureObservation = reqFutureObservation,
name = workingName
)

# all output cohorts in one table to filter all at the same time:
workingTable <- workingTable |>
Expand Down
9 changes: 7 additions & 2 deletions R/requireTableIntersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ requireTableIntersect <- function(cohort,
cli::cli_abort("Currently just one table supported.")
}

subsetName <- omopgenerics::uniqueTableName()
subsetCohort <- cohort %>%
dplyr::select(dplyr::all_of(.env$cols)) %>%
PatientProfiles::addTableIntersectCount(
Expand All @@ -76,7 +77,8 @@ requireTableIntersect <- function(cohort,
targetEndDate = targetEndDate,
window = window,
censorDate = censorDate,
nameStyle = "intersect_table"
nameStyle = "intersect_table",
name = subsetName
)

subsetCohort <- subsetCohort %>%
Expand All @@ -88,7 +90,8 @@ requireTableIntersect <- function(cohort,
) |
(!.data$cohort_definition_id %in% .env$cohortId)
) %>%
dplyr::select(cols)
dplyr::select(cols) %>%
dplyr::compute(name = subsetName, temporary = FALSE)

# attrition reason
if (all(intersections == 0)) {
Expand Down Expand Up @@ -119,5 +122,7 @@ requireTableIntersect <- function(cohort,
omopgenerics::newCohortTable(.softValidation = TRUE) %>%
omopgenerics::recordCohortAttrition(reason = reason, cohortId = cohortId)

omopgenerics::dropTable(cdm = cdm, name = subsetName)

return(x)
}

0 comments on commit 8159619

Please sign in to comment.