Skip to content

Commit

Permalink
update/412023
Browse files Browse the repository at this point in the history
  • Loading branch information
jcnamendiOdysseus committed Jan 5, 2024
1 parent b9b5820 commit 6fd8ecd
Show file tree
Hide file tree
Showing 102 changed files with 555 additions and 496 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
20 changes: 11 additions & 9 deletions src/main/java/org/ohdsi/circe/check/checkers/Comparisons.java
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 Expand Up @@ -105,16 +106,17 @@ public static Predicate<Concept> compare(Concept source) {
.append(concept.vocabularyId, source.vocabularyId)
.build();
}

public static int compareTo(ObservationFilter filter, Window window) {
int range1 , range2Start , range2End;
if(Objects.nonNull(window.start) && Objects.nonNull(window.start.days)){
range1 = filter.postDays + filter.priorDays;
range2Start = window.start.coeff * window.start.days;
range2End = Objects.nonNull(window.end) && Objects.nonNull(window.end.days) ? window.end.coeff * window.end.days : 0;
}else {
range1 = (filter.postDays + filter.priorDays) * 24 * 60 * 60;
range2Start = getTimeInSeconds(window.start);
range2End = getTimeInSeconds(window.end);
int range1, range2Start, range2End;
if (Objects.nonNull(window.start) && (Objects.nonNull(window.start.days) && window.start.days != 0)) {
range1 = filter.postDays + filter.priorDays;
range2Start = window.start.coeff * window.start.days;
range2End = Objects.nonNull(window.end) && Objects.nonNull(window.end.days) ? window.end.coeff * window.end.days : 0;
} else {
range1 = (filter.postDays + filter.priorDays) * 24 * 60 * 60;
range2Start = getTimeInSeconds(window.start);
range2End = getTimeInSeconds(window.end);
}
return range1 - (range2End - range2Start);
}
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
27 changes: 12 additions & 15 deletions src/main/java/org/ohdsi/circe/check/checkers/RangeCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,23 @@ 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);
if (Objects.isNull(window)) {
return;
}
checkAndReportIfNegative(window.start, reporter, name, "start");
checkAndReportIfNegative(window.end, reporter, name, "end");

Optional.ofNullable(window)
.map(w -> checkEndpoint(w, name, "end"))
.ifPresent(reporter::add);
}

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 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)) {
if (filter.priorDays < 0) {
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, "@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 @@ -798,8 +801,18 @@ public String getStrategySql(CustomEraStrategy strat, String eventTable) {
}
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));
} else {
strategySql = StringUtils.replace(strategySql, "@gapUnitValue", Integer.toString(strat.gapUnitValue));
}
strategySql = StringUtils.replace(strategySql, "@gapUnit", strat.gapUnit);
if (IntervalUnit.DAY.getName().equals(strat.offsetUnit)) {
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, "@drugExposureEndDateExpression", drugExposureEndDateExpression);

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 @@ -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,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 All @@ -38,7 +40,7 @@ public enum DateField {
@JsonProperty("OffsetUnitValue")
public int offsetUnitValue = 0;
@JsonProperty("OffsetUnit")
public String offsetUnit;
public String offsetUnit = "day";

@Override
public String accept(IGetEndStrategySqlDispatcher dispatcher, String eventTable) {
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="day";

@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 @@ -279,46 +279,14 @@ group.demographicCriteriaList?size == 1><@DemographicCriteria c=group.demographi
<#macro CountCriteria countCriteria level=0 indexLabel="cohort entry">having <#if countCriteria.occurrence.type == 0 && countCriteria.occurrence.count == 0>no<#else>${inputTypes.getCountType(countCriteria)} ${countCriteria.occurrence.count}</#if><#--
--><#if countCriteria.occurrence.isDistinct> distinct <@utils.countColumn countCriteria.occurrence.countColumn!"DOMAIN_CONCEPT" /> from</#if> <@Criteria c=countCriteria.criteria level=level isPlural=(countCriteria.occurrence.count != 1) countCriteria=countCriteria indexLabel=indexLabel /></#macro>

<#macro WindowCriteria countCriteria indexLabel="cohort entry" level=0><#local windowParts=[] restrictParts=[]>
<#if countCriteria.startWindow.start.days?? || countCriteria.startWindow.end.days??>
<#local temp>
<@inputTypes.Window countCriteria.startWindow indexLabel />
</#local>
<#local windowParts+=[temp]>
</#if>
<#if countCriteria.endWindow?? && (countCriteria.endWindow.start.days?? || countCriteria.endWindow.end.days??)>
<#local temp>
<@inputTypes.Window countCriteria.endWindow indexLabel />
</#local>
<#local windowParts+=[temp]>
</#if>
<#if countCriteria.startWindow.start.timeUnitValue?? || countCriteria.startWindow.end.timeUnitValue??>
<#local temp>
<@inputTypes.Window countCriteria.startWindow indexLabel />
</#local>
<#local windowParts+=[temp]>
</#if>
<#if countCriteria.endWindow?? && (countCriteria.endWindow.start.timeUnitValue?? || countCriteria.endWindow.end.timeUnitValue??)>
<#local temp>
<@inputTypes.Window countCriteria.endWindow indexLabel />
</#local>
<#local windowParts+=[temp]>
</#if>
<#if countCriteria.restrictVisit!false>
<#local temp>at same visit as ${indexLabel}
</#local>
<#local restrictParts+=[temp]>
</#if>
<#if countCriteria.ignoreObservationPeriod!false>
<#local temp>allow events outside observation period</#local>
<#local restrictParts+=[temp]>
</#if>
<#if windowParts?size gt 0>${windowParts?join(" and ")}
<#if restrictParts?size gt 0>; </#if>
</#if>
<#if restrictParts?size gt 0>${restrictParts?join(" and ")}</#if>

</#macro>
<#macro WindowCriteria countCriteria indexLabel="cohort entry" level=0><#local windowParts=[] restrictParts=[]><#if
countCriteria.startWindow.start.timeUnitValue?? || countCriteria.startWindow.end.timeUnitValue??><#local temp><@inputTypes.Window countCriteria.startWindow indexLabel /></#local><#local windowParts+=[temp]></#if><#if
countCriteria.endWindow?? && (countCriteria.endWindow.start.timeUnitValue?? || countCriteria.endWindow.end.timeUnitValue??)>
<#local temp><@inputTypes.Window countCriteria.endWindow indexLabel /></#local><#local windowParts+=[temp]></#if>
<#if countCriteria.restrictVisit!false><#local temp>at same visit as ${indexLabel}</#local><#local restrictParts+=[temp]></#if>
<#if countCriteria.ignoreObservationPeriod!false><#local temp>allow events outside observation period</#local><#local restrictParts+=[temp]></#if><#if
windowParts?size gt 0>${windowParts?join(" and ")}<#if restrictParts?size gt 0>; </#if></#if><#if
restrictParts?size gt 0>${restrictParts?join(" and ")}</#if></#macro>
<#-- Demographic Criteria -->
<#macro DemographicCriteria c level=0 indexLabel = "cohort entry"><#local attrs = []><#local
temp><@AgeGenderCriteria ageAtStart=c.age!{} gender=c.gender!{} /></#local><#if temp?has_content><#local attrs+=[temp]></#if><#local
Expand Down
Loading

0 comments on commit 6fd8ecd

Please sign in to comment.