Skip to content

config_utils

non_schema_checks(config)

Do additional voluntary checks for config consistency beyond what the schema validates.

Source code in emodpy_malaria/utils/config_utils.py
def non_schema_checks(config):
    """
    Do additional voluntary checks for config consistency beyond what the schema validates.
    """
    p = config.parameters
    if hasattr(p, 'Simulation_Type') and p.Simulation_Type != 'MALARIA_SIM':
        raise ValueError(f"Simulation_Type must be 'MALARIA_SIM', got '{p.Simulation_Type}'")
    return

validate_allele_combo(species_params, allele_combo)

Validate that a user provided an acceptable allele_combo where it is a two dimensional array of strings and the inner array has two elements where each element is an allele of the same gene.

Source code in emodpy_malaria/utils/config_utils.py
def validate_allele_combo(species_params, allele_combo):
    """
    Validate that a user provided an acceptable allele_combo
    where it is a two dimensional array of strings and the inner array has
    two elements where each element is an allele of the same gene.
    """
    if len(allele_combo) == 0:
        raise ValueError("allele_combo must define some alleles to target")

    for combo in allele_combo:
        if len(combo) != 2:
            raise ValueError(
                "Each combo in allele_combo must have two values - one for each chromosome, '*' is acceptable. \n")

    allele_names = []
    allele_names_in_combo = []
    for gene in species_params.Genes:
        for allele in gene["Alleles"]:
            allele_names.append(allele["Name"])

    for combo in allele_combo:
        for allele_name in combo:
            if allele_name != "X" and allele_name != "Y" and allele_name != "*":
                allele_names_in_combo.append(allele_name)

    for alnic in allele_names_in_combo:
        if alnic not in allele_names:
            raise ValueError(f"Allele name {alnic} submitted in one of the allele_combos is not found"
                             f" in the Genes parameter for {species_params.Name}.\n")

    return

validate_bins(bins, param_name, min_value=None, max_value=None)

Validate that a list of bin edges is in strictly ascending order and within optional bounds.

Parameters:

Name Type Description Default
bins list

List of numeric bin edges.

required
param_name str

Name of the parameter (for error messages).

required
min_value float

If set, all values must be >= this.

None
max_value float

If set, all values must be <= this.

None

Returns:

Type Description
list

The validated list of bins.

Raises:

Type Description
ValueError

If bins are not in ascending order or out of bounds.

Source code in emodpy_malaria/utils/config_utils.py
def validate_bins(bins: list, param_name: str, min_value: float = None, max_value: float = None) -> list:
    """
    Validate that a list of bin edges is in strictly ascending order and within optional bounds.

    Args:
        bins (list): List of numeric bin edges.
        param_name (str): Name of the parameter (for error messages).
        min_value (float): If set, all values must be >= this.
        max_value (float): If set, all values must be <= this.

    Returns:
        The validated list of bins.

    Raises:
        ValueError: If bins are not in ascending order or out of bounds.
    """
    if not bins:
        return bins
    for i in range(1, len(bins)):
        if bins[i] <= bins[i - 1]:
            raise ValueError(
                f"{param_name} must be in strictly ascending order, "
                f"but got {bins[i]} at index {i} after {bins[i - 1]} at index {i - 1}.")
    if min_value is not None and bins[0] < min_value:
        raise ValueError(
            f"{param_name} values must be >= {min_value}, but got {bins[0]}.")
    if max_value is not None and bins[-1] > max_value:
        raise ValueError(
            f"{param_name} values must be <= {max_value}, but got {bins[-1]}.")
    return bins