Skip to content

Commit

Permalink
set the outpatient visit_start_datetime to be at the start of the day
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoPang committed Jan 8, 2025
1 parent bca6885 commit 2f02967
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cehrbert_data/decorators/clinical_event_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def _decorate(self, patient_events: DataFrame):
),
).otherwise(F.col("visit_start_date"))

# We need to set the visit_start_datetime at the beginning of the visit start date
# because there could be outpatient visit records whose visit_start_datetime is set to the end of the day
visit_start_datetime_udf = F.when(
F.col("is_inpatient") == 0,
F.col("visit_start_date").cast(T.TimestampType())
).otherwise(F.col("visit_start_datetime"))

# We need to bound the medical event dates between visit_start_date and visit_end_date
bound_medical_event_date = F.when(
F.col("date") < F.col("visit_start_date"), F.col("visit_start_date")
Expand All @@ -112,6 +119,7 @@ def _decorate(self, patient_events: DataFrame):

patient_events = (
patient_events.join(visits, ["cohort_member_id", "visit_occurrence_id"])
.withColumn("visit_start_datetime", visit_start_datetime_udf)
.withColumn("visit_end_date", visit_end_date_udf)
.withColumn("visit_end_datetime", F.date_add("visit_end_date", 1))
.withColumn("visit_end_datetime", F.expr("visit_end_datetime - INTERVAL 1 MINUTE"))
Expand Down

0 comments on commit 2f02967

Please sign in to comment.