Skip to content

elapid.types

Custom maxent and typing data types.

to_iterable(var)

Checks and converts variables to an iterable type.

Parameters:

Name Type Description Default
var Any

the input variable to check and convert.

required

Returns:

Type Description
list

var wrapped in a list.

Source code in elapid/types.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def to_iterable(var: Any) -> list:
    """Checks and converts variables to an iterable type.

    Args:
        var: the input variable to check and convert.

    Returns:
        `var` wrapped in a list.
    """
    if not hasattr(var, "__iter__"):
        return [var]
    elif isinstance(var, (str)):
        return [var]
    else:
        return var

validate_boolean(var)

Evaluates whether an argument is boolean True/False

Parameters:

Name Type Description Default
var Any

the input argument to validate

required

Returns:

Name Type Description
var bool

the value if it passes validation

Raises:

Type Description
AssertionError

var was not boolean

Source code in elapid/types.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def validate_boolean(var: Any) -> bool:
    """Evaluates whether an argument is boolean True/False

    Args:
        var: the input argument to validate

    Returns:
        var: the value if it passes validation

    Raises:
        AssertionError: `var` was not boolean
    """
    assert isinstance(var, bool), "Argument must be True/False"
    return var

validate_feature_types(features)

Ensures the feature classes passed are maxent-legible

Parameters:

Name Type Description Default
features Union[str, list]

List or string that must be in ["linear", "quadratic", "product", "hinge", "threshold", "auto"] or string "lqphta"

required

Returns:

Name Type Description
valid_features list

List of formatted valid feature values

Source code in elapid/types.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def validate_feature_types(features: Union[str, list]) -> list:
    """Ensures the feature classes passed are maxent-legible

    Args:
        features: List or string that must be in ["linear", "quadratic", "product",
            "hinge", "threshold", "auto"] or string "lqphta"

    Returns:
        valid_features: List of formatted valid feature values
    """
    valid_list = get_feature_types(return_string=False)
    valid_string = get_feature_types(return_string=True)
    valid_features = list()

    # ensure the string features are valid, and translate to a standard feature list
    if type(features) is str:
        for feature in features:
            if feature == "a":
                return valid_list
            assert feature in valid_string, "Invalid feature passed: {}".format(feature)
            if feature == "l":
                valid_features.append("linear")
            elif feature == "q":
                valid_features.append("quadratic")
            elif feature == "p":
                valid_features.append("product")
            elif feature == "h":
                valid_features.append("hinge")
            elif feature == "t":
                valid_features.append("threshold")

    # or ensure the list features are valid
    elif type(features) is list:
        for feature in features:
            if feature == "auto":
                return valid_list
            assert feature in valid_list, "Invalid feature passed: {}".format(feature)
            valid_features.append(feature)

    return valid_features

validate_numeric_scalar(var)

Evaluates whether an argument is a single numeric value.

Parameters:

Name Type Description Default
var Any

the input argument to validate

required

Returns:

Name Type Description
var bool

the value if it passes validation

Raises:

Type Description
AssertionError

var was not numeric.

Source code in elapid/types.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def validate_numeric_scalar(var: Any) -> bool:
    """Evaluates whether an argument is a single numeric value.

    Args:
        var: the input argument to validate

    Returns:
        var: the value if it passes validation

    Raises:
        AssertionError: `var` was not numeric.
    """
    assert isinstance(var, (int, float)), "Argument must be single numeric value"
    return var