Skip to content

Commit

Permalink
fixed continuous coverage phenotype. filters now do not mutate tables…
Browse files Browse the repository at this point in the history
… at all.
  • Loading branch information
a-hartens committed Nov 22, 2024
1 parent 1f6c266 commit 1b2db2b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions phenex/filters/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Filter:
but cannot trigger recursive execution. Fitlers can add columns but may not remove columns.
All classes in the filters module should subclass this class. Subclasses must implement
the _filter method.
"""

def __init__(self):
Expand All @@ -19,6 +20,7 @@ def filter(self, table: Table) -> Table:
if not set(input_columns) <= set(table.columns):
raise ValueError(f"Filter must not remove columns.")

table = table.select(input_columns)
return table

def _filter(self, table: Table) -> Table:
Expand Down
2 changes: 1 addition & 1 deletion phenex/test/phenotype_test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def df_from_test_info(test_info):
if test_info.get("dates") is not None:
df[columnname_date] = test_info["dates"]
else:
test_info[columnname_date] = ibis.null(type=datetime.date)
df[columnname_date] = None

if test_info.get("values") is not None:
df[columnname_value] = test_info["values"]
Expand Down
54 changes: 52 additions & 2 deletions phenex/test/phenotypes/test_continuous_coverage_phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import pandas as pd

from phenex.phenotypes.continuous_coverage_phenotype import ContinuousCoveragePhenotype
from phenex.codelists import LocalCSVCodelistFactory
from phenex.phenotypes.codelist_phenotype import CodelistPhenotype
from phenex.codelists import LocalCSVCodelistFactory, Codelist
from phenex.filters.date_range_filter import DateRangeFilter
from phenex.filters.relative_time_range_filter import RelativeTimeRangeFilter

Expand Down Expand Up @@ -124,11 +125,54 @@ def define_phenotype_tests(self):
min_days=test_info.get("coverage_period_min"),
when="after",
)
test_info["column_types"] = {f"{test_info['name']}_date": "date"}

return test_infos


class ContinuousCoverageWithAnchorPhenotype(ContinuousCoveragePhenotypeTestGenerator):
name_space = "ccpt_anchorphenotype"

def define_input_tables(self):
tables = super().define_input_tables()

tables[0]["df"].drop(columns=["INDEX_DATE"], inplace=True)
df = pd.DataFrame()
n_patients = tables[0]["df"]["PERSON_ID"].unique().shape[0]
c1s = ["c1"] * 12
df["CODE"] = c1s + ["c2"] * (n_patients - len(c1s))
df["CODE_TYPE"] = "ICD10"
df["EVENT_DATE"] = datetime.date(2022, 1, 1)
df["PERSON_ID"] = ["P" + str(x) for x in range(n_patients)]
tables.append({"name": "CONDITION_OCCURRENCE", "df": df})
return tables

def define_phenotype_tests(self):

entry = CodelistPhenotype(
name="entry",
codelist=Codelist(name="c1", codelist={"ICD10": ["c1"]}),
domain="CONDITION_OCCURRENCE",
)

cc1 = ContinuousCoveragePhenotype(
name="cc_prior_entry",
min_days=GreaterThanOrEqualTo(90),
when="before",
anchor_phenotype=entry,
)

persons = ["P7", "P10", "P11"]

t1 = {
"name": "coverage_min_geq_90",
"persons": persons,
"phenotype": cc1,
}

test_infos = [t1]
return test_infos


def test_continuous_coverage_phenotypes():
spg = ContinuousCoveragePhenotypeTestGenerator()
spg.run_tests()
Expand All @@ -139,6 +183,12 @@ def test_continuous_coverage_return_last():
spg.run_tests()


def test_continuous_coverage_with_anchor_phenotype():
spg = ContinuousCoverageWithAnchorPhenotype()
spg.run_tests()


if __name__ == "__main__":
test_continuous_coverage_phenotypes()
test_continuous_coverage_return_last()
test_continuous_coverage_with_anchor_phenotype()
1 change: 1 addition & 0 deletions phenex/test/util/check_equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def check_equality(
result.loc[:, "DUMMY"] = 1
expected = expected.to_pandas()
expected.loc[:, "DUMMY"] = 1

full_results = result.merge(
expected, on=join_on, suffixes=("_result", "_expected"), how="outer"
)
Expand Down

0 comments on commit 1b2db2b

Please sign in to comment.