Skip to content

Commit

Permalink
Merge pull request #245 from CODEX-CELIDA/add-presence-indicator
Browse files Browse the repository at this point in the history
feat: add AnyTime temporal criterion
  • Loading branch information
glichtner authored Dec 6, 2024
2 parents e248b35 + de284b7 commit 4c1ce41
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
13 changes: 13 additions & 0 deletions execution_engine/omop/criterion/combination/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TimeIntervalType(StrEnum):
AFTERNOON_SHIFT = "afternoon_shift"
NIGHT_SHIFT = "night_shift"
DAY = "day"
ANY_TIME = "any_time"


class TemporalIndicatorCombination(CriterionCombination):
Expand Down Expand Up @@ -272,3 +273,15 @@ def Day(
"""

return cls.Presence(criterion, category, TimeIntervalType.DAY)

@classmethod
def AnyTime(
cls,
criterion: Union[Criterion, "CriterionCombination"],
category: CohortCategory,
) -> "TemporalIndicatorCombination":
"""
Create a AnyTime combination of criteria.
"""

return cls.Presence(criterion, category, TimeIntervalType.ANY_TIME)
11 changes: 0 additions & 11 deletions execution_engine/task/process/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,17 +639,6 @@ def create_time_intervals(
Raises:
ValueError: If start_datetime and end_datetime are not in the same timezone.
"""

# if start_datetime.tzinfo is not None or end_datetime.tzinfo is not None:
# if start_datetime.tzinfo != end_datetime.tzinfo:
# raise ValueError(
# "start_datetime and end_datetime must have the same timezone"
# )
# timezone = start_datetime.tzinfo
# else:
# # use local timezone if no timezone is provided
# timezone = datetime.datetime.now().astimezone().tzinfo

if isinstance(timezone, str):
timezone = cast(pytz.tzinfo.DstTzInfo, pytz.timezone(timezone))

Expand Down
43 changes: 26 additions & 17 deletions execution_engine/task/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from execution_engine.omop.db.celida.tables import ResultInterval
from execution_engine.omop.sqlclient import OMOPSQLClient
from execution_engine.settings import get_config
from execution_engine.task.process import get_processing_module
from execution_engine.task.process import Interval, get_processing_module
from execution_engine.util.interval import IntervalType
from execution_engine.util.types import PersonIntervals, TimeRange

Expand Down Expand Up @@ -467,23 +467,32 @@ def get_start_end_from_interval_type(

assert isinstance(self.expr, logic.TemporalCount), "Invalid expression type"

if self.expr.interval_type is not None:
start_time, end_time = get_start_end_from_interval_type(
self.expr.interval_type
)
elif self.expr.start_time is not None and self.expr.end_time is not None:
start_time, end_time = self.expr.start_time, self.expr.end_time
if self.expr.interval_type == TimeIntervalType.ANY_TIME:
indicator_windows = [
Interval(
lower=observation_window.start.timestamp(),
upper=observation_window.end.timestamp(),
type=IntervalType.POSITIVE,
)
]
else:
raise ValueError("Invalid time interval settings")

indicator_windows = process.create_time_intervals(
start_datetime=observation_window.start,
end_datetime=observation_window.end,
start_time=start_time,
end_time=end_time,
interval_type=IntervalType.POSITIVE,
timezone=get_config().timezone,
)
if self.expr.interval_type is not None:
start_time, end_time = get_start_end_from_interval_type(
self.expr.interval_type
)
elif self.expr.start_time is not None and self.expr.end_time is not None:
start_time, end_time = self.expr.start_time, self.expr.end_time
else:
raise ValueError("Invalid time interval settings")

indicator_windows = process.create_time_intervals(
start_datetime=observation_window.start,
end_datetime=observation_window.end,
start_time=start_time,
end_time=end_time,
interval_type=IntervalType.POSITIVE,
timezone=get_config().timezone,
)

result = process.find_overlapping_windows(indicator_windows, data_p)

Expand Down
7 changes: 7 additions & 0 deletions scripts/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import os
import re
import sys
import time

import pendulum
from sqlalchemy import text
Expand Down Expand Up @@ -106,6 +107,7 @@
e = ExecutionEngine()
logging.getLogger().setLevel(logging.DEBUG)

start_time = time.time()

for recommendation_url in urls:
print(recommendation_url)
Expand All @@ -115,3 +117,8 @@
)

e.execute(cdd, start_datetime=start_datetime, end_datetime=end_datetime)

end_time = time.time()
runtime_seconds = end_time - start_time

logging.info(f"Total runtime: {runtime_seconds:.2f} seconds")

0 comments on commit 4c1ce41

Please sign in to comment.