Skip to content

Commit

Permalink
Update AggregateCovariates.R
Browse files Browse the repository at this point in the history
  • Loading branch information
jreps committed Jan 10, 2025
1 parent 379ec32 commit b42808a
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 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 @@ -233,7 +236,7 @@ computeTargetAggregateCovariateAnalyses <- function(
tempEmulationSchema = tempEmulationSchema
)

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

sql <- SqlRender::loadRenderTranslateSql(
Expand All @@ -256,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 @@ -280,11 +283,10 @@ computeTargetAggregateCovariateAnalyses <- function(
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,
Expand All @@ -297,7 +299,7 @@ computeTargetAggregateCovariateAnalyses <- function(
)

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

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

message("Target Aggregate: ending")

return(invisible(T))
}

Expand All @@ -340,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 @@ -417,7 +424,7 @@ computeCaseAggregateCovariateAnalyses <- function(
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 @@ -471,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 @@ -495,11 +502,10 @@ computeCaseAggregateCovariateAnalyses <- function(
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,
Expand All @@ -511,13 +517,12 @@ computeCaseAggregateCovariateAnalyses <- function(
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,
Expand All @@ -539,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 @@ -553,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 @@ -574,6 +579,8 @@ computeCaseAggregateCovariateAnalyses <- function(
minCellCount = minCellCount
)

message("Case Aggregates: ending")

return(invisible(T))
}

Expand Down

0 comments on commit b42808a

Please sign in to comment.