Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
sprivite committed Feb 18, 2025
1 parent 723bb59 commit 29e1ce5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
14 changes: 7 additions & 7 deletions phenex/filters/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def filter(self, table: Table) -> Table:
raise ValueError(f"Filter must not remove columns.")

return type(table)(filtered_table.select(input_columns))

def _filter(self, table: Table) -> Table:
"""
Returns the logical condition that the filter is applying.
Expand All @@ -43,7 +43,7 @@ def __or__(self, other):

def __invert__(self):
return NotFilter(self)


class AndFilter(Filter):
"""
Expand All @@ -56,7 +56,7 @@ def __init__(self, filter1, filter2):

def _get_predicate(self, table: Table) -> Table:
return self.filter1._get_predicate(table) & self.filter2._get_predicate(table)

def filter(self, table: Table) -> Table:
table = self.filter1.filter(table)
return self.filter2.filter(table)
Expand All @@ -77,7 +77,7 @@ def __init__(self, filter1, filter2):

def _get_predicate(self, table: Table) -> Table:
return self.filter1._get_predicate(table) | self.filter2._get_predicate(table)

def filter(self, table: Table) -> Table:
table1 = self.filter1.filter(table).table
table2 = self.filter2.filter(table).table
Expand All @@ -87,7 +87,7 @@ def autojoin_filter(self, table: "PhenexTable", tables: dict = None):
table1 = self.filter1.autojoin_filter(table, tables).table
table2 = self.filter2.autojoin_filter(table, tables).table
return type(table)(table1.union(table2, distinct=True))


class NotFilter(Filter):
"""
Expand All @@ -103,7 +103,7 @@ def _get_predicate(self, table: Table) -> Table:
def filter(self, table: Table) -> Table:
filtered_table = self.filter.filter(table).table
return type(table)(table.difference(filtered_table))

def autojoin_filter(self, table: "PhenexTable", tables: dict = None):
filtered_table = self.filter.autojoin_filter(table, tables).table
return type(table)(table.difference(filtered_table))
return type(table)(table.difference(filtered_table))
9 changes: 7 additions & 2 deletions phenex/phenotypes/phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

logger = create_logger(__name__)


class Phenotype:
"""
All Phenotype's in PhenEx derive from the Phenotype class. Phenotype's take in the complete specification of what the Phenotype
Expand Down Expand Up @@ -55,10 +56,14 @@ def execute(self, tables: Dict[str, Table]) -> PhenotypeTable:
logger.info(f"Phenotype '{self.name}': executing...")
for child in self.children:
if child.table is None:
logger.debug(f"Phenotype {self.name}: executing child phenotype '{child.name}'...")
logger.debug(
f"Phenotype {self.name}: executing child phenotype '{child.name}'..."
)
child.execute(tables)
else:
logger.debug(f"Phenotype {self.name}: skipping already computed child phenotype '{child.name}'.")
logger.debug(
f"Phenotype {self.name}: skipping already computed child phenotype '{child.name}'."
)

table = self._execute(tables).mutate(BOOLEAN=True)

Expand Down
37 changes: 23 additions & 14 deletions phenex/test/phenotypes/test_codelist_phenotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
from phenex.filters.value import *
from phenex.filters.categorical_filter import CategoricalFilter

import ibis
ibis.options.interactive =True
import ibis

ibis.options.interactive = True


class CodelistPhenotypeTestGenerator(PhenotypeTestGenerator):
name_space = "clpt"
Expand Down Expand Up @@ -664,7 +666,9 @@ def define_phenotype_tests(self):
return test_infos


class CodelistPhenotypeCategoricalFilterLogicalCombinationsTestGenerator(PhenotypeTestGenerator):
class CodelistPhenotypeCategoricalFilterLogicalCombinationsTestGenerator(
PhenotypeTestGenerator
):
name_space = "clpt_categorical_filter_logic"

