Skip to content

model_handler

ModelHandler(model)

Load a Flowcean model and expose its underlying ML model.

Attributes:

model: flowcean.core.model.Model The loaded Flowcean model.

Methods:

get_ml_model() Returns the underlying machine learning model from the Flowcean model.

get_model_prediction() Returns predictions from the Flowcean model as a LazyFrame.

get_model_prediction_as_lst() Returns predictions from the Flowcean model as a Python list.

Initializes the ModelHandler.

Parameters:

Name Type Description Default
model Model

Flowcean model instance.

required
Source code in src/flowcean/testing/generator/ddtig/application/model_handler.py
34
35
36
37
38
39
40
41
42
43
44
def __init__(
    self,
    model: Model,
) -> None:
    """Initializes the ModelHandler.

    Args:
        model: Flowcean model instance.
    """
    # Load the Flowcean model from the given file
    self.model = model

get_ml_model()

Extract the underlying machine learning model.

Returns:

Type Description
SupportsPredict | Module

The machine learning model.

Source code in src/flowcean/testing/generator/ddtig/application/model_handler.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def get_ml_model(self) -> SupportsPredict | Module:
    """Extract the underlying machine learning model.

    Returns:
        The machine learning model.
    """
    if type(self.model) is SciKitModel:
        ml_model = self.model.estimator
    elif type(self.model) is PyTorchModel:
        ml_model = self.model.module
    else:
        msg = f"Unsupported model type: {type(self.model)}"
        raise ValueError(msg)
    logger.info("Extracted underlying ML model successfully.")
    return ml_model

get_model_prediction(input_features)

Generates predictions using the Flowcean model.

Parameters:

Name Type Description Default
input_features DataFrame

A Polars DataFrame containing input features.

required

Returns:

Type Description
LazyFrame

A LazyFrame with predicted outputs.

Source code in src/flowcean/testing/generator/ddtig/application/model_handler.py
62
63
64
65
66
67
68
69
70
71
72
73
74
def get_model_prediction(
    self,
    input_features: pl.DataFrame,
) -> pl.LazyFrame:
    """Generates predictions using the Flowcean model.

    Args:
        input_features: A Polars DataFrame containing input features.

    Returns:
        A LazyFrame with predicted outputs.
    """
    return self.model.predict(input_features.lazy())

get_model_prediction_as_lst(input_features)

Generate predictions and return them as a Python list.

Parameters:

Name Type Description Default
input_features DataFrame

A Polars DataFrame containing input features.

required

Returns:

Type Description
list

A list of predicted output values.

Source code in src/flowcean/testing/generator/ddtig/application/model_handler.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def get_model_prediction_as_lst(
    self,
    input_features: pl.DataFrame,
) -> list:
    """Generate predictions and return them as a Python list.

    Args:
        input_features: A Polars DataFrame containing input features.

    Returns:
        A list of predicted output values.
    """
    pred_df = self.model.predict(input_features.lazy()).collect()
    target_name = pred_df.columns[-1]
    return pred_df[target_name].to_list()