Core¶
flowcean.core provides the shared interfaces and learning strategies used across Flowcean.
See learning strategies for how environments, learners, and models fit together.
core
¶
Attributes¶
Classes¶
Adapter
¶
Bases: ABC
Abstract base class for adapters.
A component connecting Flowcean to cyber-physical systems. An adapter handles the interface between learned models and real cyber-physical systems during deployment. It manages communication with the system's sensors (for observations) and actuators (for applying actions), enabling the deployment of trained models in production.
Methods:¶
start
abstractmethod
¶
Start the adapter.
This method is called when the tool loop is started. It should be used to initialize the adapter, start any background processes and establish connections to the CPS.
stop
abstractmethod
¶
Stop the adapter.
This method is called when the tool loop is stopped. It should be used to clean up resources, stop any background processes and close connections to the CPS.
get_data
abstractmethod
¶
get_data() -> Data
Get data from the CPS through the adapter.
Retrieve a data record from the CPS. This method should block until data is available. If no more data is available, it should raise a Stop exception.
Returns:
| Type | Description |
|---|---|
Data
|
The data retrieved from the CPS. |
send_data
abstractmethod
¶
send_data(data: Data) -> None
Send data to the CPS through the adapter.
This method allows sending data to the CPS. It is used by the tool loop to send the results for the tool evaluation back to the CPS for further processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Data
|
The data to send. |
required |
CallbackManager
¶
CallbackManager(callbacks: list[LearnerCallback] | None = None)
Manage multiple callbacks through a single interface.
Initialize the callback manager.
Attributes¶
Methods:¶
on_learning_start
¶
Notify all callbacks that learning has started.
on_learning_progress
¶
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None
Notify all callbacks of learning progress.
on_learning_end
¶
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None
Notify all callbacks that learning has completed.
on_learning_error
¶
Notify all callbacks that learning has failed.
LearnerCallback
¶
Bases: Protocol
Protocol for learner callbacks.
Methods:¶
on_learning_start
abstractmethod
¶
Called when learning starts.
on_learning_progress
abstractmethod
¶
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None
Called during learning with progress updates.
on_learning_end
abstractmethod
¶
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None
Called when learning completes successfully.
on_learning_error
abstractmethod
¶
Called if learning fails with an error.
LoggingCallback
¶
LoggingCallback(logger: Logger | None = None, level_start: int = INFO, level_progress: int = DEBUG, level_end: int = INFO, level_error: int = ERROR)
Bases: LearnerCallback
Standard Python logging callback.
Initialize the logging callback.
Attributes¶
Methods:¶
on_learning_start
¶
Log learning start event.
on_learning_progress
¶
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None
Log learning progress.
RichCallback
¶
Bases: LearnerCallback
Rich console callback with adaptive progress display.
Initialize the Rich callback.
Attributes¶
Methods:¶
on_learning_start
¶
Display learning start message.
on_learning_progress
¶
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None
Update progress display with current progress and metrics.
on_learning_end
¶
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None
Display learning completion message.
on_learning_error
¶
Display error message.
RichSpinnerCallback
¶
Bases: LearnerCallback
Simplified Rich callback with just a spinner.
Initialize the Rich spinner callback.
Attributes¶
Methods:¶
on_learning_start
¶
Display learning start message with spinner.
on_learning_progress
¶
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None
Update spinner state.
on_learning_end
¶
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None
Display learning completion message.
on_learning_error
¶
Display error message.
SilentCallback
¶
A callback that intentionally produces no output.
Methods:¶
on_learning_start
¶
Do nothing when learning starts.
on_learning_progress
¶
on_learning_progress(learner: Named, progress: float | None = None, metrics: dict[str, Any] | None = None) -> None
Do nothing during learning progress.
on_learning_end
¶
on_learning_end(learner: Named, model: Model, metrics: dict[str, Any] | None = None) -> None
Do nothing when learning ends.
on_learning_error
¶
Do nothing when learning errors.
Actable
¶
ActiveEnvironment
¶
Bases: Environment, Stepable, Actable, Protocol
An environment supporting active learning through interaction.
An active environment loads data interactively from simulations or real systems. The learner influences the environment by selecting actions, which the environment responds to with observations and rewards. This supports active learning strategies where the learner explores the environment to optimize its behavior.
Environment
¶
Bases: Named, Protocol
Abstraction for data sources in learning and evaluation.
An environment describes the possible data sources for the learning and evaluation procedure. Environments can be offline (pre-recorded datasets), incremental (streaming data), or active (interactive systems where the learner can influence the environment). All environments support applying transforms to observations.
Finished
¶
Bases: Exception
Exception raised when the environment is finished.
This exception is raised when the environment is finished, and no more data can be retrieved.
IncrementalEnvironment
¶
Bases: Environment, Stepable, Iterable[Data], Protocol
An environment providing incremental (streaming) learning data.
Incremental environments provide data as a continuous stream of samples or small batches. The environment is advanced by stepping through data, observing the current state at each step. This supports incremental learning (also known as passive online learning), where the model is continuously updated as new data arrives.
Stepable
¶
ChainedOfflineEnvironments
¶
ChainedOfflineEnvironments(environments: Iterable[Environment])
Bases: IncrementalEnvironment
Chained offline environments.
This environment chains multiple offline environments together. The environment will first observe the data from the first environment and then the data from the other environments.
Initialize the chained offline environments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environments
|
Iterable[Environment]
|
The offline environments to chain. |
required |
OfflineEnvironment
¶
Bases: Environment, Protocol
Base class for offline environments.
Offline environments represent static, pre-recorded datasets collected upfront. They support the offline learning strategy where a fixed batch of data is processed at once to train a model. Offline environments can be transformed and chained together to create new datasets.
Methods:¶
chain
¶
chain(*other: Environment) -> ChainedOfflineEnvironments
Chain this offline environment with other offline environments.
Chaining offline environments will create a new incremental environment that will first observe the data from this environment and then the data from the other environments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Environment
|
The other offline environments to chain. |
()
|
Returns:
| Type | Description |
|---|---|
ChainedOfflineEnvironments
|
The chained offline environments. |
ActiveLearner
¶
SupervisedIncrementalLearner
¶
SupervisedLearner
¶
Bases: Named, Protocol
Base class for supervised learners.
A supervised learner learns from input-output pairs.
ActiveMetric
¶
Bases: Named, Protocol
Base class for metrics for active environments.
Metric
¶
Bases: Named, Protocol
Quantitative measure for evaluating model performance.
Metrics compare predictions with true outputs to assess model quality.
Call flow
call -> prepare(true), prepare(predicted) -> compute(true, predicted)
ClassifierModel
¶
Model
¶
Bases: Named, Protocol
Base class for models.
A model is used to predict outputs for given inputs.
Attributes¶
Methods:¶
predict
¶
Predict outputs for given inputs, applying transforms and hooks.
save
¶
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
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 |
load
staticmethod
¶
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
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 |
Report
¶
Bases: dict[str, ReportEntry]
A structured container for evaluation results of multiple models.
The Report maps model names to their metric results. For each model:
- top-level keys are metric names,
- values are either:
- a single
Reportable(e.g., scalar metric result), or - a nested mapping from submetric names to
Reportableobjects (e.g., per-class F1 scores, per-feature regression errors, or multi-output results).
- a single
This hierarchical structure allows uniform representation of both simple metrics and complex hierarchical metrics across multiple models.
Example:
report = Report( ... { ... "model_a": { ... "accuracy": 0.95, ... "f1": {"class_0": 0.91, "class_1": 0.89}, ... }, ... "model_b": { ... "mae": {"feature_x": 0.2, "feature_y": 0.3}, ... }, ... } ... )
Reportable
¶
Bases: Protocol
Action
dataclass
¶
Action(actuators: list[ActiveInterface])
An action in an active environment.
The action contains 'actuators', which represent setpoints in the environment. Each actuator targets exactly one input feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actuators
|
list[ActiveInterface]
|
List of interface objects, which are setpoints |
required |
Attributes¶
ActiveInterface
dataclass
¶
ActiveInterface(uid: str, value: int | float | NDArray[Any] | None, value_min: SupportsFloat | NDArray[Any] | list[Any], value_max: SupportsFloat | NDArray[Any] | list[Any], shape: Sequence[int], dtype: type[floating[Any]] | type[integer[Any]])
Interface to a feature in an active environment.
Represents a single feature of the environment, which can be either an input, an output, or the reward of the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uid
|
str
|
Identifier of the feature inside the environment |
required |
value
|
int | float | NDArray[Any] | None
|
The value of the feature |
required |
value_min
|
SupportsFloat | NDArray[Any] | list[Any]
|
Simple representation of the minimum value |
required |
value_max
|
SupportsFloat | NDArray[Any] | list[Any]
|
Simple representation of the maximum value |
required |
shape
|
Sequence[int]
|
Tuple representing the shape of the value |
required |
dtype
|
type[floating[Any]] | type[integer[Any]]
|
Data type of this interface, e.g., numpy.float32 |
required |
Attributes¶
Observation
dataclass
¶
Observation(sensors: list[ActiveInterface], rewards: list[ActiveInterface])
An observation of an active environment.
The observation contains 'sensors', which are the raw observations of featured values, and rewards, which are a rated quantification of the environment state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sensors
|
list[ActiveInterface]
|
List of interface objects, i.e., raw observations |
required |
rewards
|
list[ActiveInterface]
|
List of interface objects, i.e., rated state |
required |
Attributes¶
StopLearning
¶
Bases: Exception
Stop learning.
This exception is raised when the learning process should stop.
ChainedTransforms
¶
ChainedTransforms(*transforms: Transform)
Bases: Invertible, Transform
A composition of multiple transforms applied sequentially.
Chained transforms are applied left-to-right. Useful for building preprocessing pipelines.
Initialize the chained transforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
Transform
|
The transforms to chain. |
()
|
Identity
¶
Invertible
¶
Bases: Protocol
Protocol for transforms that support inversion.
An invertible transform can undo its effect via inverse().
InvertibleTransform
¶
Bases: Transform, Invertible, Protocol
Lambda
¶
Bases: Transform, Invertible
A transform wrapping a function.
Useful for quick one-off transformations without creating a dedicated class.
Example
Initialize the lambda transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[Data], Data]
|
Function that transforms data. |
required |
inverse_func
|
Callable[[Data], Data] | None
|
Optional function that inverts |
None
|
Transform
¶
Bases: Named, Protocol
Base protocol for all transforms in Flowcean.
A transform is a reusable operation that modifies data. Examples include preprocessing (e.g., standardization), feature engineering (e.g., feature selection, PCA), or augmentation (noise injection, synthetic features).
Transforms are composable via the | operator, allowing complex
transformation pipelines to be expressed in a clean and functional style:
Methods:¶
apply
abstractmethod
¶
chain
¶
fit
¶
fit(data: Data) -> Self
Fit the transform to data.
Many transforms (e.g. scaling, PCA) require statistics from the dataset
before applying. Default implementation is a no-op.
This is meant to be idempotent, i.e., calling fit() multiple times
should have the same effect as calling it once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Data
|
The data to fit to. |
required |
Functions:¶
create_callback_manager
¶
create_callback_manager(callbacks: list[LearnerCallback] | LearnerCallback | None) -> CallbackManager
Create a CallbackManager from supported callback inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callbacks
|
list[LearnerCallback] | LearnerCallback | None
|
Callbacks to manage. Can be: - None: Uses no callbacks - Single callback: Wraps in a list - List of callbacks: Uses directly |
required |
Returns:
| Type | Description |
|---|---|
CallbackManager
|
CallbackManager instance. |
get_default_callbacks
¶
get_default_callbacks() -> list[LearnerCallback]
Return the default callbacks for learners.
Returns:
| Type | Description |
|---|---|
list[LearnerCallback]
|
An empty list so learners stay silent unless callbacks are provided. |
learn_active
¶
learn_active(environment: ActiveEnvironment, learner: ActiveLearner) -> Model
Learn from an active environment.
Learn from an active environment by interacting with it and learning from the observations. The learning process stops when the environment ends or when the learner requests to stop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environment
|
ActiveEnvironment
|
The active environment. |
required |
learner
|
ActiveLearner
|
The active learner. |
required |
Returns:
| Type | Description |
|---|---|
Model
|
The model learned from the environment. |
deploy
¶
deploy(environment: ActiveEnvironment | IncrementalEnvironment, model: Model, input_transforms: Transform | None = None, output_transforms: Transform | None = None) -> None
Deploy a trained model to a custom environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environment
|
ActiveEnvironment | IncrementalEnvironment
|
custom system environment |
required |
model
|
Model
|
the trained model |
required |
input_transforms
|
Transform | None
|
system specific transforms for model input |
None
|
output_transforms
|
Transform | None
|
system specific transforms for system input |
None
|
learn_incremental
¶
learn_incremental(environment: IncrementalEnvironment, learner: SupervisedIncrementalLearner, inputs: list[str], outputs: list[str], input_transform: Transform | None = None, output_transform: InvertibleTransform | None = None) -> Model
Learn from a incremental environment.
Learn from a incremental environment by incrementally learning from the input-output pairs. The learning process stops when the environment ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environment
|
IncrementalEnvironment
|
The incremental environment. |
required |
learner
|
SupervisedIncrementalLearner
|
The supervised incremental learner. |
required |
inputs
|
list[str]
|
The input feature names. |
required |
outputs
|
list[str]
|
The output feature names. |
required |
input_transform
|
Transform | None
|
The transform to apply to the input features. Will be part of the final model. |
None
|
output_transform
|
InvertibleTransform | None
|
The transform to apply to the output features. Its inverse will be part of the final model. |
None
|
Returns:
| Type | Description |
|---|---|
Model
|
The model learned from the environment. |
evaluate_offline
¶
evaluate_offline(models: Model | Iterable[Model], environment: OfflineEnvironment, inputs: Sequence[str], outputs: Sequence[str], metrics: Sequence[Metric]) -> Report
Evaluate a model on an offline environment.
Evaluate a model on an offline environment by predicting the outputs from the inputs and comparing them to the true outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models
|
Model | Iterable[Model]
|
The models to evaluate. |
required |
environment
|
OfflineEnvironment
|
The offline environment. |
required |
inputs
|
Sequence[str]
|
The input feature names. |
required |
outputs
|
Sequence[str]
|
The output feature names. |
required |
metrics
|
Sequence[Metric]
|
The metrics to evaluate the model with. |
required |
Returns:
| Type | Description |
|---|---|
Report
|
The evaluation report. |
learn_offline
¶
learn_offline(environment: OfflineEnvironment, learner: SupervisedLearner, inputs: list[str], outputs: list[str], *, input_transform: Transform | None = None, output_transform: InvertibleTransform | None = None) -> Model
Learn from an offline environment.
Learn from an offline environment by learning from the input-output pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environment
|
OfflineEnvironment
|
The offline environment. |
required |
learner
|
SupervisedLearner
|
The supervised learner. |
required |
inputs
|
list[str]
|
The input feature names. |
required |
outputs
|
list[str]
|
The output feature names. |
required |
input_transform
|
Transform | None
|
The transform to apply to the input features. Will be part of the final model. |
None
|
output_transform
|
InvertibleTransform | None
|
The transform to apply to the output features. Its inverse will be part of the final model. |
None
|
Returns:
| Type | Description |
|---|---|
Model
|
The model learned from the environment. |
tune_threshold
¶
tune_threshold(model: ClassifierModel, environment: OfflineEnvironment, inputs: Sequence[str], outputs: Sequence[str], metric: Metric, *, thresholds: Sequence[float] | None = None, num_thresholds: int = 19) -> tuple[float, dict[float, float]]
Find optimal decision threshold for a classifier.
Evaluates the model at multiple threshold values and returns the threshold that maximizes the given metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ClassifierModel
|
The classifier model to tune. |
required |
environment
|
OfflineEnvironment
|
The offline environment with validation/test data. |
required |
inputs
|
Sequence[str]
|
The input feature names. |
required |
outputs
|
Sequence[str]
|
The output feature names. |
required |
metric
|
Metric
|
The metric to optimize (e.g., FBetaScore, Accuracy). |
required |
thresholds
|
Sequence[float] | None
|
Specific thresholds to evaluate. If None, generates num_thresholds evenly spaced values between 0.05 and 0.95. |
None
|
num_thresholds
|
int
|
Number of thresholds to evaluate if thresholds is None (default: 19). |
19
|
Returns:
| Type | Description |
|---|---|
float
|
Tuple of (best_threshold, results_dict) where results_dict maps |
dict[float, float]
|
each threshold to its metric score. |
Example
from flowcean.sklearn.metrics.classification import FBetaScore metric = FBetaScore(beta=1.0) best_threshold, results = tune_threshold( ... model, eval_env, inputs, outputs, metric ... ) print(f"Best threshold: {best_threshold:.3f}") model.threshold = best_threshold # Apply the best threshold