Skip to content

aeromaps.models.impacts.life_cycle_assessment.utils.functions

Helper functions to support AeroMAPS life cycle assessment models.

tuple_to_varname

tuple_to_varname(items)

Convert a tuple or list of strings into a clean, Python-friendly variable name.

Parameters:

Name Type Description Default
items tuple or list

The tuple or list of strings to convert.

required

Returns:

Type Description
str

A cleaned variable name string.

Source code in aeromaps/models/impacts/life_cycle_assessment/utils/functions.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def tuple_to_varname(items):
    """
    Convert a tuple or list of strings into a clean, Python-friendly variable name.

    Parameters
    ----------
    items : tuple or list
        The tuple or list of strings to convert.

    Returns
    -------
    str
        A cleaned variable name string.
    """
    if isinstance(items, (list, tuple)):
        text = "__".join(items)  # join parts with double underscores
    else:
        text = str(items)

    # Lowercase everything
    text = text.lower()

    # Replace anything that’s not alphanumeric or underscore with underscore
    text = re.sub(r"[^0-9a-zA-Z_]+", "_", text)

    # Remove leading/trailing underscores and collapse multiple underscores
    text = re.sub(r"_+", "_", text).strip("_")

    # Add lca to variable
    text = "lca_" + text

    return text

is_not_nan

is_not_nan(x)

Return True if x is not NaN or None. Handles single values and arrays/lists.

Parameters:

Name Type Description Default
x any

The input to check.

required

Returns:

Type Description
bool

True if x is not NaN or None, False otherwise.

Source code in aeromaps/models/impacts/life_cycle_assessment/utils/functions.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def is_not_nan(x):
    """
    Return True if x is not NaN or None. Handles single values and arrays/lists.

    Parameters
    ----------
    x : any
        The input to check.

    Returns
    -------
    bool
        True if x is not NaN or None, False otherwise.
    """
    if x is None:
        return False
    if isinstance(x, (float, int, np.number)):
        return not pd.isna(x)
    if isinstance(x, (pd.Series, np.ndarray, list, tuple)):
        return pd.notna(np.asarray(x)).any()
    return True

compute_param_length

compute_param_length(params)

Compute the length of parameter values, ensuring consistency across all parameters.

Parameters:

Name Type Description Default
params Dict[str, ListOrScalar]

Dictionary of parameter names and their corresponding values.

required

Returns:

Type Description
int

The length of the parameter values.

Source code in aeromaps/models/impacts/life_cycle_assessment/utils/functions.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def compute_param_length(params):
    """
    Compute the length of parameter values, ensuring consistency across all parameters.

    Parameters
    ----------
    params : Dict[str, ListOrScalar]
        Dictionary of parameter names and their corresponding values.

    Returns
    -------
    int
        The length of the parameter values.
    """
    # Check length of parameter values
    param_length = 1
    for key, val in params.items():
        if isinstance(val, (list, np.ndarray)):
            if param_length == 1:
                param_length = len(val)
            elif param_length != len(val):
                raise Exception("Parameters should be a single value or a list of same number of values")
    return param_length