From 98911c6e19a8b013fd433d28acd885b9e83b3ae5 Mon Sep 17 00:00:00 2001 From: Vitaly Koulakov Date: Wed, 23 Jan 2019 00:14:48 +0300 Subject: [PATCH] Fix inclusion impact analysis where censor period is being used (#66) * Added cohort_censor_stats table. * censor stats only calculated when there is a censor window specified. --- .../cohortdefinition/CohortExpressionQueryBuilder.java | 10 ++++++++-- .../cohortdefinition/sql/cohortCensoredStats.sql | 7 +++++++ .../resources/cohortdefinition/sql/generateCohort.sql | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/resources/cohortdefinition/sql/cohortCensoredStats.sql diff --git a/src/main/java/org/ohdsi/circe/cohortdefinition/CohortExpressionQueryBuilder.java b/src/main/java/org/ohdsi/circe/cohortdefinition/CohortExpressionQueryBuilder.java index bb0b5f43..8134f491 100644 --- a/src/main/java/org/ohdsi/circe/cohortdefinition/CohortExpressionQueryBuilder.java +++ b/src/main/java/org/ohdsi/circe/cohortdefinition/CohortExpressionQueryBuilder.java @@ -68,6 +68,7 @@ public class CohortExpressionQueryBuilder implements IGetCriteriaSqlDispatcher, private final static String CODESET_JOIN_TEMPLATE = "JOIN #Codesets codesets on (@codesetClauses)"; private final static String COHORT_INCLUSION_ANALYSIS_TEMPALTE = ResourceHelper.GetResourceAsString("/resources/cohortdefinition/sql/cohortInclusionAnalysis.sql"); + private final static String COHORT_CENSORED_STATS_TEMPLATE = ResourceHelper.GetResourceAsString("/resources/cohortdefinition/sql/cohortCensoredStats.sql"); // Strategy templates private final static String DATE_OFFSET_STRATEGY_TEMPLATE = ResourceHelper.GetResourceAsString("/resources/cohortdefinition/sql/dateOffsetStrategy.sql"); @@ -365,7 +366,7 @@ private String getInclusionAnalysisQuery(String eventTable, int modeId) { resultSql = StringUtils.replace(resultSql, "@eventTable", eventTable); return resultSql; } - + public String buildExpressionQuery(CohortExpression expression, BuildExpressionQueryOptions options) { String resultSql = COHORT_QUERY_TEMPLATE; @@ -463,7 +464,12 @@ public String buildExpressionQuery(CohortExpression expression, BuildExpressionQ resultSql = StringUtils.replace(resultSql, "@inclusionImpactAnalysisByEventQuery", getInclusionAnalysisQuery("#qualified_events", 0)); resultSql = StringUtils.replace(resultSql, "@inclusionImpactAnalysisByPersonQuery", getInclusionAnalysisQuery("#best_events", 1)); - + + resultSql = StringUtils.replace(resultSql, "@cohortCensoredStatsQuery", + (expression.censorWindow != null && (!StringUtils.isEmpty(expression.censorWindow.startDate) || !StringUtils.isEmpty(expression.censorWindow.endDate))) + ? COHORT_CENSORED_STATS_TEMPLATE + : ""); + if (options != null) { // replease query parameters with tokens diff --git a/src/main/resources/resources/cohortdefinition/sql/cohortCensoredStats.sql b/src/main/resources/resources/cohortdefinition/sql/cohortCensoredStats.sql new file mode 100644 index 00000000..5826970e --- /dev/null +++ b/src/main/resources/resources/cohortdefinition/sql/cohortCensoredStats.sql @@ -0,0 +1,7 @@ +-- calculate censored +delete from @results_database_schema.cohort_censor_stats where cohort_definition_id = @target_cohort_id; +insert into @results_database_schema.cohort_censor_stats (cohort_definition_id, lost_count) + select @target_cohort_id as cohort_definition_id, coalesce(FCC.total_people - TC.total, 0) as lost_count + FROM + (select count_big(distinct person_id) as total_people from #final_cohort) FCC, + (select count_big(distinct subject_id) as total from @target_database_schema.@target_cohort_table t where t.cohort_definition_id = @target_cohort_id) TC; \ No newline at end of file diff --git a/src/main/resources/resources/cohortdefinition/sql/generateCohort.sql b/src/main/resources/resources/cohortdefinition/sql/generateCohort.sql index 1660b4bf..9ad9f9fd 100644 --- a/src/main/resources/resources/cohortdefinition/sql/generateCohort.sql +++ b/src/main/resources/resources/cohortdefinition/sql/generateCohort.sql @@ -151,6 +151,10 @@ WHERE ranked.rank_value = 1 @inclusionImpactAnalysisByPersonQuery -- END: Inclusion Impact Analysis - person +-- BEGIN: Censored Stats +@cohortCensoredStatsQuery +-- END: Censored Stats + TRUNCATE TABLE #best_events; DROP TABLE #best_events;