Skip to content

Filter

Filters operate on single tables and return these tables with rows removed. Filters are generally used within a Phenotype as a subquery. Filters know about their dependencies but cannot trigger recursive execution. Fitlers can add columns but may not remove columns. All classes in the filters module should subclass this class. This class is not intended to be used directly, but should be subclassed to create useful filters. Subclasses must implement the _filter method.

Methods:

Name Description
filter

Filters the given table according to the rules of the Filter.

autojoin_filter

Like filter, but magically joins needed tables in order to filter table, for instance, when filtering a CONDITION_OCCURRENCE-like table based on contextual information in a VISIT-like table.

Source code in phenex/filters/filter.py
class Filter:
    """
    Filters operate on single tables and return these tables with rows removed. Filters are generally used within a Phenotype as a subquery. Filters know about their dependencies but cannot trigger recursive execution. Fitlers can add columns but may not remove columns. All classes in the filters module should subclass this class. This class is not intended to be used directly, but should be subclassed to create useful filters. Subclasses must implement the _filter method.

    Methods:
        filter: Filters the given table according to the rules of the Filter.

        autojoin_filter: Like filter, but magically joins needed tables in order to filter table, for instance, when filtering a CONDITION_OCCURRENCE-like table based on contextual information in a VISIT-like table.
    """

    def __init__(self):
        pass

    def filter(self, table: PhenexTable) -> PhenexTable:
        """
        Filters the given table according to the rules of the Filter.

        Args:
            table (PhenexTable): The table to be filtered.

        Returns:
            PhenexTable: The filtered table. The returned table has the exact same schema as the input table but has rows removed.
        """
        input_columns = table.columns
        filtered_table = self._filter(table)
        if not set(input_columns) <= set(filtered_table.columns):
            raise ValueError(f"Filter must not remove columns.")

        filtered_table = filtered_table.select(input_columns)
        if isinstance(table, PhenexTable):
            return type(table)(filtered_table)
        else:
            return filtered_table

    def _filter(self, table: Table) -> Table:
        """
        Performs the operations required to filter the table.
        """
        raise NotImplementedError()

    def autojoin_filter(
        self, table: "PhenexTable", tables: Optional[Dict[str, "PhenexTable"]] = None
    ) -> "PhenexTable":
        raise NotImplementedError()

    def __and__(self, other):
        return AndFilter(self, other)

    def __or__(self, other):
        return OrFilter(self, other)

    def __invert__(self):
        return NotFilter(self)

    def to_dict(self):
        return to_dict(self)

_filter(table)

Performs the operations required to filter the table.

Source code in phenex/filters/filter.py
def _filter(self, table: Table) -> Table:
    """
    Performs the operations required to filter the table.
    """
    raise NotImplementedError()

filter(table)

Filters the given table according to the rules of the Filter.

Parameters:

Name Type Description Default
table PhenexTable

The table to be filtered.

required

Returns:

Name Type Description
PhenexTable PhenexTable

The filtered table. The returned table has the exact same schema as the input table but has rows removed.

Source code in phenex/filters/filter.py
def filter(self, table: PhenexTable) -> PhenexTable:
    """
    Filters the given table according to the rules of the Filter.

    Args:
        table (PhenexTable): The table to be filtered.

    Returns:
        PhenexTable: The filtered table. The returned table has the exact same schema as the input table but has rows removed.
    """
    input_columns = table.columns
    filtered_table = self._filter(table)
    if not set(input_columns) <= set(filtered_table.columns):
        raise ValueError(f"Filter must not remove columns.")

    filtered_table = filtered_table.select(input_columns)
    if isinstance(table, PhenexTable):
        return type(table)(filtered_table)
    else:
        return filtered_table