Skip to content

SexPhenotype

Bases: CategoricalPhenotype

SexPhenotype represents a sex-based phenotype. It returns the sex of individuals in the VALUE column and optionally filters based on identified sex. DATE is not defined for SexPhenotype.

Parameters:

Name Type Description Default
name str

Name of the phenotype, default is 'sex'.

'sex'
domain str

Domain of the phenotype, default is 'PERSON'.

'PERSON'
allowed_values Optional[List[Union[str, int, float]]]

List of allowed values for the categorical variable.

None
column_name

Name of the column containing the required categorical variable. Default is 'SEX'.

'SEX'

Examples:

Example: Return the recorded sex of all patients.

from phenex.phenotypes import SexPhenotype
sex = SexPhenotype()

Example: Extract all male patients from the database.

from phenex.phenotypes import SexPhenotype
sex = SexPhenotype(
    allowed_values=['M'],
    column_name='GENDER_SOURCE_VALUE'
    )

Source code in phenex/phenotypes/sex_phenotype.py
class SexPhenotype(CategoricalPhenotype):
    """
    SexPhenotype represents a sex-based phenotype. It returns the sex of individuals in the VALUE column and optionally filters based on identified sex. DATE is not defined for SexPhenotype.

    Parameters:
        name: Name of the phenotype, default is 'sex'.
        domain: Domain of the phenotype, default is 'PERSON'.
        allowed_values: List of allowed values for the categorical variable.
        column_name: Name of the column containing the required categorical variable. Default is 'SEX'.

    Examples:

    Example: Return the recorded sex of all patients.
    ```python
    from phenex.phenotypes import SexPhenotype
    sex = SexPhenotype()
    ```

    Example: Extract all male patients from the database.
    ```python
    from phenex.phenotypes import SexPhenotype
    sex = SexPhenotype(
        allowed_values=['M'],
        column_name='GENDER_SOURCE_VALUE'
        )
    ```
    """

    def __init__(
        self,
        name: str = "sex",
        allowed_values: Optional[List[Union[str, int, float]]] = None,
        domain: str = "PERSON",
        column_name="SEX",
        **kwargs
    ):
        super(SexPhenotype, self).__init__(
            name=name,
            allowed_values=allowed_values,
            domain=domain,
            column_name=column_name,
            **kwargs
        )

namespaced_table property

A PhenotypeTable has generic column names 'person_id', 'boolean', 'event_date', and 'value'. The namespaced_table appends the phenotype name to all of these columns. This is useful when joining multiple phenotype tables together.

Returns:

Name Type Description
table Table

The namespaced table for the current phenotype.

execute(tables)

Executes the phenotype computation for the current object and its children. This method recursively iterates over the children of the current object and calls their execute method if their table attribute is None.

Parameters:

Name Type Description Default
tables Dict[str, PhenexTable]

A dictionary mapping table names to PhenexTable objects. See phenex.mappers.DomainsDictionary.get_mapped_tables().

required

Returns:

Name Type Description
table PhenotypeTable

The resulting phenotype table containing the required columns. The PhenotypeTable will contain the columns: PERSON_ID, EVENT_DATE, VALUE. DATE is determined by the return_date parameter. VALUE is different for each phenotype. For example, AgePhenotype will return the age in the VALUE column. A MeasurementPhenotype will return the observed value for the measurement. See the specific phenotype of interest to understand more.

Source code in phenex/phenotypes/phenotype.py
def execute(self, tables: Dict[str, Table]) -> PhenotypeTable:
    """
    Executes the phenotype computation for the current object and its children. This method recursively iterates over the children of the current object and calls their execute method if their table attribute is None.

    Args:
        tables (Dict[str, PhenexTable]): A dictionary mapping table names to PhenexTable objects. See phenex.mappers.DomainsDictionary.get_mapped_tables().

    Returns:
        table (PhenotypeTable): The resulting phenotype table containing the required columns. The PhenotypeTable will contain the columns: PERSON_ID, EVENT_DATE, VALUE. DATE is determined by the return_date parameter. VALUE is different for each phenotype. For example, AgePhenotype will return the age in the VALUE column. A MeasurementPhenotype will return the observed value for the measurement. See the specific phenotype of interest to understand more.
    """
    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}'..."
            )
            child.execute(tables)
        else:
            logger.debug(
                f"Phenotype {self.name}: skipping already computed child phenotype '{child.name}'."
            )

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

    if not set(PHENOTYPE_TABLE_COLUMNS) <= set(table.columns):
        raise ValueError(
            f"Phenotype {self.name} must return columns {PHENOTYPE_TABLE_COLUMNS}. Found {table.columns}."
        )

    self.table = table.select(PHENOTYPE_TABLE_COLUMNS)
    # for some reason, having NULL datatype screws up writing the table to disk; here we make explicit cast
    if type(self.table.schema()["VALUE"]) == ibis.expr.datatypes.core.Null:
        self.table = self.table.cast({"VALUE": "float64"})

    assert is_phenex_phenotype_table(self.table)
    logger.info(f"Phenotype '{self.name}': execution completed.")
    return self.table