Module phc.easy.omics.genomic_test

Expand source code
import inspect
from enum import Enum
from typing import Optional

import pandas as pd
from phc.easy.auth import Auth
from phc.easy.frame import Frame
from phc.easy.omics.options.genomic_test import (
    GenomicTestStatus,
    GenomicTestType,
)
from phc.easy.abstract.paging_api_item import PagingApiItem, PagingApiOptions


class GenomicTestOptions(PagingApiOptions):
    """Options to pass to `/v1/genomics/projects/{project_id}/tests`"""

    patient_id: Optional[str]
    status: Optional[GenomicTestStatus]
    test_type: Optional[GenomicTestType]

    @staticmethod
    def transform(key, value):
        new_key = {"patient_id": "patientId", "test_type": "type"}.get(key, key)

        return (new_key, value)


class GenomicTest(PagingApiItem):
    @staticmethod
    def resource_path():
        return "genomics/projects/{project_id}/tests"

    @staticmethod
    def params_class():
        return GenomicTestOptions

    @staticmethod
    def transform_results(
        data_frame: pd.DataFrame, params: dict, **expand_args
    ):
        args = {
            **expand_args,
            "code_columns": [
                *expand_args.get("code_columns", []),
                "bodySite",
                "patient",
            ],
            "custom_columns": [
                *expand_args.get("custom_columns", []),
                Frame.codeable_like_column_expander("sourceFile"),
            ],
        }
        df = Frame.expand(data_frame, **args)

        if "sets" in df.columns:
            df = (
                pd.concat(
                    data_frame.apply(
                        lambda x: pd.DataFrame(
                            [{"index": x.name, **s} for s in x.sets]
                        ),
                        axis=1,
                    ).values
                )
                .join(df.drop(["sets"], axis=1), on="index", rsuffix=".test")
                .drop(["index"], axis=1)
                .reset_index(drop=True)
            )

        test_type = params.get("type", None)

        if test_type and len(df) > 0:
            # TODO: Remove when API fixed

            # NOTE: The API does not filter the returned sets because it is a
            # nested structure. Since it's not a boatload of information, we opt
            # to filter client-side for now.
            return df[df.setType == test_type].reset_index(drop=True)

        return df

    @classmethod
    def get_data_frame(
        cls,
        patient_id: Optional[str] = None,
        status: Optional[GenomicTestStatus] = GenomicTestStatus.ACTIVE,
        test_type: Optional[GenomicTestType] = None,
        all_results: bool = False,
        auth_args: Auth = Auth.shared(),
        max_pages: Optional[int] = None,
        page_size: Optional[int] = None,
        log: bool = False,
        ignore_cache: bool = False,
        **kw_args,
    ):
        """Execute a request for genomic tests

        ## Parameters

        Query: `phc.easy.omics.options.genomic_test.GenomicTestOptions`

        Execution: `phc.easy.query.Query.execute_paging_api`

        Expansion: `phc.easy.frame.Frame.expand`

        NOTE: `test_type` is translated to `type` as a parameter
        """
        df = super().get_data_frame(
            **kw_args, **cls._get_current_args(inspect.currentframe(), locals())
        )

        return df

Classes

class GenomicTest
Expand source code
class GenomicTest(PagingApiItem):
    @staticmethod
    def resource_path():
        return "genomics/projects/{project_id}/tests"

    @staticmethod
    def params_class():
        return GenomicTestOptions

    @staticmethod
    def transform_results(
        data_frame: pd.DataFrame, params: dict, **expand_args
    ):
        args = {
            **expand_args,
            "code_columns": [
                *expand_args.get("code_columns", []),
                "bodySite",
                "patient",
            ],
            "custom_columns": [
                *expand_args.get("custom_columns", []),
                Frame.codeable_like_column_expander("sourceFile"),
            ],
        }
        df = Frame.expand(data_frame, **args)

        if "sets" in df.columns:
            df = (
                pd.concat(
                    data_frame.apply(
                        lambda x: pd.DataFrame(
                            [{"index": x.name, **s} for s in x.sets]
                        ),
                        axis=1,
                    ).values
                )
                .join(df.drop(["sets"], axis=1), on="index", rsuffix=".test")
                .drop(["index"], axis=1)
                .reset_index(drop=True)
            )

        test_type = params.get("type", None)

        if test_type and len(df) > 0:
            # TODO: Remove when API fixed

            # NOTE: The API does not filter the returned sets because it is a
            # nested structure. Since it's not a boatload of information, we opt
            # to filter client-side for now.
            return df[df.setType == test_type].reset_index(drop=True)

        return df

    @classmethod
    def get_data_frame(
        cls,
        patient_id: Optional[str] = None,
        status: Optional[GenomicTestStatus] = GenomicTestStatus.ACTIVE,
        test_type: Optional[GenomicTestType] = None,
        all_results: bool = False,
        auth_args: Auth = Auth.shared(),
        max_pages: Optional[int] = None,
        page_size: Optional[int] = None,
        log: bool = False,
        ignore_cache: bool = False,
        **kw_args,
    ):
        """Execute a request for genomic tests

        ## Parameters

        Query: `phc.easy.omics.options.genomic_test.GenomicTestOptions`

        Execution: `phc.easy.query.Query.execute_paging_api`

        Expansion: `phc.easy.frame.Frame.expand`

        NOTE: `test_type` is translated to `type` as a parameter
        """
        df = super().get_data_frame(
            **kw_args, **cls._get_current_args(inspect.currentframe(), locals())
        )

        return df

