Skip to content

Utilities

variables_as_yaml(input_variables, output_variables, file=None)

Returns and optionally saves YAML formatted string defining the in- and output variables.

Parameters:

Name Type Description Default
input_variables list[InputVariable]

List of input variables.

required
output_variables list[OutputVariable]

List of output variables.

required
file Union[str, PathLike]

If not None, YAML formatted string is saved to given file path.

None

Returns:

Type Description
str

YAML formatted string defining the in- and output variables.

Source code in lume_model/utils.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def variables_as_yaml(
        input_variables: list[InputVariable],
        output_variables: list[OutputVariable],
        file: Union[str, os.PathLike] = None,
) -> str:
    """Returns and optionally saves YAML formatted string defining the in- and output variables.

    Args:
        input_variables: List of input variables.
        output_variables: List of output variables.
        file: If not None, YAML formatted string is saved to given file path.

    Returns:
        YAML formatted string defining the in- and output variables.
    """
    for variables in [input_variables, output_variables]:
        verify_unique_variable_names(variables)
    v = {"input_variables": [var.dict() for var in input_variables],
         "output_variables": [var.dict() for var in output_variables]}
    s = yaml.dump(serialize_variables(v), default_flow_style=None, sort_keys=False)
    if file is not None:
        with open(file, "w") as f:
            f.write(s)
    return s

variables_from_dict(config)

Parses given config and returns in- and output variable lists.

Parameters:

Name Type Description Default
config dict

Variable configuration.

required

Returns:

Type Description
tuple[list[InputVariable], list[OutputVariable]]

In- and output variable lists.

Source code in lume_model/utils.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def variables_from_dict(config: dict) -> tuple[list[InputVariable], list[OutputVariable]]:
    """Parses given config and returns in- and output variable lists.

    Args:
        config: Variable configuration.

    Returns:
        In- and output variable lists.
    """
    input_variables, output_variables = [], []
    for key, value in {**config}.items():
        if key in ["input_variables", "output_variables"]:
            for var in value:
                variable_type = var.get("variable_type", var.get("type"))
                if variable_type == "scalar":
                    if key == "input_variables":
                        input_variables.append(ScalarInputVariable(**var))
                    elif key == "output_variables":
                        output_variables.append(ScalarOutputVariable(**var))
                elif variable_type in ["array", "image"]:
                    raise ValueError(f"Parsing of variable type {variable_type} is not yet implemented.")
                else:
                    raise ValueError(f"Unknown variable type {variable_type}.")
    for variables in [input_variables, output_variables]:
        verify_unique_variable_names(variables)
    return input_variables, output_variables

variables_from_yaml(yaml_obj)

Parses YAML object and returns in- and output variable lists.

Parameters:

Name Type Description Default
yaml_obj Union[str, PathLike]

YAML formatted string or file path.

required

Returns:

Type Description
tuple[list[InputVariable], list[OutputVariable]]

In- and output variable lists.

Source code in lume_model/utils.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def variables_from_yaml(yaml_obj: Union[str, os.PathLike]) -> tuple[list[InputVariable], list[OutputVariable]]:
    """Parses YAML object and returns in- and output variable lists.

    Args:
        yaml_obj: YAML formatted string or file path.

    Returns:
        In- and output variable lists.
    """
    if os.path.exists(yaml_obj):
        with open(yaml_obj) as f:
            yaml_str = f.read()
    else:
        yaml_str = yaml_obj
    config = deserialize_variables(yaml.safe_load(yaml_str))
    return variables_from_dict(config)