Skip to content

model

Model

Bases: Named, Protocol

Base class for models.

A model is used to predict outputs for given inputs.

preprocess(input_features)

Preprocess pipeline step.

Source code in src/flowcean/core/model.py
25
26
27
def preprocess(self, input_features: Data) -> Data:
    """Preprocess pipeline step."""
    return self.pre_transform.apply(input_features)

predict(input_features)

Predict outputs for given inputs, applying transforms and hooks.

Source code in src/flowcean/core/model.py
40
41
42
43
44
def predict(self, input_features: Data) -> Data:
    """Predict outputs for given inputs, applying transforms and hooks."""
    input_features = self.preprocess(input_features)
    result = self._predict(input_features)
    return self.postprocess(result)

__call__(input_features)

Predict outputs for given inputs, applying transforms and hooks.

Source code in src/flowcean/core/model.py
46
47
48
49
@final
def __call__(self, input_features: Data) -> Data:
    """Predict outputs for given inputs, applying transforms and hooks."""
    return self.predict(input_features)

postprocess(output)

Postprocess pipeline step.

Source code in src/flowcean/core/model.py
51
52
53
def postprocess(self, output: Data) -> Data:
    """Postprocess pipeline step."""
    return self.post_transform.apply(output)

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

model.save("model.fml")

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

This method uses pickle to serialize the model, so child classes should ensure that all attributes are pickleable. If this is not the case, the child class should override this method to implement custom serialization logic, or use the __getstate__ and __setstate__ methods to control what is serialized (see https://docs.python.org/3/library/pickle.html#pickling-class-instances).

Parameters:

Name Type Description Default
file Path | str | BinaryIO

The file like object to save the model to.

required
Source code in src/flowcean/core/model.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def save(self, file: Path | str | 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
    model.save("model.fml")
    ```

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

    This method uses pickle to serialize the model, so child classes should
    ensure that all attributes are pickleable. If this is not the case, the
    child class should override this method to implement custom
    serialization logic, or use the `__getstate__` and `__setstate__`
    methods to control what is serialized (see https://docs.python.org/3/library/pickle.html#pickling-class-instances).

    Args:
        file: The file like object to save the model to.
    """
    if isinstance(file, Path | str):
        path = Path(file)
        path.parent.mkdir(parents=True, exist_ok=True)
        with path.open("wb") as f:
            pickle.dump(self, f)
    else:
        pickle.dump(self, 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

model = Model.load("model.fml")

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 | str | BinaryIO

The file like object to load the model from.

required
Source code in src/flowcean/core/model.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
@staticmethod
def load(file: Path | str | 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
    model = Model.load("model.fml")
    ```

    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.
    """
    if isinstance(file, Path | str):
        with Path(file).open("rb") as f:
            instance = pickle.load(f)
    else:
        instance = pickle.load(file)

    return instance