Skip to content

model

Model

Bases: ABC

Base class for models.

A model is used to predict outputs for given inputs.

predict(input_features) abstractmethod

Predict outputs for the given inputs.

Parameters:

Name Type Description Default
input_features Data

The inputs for which to predict the outputs.

required

Returns:

Type Description
Data

The predicted outputs.

Source code in src/flowcean/core/model.py
24
25
26
27
28
29
30
31
32
33
@abstractmethod
def predict(self, input_features: Data) -> Data:
    """Predict outputs for the given inputs.

    Args:
        input_features: The inputs for which to predict the outputs.

    Returns:
        The predicted outputs.
    """

save(file)

Save the model to the file.

This method can be used to save a flowcean model to a file or a file-like object. To save a model to a file use

with open("model.fml", "wb") as f:
    model.save(f)

The resulting file will contain the model any any attached transforms. It can be loaded again using the load method from the Model class.

Parameters:

Name Type Description Default
file Path | BinaryIO

The file like object to save the model to.

required
Source code in src/flowcean/core/model.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
@final
def save(self, file: Path | BinaryIO) -> None:
    """Save the model to the file.

    This method can be used to save a flowcean model to a file or a
    file-like object. To save a model to a file use

    ```python
    with open("model.fml", "wb") as f:
        model.save(f)
    ```

    The resulting file will contain the model any any attached transforms.
    It can be loaded again using the `load` method from the `Model` class.

    Args:
        file: The file like object to save the model to.
    """
    data = {
        "model": self.save_state(),
        "model_type": fullname(self),
    }
    if isinstance(file, Path):
        with file.open("wb") as f:
            pickle.dump(data, f)
    else:
        pickle.dump(data, file)

load(file) staticmethod

Load a model from file.

This method can be used to load a previously saved flowcean model from a file or a file-like object. To load a model from a file use

with open("model.fml", "rb") as f:
    model = Model.load(f)

The load method will automatically determine the model type and and any attached transforms and will load them into the correct model class.

As this method uses the pickle module to load the model, it is not safe to load models from untrusted sources as this could lead to arbitrary code execution!

Parameters:

Name Type Description Default
file Path | BinaryIO

The file like object to load the model from.

required
Source code in src/flowcean/core/model.py
 63
 64
 65
 66
 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
102
103
104
@staticmethod
def load(file: Path | BinaryIO) -> Model:
    """Load a model from file.

    This method can be used to load a previously saved flowcean model from
    a file or a file-like object.
    To load a model from a file use

    ```python
    with open("model.fml", "rb") as f:
        model = Model.load(f)
    ```

    The `load` method will automatically determine the model type and and
    any attached transforms and will load them into the correct model
    class.

    As this method uses the `pickle` module to load the model, it is not
    safe to load models from untrusted sources as this could lead to
    arbitrary code execution!

    Args:
        file: The file like object to load the model from.
    """
    # Read the general model from the file
    if isinstance(file, Path):
        with file.open("rb") as f:
            data = pickle.load(f)  # noqa: S301
    else:
        data = pickle.load(file)  # noqa: S301
    if not isinstance(data, dict):
        msg = "Invalid model file"
        raise ValueError(msg)  # noqa: TRY004, it's really a value error and not a type error
    data = cast(dict[str, Any], data)

    # Create a model based on the type
    model_type = data["model_type"]
    module_name, class_name = model_type.rsplit(".", 1)
    module = importlib.import_module(module_name)
    model_class = getattr(module, class_name)

    return model_class.load_from_state(data["model"])

save_state() abstractmethod

Save the model state to a dictionary.

To save the model to a file, use the save method. To create a model from a state dictionary, use the load_from_state method.

Returns:

Type Description
dict[str, Any]

A dictionary containing the model state.

Source code in src/flowcean/core/model.py
106
107
108
109
110
111
112
113
114
115
116
@abstractmethod
def save_state(self) -> dict[str, Any]:
    """Save the model state to a dictionary.

    To save the model to a file, use the `save` method.
    To create a model from a state dictionary, use the `load_from_state`
    method.

    Returns:
        A dictionary containing the model state.
    """

load_from_state(state) abstractmethod classmethod

Load the model from a state dictionary.

To load a model from a file use the load method. To save the model state to a dictionary, use the save_state method.

Parameters:

Name Type Description Default
state dict[str, Any]

A dictionary containing the model state.

required
Source code in src/flowcean/core/model.py
118
119
120
121
122
123
124
125
126
127
128
@classmethod
@abstractmethod
def load_from_state(cls, state: dict[str, Any]) -> Model:
    """Load the model from a state dictionary.

    To load a model from a file use the `load` method.
    To save the model state to a dictionary, use the `save_state` method.

    Args:
        state: A dictionary containing the model state.
    """

ModelWithTransform(model, input_transform, output_transform) dataclass

Bases: Model

Model that carries a transform.

Attributes:

Name Type Description
model Model

Model

transform Model

Transform