Skip to content

Commit

Permalink
Merge pull request #67 from OHDSI/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jreps authored Feb 11, 2025
2 parents f4034b3 + aeb33da commit b8b5976
Show file tree
Hide file tree
Showing 36 changed files with 275 additions and 194 deletions.
12 changes: 5 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Package: Characterization
Type: Package
Title: Characterizations of Cohorts
Version: 2.1.0
Date: 2024-10-07
Version: 2.1.2
Date: 2024-1-22
Authors@R: c(
person("Jenna", "Reps", , "[email protected]", role = c("aut", "cre")),
person("Patrick", "Ryan", , "[email protected]", role = c("aut")),
person("Chris", "Knoll", , "[email protected]", role = c("aut"))
)
Maintainer: Jenna Reps <[email protected]>
Description: Various characterizations of target and outcome cohorts.
Description: Various characterizations of target and outcome cohorts for data mapped to the OMOP CDM.
License: Apache License 2.0
URL: https://ohdsi.github.io/Characterization, https://github.com/OHDSI/Characterization
BugReports: https://github.com/OHDSI/Characterization/issues
Expand All @@ -34,12 +34,10 @@ Suggests:
knitr,
markdown,
rmarkdown,
ShinyAppBuilder,
OhdsiShinyAppBuilder,
shiny,
withr
Remotes:
ohdsi/ShinyAppBuilder
NeedsCompilation: no
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Encoding: UTF-8
VignetteBuilder: knitr
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Characterization 2.1.2
======================
- added input ignoreWhenEmpty to cleanIncremental() that does not run incrementalClean if there are no incremental files

Characterization 2.1.1
======================
- fixed result database column type for mean_exposure_time

