Skip to content

Commit

Permalink
do not add the original visit_type and discharge_facility to the cehr…
Browse files Browse the repository at this point in the history
…bert visit because their orders could be wrong
  • Loading branch information
ChaoPang committed Oct 26, 2024
1 parent 1d3a72b commit b509935
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def __init__(self, **kwargs):
self._discharge_matching_rules = self._create_discharge_matching_rules()
self._text_event_numeric_event_map = {r.code: r for r in self._create_text_event_to_numeric_event_rules()}

@abstractmethod
def _create_visit_matching_rules(self) -> List[str]:
raise NotImplementedError(
"Must implement the matching rules for identifying the visits other than ED/admission"
)

@abstractmethod
def _create_ed_admission_matching_rules(self) -> List[str]:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def __init__(self, default_visit_id, **kwargs):
super().__init__(**kwargs)
self.default_visit_id = default_visit_id

def _create_visit_matching_rules(self) -> List[str]:
return []

def _create_ed_admission_matching_rules(self) -> List[str]:
return ["ED_REGISTRATION//", "TRANSFER_TO//ED"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class MedsToCehrbertOMOP(MedsToCehrBertConversion):

def _create_visit_matching_rules(self) -> List[str]:
return ["Visit/"]

def _create_ed_admission_matching_rules(self) -> List[str]:
return ["Visit/ER"]

Expand Down
8 changes: 6 additions & 2 deletions src/cehrbert/data_generators/hf_data_generator/meds_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
from datasets import Dataset, DatasetDict, Split
from transformers.utils import logging

from cehrbert.data_generators.hf_data_generator import DEFAULT_INPATIENT_CONCEPT_ID, UNKNOWN_VALUE
from cehrbert.data_generators.hf_data_generator import (
DEFAULT_ED_CONCEPT_ID,
DEFAULT_INPATIENT_CONCEPT_ID,
UNKNOWN_VALUE,
)
from cehrbert.data_generators.hf_data_generator.hf_dataset import apply_cehrbert_dataset_mapping
from cehrbert.data_generators.hf_data_generator.hf_dataset_mapping import MedToCehrBertDatasetMapping
from cehrbert.data_generators.hf_data_generator.meds_to_cehrbert_conversion_rules import MedsToCehrBertConversion
Expand Down Expand Up @@ -132,7 +136,7 @@ def convert_one_patient(
visit_end_datetime = max([b.max_time for b in blocks])
discharge_facility = (
next(filter(None, [b.get_discharge_facility() for b in blocks]), None)
if visit_type == DEFAULT_INPATIENT_CONCEPT_ID
if visit_type in [DEFAULT_INPATIENT_CONCEPT_ID, DEFAULT_ED_CONCEPT_ID]
else None
)
visit_events = list()
Expand Down
13 changes: 12 additions & 1 deletion src/cehrbert/data_generators/hf_data_generator/patient_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __init__(
self.has_ed_admission = self._has_ed_admission()
self.has_admission = self._has_admission()
self.has_discharge = self._has_discharge()
self.discharged_to = self.get_discharge_facility()

# Infer the visit_type from the events
# Admission takes precedence over ED
Expand All @@ -82,7 +83,14 @@ def __init__(
elif self.has_ed_admission:
self.visit_type = DEFAULT_ED_CONCEPT_ID
else:
self.visit_type = DEFAULT_OUTPATIENT_CONCEPT_ID
self.visit_type = self._infer_visit_type()

def _infer_visit_type(self) -> str:
for event in self.events:
for matching_rule in self.conversion.get_ed_admission_matching_rules():
if re.match(matching_rule, event.code):
return event.code
return DEFAULT_OUTPATIENT_CONCEPT_ID

def _has_ed_admission(self) -> bool:
"""
Expand Down Expand Up @@ -196,6 +204,9 @@ def get_meds_events(self) -> Iterable[Event]:
"""
events = []
for e in self.events:
# We only convert the events that are not visit type and discharge facility events
if (e.code == self.visit_type) or (self.discharged_to is not None and e.code == self.discharged_to):
continue
events.extend(self._convert_event(e))
return events

Expand Down

0 comments on commit b509935

Please sign in to comment.