Skip to content

Commit

Permalink
Merge branch 'main' into cohorttutorialupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
a-hartens authored Jan 23, 2025
2 parents 2ec4d6e + 73e37c5 commit 5f8cf5f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
3 changes: 1 addition & 2 deletions phenex/filters/codelist_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,5 @@ def _filter(self, code_table: CodeTable) -> CodeTable:
filtered_table = code_table.inner_join(codelist_table, join_condition).select(
code_table.columns
)

# return table with downselected columns, of same type as input table
return type(code_table)(filtered_table.select(input_columns))
return filtered_table
7 changes: 3 additions & 4 deletions phenex/filters/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ def __init__(self):

def filter(self, table: Table) -> Table:
input_columns = table.columns
table = self._filter(table)
if not set(input_columns) <= set(table.columns):
filtered_table = self._filter(table)
if not set(input_columns) <= set(filtered_table.columns):
raise ValueError(f"Filter must not remove columns.")

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

def _filter(self, table: Table) -> Table:
raise NotImplementedError()
37 changes: 20 additions & 17 deletions phenex/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,38 @@ def join(self, other: "PhenexTable", *args, domains=None, **kwargs):
return type(self)(self.table.join(other.table, *args, **kwargs))

# Do an autojoin by finding a path from the left to the right table and sequentially joining as necessary
joined_table = current_table = self
for next_table_class_name in self._find_path(other):
next_table = [
# joined table is the sequentially joined table
# current table is the table for the left join in the current iteration
joined_table = current_left_table = self
for right_table_class_name in self._find_path(other):
# get the next right table
right_table_search_results = [
v
for k, v in domains.items()
if v.__class__.__name__ == next_table_class_name
if v.__class__.__name__ == right_table_class_name
]
if len(next_table) != 1:
if len(right_table_search_results) != 1:
raise ValueError(
f"Unable to find unqiue {next_table_class_name} required to join {other.__class__.__name__}"
f"Unable to find unqiue {right_table_class_name} required to join {other.__class__.__name__}"
)

next_table = next_table[0]
right_table = right_table_search_results[0]
print(
f"Joining : {current_table.__class__.__name__} to {next_table.__class__.__name__}"
f"\tJoining : {current_left_table.__class__.__name__} to {right_table.__class__.__name__}"
)

# join keys are defined by the left table; in theory should enforce symmetry
join_keys = current_table.JOIN_KEYS[next_table_class_name]
columns = list(set(joined_table.columns + next_table.columns))
joined_table = joined_table.join(next_table, join_keys, **kwargs).select(
columns
join_keys = current_left_table.JOIN_KEYS[right_table_class_name]
columns = list(set(joined_table.columns + right_table.columns))
# subset columns, making sure to set type of table to the very left table (self)
joined_table = type(self)(
joined_table.join(right_table, join_keys, **kwargs).select(columns)
)
current_table = next_table
current_left_table = right_table
return joined_table

def mutate(self, *args, **kwargs):
return type(self)(self.table.mutate(*args, **kwargs), name=self.NAME_TABLE)

def _find_path(self, other):

start_name = self.__class__.__name__
Expand All @@ -129,9 +135,6 @@ def _find_path(self, other):
f"Cannot autojoin {start_name} --> {end_name}. Please specify join path in PATHS."
)

def select(self, *args, **kwargs):
return type(self)(self.table.select(*args, **kwargs), name=self.NAME_TABLE)

def filter(self, expr):
"""
Filter the table by an Ibis Expression or using a PhenExFilter.
Expand Down

0 comments on commit 5f8cf5f

Please sign in to comment.