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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix
  • Loading branch information
jcnamendiOdysseus committed Jan 16, 2024

Verified

This commit was signed with the committer’s verified signature.
JasonvanBrackel Jason L. van Brackel
commit f819d5e6d6f880cf558274d124e152ec38f73954
Original file line number Diff line number Diff line change
@@ -22,8 +22,6 @@
import org.ohdsi.circe.cohortdefinition.CohortExpression;
import org.ohdsi.circe.cohortdefinition.DateOffsetStrategy;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static org.ohdsi.circe.check.operations.Operations.match;
@@ -43,9 +41,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.offsetUnitValue)
.then(dateOffsetStrategy -> reporter.add(String.format(DAYS_OFFSET_WARNING, dateOffsetStrategy.offsetUnit))));
.isA(DateOffsetStrategy.class)
.then(s -> match((DateOffsetStrategy)s)
.when(dateOffsetStrategy -> Objects.equals(StartDate, dateOffsetStrategy.dateField) && (0 == dateOffsetStrategy.offsetUnitValue || 0 == dateOffsetStrategy.offset))
.then(dateOffsetStrategy -> reporter.add(String.format(DAYS_OFFSET_WARNING, dateOffsetStrategy.offsetUnit))));
}
}
Original file line number Diff line number Diff line change
@@ -42,10 +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 != null && (( c.startWindow.start != 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)
Original file line number Diff line number Diff line change
@@ -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 CustomEraStrategy extends EndStrategy {

@JsonProperty("DrugCodesetId")
@@ -32,17 +34,19 @@ public class CustomEraStrategy extends EndStrategy {
@JsonProperty("GapDays")
public int gapDays = 0;
@JsonProperty("GapUnit")
public String gapUnit = "day";
public String gapUnit = IntervalUnit.DAY.getName();;
@JsonProperty("GapUnitValue")
public int gapUnitValue = 0;
public Integer gapUnitValue = null;

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



@JsonProperty("DaysSupplyOverride")
public Integer daysSupplyOverride = null;

Original file line number Diff line number Diff line change
@@ -27,22 +27,24 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class DateOffsetStrategy extends EndStrategy {

@JsonProperty("Offset")
public int offset = 0;
public enum DateField {
StartDate,
EndDate
}

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

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

@JsonProperty("OffsetUnit")
public String offsetUnit = IntervalUnit.DAY.getName();;
@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);
}

public enum DateField {
StartDate,
EndDate
}
}
33 changes: 19 additions & 14 deletions src/main/java/org/ohdsi/circe/cohortdefinition/Window.java
Original file line number Diff line number Diff line change
@@ -27,25 +27,30 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class Window {

@JsonProperty("Start")
public Endpoint start;
@JsonProperty("End")
public Endpoint end;
@JsonProperty("UseIndexEnd")
public Boolean useIndexEnd;
@JsonProperty("UseEventEnd")
public Boolean useEventEnd;

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Endpoint {

@JsonProperty("Days")
public Integer days;

@JsonProperty("Coeff")
public int coeff;
@JsonProperty("TimeUnitValue")
public Integer timeUnitValue;

@JsonProperty("TimeUnit")
public String timeUnit = "day";
public String timeUnit = IntervalUnit.DAY.getName();
}

@JsonProperty("Start")
public Endpoint start;

@JsonProperty("Coeff")
public int coeff;
}
@JsonProperty("End")
public Endpoint end;

@JsonProperty("UseIndexEnd")
public Boolean useIndexEnd;

@JsonProperty("UseEventEnd")
public Boolean useEventEnd;

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
-- date offset strategy

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
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;
Original file line number Diff line number Diff line change
@@ -62,8 +62,8 @@ from ( --cteEnds
FROM #cohort_rows c
JOIN ( -- cteEndDates
SELECT
person_id,
DATEADD(@era_pad_unit, -1 * @eraconstructorpad, event_date) as end_date
person_id
, DATEADD(@eraPadUnit,-1 * @eraPadValue, event_date) as end_date
FROM
(
SELECT
@@ -83,7 +83,7 @@ from ( --cteEnds

SELECT
person_id
, DATEADD(@era_pad_unit, -1 * @eraconstructorpad, end_date) as end_date
, DATEADD(@eraPadUnit,@eraPadValue,end_date) as end_date
, 1 AS event_type
FROM #cohort_rows
) RAWDATA
Original file line number Diff line number Diff line change
@@ -137,7 +137,57 @@ public void compareTo() {

assertEquals(5, Comparisons.compareTo(filter, window));
}

@Test
public void compareToHours(){
ObservationFilter filter = new ObservationFilter();
filter.postDays = 10;
filter.priorDays = 5;
Window window = new Window();
window.start = new Window.Endpoint();
window.end = new Window.Endpoint();
window.start.timeUnit = "hour";
window.start.timeUnitValue = 72;
window.start.coeff = -1;

window.end.timeUnit = "hour";
window.end.timeUnitValue = 120;
window.end.coeff = 1;
assertEquals(604800, Comparisons.compareTo(filter, window));
}
@Test
public void compareToMinute() {
ObservationFilter filter = new ObservationFilter();
filter.postDays = 10;
filter.priorDays = 5;
Window window = new Window();
window.start = new Window.Endpoint();
window.end = new Window.Endpoint();
window.start.timeUnit = "minute";
window.start.timeUnitValue = 30;
window.start.coeff = -1;

window.end.timeUnit = "minute";
window.end.timeUnitValue = 75;
window.end.coeff = 1;
assertEquals(1289700, Comparisons.compareTo(filter, window));
}
@Test
public void compareToSecond() {
ObservationFilter filter = new ObservationFilter();
filter.postDays = 10;
filter.priorDays = 5;
Window window = new Window();
window.start = new Window.Endpoint();
window.end = new Window.Endpoint();
window.start.timeUnit = "second";
window.start.timeUnitValue = 30;
window.start.coeff = -1;

window.end.timeUnit = "second";
window.end.timeUnitValue = 75;
window.end.coeff = 1;
assertEquals(1295895, Comparisons.compareTo(filter, window));
}
@Test
public void compareToHours() {
ObservationFilter filter = new ObservationFilter();
Original file line number Diff line number Diff line change
@@ -31,9 +31,9 @@ public void checkCorrect() {
List<Warning> warnings = check.check(CORRECT_EXPRESSION);
assertEquals(0, warnings.size());
}
@Test
public void checkCorrectSecond() {
List<Warning> warnings = check.check(CORRECT_EXPRESSION_SECOND);
assertEquals(0, warnings.size());
}
@Test
public void checkCorrectSecond() {
List<Warning> warnings = check.check(CORRECT_EXPRESSION_SECOND);
assertEquals(0, warnings.size());
}
}
Original file line number Diff line number Diff line change
@@ -485,7 +485,7 @@ public void customEraExitTestSecond() {
));

}

@Test
public void conceptSetSimpleTest() {
CohortExpression expression = CohortExpression.fromJson(ResourceHelper.GetResourceAsString("/printfriendly/conceptSet_simple.json"));
@@ -671,7 +671,7 @@ public void nullConceptSetListTest() {
pf.renderConceptSetList((ConceptSet[])null);

}

@Test
public void dateAdjustTest() {
CohortExpression expression = CohortExpression.fromJson(ResourceHelper.GetResourceAsString("/printfriendly/dateAdjust.json"));
@@ -690,16 +690,16 @@ public void dateAdjustTest() {
"12. visit occurrences of 'Concept Set 1', starting 10 days after and ending 20 days after the event start date.",
"13. visit details of 'Concept Set 1', starting 10 days after and ending 20 days after the event start date."
));

}

@Test
public void emptyConceptListTest() {
CohortExpression expression = CohortExpression.fromJson(ResourceHelper.GetResourceAsString("/printfriendly/emptyConceptList.json"));
String markdown = pf.renderCohort(expression);
assertThat(markdown, stringContainsInOrder(
"1. condition occurrences of 'Concept Set 1', a provider specialty that is: [none specified]; a visit occurrence that is: [none specified]."
));

}
}