Skip to content

Commit

Permalink
fix timeframe filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-B98 committed Jan 27, 2025
1 parent 0bc9d05 commit 02e3fed
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
15 changes: 15 additions & 0 deletions examples/definitions/concepts/ICUEncounter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: ICUEncounter
description: Period of time during which a patient is admitted to the ICU
identifiers:
loinc: ''
snomed: ''
unit: {}
limits:
lower: ''
upper: ''

sources:
- source: mimic
extractor: open_icu.steps.source.mimic.encounter.ICUEncounterExtractor
unit: {}
params: {}
6 changes: 4 additions & 2 deletions examples/pipeline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@
"metadata": {},
"outputs": [],
"source": [
"d.data[\"aki_urineoutput_stage\"]"
"d.data[\"BodyWeight\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"d.data[\"BodyWeight\"][\"identifier__coding\"].values[0]"
]
}
],
"metadata": {
Expand Down
9 changes: 5 additions & 4 deletions open_icu/steps/cohort/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ def __init__(
) -> None:
super().__init__(configs=configs, concept_configs=concept_configs, parent=parent)

self._filter_conigs: list[CohortFilterConfig] = []
self._filter_configs: list[CohortFilterConfig] = []
if isinstance(configs, list):
self._filter_conigs = configs
self._filter_configs = configs
elif self._config_path is not None:
self._filter_conigs = self._read_config(self._config_path / "cohort", CohortFilterConfig)
self._filter_configs = self._read_config(self._config_path / "cohort", CohortFilterConfig)

def filter(self, subject_data: SubjectData) -> bool:
for conf in self._filter_conigs:
for conf in self._filter_configs:
print(conf)
if not all(concept in subject_data.data.keys() for concept in conf.concepts):
continue

Expand Down
15 changes: 9 additions & 6 deletions open_icu/steps/cohort/filters/timeframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import pandas as pd
from pandera.errors import SchemaError
from pandera.typing import DataFrame

from open_icu.steps.cohort.filters.base import CohortFilter
from open_icu.types.base import SubjectData
from open_icu.types.fhir import FHIRObjectEncounter, Period
from open_icu.types.fhir.encounter import FHIREncounter


class TimeframeFilter(CohortFilter):
Expand All @@ -32,16 +33,18 @@ def __init__(
self._weeks = weeks
self._strategy = strategy

def _get_delta(self, period: Period) -> pd.Timedelta:
return period["end"] - period["start"]
def _get_delta(self, df: DataFrame[FHIREncounter]) -> pd.Timedelta:
dt = df[FHIREncounter.actual_period__end] - df[FHIREncounter.actual_period__start]
assert isinstance(dt, pd.Timedelta)
return dt

def filter(self, subject_data: SubjectData) -> bool:
for concept in self.concepts:
if (concept_data := subject_data.data.get(concept, None)) is None:
continue

try:
FHIRObjectEncounter.validate(concept_data)
FHIREncounter.validate(concept_data)
except SchemaError:
continue

Expand All @@ -56,8 +59,8 @@ def filter(self, subject_data: SubjectData) -> bool:
)

if self._strategy == "all":
return not (concept_data[FHIRObjectEncounter.actual_period].map(self._get_delta) >= td).all()
return not (concept_data.apply(self._get_delta, axis=1) >= td).all()

return not (concept_data[FHIRObjectEncounter.actual_period].map(self._get_delta) >= td).any()
return not (concept_data.apply(self._get_delta, axis=1) >= td).any()

return False
13 changes: 13 additions & 0 deletions open_icu/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@


class SubjectData(BaseModel):
"""
Subject data model.
Attributes
----------
id: str
Subject identifier.
source: str
Source of the data.
data: dict[str, DataFrame[FHIRSchema]]
Dataframes of Concept data extracted from the source for a subject.
"""

id: str
source: str
data: dict[str, DataFrame[FHIRSchema]]

0 comments on commit 02e3fed

Please sign in to comment.