Skip to content

Commit b14989f

Browse files
committed
feat: add partial night shifts
1 parent 0c61304 commit b14989f

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

execution_engine/omop/criterion/combination/temporal.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class TimeIntervalType(StrEnum):
1515
MORNING_SHIFT = "morning_shift"
1616
AFTERNOON_SHIFT = "afternoon_shift"
1717
NIGHT_SHIFT = "night_shift"
18+
NIGHT_SHIFT_BEFORE_MIDNIGHT = "night_shift_before_midnight"
19+
NIGHT_SHIFT_AFTER_MIDNIGHT = "night_shift_after_midnight"
1820
DAY = "day"
1921
ANY_TIME = "any_time"
2022

@@ -259,6 +261,34 @@ def NightShift(
259261

260262
return cls.Presence(criterion, category, TimeIntervalType.NIGHT_SHIFT)
261263

264+
@classmethod
265+
def NightShiftBeforeMidnight(
266+
cls,
267+
criterion: Union[Criterion, "CriterionCombination"],
268+
category: CohortCategory,
269+
) -> "TemporalIndicatorCombination":
270+
"""
271+
Create a NightShiftBeforeMidnight combination of criteria.
272+
"""
273+
274+
return cls.Presence(
275+
criterion, category, TimeIntervalType.NIGHT_SHIFT_BEFORE_MIDNIGHT
276+
)
277+
278+
@classmethod
279+
def NightShiftAfterMidnight(
280+
cls,
281+
criterion: Union[Criterion, "CriterionCombination"],
282+
category: CohortCategory,
283+
) -> "TemporalIndicatorCombination":
284+
"""
285+
Create a NightShiftAfterMidnight combination of criteria.
286+
"""
287+
288+
return cls.Presence(
289+
criterion, category, TimeIntervalType.NIGHT_SHIFT_AFTER_MIDNIGHT
290+
)
291+
262292
@classmethod
263293
def Day(
264294
cls,

execution_engine/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class TimeIntervalSettings(BaseModel):
4444
night_shift: TimeInterval = TimeInterval(
4545
start=datetime.time(22, 0, 0), end=datetime.time(5, 59, 59)
4646
)
47+
night_shift_before_midnight: TimeInterval = TimeInterval(
48+
start=datetime.time(22, 0, 0), end=datetime.time(23, 59, 59)
49+
)
50+
night_shift_after_midnight: TimeInterval = TimeInterval(
51+
start=datetime.time(0, 0, 0), end=datetime.time(5, 59, 59)
52+
)
4753
day: TimeInterval = TimeInterval(
4854
start=datetime.time(0, 0, 0), end=datetime.time(23, 59, 59)
4955
)

tests/execution_engine/omop/criterion/combination/test_temporal_combination.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,88 @@ def patient_events(self, db_session, person_visit):
683683
),
684684
{1: set(), 2: set(), 3: set()},
685685
),
686+
#######################
687+
# Partial Night Shifts (before midnight)
688+
#######################
689+
(
690+
TemporalIndicatorCombination.NightShiftBeforeMidnight(
691+
c1, category=CohortCategory.POPULATION
692+
),
693+
{
694+
1: {
695+
(
696+
pendulum.parse("2023-03-01 22:00:00+01:00"),
697+
pendulum.parse("2023-03-01 23:59:59+01:00"),
698+
),
699+
(
700+
pendulum.parse("2023-03-02 22:00:00+01:00"),
701+
pendulum.parse("2023-03-02 23:59:59+01:00"),
702+
),
703+
},
704+
2: set(),
705+
3: set(),
706+
},
707+
),
708+
(
709+
TemporalIndicatorCombination.NightShiftBeforeMidnight(
710+
c2, category=CohortCategory.POPULATION
711+
),
712+
{
713+
1: {
714+
(
715+
pendulum.parse("2023-03-02 22:00:00+01:00"),
716+
pendulum.parse("2023-03-02 23:59:59+01:00"),
717+
),
718+
},
719+
2: set(),
720+
3: set(),
721+
},
722+
),
723+
(
724+
TemporalIndicatorCombination.NightShiftBeforeMidnight(
725+
c3, category=CohortCategory.POPULATION
726+
),
727+
{1: set(), 2: set(), 3: set()},
728+
),
729+
#######################
730+
# Partial Night Shifts (after midnight)
731+
#######################
732+
(
733+
TemporalIndicatorCombination.NightShiftAfterMidnight(
734+
c1, category=CohortCategory.POPULATION
735+
),
736+
{
737+
1: {
738+
(
739+
pendulum.parse("2023-03-02 00:00:00+01:00"),
740+
pendulum.parse("2023-03-02 05:59:59+01:00"),
741+
),
742+
},
743+
2: set(),
744+
3: set(),
745+
},
746+
),
747+
(
748+
TemporalIndicatorCombination.NightShiftAfterMidnight(
749+
c2, category=CohortCategory.POPULATION
750+
),
751+
{
752+
1: {
753+
(
754+
pendulum.parse("2023-03-03 00:00:00+01:00"),
755+
pendulum.parse("2023-03-03 05:59:59+01:00"),
756+
),
757+
},
758+
2: set(),
759+
3: set(),
760+
},
761+
),
762+
(
763+
TemporalIndicatorCombination.NightShiftAfterMidnight(
764+
c3, category=CohortCategory.POPULATION
765+
),
766+
{1: set(), 2: set(), 3: set()},
767+
),
686768
],
687769
)
688770
def test_combination_on_database(

0 commit comments

Comments
 (0)