Skip to content

ValueFilter

ValueFilter

Bases: Filter

ValueFilter filters events in an PhenexTable based on a specified value range.

Attributes:

Name Type Description
min Optional[Value]

Minimum value required to pass through the filter.

max Optional[Value]

Maximum value required to pass through the filter.

column_name Optional[str]

The column name to which the value range should be applied. Default to VALUE, which is the default name of the value column in PhenotypeTable's.

Methods:

Name Description
filter

Filters the given PhenexTable based on the range of values specified by the min and max attributes. See Filter.

Source code in phenex/filters/value_filter.py
class ValueFilter(Filter):
    """
    ValueFilter filters events in an PhenexTable based on a specified value range.

    Attributes:
        min (Optional[Value]): Minimum value required to pass through the filter.
        max (Optional[Value]): Maximum value required to pass through the filter.
        column_name (Optional[str]): The column name to which the value range should be applied. Default to VALUE, which is the default name of the value column in PhenotypeTable's.

    Methods:
        filter: Filters the given PhenexTable based on the range of values specified by the min and max attributes. See Filter.
    """

    def __init__(
        self,
        min: Optional[Value] = None,
        max: Optional[Value] = None,
        column_name: Optional[str] = "VALUE",
    ):
        if min is not None:
            assert min.operator in [
                ">",
                ">=",
            ], f"min operator must be > or >=, not {min.operator}"
        if max is not None:
            assert max.operator in [
                "<",
                "<=",
            ], f"max operator must be > or >=, not {max.operator}"
        if max is not None and min is not None:
            assert min.value <= max.value, f"min must be less than or equal to max"
        self.min = min
        self.max = max
        self.column_name = column_name
        super(ValueFilter, self).__init__()

    def _filter(self, table: PhenexTable) -> PhenexTable:

        conditions = []
        value_column = getattr(table, self.column_name)
        if self.min is not None:
            if self.min.operator == ">":
                conditions.append(value_column > self.min.value)
            elif self.min.operator == ">=":
                conditions.append(value_column >= self.min.value)
            else:
                raise ValueError("Operator for min days be > or >=")
        if self.max is not None:
            if self.max.operator == "<":
                conditions.append(value_column < self.max.value)
            elif self.max.operator == "<=":
                conditions.append(value_column <= self.max.value)
            else:
                raise ValueError("Operator for max days be < or <=")
        if conditions:
            table = table.filter(conditions)
        return table

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