Skip to content

ValueFilter

ValueFilter

Bases: Filter

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

Parameters:

Name Type Description Default
min_value Optional[Union[Value, GreaterThan, GreaterThanOrEqualTo]]

Minimum value required to pass through the filter.

None
max_value Optional[Union[Value, LessThan, LessThanOrEqualTo]]

Maximum value required to pass through the filter.

None
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.

'VALUE'

Methods:

Name Description
filter

Filters the given PhenexTable based on the range of values specified by the min_value and max_value 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.

    Parameters:
        min_value: Minimum value required to pass through the filter.
        max_value: Maximum value required to pass through the filter.
        column_name: 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_value and max_value attributes. See Filter.
    """

    def __init__(
        self,
        min_value: Optional[Union[Value, GreaterThan, GreaterThanOrEqualTo]] = None,
        max_value: Optional[Union[Value, LessThan, LessThanOrEqualTo]] = None,
        column_name: Optional[str] = "VALUE",
    ):
        if min_value is not None:
            assert min_value.operator in [
                ">",
                ">=",
            ], f"min_value operator must be > or >=, not {min_value.operator}"
        if max_value is not None:
            assert max_value.operator in [
                "<",
                "<=",
            ], f"max_value operator must be > or >=, not {max_value.operator}"
        if max_value is not None and min_value is not None:
            assert (
                min_value.value <= max_value.value
            ), f"min_value must be less than or equal to max_value"
        self.min_value = min_value
        self.max_value = max_value
        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_value is not None:
            if self.min_value.operator == ">":
                conditions.append(value_column > self.min_value.value)
            elif self.min_value.operator == ">=":
                conditions.append(value_column >= self.min_value.value)
            else:
                raise ValueError("Operator for min_value days be > or >=")
        if self.max_value is not None:
            if self.max_value.operator == "<":
                conditions.append(value_column < self.max_value.value)
            elif self.max_value.operator == "<=":
                conditions.append(value_column <= self.max_value.value)
            else:
                raise ValueError("Operator for max_value 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