Skip to content

Utils

Contains all pheval utility methods

download_hgnc_data()

Download latest HGNC complete set file.

Source code in src/pheval/utils/utils.py
126
127
128
def download_hgnc_data() -> None:
    """Download latest HGNC complete set file."""
    _download_file(HGNC_URL, RESOURCES_DIR / "hgnc_complete_set.txt")

download_mondo_mapping()

Download latest MONDO SSSOM mapping file.

Source code in src/pheval/utils/utils.py
121
122
123
def download_mondo_mapping() -> None:
    """Download latest MONDO SSSOM mapping file."""
    _download_file(MONDO_URL, RESOURCES_DIR / "mondo.sssom.tsv")

get_resource_timestamp(file_name)

Return the ISO timestamp when the resource file was last updated. Args: file_name (str): The file name.

Source code in src/pheval/utils/utils.py
131
132
133
134
135
136
137
138
139
140
def get_resource_timestamp(file_name: str) -> str | None:
    """
    Return the ISO timestamp when the resource file was last updated.
    Args:
        file_name (str): The file name.
    """
    if METADATA_PATH.exists():
        with open(METADATA_PATH) as f:
            return json.load(f).get(file_name)
    return None

rand(df, min_num, max_num, scramble_factor)

Numeric scrambling Args: df (pd.DataFrame): dataframe records min_num (int): min value from this records max_num (int): max value from this records scramble_factor (float): scramble factor scalar Returns: float: randomized number

Source code in src/pheval/utils/utils.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def rand(df: pd.DataFrame, min_num: int, max_num: int, scramble_factor: float) -> float:
    """
    Numeric scrambling
    Args:
        df (pd.DataFrame): dataframe records
        min_num (int): min value from this records
        max_num (int): max value from this records
        scramble_factor (float): scramble factor scalar
    Returns:
        float: randomized number
    """
    try:
        return df + (random.uniform(min_num, max_num) * scramble_factor)
    except TypeError as err:
        logger.error(df, exc_info=err)
        return df

semsim_scramble(input, output, columns_to_be_scrambled, scramble_factor=0.5)

Scrambles semantic similarity profile with a magnitude between 0 and 1 (scramble_factor: 0 means no scrambling and 1 means complete randomisation). It then randomises the above scores with a degree of the scramble_factor and returns a scrambles pandas dataframe. Args: input (Path): scramble_factor (float) scalar scramble factor columns_to_be_scrambled (List[str]): columns that will be scrambled in semsim file (e.g. jaccard_similarity). output (Path) Returns: pd.Dataframe: scrambled dataframe

Source code in src/pheval/utils/utils.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def semsim_scramble(
    input: Path,
    output: Path,
    columns_to_be_scrambled: list[str],
    scramble_factor: float = 0.5,
) -> pd.DataFrame:
    """
    Scrambles semantic similarity profile with a magnitude between 0 and 1 (scramble_factor:
    0 means no scrambling and 1 means complete randomisation).
    It then randomises the above scores with a degree of the scramble_factor
    and returns a scrambles pandas dataframe.
        Args:
              input (Path):
              scramble_factor (float) scalar scramble factor
              columns_to_be_scrambled (List[str]):
              columns that will be scrambled in semsim file (e.g. jaccard_similarity).
              output (Path)
        Returns:
            pd.Dataframe: scrambled dataframe
    """
    semsim = pd.read_csv(input, sep="\t")
    dataframe = semsim_scramble_df(semsim, columns_to_be_scrambled, scramble_factor)
    dataframe.to_csv(output, sep="\t", index=False)

semsim_scramble_df(dataframe, columns_to_be_scrambled, scramble_factor)

scramble_semsim_df Args: dataframe (pd.DataFrame): dataframe that contains semsim profile scramble_factor (float) scalar scramble factor columns_to_be_scrambled (List[str]): Returns: pd.Dataframe: scrambled dataframe

Source code in src/pheval/utils/utils.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def semsim_scramble_df(
    dataframe: pd.DataFrame,
    columns_to_be_scrambled: list[str],
    scramble_factor: float,
) -> pd.DataFrame:
    """scramble_semsim_df
    Args:
        dataframe (pd.DataFrame): dataframe that contains semsim profile
        scramble_factor (float) scalar scramble factor
        columns_to_be_scrambled (List[str]):
    Returns:
        pd.Dataframe: scrambled dataframe
    """
    for col in columns_to_be_scrambled:
        min_num = dataframe[col].min()
        max_num = dataframe[col].max()
        dataframe[col] = dataframe[col].apply(rand, args=(min_num, max_num, scramble_factor))
    return dataframe