Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
- fixing tests
- updates for Characterization v1.1.0
- updating example app
  • Loading branch information
jreps committed Jul 31, 2024
1 parent a7b6685 commit 9888845
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 100 deletions.
18 changes: 9 additions & 9 deletions R/characterization-caseSeries.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ characterizationGetCaseSeriesOptions <- function(
and s.database_id = cd.database_id
and cd.target_cohort_id = @target_id
and cd.outcome_cohort_id = @outcome_id
and cd.cohort_type = 'TnO'
and cd.cohort_type = 'Cases'
inner join
@schema.@cg_table_prefixcohort_definition ct1
Expand Down Expand Up @@ -256,9 +256,9 @@ characterizationGetCaseSeriesData <- function(

sql <- "SELECT
case
when cov.cohort_type = 'TnO' then 'Before'
when cov.cohort_type = 'TnObetween' then 'During'
when cov.cohort_type = 'OnT' then 'After'
when cov.cohort_type = 'CasesBefore' then 'Before'
when cov.cohort_type = 'CasesBetween' then 'During'
when cov.cohort_type = 'CaseAfter' then 'After'
end as type,
cr.covariate_name,
s.min_prior_observation, s.outcome_washout_days,
Expand All @@ -277,7 +277,7 @@ characterizationGetCaseSeriesData <- function(
where cov.target_cohort_id = @target_id
and cov.outcome_cohort_id = @outcome_id
and cov.cohort_type in ('TnObetween','OnT','TnO')
and cov.cohort_type in ('CasesBetween','CasesAfter','CasesBefore')
--and cov.setting_id = @setting_id
and s.risk_window_start = @risk_window_start
and s.risk_window_end = @risk_window_end
Expand Down Expand Up @@ -309,9 +309,9 @@ characterizationGetCaseSeriesData <- function(

sql <- "SELECT
case
when cov.cohort_type = 'TnO' then 'Before'
when cov.cohort_type = 'TnObetween' then 'During'
when cov.cohort_type = 'OnT' then 'After'
when cov.cohort_type = 'CasesBefore' then 'Before'
when cov.cohort_type = 'CasesBetween' then 'During'
when cov.cohort_type = 'CasesAfter' then 'After'
end as type,
cr.covariate_name,
s.min_prior_observation, s.outcome_washout_days,
Expand All @@ -333,7 +333,7 @@ characterizationGetCaseSeriesData <- function(
where cov.target_cohort_id = @target_id
and cov.outcome_cohort_id = @outcome_id
and cov.cohort_type in ('TnObetween','OnT','TnO')
and cov.cohort_type in ('CasesBetween','CasesAfter','CasesBefore')
and s.risk_window_start = @risk_window_start
and s.risk_window_end = @risk_window_end
and s.start_anchor = '@start_anchor'
Expand Down
2 changes: 1 addition & 1 deletion R/characterization-cohorts.R
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ characterizatonGetCohortComparisonDataContinuous <- function(
res <- res %>% dplyr::select(-"cohortDefinitionId", -"databaseId", -"type",
-"settingId", -"targetCohortId", -"outcomeCohortId",
-"cohortType") %>%
dplyr::relocate(.data$databaseName, .after = .data$covariateName)
dplyr::relocate("databaseName", .after = "covariateName")
}

shiny::incProgress(4/4, detail = paste("Done"))
Expand Down
24 changes: 12 additions & 12 deletions R/characterization-incidence.R
Original file line number Diff line number Diff line change
Expand Up @@ -1215,9 +1215,9 @@ characterizationIncidenceServer <- function(
outcomeLabel = paste(.data$outcomeIdShort, " = ", .data$outcomeName),
ageGroupName = factor(.data$ageGroupName, levels = custom_age_sort(.data$ageGroupName), ordered = TRUE)
) %>%
dplyr::rename("Target" = "targetIdShort",
"Outcome" = "outcomeIdShort",
"Age" = "ageGroupName")
dplyr::rename(Target = "targetIdShort",
Outcome = "outcomeIdShort",
Age = "ageGroupName")

# Get unique target and outcome labels
unique_target_labels <- strwrap(unique(plotData$targetLabel), width = 300)
Expand Down Expand Up @@ -1362,9 +1362,9 @@ renderIrPlotStandardAgeSex <- shiny::reactive(
outcomeLabel = paste(outcomeIdShort, " = ", outcomeName),
ageGroupName = factor(ageGroupName, levels = custom_age_sort(ageGroupName), ordered = TRUE)
) %>%
dplyr::rename("Target" = targetIdShort,
"Outcome" = outcomeIdShort,
"Age" = ageGroupName)
dplyr::rename(Target = "targetIdShort",
Outcome = "outcomeIdShort",
Age = "ageGroupName")

# plotHeightStandardAgeSex <- shiny::reactive({
# paste(sum(length(unique(plotData$targetLabel)), length(unique(plotData$Age)), -3)*100, "px", sep="")
Expand Down Expand Up @@ -1512,9 +1512,9 @@ renderIrPlotStandardYear <- shiny::reactive(
outcomeLabel = paste(outcomeIdShort, " = ", outcomeName),
ageGroupName = factor(ageGroupName, levels = custom_age_sort(ageGroupName), ordered = TRUE)
) %>%
dplyr::rename("Target" = targetIdShort,
"Outcome" = outcomeIdShort,
"Age" = ageGroupName)
dplyr::rename(Target = "targetIdShort",
Outcome = "outcomeIdShort",
Age = "ageGroupName")

#get unique shorthand cohort name
unique_target <- unique(plotData$Target)
Expand Down Expand Up @@ -1668,9 +1668,9 @@ renderIrPlotStandardAggregate <- shiny::reactive(
dplyr::mutate(targetLabel = paste(targetIdShort, " = ", targetName),
outcomeLabel = paste(outcomeIdShort, " = ", outcomeName)
) %>%
dplyr::rename("Target" = targetIdShort,
"Outcome" = outcomeIdShort,
"Age" = ageGroupName)
dplyr::rename(Target = "targetIdShort",
Outcome = "outcomeIdShort",
Age = "ageGroupName")

# Get unique target and outcome labels
unique_target_labels <- strwrap(unique(plotData$targetLabel), width = 300)
Expand Down
2 changes: 1 addition & 1 deletion R/characterization-main.R
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ select distinct
target_cohort_id,
outcome_cohort_id
from @schema.@c_table_prefixcohort_details
where cohort_type = 'TnO'
where cohort_type = 'Cases'
{@include_incidence} ? {
union
Expand Down
21 changes: 14 additions & 7 deletions R/characterization-riskFactors.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ characterizationGetRiskFactorData <- function(
where
cov.target_cohort_id = @target_id
and cov.outcome_cohort_id in (0,@outcome_id)
and cov.cohort_type in ('Target','TnO', 'TnOprior')
and cov.cohort_type in ('Target','Cases', 'Exclude')
and cov.database_id = '@database_id'
and cov.setting_id in (@setting_ids)
and cr.analysis_id not in (109, 110, 217, 218, 305, 417, 418, 505, 605, 713, 805, 926, 927)
Expand Down Expand Up @@ -264,7 +264,7 @@ characterizationGetRiskFactorData <- function(
where cov.target_cohort_id = @target_id
and cov.outcome_cohort_id in (0,@outcome_id)
and cov.cohort_type in ('Target','TnO', 'TnOprior')
and cov.cohort_type in ('Target','Cases', 'Exclude')
and cov.database_id = '@database_id'
and cov.setting_id in (@setting_ids)
and cr.analysis_id not in (109, 110, 217, 218, 305, 417, 418, 505, 605, 713, 805, 926, 927)
Expand Down Expand Up @@ -354,7 +354,7 @@ riskFactorTable <- function(

caseData <- data %>%
dplyr::filter(
.data$cohortType == 'TnO' &
.data$cohortType == 'Cases' &
.data$outcomeWashoutDays == !!outcomeWashoutDay
) %>%
dplyr::select(-"cohortType")
Expand All @@ -367,7 +367,7 @@ riskFactorTable <- function(

excludeData <- data %>%
dplyr::filter(
.data$cohortType == 'TnOprior' &
.data$cohortType == 'Exclude' &
.data$outcomeWashoutDays == !!outcomeWashoutDay
) %>%
dplyr::select(-"cohortType")
Expand Down Expand Up @@ -417,6 +417,13 @@ riskFactorTable <- function(
})
}

} else{
# if no excludes we need to add N for the target
allData <- allData %>%
dplyr::inner_join( # add N per washout/min obs
allcounts,
by = c('minPriorObservation')
)
}

if(nrow(caseData) > 0){
Expand Down Expand Up @@ -487,8 +494,8 @@ riskFactorTable <- function(
dplyr::mutate(
outcomeWashoutDays = !!outcomeWashoutDay
) %>%
dplyr::relocate(.data$outcomeWashoutDays,
.after = .data$minPriorObservation)
dplyr::relocate("outcomeWashoutDays",
.after = "minPriorObservation")

completeData <- rbind(allData, completeData)

Expand Down Expand Up @@ -522,7 +529,7 @@ riskFactorContinuousTable <- function(
data <- unique(data)

caseData <- data %>%
dplyr::filter(.data$cohortType == 'TnO') %>%
dplyr::filter(.data$cohortType == 'Cases') %>%
dplyr::select(-"cohortType")

allData <- data %>%
Expand Down
4 changes: 2 additions & 2 deletions R/cohort-diagnostics-characterization.R
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ prepareTable1 <- function(covariates,
"characteristic",
"valueCount"
) %>%
dplyr::rename("count" = "valueCount") %>%
dplyr::rename(count = "valueCount") %>%
dplyr::inner_join(cohort %>%
dplyr::select(
"cohortId",
Expand Down Expand Up @@ -739,7 +739,7 @@ cohortDiagCharacterizationModule <- function(
"covariateName",
"mean"
) %>%
dplyr::rename("sumValue" = "mean")
dplyr::rename(sumValue = "mean")


table <- data %>%
Expand Down
4 changes: 2 additions & 2 deletions R/estimation-cohort-method-covariateBalance.R
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ plotCohortMethodCovariateBalanceSummary <- function(balanceSummary,
upper = .data$upper,
ymax = .data$ymax,
group = .data$databaseId)) +
ggplot2::geom_errorbar(ggplot2::aes(ymin = .data$ymin, ymax = .data$ymin), size = 1) +
ggplot2::geom_errorbar(ggplot2::aes(ymin = .data$ymax, ymax = .data$ymax), size = 1) +
ggplot2::geom_errorbar(ggplot2::aes(ymin = .data$ymin, ymax = .data$ymin), linewidth = 1) +
ggplot2::geom_errorbar(ggplot2::aes(ymin = .data$ymax, ymax = .data$ymax), linewidth = 1) +
ggplot2::geom_boxplot(stat = "identity", fill = grDevices::rgb(0, 0, 0.8, alpha = 0.25), size = 1) +
ggplot2::geom_hline(yintercept = 0) +
ggplot2::scale_x_continuous(limits = c(0.5, max(vizData$x) + 1.75)) +
Expand Down
2 changes: 1 addition & 1 deletion R/estimation-cohort-method-diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ estimationGetCmDiagnostics <- function(

# add summaryValue after outcome
result <- result %>%
dplyr::relocate(.data$summaryValue, .after = .data$outcome)
dplyr::relocate("summaryValue", .after = "outcome")

return(
result
Expand Down
10 changes: 5 additions & 5 deletions R/estimation-cohort-method-full-result.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ estimationCmFullResultServer <- function(
"cdmSourceAbbreviation"
) %>%
dplyr::rename(
'Target' = .data$target,
'Comparator' = .data$comparator,
'Outcome' = .data$outcome,
'Analysis' = .data$description,
'Database' = .data$cdmSourceAbbreviation
Target = "target",
Comparator = "comparator",
Outcome = "outcome",
Analysis = "description",
Database = "cdmSourceAbbreviation"
)
})

Expand Down
8 changes: 4 additions & 4 deletions R/estimation-sccs-results-full.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ estimationSccsFullResultServer <- function(
"databaseName"
) %>%
dplyr::rename(
'Indication' = .data$indication,
'Outcome' = .data$outcome,
'Analysis' = .data$description,
'Database' = .data$databaseName
Indication = "indication",
Outcome = "outcome",
Analysis = "description",
Database = "databaseName"
)
})

Expand Down
17 changes: 7 additions & 10 deletions R/helpers-sccsPlots.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,21 @@ plotTimeToEventSccs <- function(timeToEvent) {

drawAttritionDiagram <- function(attrition) {

formatNumber <- function(x) {
return(formatC(x, big.mark = ","))
}


addStep <- function(data, attrition, row) {
data$leftBoxText[length(data$leftBoxText) + 1] <- paste(attrition$description[row],
"\n",
"Cases: ",
formatNumber(attrition$outcomeSubjects[row]),
format(attrition$outcomeSubjects[row], scientific = FALSE),
"\n",
"Outcomes: ",
formatNumber(attrition$outcomeEvents[row]),
format(attrition$outcomeEvents[row], scientific = FALSE),
sep = "")
data$rightBoxText[length(data$rightBoxText) + 1] <- paste("Cases: ",
formatNumber(data$currentCases - attrition$outcomeSubjects[row]),
format(data$currentCases - attrition$outcomeSubjects[row], scientific = FALSE),
"\n",
"Outcomes: ",
formatNumber(data$currentOutcomes - attrition$outcomeEvents[row]),
format(data$currentOutcomes - attrition$outcomeEvents[row], scientific = FALSE),
sep = "")
data$currentCases <- attrition$outcomeSubjects[row]
data$currentOutcomes <- attrition$outcomeEvents[row]
Expand All @@ -201,10 +198,10 @@ drawAttritionDiagram <- function(attrition) {

data <- list(leftBoxText = c(paste("All outcomes occurrences:\n",
"Cases: ",
formatNumber(attrition$outcomeSubjects[1]),
format(attrition$outcomeSubjects[1], scientific = FALSE),
"\n",
"Outcomes: ",
formatNumber(attrition$outcomeEvents[1]),
format(attrition$outcomeEvents[1], scientific = FALSE),
sep = "")),
rightBoxText = c(""),
currentCases = attrition$outcomeSubjects[1],
Expand Down
6 changes: 3 additions & 3 deletions R/report-main.R
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ getTandOs <- function(
by.y = 'cohortDefinitionId'
) %>%
dplyr::rename(
targetName = 'cohortName'
targetName = "cohortName"
)

res <- merge(
Expand All @@ -942,7 +942,7 @@ getTandOs <- function(
by.y = 'cohortDefinitionId'
) %>%
dplyr::rename(
outcomeName = 'cohortName'
outcomeName = "cohortName"
) %>%
dplyr::arrange(
.data$targetName,
Expand Down Expand Up @@ -1021,7 +1021,7 @@ getTandOs <- function(
by.x = 'comparatorId',
by.y = 'cohortDefinitionId'
) %>%
dplyr::rename(comparatorName = 'cohortName')
dplyr::rename(comparatorName = "cohortName")

cs <- lapply(unique(comps$targetId), function(tid){
data.frame(
Expand Down
25 changes: 20 additions & 5 deletions extras/codeToCreateCharacterizationDatabase.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,44 @@ Characterization::runCharacterizationAnalyses(
outcomeDatabaseSchema = "main",
outcomeTable = "cohort",
cdmDatabaseSchema = "main",
characterizationSettings = charSet,
saveDirectory = file.path(testDir,'charDatabase'),
characterizationSettings = charSet, incremental = F,
executionPath = file.path(testDir,'charDatabase','execution'),
outputDirectory = file.path(testDir,'charDatabase', 'results'),
databaseId = 'eunomia',
tablePrefix = 'c_'
csvFilePrefix = 'c_'
)

#serverDesc <- file.path(getwd(),
# 'inst/extdata/results.sqlite')
serverDesc <- "tests/resources/charDatabase/databaseFile.sqlite"
connectionDetailsDesc <- DatabaseConnector::createConnectionDetails(
dbms = 'sqlite',
server = serverDesc
)

if(F){
for(table in c('c_time_to_event', 'c_dechallenge_rechallenge',
'c_analysis_ref', 'c_covariate_ref', 'c_covariates',
'c_covariates_continuous', 'c_settings',
'c_cohort_details', 'c_cohort_counts')){
sql <- "UPDATE main.@tbl SET database_id = '85642205.0';"
sql <- SqlRender::render(sql, tbl = table)
DatabaseConnector::executeSql(con, sql)
}
}

Characterization::createCharacterizationTables(
connectionDetails = connectionDetailsDesc,
resultSchema = 'main',
createTables = T
createTables = T,
deleteExistingTables = T
)

Characterization::insertResultsToDatabase(
connectionDetails = connectionDetailsDesc,
schema = 'main',
resultsFolder = file.path(testDir,'charDatabase', 'results'),
tablePrefix = 'c_'
csvTablePrefix = 'c_'
)

if(F){
Expand Down
Loading

0 comments on commit 9888845

Please sign in to comment.