Skip to content

aeromaps.models.templates

Template classes to implement models in AeroMAPS.

CustomTemplate

CustomTemplate(name='custom_template', configuration_file=None, *args, **kwargs)

Bases: AeroMAPSModel

AeroMAPS model template to be used as a starting point for custom models. The compute() method of custom models does not need to explicitly define inputs, and outputs can be of varying size. However, inputs and outputs must be defined in the init method using input_names and output_names dictionaries.

Parameters:

Name Type Description Default
name str

Name of the model instance ('custom_template' by default).

'custom_template'
configuration_file str

Path to a configuration file (YAML) that contains user-defined input and output names and default values.

None

Attributes:

Name Type Description
input_names dict

Dictionary of input variable names populated at model initialisation before MDA chain creation.

output_names dict

Dictionary of output variable names populated at model initialisation before MDA chain creation.

Source code in aeromaps/models/templates.py
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
62
63
64
65
def __init__(
    self,
    name: str = "custom_template",
    configuration_file: str = None,
    *args,
    **kwargs,
):
    super().__init__(
        name=name,
        model_type="custom",  # inputs/outputs are defined in __init__ rather than auto generated from compute() signature
        *args,
        **kwargs,
    )

    # Fill in explicit inputs and outputs with default values
    self.input_names = {"input1": np.array([0.0]), "input2": np.array([0.0])}
    self.output_names = {"output1": np.array([0.0]), "output2": np.array([0.0])}

    # Read configuration file that contains user-defined data.
    self.configuration_file = configuration_file
    self.configuration_data = read_yaml_file(
        configuration_file
    )  # read and process configuration file

    # Update inputs and outputs (name + default value) with user-defined data
    if "input_names" in self.configuration_data:
        for key, val in self.configuration_data["input_names"].items():
            self.input_names[key] = val
    if "output_names" in self.configuration_data:
        for key, val in self.configuration_data["output_names"].items():
            self.output_names[key] = val

compute

compute(input_data)

Compute the outputs based on the inputs.

Parameters:

Name Type Description Default
input_data

Dictionary containing all input data required for the computation, completed at model instantiation with information from yaml files and outputs of other models.

required

Returns:

Type Description
output_data

Dictionary containing all output data resulting from the computation. Contains outputs defined during model instantiation.

Source code in aeromaps/models/templates.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def compute(self, input_data) -> dict:
    """
    Compute the outputs based on the inputs.

    Parameters
    ----------
    input_data
        Dictionary containing all input data required for the computation, completed at model instantiation with information from yaml files and outputs of other models.

    Returns
    -------
    output_data
        Dictionary containing all output data resulting from the computation. Contains outputs defined during model instantiation.

    """
    # Get input data
    input1 = input_data["input1"]
    input2 = input_data["input2"]

    # Initialize output data
    output_data = {}

    # perform computation on explicit inputs and add to output data
    output_1 = input1 + input2
    output_2 = input1 * input2
    output_data["output_1"] = output_1
    output_data["output_1"] = output_2

    # perform computation on any input
    for i, input in input_data:
        output = input_data[input] ** 2  # any computation
        output_data[f"output_{i}"] = output  # add to output data

    # return output data
    return {name: output_data[name] for name in self.output_names}

AutoTemplate

AutoTemplate(name='auto_template', *args, **kwargs)

Bases: AeroMAPSModel

AeroMAPS model template to be used as a starting point for auto models. Auto models do not require definition of inputs and outputs in init method. Inputs and outputs are auto generated from compute() method signature. Therefore, the compute() method must explicitly define inputs and outputs.

Parameters:

Name Type Description Default
name str

Name of the model instance ('auto_template' by default).

'auto_template'
Source code in aeromaps/models/templates.py
117
118
119
120
121
122
123
124
125
126
127
128
def __init__(
    self,
    name: str = "auto_template",
    *args,
    **kwargs,
):
    super().__init__(
        name=name,
        model_type="auto",  # inputs/outputs are auto generated from compute() signature
        *args,
        **kwargs,
    )

compute

compute(input1, input2)

Compute the outputs based on the inputs.

Parameters:

Name Type Description Default
input1

First input variable.

required
input2

Second input variable.

required

Returns:

Type Description
output1

First output variable.

output2

Second output variable.

Source code in aeromaps/models/templates.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def compute(self, input1, input2) -> Tuple[pd.Series, float]:
    """
    Compute the outputs based on the inputs.

    Parameters
    ----------
    input1
        First input variable.
    input2
        Second input variable.

    Returns
    -------
    output1
        First output variable.
    output2
        Second output variable.

    """
    output_1 = input1 * input2
    output_2 = input2**2

    return output_1, output_2