Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for datetime calculations in cohort definitions #200

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1fa1d69
Support for datetime calculations in cohort definitions
jcnamendiOdysseus Dec 18, 2023
b9b5820
update
jcnamendiOdysseus Dec 29, 2023
6fd8ecd
update/412023
jcnamendiOdysseus Jan 4, 2024
07a7160
add sql test
jcnamendiOdysseus Jan 8, 2024
eaf44d5
fix
jcnamendiOdysseus Jan 9, 2024
6dc3918
Merge remote-tracking branch 'origin/is/2886' into issues-2886
jcnamendiOdysseus Jan 9, 2024
603c800
update test
jcnamendiOdysseus Jan 9, 2024
fe4f7e5
fix indent on editor
jcnamendiOdysseus Jan 9, 2024
75a2906
Merge pull request #6 from OHDSI/master
alex-odysseus Jan 15, 2024
3210fae
Merge remote-tracking branch 'remotes/origin/master' into issues-2886
alex-odysseus Jan 15, 2024
e7f90a6
remove jar
jcnamendiOdysseus Jan 15, 2024
0036dc0
Merge remote-tracking branch 'origin/issues-2886' into issues-2886
jcnamendiOdysseus Jan 15, 2024
4d0cb33
Support for datetime calculations in cohort definitions
jcnamendiOdysseus Dec 18, 2023
55b2eef
update
jcnamendiOdysseus Dec 29, 2023
806c85b
update/412023
jcnamendiOdysseus Jan 4, 2024
8a4eb23
add sql test
jcnamendiOdysseus Jan 8, 2024
f819d5e
fix
jcnamendiOdysseus Jan 9, 2024
5e03963
update test
jcnamendiOdysseus Jan 9, 2024
de9527c
fix indent on editor
jcnamendiOdysseus Jan 9, 2024
ca6bbfd
remove jar
jcnamendiOdysseus Jan 15, 2024
cebb8c5
Revert "fix"
jcnamendiOdysseus Jan 16, 2024
2ada34d
Merge remote-tracking branch 'origin/issues-2886' into issues-2886
jcnamendiOdysseus Jan 17, 2024
6fb7fd2
add tests
jcnamendiOdysseus Jan 17, 2024
da13f6e
Adding IntervalUnit to Criteria to specify datetime table columns whi…
alex-odysseus Jan 23, 2024
690b273
Replace wildcard import with specific imports
jcnamendiOdysseus Jan 23, 2024
2f97201
Enhance time unit handling in compareTo() method
jcnamendiOdysseus Jan 25, 2024
4209ca0
Introduce time interval testing functionality and enhance the impleme…
jcnamendiOdysseus Jan 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions src/main/java/org/ohdsi/circe/check/checkers/Comparisons.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,48 @@ public static Predicate<Concept> compare(Concept source) {
.build();
}