Ancestors

Static methods

def get_data_frame(patient_id: Optional[str] = None, status: Optional[GenomicTestStatus] = GenomicTestStatus.ACTIVE, test_type: Optional[GenomicTestType] = None, all_results: bool = False, auth_args: Auth = <phc.easy.auth.Auth object>, max_pages: Optional[int] = None, page_size: Optional[int] = None, log: bool = False, ignore_cache: bool = False, **kw_args)

Execute a request for genomic tests

Parameters

Query: phc.easy.omics.options.genomic_test.GenomicTestOptions

Execution: Query.execute_paging_api()

Expansion: Frame.expand()

NOTE: test_type is translated to type as a parameter

Expand source code
@classmethod
def get_data_frame(
    cls,
    patient_id: Optional[str] = None,
    status: Optional[GenomicTestStatus] = GenomicTestStatus.ACTIVE,
    test_type: Optional[GenomicTestType] = None,
    all_results: bool = False,
    auth_args: Auth = Auth.shared(),
    max_pages: Optional[int] = None,
    page_size: Optional[int] = None,
    log: bool = False,
    ignore_cache: bool = False,
    **kw_args,
):
    """Execute a request for genomic tests

    ## Parameters

    Query: `phc.easy.omics.options.genomic_test.GenomicTestOptions`

    Execution: `phc.easy.query.Query.execute_paging_api`

    Expansion: `phc.easy.frame.Frame.expand`

    NOTE: `test_type` is translated to `type` as a parameter
    """
    df = super().get_data_frame(
        **kw_args, **cls._get_current_args(inspect.currentframe(), locals())
    )

    return df
def params_class()

Inherited from: PagingApiItem.params_class

Returns a pydantic type that validates and transforms the params with dict()

Expand source code
@staticmethod
def params_class():
    return GenomicTestOptions
def process_params(params: dict) ‑> dict

Inherited from: PagingApiItem.process_params

Validates and transforms the API query parameters

def resource_path()

Inherited from: PagingApiItem.resource_path

Returns the API url name for retrieval

Expand source code
@staticmethod
def resource_path():
    return "genomics/projects/{project_id}/tests"
def transform_results(data_frame: pandas.core.frame.DataFrame, params: dict, **expand_args)

Inherited from: PagingApiItem.transform_results

Transform data frame batch

Expand source code
@staticmethod
def transform_results(
    data_frame: pd.DataFrame, params: dict, **expand_args
):
    args = {
        **expand_args,
        "code_columns": [
            *expand_args.get("code_columns", []),
            "bodySite",
            "patient",
        ],
        "custom_columns": [
            *expand_args.get("custom_columns", []),
            Frame.codeable_like_column_expander("sourceFile"),
        ],
    }
    df = Frame.expand(data_frame, **args)

    if "sets" in df.columns:
        df = (
            pd.concat(
                data_frame.apply(
                    lambda x: pd.DataFrame(
                        [{"index": x.name, **s} for s in x.sets]
                    ),
                    axis=1,
                ).values
            )
            .join(df.drop(["sets"], axis=1), on="index", rsuffix=".test")
            .drop(["index"], axis=1)
            .reset_index(drop=True)
        )

    test_type = params.get("type", None)

    if test_type and len(df) > 0:
        # TODO: Remove when API fixed

        # NOTE: The API does not filter the returned sets because it is a
        # nested structure. Since it's not a boatload of information, we opt
        # to filter client-side for now.
        return df[df.setType == test_type].reset_index(drop=True)

    return df
class GenomicTestOptions (**data: Any)

Options to pass to /v1/genomics/projects/{project_id}/tests

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class GenomicTestOptions(PagingApiOptions):
    """Options to pass to `/v1/genomics/projects/{project_id}/tests`"""

    patient_id: Optional[str]
    status: Optional[GenomicTestStatus]
    test_type: Optional[GenomicTestType]

    @staticmethod
    def transform(key, value):
        new_key = {"patient_id": "patientId", "test_type": "type"}.get(key, key)

        return (new_key, value)

Ancestors

Class variables

var patient_id : Optional[str]
var status : Optional[GenomicTestStatus]
var test_type : Optional[GenomicTestType]

Static methods

def transform(key, value)
Expand source code
@staticmethod
def transform(key, value):
    new_key = {"patient_id": "patientId", "test_type": "type"}.get(key, key)

    return (new_key, value)

Methods

def dict(self)

Inherited from: PagingApiOptions.dict

Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.