Characterization 2.1.0
======================
- risk factors and case series now restrict to first outcome only.
Expand Down
56 changes: 35 additions & 21 deletions R/AggregateCovariates.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ computeTargetAggregateCovariateAnalyses <- function(
minCharacterizationMean = 0,
minCellCount = 0,
...) {

message("Target Aggregate: starting")

# get settings
settingId <- unique(settings$settingId)
targetIds <- unique(settings$targetId)
Expand Down Expand Up @@ -229,10 +232,11 @@ computeTargetAggregateCovariateAnalyses <- function(
tempTable = T,
dropTableIfExists = T,
createTable = T,
progressBar = F
progressBar = F,
tempEmulationSchema = tempEmulationSchema
)

message("Computing aggregate target cohorts")
message("Target Aggregate: Computing aggregate target cohorts")
start <- Sys.time()

sql <- SqlRender::loadRenderTranslateSql(
Expand All @@ -255,7 +259,7 @@ computeTargetAggregateCovariateAnalyses <- function(
)
completionTime <- Sys.time() - start

message(paste0("Computing target cohorts took ", round(completionTime, digits = 1), " ", units(completionTime)))
message(paste0("Target Aggregate: Computing target cohorts took ", round(completionTime, digits = 1), " ", units(completionTime)))
## get counts
message("Extracting target cohort counts")
sql <- "select
Expand All @@ -270,31 +274,32 @@ computeTargetAggregateCovariateAnalyses <- function(
group by cohort_definition_id;"
sql <- SqlRender::translate(
sql = sql,
targetDialect = connectionDetails$dbms
targetDialect = connectionDetails$dbms,
tempEmulationSchema = tempEmulationSchema
)
counts <- DatabaseConnector::querySql(
connection = connection,
sql = sql,
snakeCaseToCamelCase = T,
)

message("Computing aggregate target covariate results")
message("Target Aggregate: Computing aggregate target covariate results")

result <- FeatureExtraction::getDbCovariateData(
connection = connection,
oracleTempSchema = tempEmulationSchema,
cdmDatabaseSchema = cdmDatabaseSchema,
cohortTable = "#agg_cohorts_before",
cohortTableIsTemp = T,
cohortIds = -1,
covariateSettings = ParallelLogger::convertJsonToSettings(covariateSettings),
cdmVersion = cdmVersion,
aggregated = T,
minCharacterizationMean = minCharacterizationMean
minCharacterizationMean = minCharacterizationMean,
tempEmulationSchema = tempEmulationSchema
)

# drop temp tables
message("Dropping temp tables")
message("Target Aggregate: Dropping temp tables")
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = "DropTargetCovariate.sql",
packageName = "Characterization",
Expand All @@ -308,6 +313,7 @@ computeTargetAggregateCovariateAnalyses <- function(
)

# export all results to csv files
message("Target Aggregate: Exporting to csv")
exportAndromedaToCsv(
andromeda = result,
outputFolder = outputFolder,
Expand All @@ -318,6 +324,8 @@ computeTargetAggregateCovariateAnalyses <- function(
minCellCount = minCellCount
)

message("Target Aggregate: ending")

return(invisible(T))
}

Expand All @@ -337,6 +345,8 @@ computeCaseAggregateCovariateAnalyses <- function(
minCharacterizationMean = 0,
minCellCount = 0,
...) {

message("Case Aggregates: starting")
# check inputs

# create cohortDetails - all Ts, minPriorObservation, twice (type = Tall, Target)
Expand Down Expand Up @@ -410,10 +420,11 @@ computeCaseAggregateCovariateAnalyses <- function(
tempTable = T,
dropTableIfExists = T,
createTable = T,
progressBar = F
progressBar = F,
tempEmulationSchema = tempEmulationSchema
)

message("Computing aggregate case covariate cohorts")
message("Case Aggregates: Computing aggregate case covariate cohorts")
start <- Sys.time()

# this is run for all tars
Expand Down Expand Up @@ -467,10 +478,10 @@ computeCaseAggregateCovariateAnalyses <- function(
}
completionTime <- Sys.time() - start

message(paste0("Computing case cohorts took ", round(completionTime, digits = 1), " ", units(completionTime)))
message(paste0("Case Aggregates: Computing case cohorts took ", round(completionTime, digits = 1), " ", units(completionTime)))

## get counts
message("Extracting case cohort counts")
message("Case Aggregates: Extracting case cohort counts")
sql <- "select
cohort_definition_id,
count(*) row_count,
Expand All @@ -482,44 +493,45 @@ computeCaseAggregateCovariateAnalyses <- function(
group by cohort_definition_id;"
sql <- SqlRender::translate(
sql = sql,
targetDialect = connectionDetails$dbms
targetDialect = connectionDetails$dbms,
tempEmulationSchema = tempEmulationSchema
)
counts <- DatabaseConnector::querySql(
connection = connection,
sql = sql,
snakeCaseToCamelCase = T,
)

message("Computing aggregate before case covariate results")
message("Case Aggregates: Computing aggregate before case covariate results")

result <- FeatureExtraction::getDbCovariateData(
connection = connection,
oracleTempSchema = tempEmulationSchema,
cdmDatabaseSchema = cdmDatabaseSchema,
cohortTable = "#cases",
cohortTableIsTemp = T,
cohortIds = -1,
covariateSettings = ParallelLogger::convertJsonToSettings(covariateSettings),
cdmVersion = cdmVersion,
aggregated = T,
minCharacterizationMean = minCharacterizationMean
minCharacterizationMean = minCharacterizationMean,
tempEmulationSchema = tempEmulationSchema
)

message("Computing aggregate during case covariate results")
message("Case Aggregates: Computing aggregate during case covariate results")

result2 <- tryCatch(
{
FeatureExtraction::getDbCovariateData(
connection = connection,
oracleTempSchema = tempEmulationSchema,
cdmDatabaseSchema = cdmDatabaseSchema,
cohortTable = "#case_series",
cohortTableIsTemp = T,
cohortIds = -1,
covariateSettings = ParallelLogger::convertJsonToSettings(caseCovariateSettings),
cdmVersion = cdmVersion,
aggregated = T,
minCharacterizationMean = minCharacterizationMean
minCharacterizationMean = minCharacterizationMean,
tempEmulationSchema = tempEmulationSchema
)
},
error = function(e) {
Expand All @@ -532,7 +544,7 @@ computeCaseAggregateCovariateAnalyses <- function(
}

# drop temp tables
message("Dropping temp tables")
message("Case Aggregates: Dropping temp tables")
sql <- SqlRender::loadRenderTranslateSql(
sqlFilename = "DropCaseCovariate.sql",
packageName = "Characterization",
Expand All @@ -546,7 +558,7 @@ computeCaseAggregateCovariateAnalyses <- function(
)

# export all results to csv files
message("Exporting results to csv")
message("Case Aggregates: Exporting results to csv")
exportAndromedaToCsv( # TODO combine export of result and result2
andromeda = result,
outputFolder = outputFolder,
Expand All @@ -567,6 +579,8 @@ computeCaseAggregateCovariateAnalyses <- function(
minCellCount = minCellCount
)

message("Case Aggregates: ending")

return(invisible(T))
}

Expand Down
36 changes: 24 additions & 12 deletions R/CustomCovariates.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#' to the list of concepts to exclude?
#' @param includedCovariateIds A list of covariate IDs that should be
#' restricted to.
#' @family {CovariateSetting}
#' @family CovariateSetting
#'
#' @return
#' An object of type \code{covariateSettings}, to be used in other functions.
Expand Down Expand Up @@ -122,8 +122,9 @@ createDuringCovariateSettings <- function(
#' @param cohortIds cohort id for the target cohort
#' @param covariateSettings settings for the covariate cohorts and time periods
#' @param minCharacterizationMean the minimum value for a covariate to be extracted
#' @template tempEmulationSchema
#' @param ... additional arguments from FeatureExtraction
#' @family {CovariateSetting}
#' @family CovariateSetting
#' @return
#' The the during covariates based on user settings
#'
Expand All @@ -139,6 +140,7 @@ getDbDuringCovariateData <- function(
cohortIds = c(-1),
covariateSettings,
minCharacterizationMean = 0,
tempEmulationSchema = getOption("sqlRenderTempEmulationSchema"),
...) {
writeLines("Constructing during cohort covariates")
if (!aggregated) {
Expand All @@ -160,7 +162,8 @@ getDbDuringCovariateData <- function(
);"
sql <- SqlRender::translate(
sql = sql,
targetDialect = DatabaseConnector::dbms(connection)
targetDialect = DatabaseConnector::dbms(connection),
tempEmulationSchema = tempEmulationSchema
)
DatabaseConnector::executeSql(connection, sql = sql)

Expand All @@ -176,7 +179,8 @@ getDbDuringCovariateData <- function(
);"
sql <- SqlRender::translate(
sql = sql,
targetDialect = DatabaseConnector::dbms(connection)
targetDialect = DatabaseConnector::dbms(connection),
tempEmulationSchema = tempEmulationSchema
)
DatabaseConnector::executeSql(connection, sql)

Expand All @@ -192,7 +196,8 @@ getDbDuringCovariateData <- function(
createTable = T,
tempTable = T,
data = data.frame(id = covariateSettings$includedCovariateIds),
camelCaseToSnakeCase = T
camelCaseToSnakeCase = T,
tempEmulationSchema = tempEmulationSchema
)
}

Expand All @@ -207,7 +212,8 @@ getDbDuringCovariateData <- function(
createTable = T,
tempTable = T,
data = data.frame(id = covariateSettings$includedCovariateConceptIds),
camelCaseToSnakeCase = T
camelCaseToSnakeCase = T,
tempEmulationSchema = tempEmulationSchema
)

if (covariateSettings$addDescendantsToInclude) {
Expand All @@ -216,7 +222,8 @@ getDbDuringCovariateData <- function(
packageName = "Characterization",
dbms = DatabaseConnector::dbms(connection),
table_name = includedConceptTable,
cdm_database_schema = cdmDatabaseSchema
cdm_database_schema = cdmDatabaseSchema,
tempEmulationSchema = tempEmulationSchema
)
}
}
Expand All @@ -232,7 +239,8 @@ getDbDuringCovariateData <- function(
createTable = T,
tempTable = T,
data = data.frame(id = covariateSettings$excludedCovariateConceptIds),
camelCaseToSnakeCase = T
camelCaseToSnakeCase = T,
tempEmulationSchema = tempEmulationSchema
)

if (covariateSettings$addDescendantsToInclude) {
Expand All @@ -241,7 +249,8 @@ getDbDuringCovariateData <- function(
packageName = "Characterization",
dbms = DatabaseConnector::dbms(connection),
table_name = excludedConceptTable,
cdm_database_schema = cdmDatabaseSchema
cdm_database_schema = cdmDatabaseSchema,
tempEmulationSchema = tempEmulationSchema
)
}
}
Expand Down Expand Up @@ -286,7 +295,8 @@ getDbDuringCovariateData <- function(
included_cov_table = includedCovTable,
included_concept_table = includedConceptTable,
excluded_concept_table = excludedConceptTable,
covariate_table = paste0("#cov_", i)
covariate_table = paste0("#cov_", i),
tempEmulationSchema = tempEmulationSchema
)
message(paste0("Executing during sql code for ", getDomainSettings$analysisName[domainSettingsIndex]))
start <- Sys.time()
Expand Down Expand Up @@ -321,7 +331,8 @@ getDbDuringCovariateData <- function(
)
sql <- SqlRender::translate(
sql = sql,
targetDialect = DatabaseConnector::dbms(connection)
targetDialect = DatabaseConnector::dbms(connection),
tempEmulationSchema = tempEmulationSchema
)

DatabaseConnector::querySqlToAndromeda(
Expand Down Expand Up @@ -393,7 +404,8 @@ getDbDuringCovariateData <- function(
sql <- SqlRender::translate(
sql = sql,
targetDialect = attr(connection, "dbms"),
oracleTempSchema = oracleTempSchema
oracleTempSchema = oracleTempSchema,
tempEmulationSchema = tempEmulationSchema
)
DatabaseConnector::executeSql(connection, sql, progressBar = FALSE, reportOverallTime = FALSE)
}
Expand Down
Loading

0 comments on commit b8b5976

Please sign in to comment.