public static int compareTo(ObservationFilter filter, Window window) {

int range1 = filter.postDays + filter.priorDays;
int range2Start = 0, range2End = 0;
if (Objects.nonNull(window.start) && Objects.nonNull(window.start.days)) {
range2Start = window.start.coeff * window.start.days;
}
if (Objects.nonNull(window.end) && Objects.nonNull(window.end.days)) {
range2End = window.end.coeff * window.end.days;
}
return range1 - (range2End - range2Start);
/**
* If timeUnit is equal to values such as Hours, Minutes, Seconds, the values will be converted to seconds
* Otherwise it will return to the previous logic.
* @param filter
* @param window
* @return
*/
public static int compareTo(ObservationFilter filter, Window window) {
int range1, range2Start = 0, range2End = 0;
if (Objects.nonNull(window.start) && Objects.nonNull(window.start.timeUnit) && !IntervalUnit.DAY.getName().equals(window.start.timeUnit)) {
range1 = (filter.postDays + filter.priorDays) * 24 * 60 * 60;
range2Start = getTimeInSeconds(window.start);
range2End = getTimeInSeconds(window.end);
} else {
range1 = filter.postDays + filter.priorDays;
if (Objects.nonNull(window.start) && Objects.nonNull(window.start.days)) {
range2Start = window.start.coeff * window.start.days;
}
if (Objects.nonNull(window.end) && Objects.nonNull(window.end.days)) {
range2End = window.end.coeff * window.end.days;
}
}
return range1 - (range2End - range2Start);
}

/**
* @return Convert values to seconds.
*/
private static int getTimeInSeconds(Window.Endpoint endpoint) {
if (Objects.isNull(endpoint)) {
return 0;
}
int convertRate;
if (IntervalUnit.HOUR.getName().equals(endpoint.timeUnit)) {
convertRate = 60 * 60;
} else if (IntervalUnit.MINUTE.getName().equals(endpoint.timeUnit)) {
convertRate = 60;
} else convertRate = 1;
return Objects.nonNull(endpoint.timeUnitValue)
? endpoint.coeff * endpoint.timeUnitValue * convertRate
: 0;
}

public static boolean compare(Criteria c1, Criteria c2) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class ExitCriteriaDaysOffsetCheck extends BaseCheck {

private static final String DAYS_OFFSET_WARNING = "Cohort Exit criteria: Days offset from start date should be greater than 0";
private static final String DAYS_OFFSET_WARNING = "Cohort Exit criteria: %ss offset from start date should be greater than 0";

@Override
protected WarningSeverity defineSeverity() {
Expand All @@ -40,9 +40,9 @@ protected WarningSeverity defineSeverity() {
protected void check(CohortExpression expression, WarningReporter reporter) {

match(expression.endStrategy)
.isA(DateOffsetStrategy.class)
.then(s -> match((DateOffsetStrategy)s)
.when(dateOffsetStrategy -> Objects.equals(StartDate, dateOffsetStrategy.dateField) && 0 == dateOffsetStrategy.offset)
.then(() -> reporter.add(DAYS_OFFSET_WARNING)));
.isA(DateOffsetStrategy.class)
.then(s -> match((DateOffsetStrategy)s)
.when(dateOffsetStrategy -> Objects.equals(StartDate, dateOffsetStrategy.dateField) && 0 == dateOffsetStrategy.offsetUnitValue)
.then(dateOffsetStrategy -> reporter.add(String.format(DAYS_OFFSET_WARNING, dateOffsetStrategy.offsetUnit))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ protected void checkCriteria(CorelatedCriteria criteria, String groupName, Warni
String name = CriteriaNameHelper.getCriteriaName(criteria.criteria) + " at " + groupName;
Execution addWarning = () -> reporter.add(WARNING, name);
match(criteria)
.when(c -> c.startWindow != null && ((c.startWindow.start != null
&& c.startWindow.start.days != null) || (c.startWindow.end != null
&& c.startWindow.end.days != null)))
.when(c -> c.startWindow != null && ((c.startWindow.start != null && c.startWindow.start.days != null)
|| (c.startWindow.end != null && c.startWindow.end.days != null))
|| c.startWindow != null && (( c.startWindow.start != null && c.startWindow.start.timeUnitValue != null)
|| (c.startWindow.end != null) && c.startWindow.end.timeUnitValue != null))
.then(cc -> match(cc.criteria)
.isA(ConditionEra.class)
.then(c -> match((ConditionEra)c)
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/org/ohdsi/circe/check/checkers/RangeCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,23 @@ protected void checkInclusionRules(final CohortExpression expression, WarningRep
}

private void checkWindow(Window window, WarningReporter reporter, String name) {
if (Objects.isNull(window)) {
return;
}
checkAndReportIfNegative(window.start, reporter, name, "start");
checkAndReportIfNegative(window.end, reporter, name, "end");

if (Objects.nonNull(window)) {
if (Objects.nonNull(window.start) && Objects.nonNull(window.start.days) && window.start.days < 0) {
reporter.add(NEGATIVE_VALUE_ERROR, name, window.start.days, "start");
}
if (Objects.nonNull(window.end) && Objects.nonNull(window.end.days) && window.end.days < 0) {
reporter.add(NEGATIVE_VALUE_ERROR, name, window.end.days, "end");
}
}

private void checkAndReportIfNegative(Window.Endpoint windowDetails, WarningReporter reporter, String name, String type) {
if (Objects.nonNull(windowDetails) && Objects.nonNull(windowDetails.days) && windowDetails.days < 0) {
reporter.add(NEGATIVE_VALUE_ERROR, name, windowDetails.days, type);
} else if (Objects.nonNull(windowDetails) && Objects.nonNull(windowDetails.timeUnitValue) && windowDetails.timeUnitValue < 0) {
reporter.add(NEGATIVE_VALUE_ERROR, name, windowDetails.timeUnitValue, type);
}
}


private void checkObservationFilter(ObservationFilter filter, WarningReporter reporter, String name) {

if (Objects.nonNull(filter)) {
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/org/ohdsi/circe/check/checkers/TimePatternCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.ohdsi.circe.check.WarningSeverity;
Expand Down Expand Up @@ -83,16 +84,28 @@ private String formatTimeWindow(TimeWindowInfo ti) {
}

private String formatDays(Window.Endpoint endpoint) {
return Objects.nonNull(endpoint.days) ? String.valueOf(endpoint.days) : "all";
return Objects.nonNull(endpoint.days) ? String.valueOf(endpoint.days) :
Objects.nonNull(endpoint.timeUnitValue) ? String.valueOf(endpoint.timeUnitValue) : "all";

}

private String formatCoeff(Window.Endpoint endpoint) {
return endpoint.coeff < 0 ? "before " : "after ";
}


private Integer startDays(Window window) {
return Objects.nonNull(window) && Objects.nonNull(window.start) ?
(Objects.nonNull(window.start.days) ? window.start.days : 0) * window.start.coeff : 0;
return Optional.ofNullable(window)
.map(w -> w.start)
.map(start -> {
int coefficient = Optional.ofNullable(start.coeff).orElse(0);
return Optional.ofNullable(start.days)
.map(days -> days * coefficient)
.orElseGet(() -> Optional.ofNullable(start.timeUnitValue)
.map(timeUnitValue -> timeUnitValue * coefficient)
.orElse(0));
})
.orElse(0);
}

class TimeWindowInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,13 @@ public String buildExpressionQuery(CohortExpression expression, BuildExpressionQ

resultSql = StringUtils.replace(resultSql, "@cohort_end_unions", StringUtils.join(endDateSelects, "\nUNION ALL\n"));

resultSql = StringUtils.replace(resultSql, "@eraconstructorpad", Integer.toString(expression.collapseSettings.eraPad));

if (!StringUtils.isEmpty(Integer.toString(expression.collapseSettings.eraPad)) && (expression.collapseSettings.eraPadUnit == null || IntervalUnit.DAY.getName().equals(expression.collapseSettings.eraPadUnit))) {
resultSql = StringUtils.replace(resultSql, "@eraconstructorpad", Integer.toString(expression.collapseSettings.eraPad));
resultSql = StringUtils.replace(resultSql, "@era_pad_unit", expression.collapseSettings.eraPadUnit);
} else {
resultSql = StringUtils.replace(resultSql, "@eraconstructorpad", Integer.toString(expression.collapseSettings.eraPadUnitValue));
resultSql = StringUtils.replace(resultSql, "@era_pad_unit", expression.collapseSettings.eraPadUnit);
}
resultSql = StringUtils.replace(resultSql, "@inclusionRuleTable", getInclusionRuleTableSql(expression));
resultSql = StringUtils.replace(resultSql, "@inclusionImpactAnalysisByEventQuery", getInclusionAnalysisQuery("#qualified_events", 0));
resultSql = StringUtils.replace(resultSql, "@inclusionImpactAnalysisByPersonQuery", getInclusionAnalysisQuery("#best_events", 1));
Expand Down Expand Up @@ -546,8 +551,10 @@ public String getWindowedCriteriaQuery(String sqlTemplate, WindowedCriteria crit
Window startWindow = criteria.startWindow;
String startIndexDateExpression = (startWindow.useIndexEnd != null && startWindow.useIndexEnd) ? "P.END_DATE" : "P.START_DATE";
String startEventDateExpression = (startWindow.useEventEnd != null && startWindow.useEventEnd) ? "A.END_DATE" : "A.START_DATE";
if (startWindow.start.days != null) {
if (startWindow.start.days != null && (startWindow.start.timeUnit == null || startWindow.start.timeUnit.equals(IntervalUnit.DAY.getName()))) {
startExpression = String.format("DATEADD(day,%d,%s)", startWindow.start.coeff * startWindow.start.days, startIndexDateExpression);
} else if (startWindow.start.timeUnitValue != null) {
startExpression = String.format("DATEADD(%s,%d,%s)", startWindow.start.timeUnit, startWindow.start.coeff * startWindow.start.timeUnitValue, startIndexDateExpression);
} else {
startExpression = checkObservationPeriod ? (startWindow.start.coeff == -1 ? "P.OP_START_DATE" : "P.OP_END_DATE") : null;
}
Expand All @@ -556,9 +563,12 @@ public String getWindowedCriteriaQuery(String sqlTemplate, WindowedCriteria crit
clauses.add(String.format("%s >= %s", startEventDateExpression, startExpression));
}

if (startWindow.end.days != null) {
if (startWindow.end.days != null && (startWindow.end.timeUnit == null || IntervalUnit.DAY.getName().equals(startWindow.end.timeUnit))) {
endExpression = String.format("DATEADD(day,%d,%s)", startWindow.end.coeff * startWindow.end.days, startIndexDateExpression);
} else {
}else if(startWindow.end.timeUnitValue != null){
endExpression = String.format("DATEADD(%s,%d,%s)", startWindow.end.timeUnit, startWindow.end.coeff * startWindow.end.timeUnitValue, startIndexDateExpression);
}
else {
endExpression = checkObservationPeriod ? (startWindow.end.coeff == -1 ? "P.OP_START_DATE" : "P.OP_END_DATE") : null;
}

Expand All @@ -573,19 +583,26 @@ public String getWindowedCriteriaQuery(String sqlTemplate, WindowedCriteria crit
String endIndexDateExpression = (endWindow.useIndexEnd != null && endWindow.useIndexEnd) ? "P.END_DATE" : "P.START_DATE";
// for backwards compatability, having a null endWindow.useIndexEnd means they SHOULD use the index end date.
String endEventDateExpression = (endWindow.useEventEnd == null || endWindow.useEventEnd) ? "A.END_DATE" : "A.START_DATE";
if (endWindow.start.days != null) {
if (endWindow.start.days != null && (endWindow.start.timeUnit == null || IntervalUnit.DAY.getName().equals(endWindow.start.timeUnit))) {
startExpression = String.format("DATEADD(day,%d,%s)", endWindow.start.coeff * endWindow.start.days, endIndexDateExpression);
} else {
}else if(endWindow.start.timeUnitValue != null){
startExpression = String.format("DATEADD(%s,%d,%s)", endWindow.start.timeUnit, endWindow.start.coeff * endWindow.start.timeUnitValue, endIndexDateExpression);
}
else {
startExpression = checkObservationPeriod ? (endWindow.start.coeff == -1 ? "P.OP_START_DATE" : "P.OP_END_DATE") : null;
}

if (startExpression != null) {
clauses.add(String.format("%s >= %s", endEventDateExpression, startExpression));
}

if (endWindow.end.days != null) {
if (endWindow.end.days != null && (endWindow.end.timeUnit == null || IntervalUnit.DAY.getName().equals(endWindow.end.timeUnit))) {
endExpression = String.format("DATEADD(day,%d,%s)", endWindow.end.coeff * endWindow.end.days, endIndexDateExpression);
} else {
}
else if(endWindow.end.timeUnitValue != null){
endExpression = String.format("DATEADD(%s,%d,%s)", endWindow.end.timeUnit, endWindow.end.coeff * endWindow.end.timeUnitValue, endIndexDateExpression);
}
else {
endExpression = checkObservationPeriod ? (endWindow.end.coeff == -1 ? "P.OP_START_DATE" : "P.OP_END_DATE") : null;
}

Expand Down Expand Up @@ -763,7 +780,13 @@ private String getDateFieldForOffsetStrategy(DateOffsetStrategy.DateField dateFi
@Override
public String getStrategySql(DateOffsetStrategy strat, String eventTable) {
String strategySql = StringUtils.replace(DATE_OFFSET_STRATEGY_TEMPLATE, "@eventTable", eventTable);
strategySql = StringUtils.replace(strategySql, "@offset", Integer.toString(strat.offset));
if (strat.offsetUnit == null || IntervalUnit.DAY.getName().equals(strat.offsetUnit)) {
strategySql = StringUtils.replace(strategySql, "@offsetUnitValue", Integer.toString(strat.offset));
strategySql = StringUtils.replace(strategySql, "@offsetUnit", "day");
} else {
strategySql = StringUtils.replace(strategySql, "@offsetUnitValue", Integer.toString(strat.offsetUnitValue));
strategySql = StringUtils.replace(strategySql, "@offsetUnit", strat.offsetUnit);
}
strategySql = StringUtils.replace(strategySql, "@dateField", getDateFieldForOffsetStrategy(strat.dateField));

return strategySql;
Expand All @@ -777,13 +800,33 @@ public String getStrategySql(CustomEraStrategy strat, String eventTable) {
}

String drugExposureEndDateExpression = DEFAULT_DRUG_EXPOSURE_END_DATE_EXPRESSION;
if (strat.daysSupplyOverride != null) {
if (strat.daysSupplyOverride != null && IntervalUnit.DAY.getName().equals(strat.gapUnit)) {
drugExposureEndDateExpression = String.format("DATEADD(day,%d,DRUG_EXPOSURE_START_DATE)", strat.daysSupplyOverride);
}else if(strat.daysSupplyOverride != null && IntervalUnit.HOUR.getName().equals(strat.gapUnit)){
drugExposureEndDateExpression = String.format("DATEADD(hour,%d,DRUG_EXPOSURE_START_DATE)", strat.daysSupplyOverride);
}else if(strat.daysSupplyOverride != null && IntervalUnit.MINUTE.getName().equals(strat.gapUnit)){
drugExposureEndDateExpression = String.format("DATEADD(minute,%d,DRUG_EXPOSURE_START_DATE)", strat.daysSupplyOverride);
}else if(strat.daysSupplyOverride != null && IntervalUnit.SECOND.getName().equals(strat.gapUnit)){
drugExposureEndDateExpression = String.format("DATEADD(second,%d,DRUG_EXPOSURE_START_DATE)", strat.daysSupplyOverride);
}
String strategySql = StringUtils.replace(CUSTOM_ERA_STRATEGY_TEMPLATE, "@eventTable", eventTable);
strategySql = StringUtils.replace(strategySql, "@drugCodesetId", strat.drugCodesetId.toString());
strategySql = StringUtils.replace(strategySql, "@gapDays", Integer.toString(strat.gapDays));
strategySql = StringUtils.replace(strategySql, "@offset", Integer.toString(strat.offset));
if (IntervalUnit.DAY.getName().equals(strat.gapUnit)) {
strategySql = StringUtils.replace(strategySql, "@gapUnitValue", Integer.toString(strat.gapDays));
strategySql = StringUtils.replace(strategySql, "@gapUnit", "day");
}else {
strategySql = StringUtils.replace(strategySql, "@gapUnitValue", Integer.toString(strat.gapUnitValue));
strategySql = StringUtils.replace(strategySql, "@gapUnit", strat.gapUnit);

}
if(IntervalUnit.DAY.getName().equals(strat.offsetUnit) || strat.offsetUnit == null){
strategySql = StringUtils.replace(strategySql, "@offsetUnitValue", Integer.toString(strat.offset));
strategySql = StringUtils.replace(strategySql, "@offsetUnit", "day");
}else {
strategySql = StringUtils.replace(strategySql, "@offsetUnitValue", Integer.toString(strat.offsetUnitValue));
strategySql = StringUtils.replace(strategySql, "@offsetUnit", strat.offsetUnit);
}

strategySql = StringUtils.replace(strategySql, "@drugExposureEndDateExpression", drugExposureEndDateExpression);

return strategySql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@
*/
package org.ohdsi.circe.cohortdefinition;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class CollapseSettings {

@JsonProperty("CollapseType")
public CollapseType collapseType = CollapseType.ERA;

@JsonProperty("EraPad")
public int eraPad = 0;


@JsonProperty("EraPadUnit")
public String eraPadUnit = IntervalUnit.DAY.getName();

@JsonProperty("EraPadUnitValue")
public int eraPadUnitValue = 0;

}
10 changes: 10 additions & 0 deletions src/main/java/org/ohdsi/circe/cohortdefinition/Criteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;

import org.ohdsi.circe.cohortdefinition.builders.BuilderOptions;

/**
Expand Down Expand Up @@ -60,5 +61,14 @@ public String accept(IGetCriteriaSqlDispatcher dispatcher) {

@JsonProperty("DateAdjustment")
public DateAdjustment dateAdjustment;

/**
* This is a marker for the proper table columns definition while constructing the SELECT part of the result SQL
* in the corresponding SQL builder to reflect the values being defined in the associated WindowCriteria
*
* The DAY value should be set only if all the WindowCriteria's Window.Endpoint values are DAY
*/
@JsonProperty("IntervalUnit")
public String intervalUnit;

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ public class CustomEraStrategy extends EndStrategy {

@JsonProperty("GapDays")
public int gapDays = 0;
@JsonProperty("GapUnit")
public String gapUnit = "day";
@JsonProperty("GapUnitValue")
public int gapUnitValue = 0;

@JsonProperty("Offset")
public int offset = 0;
@JsonProperty("OffsetUnit")
public String offsetUnit = "day";
@JsonProperty("OffsetUnitValue")
public int offsetUnitValue = 0;

@JsonProperty("DaysSupplyOverride")
public Integer daysSupplyOverride = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,32 @@
*/
package org.ohdsi.circe.cohortdefinition;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
*
* @author Chris Knoll <[email protected]>
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class DateOffsetStrategy extends EndStrategy {

public enum DateField {
StartDate,
EndDate
}

@JsonProperty("DateField")
public DateField dateField = DateField.StartDate;

@JsonProperty("Offset")
public int offset = 0;


@JsonProperty("OffsetUnitValue")
public int offsetUnitValue = 0;
@JsonProperty("OffsetUnit")
public String offsetUnit = "day";

@Override
public String accept(IGetEndStrategySqlDispatcher dispatcher, String eventTable) {
return dispatcher.getStrategySql(this, eventTable);
Expand Down
Loading