def define_input_tables(self):
Expand Down Expand Up @@ -755,7 +759,7 @@ def define_phenotype_tests(self):
"phenotype": CodelistPhenotype(
codelist=codelist_factory.get_codelist("c1"),
domain="condition_occurrence",
categorical_filter= ~CategoricalFilter(
categorical_filter=~CategoricalFilter(
allowed_values=["z1"], column_name="z"
),
),
Expand Down Expand Up @@ -828,7 +832,9 @@ def define_phenotype_tests(self):
return [test1, test2, test3]


class CodelistPhenotypeCategoricalFilterLogicalCombinationsAutojoinTestGenerator(PhenotypeTestGenerator):
class CodelistPhenotypeCategoricalFilterLogicalCombinationsAutojoinTestGenerator(
PhenotypeTestGenerator
):
name_space = "clpt_categorical_filter_autojoin"

def define_input_tables(self):
Expand All @@ -839,7 +845,7 @@ def define_input_tables(self):
df["CODE"] = "c1"
df["CODE_TYPE"] = "ICD10CM"
df["DATE"] = datetime.datetime.strptime("10-10-2021", "%m-%d-%Y")
df['DX_POS'] = ['first'] * 5 + ['second'] * 5
df["DX_POS"] = ["first"] * 5 + ["second"] * 5

df2 = pd.DataFrame()
df2["PERSON_ID"] = [f"P{i}" for i in range(N)]
Expand Down Expand Up @@ -880,20 +886,22 @@ def define_phenotype_tests(self):
"phenotype": CodelistPhenotype(
codelist=codelist_factory.get_codelist("c1"),
domain="condition_occurrence",
categorical_filter= a_cat_filter | b_cat_filter
categorical_filter=a_cat_filter | b_cat_filter,
),
}

primary_pos = CategoricalFilter(
allowed_values=["first"], column_name="DX_POS", domain="condition_occurrence"
primary_pos = CategoricalFilter(
allowed_values=["first"],
column_name="DX_POS",
domain="condition_occurrence",
)
c2 = {
"name": "a_and_primary_pos_autojoin_table",
"persons": [f"P{i}" for i in range(2)],
"phenotype": CodelistPhenotype(
codelist=codelist_factory.get_codelist("c1"),
domain="condition_occurrence",
categorical_filter= a_cat_filter & primary_pos
categorical_filter=a_cat_filter & primary_pos,
),
}

Expand All @@ -903,21 +911,20 @@ def define_phenotype_tests(self):
"phenotype": CodelistPhenotype(
codelist=codelist_factory.get_codelist("c1"),
domain="condition_occurrence",
categorical_filter= (a_cat_filter & primary_pos) | b_cat_filter
categorical_filter=(a_cat_filter & primary_pos) | b_cat_filter,
),
}


c_cat_filter = CategoricalFilter(
allowed_values=["c"], column_name="flag1", domain="encounter"
)
c4 = {
"name": "a_and_not_primary_pos_or_b_autojoin_table",
"persons": [f"P{i}" for i in range(2, 4)] + [f"P{i}" for i in range(5, 10)],
"persons": [f"P{i}" for i in range(2, 4)] + [f"P{i}" for i in range(5, 10)],
"phenotype": CodelistPhenotype(
codelist=codelist_factory.get_codelist("c1"),
domain="condition_occurrence",
categorical_filter= (c_cat_filter & ~primary_pos) | b_cat_filter
categorical_filter=(c_cat_filter & ~primary_pos) | b_cat_filter,
),
}

Expand All @@ -932,10 +939,12 @@ def test_categorical_filter_logic():
tg = CodelistPhenotypeCategoricalFilterLogicalCombinationsTestGenerator()
tg.run_tests()


def test_categorical_filter_logic_autojoin():
tg = CodelistPhenotypeCategoricalFilterLogicalCombinationsAutojoinTestGenerator()
tg.run_tests()


def test_fuzzy_match():
tg = CodelistPhenotypeFuzzyMatchTestGenerator()
tg.run_tests()
Expand Down

0 comments on commit 29e1ce5

Please sign in to comment.