Skip to content

Commit

Permalink
update/412023
Browse files Browse the repository at this point in the history
  • Loading branch information
jcnamendiOdysseus committed Jan 4, 2024
1 parent b9b5820 commit 5098a3a
Show file tree
Hide file tree
Showing 100 changed files with 747 additions and 620 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ root = true
[*.{java,sql}]
charset = utf-8
indent_style = space
indent_size = 2
indent_size = 4
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.ohdsi.circe.cohortdefinition.*;
import org.ohdsi.circe.vocabulary.Concept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ 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 || 0 == dateOffsetStrategy.offsetUnitValue))
.then(dateOffsetStrategy -> {
if(0 == dateOffsetStrategy.offset){
reporter.add(String.format(DAYS_OFFSET_WARNING), "Days");
}else {
reporter.add(String.format(DAYS_OFFSET_WARNING, dateOffsetStrategy.offsetUnit));
}
}));
.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,12 +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))
|| ((c.startWindow.start.timeUnitValue != null)
&& (c.startWindow.end != null)
&& c.startWindow.end.timeUnitValue != 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
25 changes: 8 additions & 17 deletions src/main/java/org/ohdsi/circe/check/checkers/RangeCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,16 @@ protected void checkInclusionRules(final CohortExpression expression, WarningRep
}

private void checkWindow(Window window, WarningReporter reporter, String name) {
Optional.ofNullable(window)
.map(w -> checkEndpoint(w, name, "start"))
.ifPresent(reporter::add);

Optional.ofNullable(window)
.map(w -> checkEndpoint(w, name, "end"))
.ifPresent(reporter::add);
if (Objects.nonNull(window)) {
if (Objects.nonNull(window.start) && Objects.nonNull(window.start.timeUnitValue) && window.start.timeUnitValue < 0) {
reporter.add(NEGATIVE_VALUE_ERROR, name, window.start.timeUnitValue, "start");
}
if (Objects.nonNull(window.end) && Objects.nonNull(window.end.timeUnitValue) && window.end.timeUnitValue < 0) {
reporter.add(NEGATIVE_VALUE_ERROR, name, window.end.timeUnitValue, "end");
}
}
}

private String checkEndpoint(Window window, String name, String endpointType) {
boolean hasValid = Objects.nonNull(window.start) && Objects.nonNull(window.start.days < 0 ? window.start.days : window.start.timeUnitValue) && window.start.days < 0 ? window.start.days < 0 : window.start.timeUnitValue < 0;
return Optional.of(window)
.filter(w -> hasValid)
.map(w -> String.format(NEGATIVE_VALUE_ERROR, name, getEndpointValue(w.start), endpointType))
.orElse(null);
}
private Object getEndpointValue(Window.Endpoint endpoint) {
return Objects.nonNull(endpoint.days) ? endpoint.days : endpoint.timeUnitValue;
}

private void checkObservationFilter(ObservationFilter filter, WarningReporter reporter, String name) {
if (Objects.nonNull(filter)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private Integer startDays(Window window) {
.orElse(0));
})
.orElse(0);
}
}

class TimeWindowInfo {
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,10 @@ public String buildExpressionQuery(CohortExpression expression, BuildExpressionQ

if(!StringUtils.isEmpty(Integer.toString(expression.collapseSettings.eraPad)) && expression.collapseSettings.eraPad != 0){
resultSql = StringUtils.replace(resultSql, "@eraconstructorpad", Integer.toString(expression.collapseSettings.eraPad));
resultSql = StringUtils.replace(resultSql, "@era_pad_unit", "day");
}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, "@era_pad", Integer.toString(expression.collapseSettings.eraPadUnitValue));
}
resultSql = StringUtils.replace(resultSql, "@inclusionRuleTable", getInclusionRuleTableSql(expression));
resultSql = StringUtils.replace(resultSql, "@inclusionImpactAnalysisByEventQuery", getInclusionAnalysisQuery("#qualified_events", 0));
Expand Down Expand Up @@ -574,7 +575,7 @@ else if (startWindow.end.timeUnitValue != null) {
clauses.add(String.format("%s <= %s", startEventDateExpression, endExpression));
}

// EndWindow
// EndWindow
Window endWindow = criteria.endWindow;

if (endWindow != null) {
Expand All @@ -584,26 +585,26 @@ else if (startWindow.end.timeUnitValue != null) {
if (endWindow.start.days != null) {
startExpression = String.format("DATEADD(day,%d,%s)", endWindow.start.coeff * endWindow.start.days, endIndexDateExpression);
} 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;
}
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 (startExpression != null) {
clauses.add(String.format("%s >= %s", endEventDateExpression, startExpression));
}

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

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

// RestrictVisit
Expand All @@ -617,7 +618,7 @@ else if (startWindow.end.timeUnitValue != null) {
return query;
}

public String getWindowedCriteriaQuery(WindowedCriteria criteria, String eventTable) {
public String getWindowedCriteriaQuery(WindowedCriteria criteria, String eventTable) {
String query = getWindowedCriteriaQuery(WINDOWED_CRITERIA_TEMPLATE, criteria, eventTable, null);
return query;
}
Expand Down Expand Up @@ -775,13 +776,15 @@ private String getDateFieldForOffsetStrategy(DateOffsetStrategy.DateField dateFi
@Override
public String getStrategySql(DateOffsetStrategy strat, String eventTable) {
String strategySql = StringUtils.replace(DATE_OFFSET_STRATEGY_TEMPLATE, "@eventTable", eventTable);
if(strat.offset != 0){
strategySql = StringUtils.replace(strategySql, "@offset", Integer.toString(strat.offset));
if(strat.offset != 0){
strategySql = StringUtils.replace(strategySql, "@offsetUnit", "day");
strategySql = StringUtils.replace(strategySql, "@offsetUnitValue", Integer.toString(strat.offset));
}else {
strategySql = StringUtils.replace(strategySql, "@offsetUnitValue", Integer.toString(strat.offsetUnitValue));
strategySql = StringUtils.replace(strategySql, "@offsetUnit", strat.offsetUnit);
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
*/
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;
public int eraPad = 0;
@JsonProperty("EraPadUnit")
public String eraPadUnit;
public String eraPadUnit = "day";
@JsonProperty("EraPadUnitValue")
public int eraPadUnitValue = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
*/
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jdk.nashorn.internal.objects.annotations.Getter;

public enum IntervalUnit {
DAY("day"),
HOUR("hour"),
MINUTE("minute"),
SECOND("second");
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/ohdsi/circe/cohortdefinition/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@
*/
package org.ohdsi.circe.cohortdefinition;

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

/**
*
* @author cknoll1
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Window {

public static class Endpoint {

@JsonProperty("Days")
public Integer days;
@JsonProperty("TimeUnitValue")
public Integer timeUnitValue;
@JsonProperty("TimeUnit")
public String timeUnit = IntervalUnit.DAY.getName();
public String timeUnit;

@JsonProperty("Coeff")
public int coeff;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ protected List<String> resolveSelectClauses(T criteria) {
}
// dateAdjustment or default start/end dates
if (criteria.dateAdjustment != null) {
selectCols.add(BuilderUtils.getDateAdjustmentExpression(criteria.dateAdjustment,
selectCols.add(
BuilderUtils.getDateAdjustmentExpression(criteria.dateAdjustment,
criteria.dateAdjustment.startWith == DateAdjustment.DateType.START_DATE ? "co.condition_start_datetime" : "COALESCE(co.condition_end_datetime, DATEADD(day,1,co.condition_start_datetime))",
criteria.dateAdjustment.endWith == DateAdjustment.DateType.START_DATE ? "co.condition_start_datetime" : "COALESCE(co.condition_end_datetime, DATEADD(day,1,co.condition_start_datetime))"));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The person exits the cohort at the end of continuous observation.
<#if s.offset?? && s.offset == "day" >
The cohort end date will be offset from index event's ${utils.optionName(dateOffsetFieldOptions, s.dateField)} plus <@utils.formatValue s.offset "day"/>.
<#else >
The cohort end date will be offset from index event's ${utils.optionName(dateOffsetFieldOptions, s.dateField)} plus <@utils.formatValue s.offsetUnitValue s.offsetUnit/>.
The cohort end date will be offset from index event's ${utils.optionName(dateOffsetFieldOptions, s.dateField)} plus <@utils.formatValue s.offsetUnitValue s.offsetUnit/>.
</#if>

</#macro>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ END Note!!!!
--><#if range.op?ends_with("bt")> and <#if range.extent?has_content>${utils.formatDate(range.extent)}<#else>_empty_</#if></#if></#macro>

<#-- ConceptList -->
<#macro ConceptList list quote="\""><#list list?map(item->(quote + item.conceptName?lower_case + quote)) as item><#if item?counter gt 1><#if
<#macro ConceptList list quote="\""><#list list?map(item->(quote + item.conceptName?lower_case + quote)) as item><#if item?counter gt 1><#if
item?counter == list?size> or <#else>, </#if></#if>${item}</#list></#macro>

<#-- ConceptSetSelection -->
<#macro ConceptSetSelection selection defaultName="any"><#if selection.isExcluded!false>not </#if>in ${utils.codesetName(selection.codesetId!"", defaultName)}</#macro>

<#-- NumericRange -->
<#assign numericRangeOptions = [
{"id": "lt", "name": "&lt;"},
{"id": "lt", "name": "&lt;"},
{"id": "lte", "name": "&lt;="},
{"id": "eq", "name": "="},
{"id": "eq", "name": "="},
{"id": "gt", "name": "&gt;"},
{"id": "gte", "name": "&gt;="},
{"id": "bt", "name": "between"},
Expand Down Expand Up @@ -91,34 +91,34 @@ END Note!!!!

<#macro Window w indexLabel="cohort entry">${whichEventPart(w.useEventEnd!false)}
<#---->
<#if !w.start.days?? && w.end.days == 0 && w.start.coeff == -1 >
<#if w.start.days?has_content && w.end.days == 0 && w.start.coeff == -1 >
anytime on or before ${indexLabel}
${whichIndexPart(w.useIndexEnd!false)}
<#---->
<#elseif (w.end.days!0) == 1 && w.start.coeff == -1 && w.end.coeff == -1>
<#if w.start.days??>in the ${w.start.days} days
<#else>anytime</#if> prior to ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#---->
<#elseif !w.start.days?? && (w.end.days!0) gt 1 && w.start.coeff == -1>anytime up to ${w.end.days} days ${temporalDirection(w.end.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#elseif w.start.days?has_content && (w.end.days!0) gt 1 && w.start.coeff == -1>anytime up to ${w.end.days} days ${temporalDirection(w.end.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#---->
<#elseif !w.end.days?? && (w.start.days!0) gt 0 && w.end.coeff ==1> ${w.start.days} days ${temporalDirection(w.start.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#elseif w.end.days?has_content && (w.start.days!0) gt 0 && w.end.coeff ==1> ${w.start.days} days ${temporalDirection(w.start.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#---->
<#else>between ${w.start.days!"all"} days ${temporalDirection(w.start.coeff)} and ${w.end.days!"all"} days ${temporalDirection(w.end.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}</#if>
<#---->
<#if !w.start.timeUnitValue?? && w.end.timeUnitValue == 0 && w.start.coeff == -1 >anytime on or before ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#if w.start.timeUnitValue?has_content && w.end.timeUnitValue == 0 && w.start.coeff == -1 >anytime on or before ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#---->
<#elseif (w.end.timeUnitValue!0) == 1 && w.start.coeff == -1 && w.end.coeff == -1><#if w.start.timeUnitValue??>in the ${w.start.timeUnitValue} ${w.start.timeUnit}s<#else>anytime</#if> prior to ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#---->
<#elseif !w.start.timeUnitValue?? && (w.end.timeUnitValue!0) gt 1 && w.start.coeff == -1>anytime up to ${w.end.timeUnitValue} ${w.end.timeUnit}s ${temporalDirection(w.end.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#elseif w.start.timeUnitValue?has_content && (w.end.timeUnitValue!0) gt 1 && w.start.coeff == -1>anytime up to ${w.end.timeUnitValue} ${w.end.timeUnit}s ${temporalDirection(w.end.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#---->
<#elseif !w.end.timeUnitValue?? && (w.start.timeUnitValue!0) gt 0 && w.end.coeff ==1> ${w.start.timeUnitValue} ${w.start.timeUnit}s ${temporalDirection(w.start.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#elseif w.end.timeUnitValue?has_content && (w.start.timeUnitValue!0) gt 0 && w.end.coeff ==1> ${w.start.timeUnitValue} ${w.start.timeUnit}s ${temporalDirection(w.start.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}
<#---->
<#else>between ${w.start.timeUnitValue!"all"} ${w.start.timeUnit}s ${temporalDirection(w.start.coeff)} and ${w.end.timeUnitValue!"all"} ${w.end.timeUnit}s ${temporalDirection(w.end.coeff)} ${indexLabel} ${whichIndexPart(w.useIndexEnd!false)}</#if>
</#macro>

<#-- User Defined Period -->

<#macro UserDefinedPeriod p><#if
<#macro UserDefinedPeriod p><#if
p.startDate?has_content>a user defiend start date of ${utils.formatDate(p.startDate)}<#if p.endDate?has_content> and</#if></#if><#if
p.endDate?has_content><#if !p.startDate?has_content>a user defined</#if> end date of ${utils.formatDate(p.endDate)}</#if>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
-- date offset strategy

SELECT
event_id,
person_id,
CASE
WHEN @offset IS NOT NULL
THEN
CASE
WHEN DATEADD(@offsetUnit, @offsetUnitValue, @dateField) > op_end_date THEN op_end_date
ELSE DATEADD(@offsetUnit, @offsetUnitValue, @dateField)
END
ELSE
CASE
WHEN DATEADD(day, @offset, @dateField) > op_end_date THEN op_end_date
ELSE DATEADD(day, @offset, @dateField)
END
END AS end_date
select event_id,
person_id,
case
when DATEADD(@offsetUnit, @offsetUnitValue, @dateField) > op_end_date then op_end_date
else DATEADD(@offsetUnit, @offsetUnitValue, @dateField) end as end_date
INTO #strategy_ends
FROM @eventTable;
from @eventTable;
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ from ( --cteEnds
JOIN ( -- cteEndDates
SELECT
person_id,
CASE
WHEN
@eraconstructorpad IS NOT NULL
THEN
DATEADD(day, -1 * @eraconstructorpad, event_date)
ELSE
DATEADD(@era_pad_unit, -1 * @era_pad, event_date) END AS end_date
DATEADD(@era_pad_unit, -1 * @eraconstructorpad, event_date) as end_date
FROM
(
SELECT
Expand All @@ -87,10 +81,9 @@ from ( --cteEnds

UNION ALL


SELECT
person_id
, DATEADD(@era_pad_unit, @era_pad, end_date) as end_date
, DATEADD(@era_pad_unit, -1 * @eraconstructorpad, end_date) as end_date
, 1 AS event_type
FROM #cohort_rows
) RAWDATA
Expand Down
Loading

0 comments on commit 5098a3a

Please sign in to comment.