core
Actable
Bases: Generic[Action]
, ABC
Base class for active environments.
Active environments require actions to be taken to advance.
act(action)
abstractmethod
Act on the environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action
|
Action
|
The action to perform. |
required |
Source code in src/flowcean/core/environment/actable.py
15 16 17 18 19 20 21 |
|
ActiveEnvironment()
Bases: TransformedObservable
, Stepable
, Actable[Data]
Base class for active environments.
An active environment loads data in an interactive way, e.g., from a simulation or real system. The environment requires actions to be taken to advance. Data can be retrieved by observing the environment.
Initialize the active environment.
Source code in src/flowcean/core/environment/active.py
23 24 25 |
|
ChainedOfflineEnvironments(environments)
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[OfflineEnvironment]
|
The offline environments to chain. |
required |
Source code in src/flowcean/core/environment/chained.py
22 23 24 25 26 27 28 29 30 |
|
IncrementalEnvironment()
Bases: TransformedObservable
, Stepable
, Iterable[Data]
Base class for incremental environments.
Incremental environments are environments that can be advanced by a step and provide a stream of data. The data can be observed at each step.
Initialize the incremental environment.
Source code in src/flowcean/core/environment/incremental.py
25 26 27 |
|
num_steps()
Return the number of steps in the environment.
Returns:
Type | Description |
---|---|
int | None
|
The number of steps in the environment, or None if the number of |
int | None
|
steps is unknown. |
Source code in src/flowcean/core/environment/incremental.py
39 40 41 42 43 44 45 46 |
|
Observable
Bases: Protocol
Protocol for observations.
observe()
abstractmethod
Observe and return the observation.
Source code in src/flowcean/core/environment/observable.py
17 18 19 20 |
|
TransformedObservable()
Bases: Observable
Base class for observations that carry a transform.
Attributes:
Name | Type | Description |
---|---|---|
transform |
Transform
|
Transform |
Initialize the observable.
Source code in src/flowcean/core/environment/observable.py
32 33 34 35 |
|
with_transform(transform)
Append a transform to the observation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transform
|
Transform
|
Transform to append. |
required |
Returns:
Type | Description |
---|---|
Self
|
This observable with the appended transform. |
Source code in src/flowcean/core/environment/observable.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
_observe()
abstractmethod
Observe and return the observation without applying the transform.
This method must be implemented by subclasses.
Returns:
Type | Description |
---|---|
Data
|
The raw observation. |
Source code in src/flowcean/core/environment/observable.py
52 53 54 55 56 57 58 59 60 |
|
__or__(transform)
Shortcut for with_transform
.
Source code in src/flowcean/core/environment/observable.py
66 67 68 69 70 71 |
|
OfflineEnvironment()
Bases: TransformedObservable
Base class for offline environments.
Offline environments are used to represent datasets. They can be used to represent static datasets. Offline environments can be transformed and joined together to create new datasets.
Initialize the offline environment.
Source code in src/flowcean/core/environment/offline.py
19 20 21 |
|
chain(*other)
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
|
OfflineEnvironment
|
The other offline environments to chain. |
()
|
Returns:
Type | Description |
---|---|
ChainedOfflineEnvironments
|
The chained offline environments. |
Source code in src/flowcean/core/environment/offline.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
__add__(other)
Shorthand for chain
.
Source code in src/flowcean/core/environment/offline.py
42 43 44 |
|
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.
Stepable
Bases: ABC
Base class for stepable environments.
Stepable environments are environments that can be advanced by a step. Usually, this is combined with an observable to provide a stream of data.
step()
abstractmethod
Advance the environment by one step.
Source code in src/flowcean/core/environment/stepable.py
13 14 15 |
|
ActiveLearner
Bases: ABC
Base class for active learners.
Active learners require actions to be taken to learn.
learn_active(action, observation)
abstractmethod
Learn from actions and observations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action
|
Data
|
The action performed. |
required |
observation
|
Data
|
The observation of the environment. |
required |
Returns:
Type | Description |
---|---|
Model
|
The model learned from the data. |
Source code in src/flowcean/core/learner.py
53 54 55 56 57 58 59 60 61 62 63 |
|
propose_action(observation)
abstractmethod
Propose an action based on an observation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
observation
|
Data
|
The observation of an environment. |
required |
Returns:
Type | Description |
---|---|
Data
|
The action to perform. |
Source code in src/flowcean/core/learner.py
65 66 67 68 69 70 71 72 73 74 |
|
SupervisedIncrementalLearner
Bases: ABC
Base class for incremental supervised learners.
An incremental supervised learner learns from input-output pairs incrementally.
learn_incremental(inputs, outputs)
abstractmethod
Learn from the data incrementally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Data
|
The input data. |
required |
outputs
|
Data
|
The output data. |
required |
Returns:
Type | Description |
---|---|
Model
|
The model learned from the data. |
Source code in src/flowcean/core/learner.py
34 35 36 37 38 39 40 41 42 43 44 |
|
SupervisedLearner
Bases: ABC
Base class for supervised learners.
A supervised learner learns from input-output pairs.
learn(inputs, outputs)
abstractmethod
Learn from the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Data
|
The input data. |
required |
outputs
|
Data
|
The output data. |
required |
Returns:
Type | Description |
---|---|
Model
|
The model learned from the data. |
Source code in src/flowcean/core/learner.py
14 15 16 17 18 19 20 21 22 23 24 |
|
OfflineMetric
Bases: ABC
Base class for metrics.
name
property
Return the name of the metric.
Returns:
Type | Description |
---|---|
str
|
The name of the metric. |
__call__(true, predicted)
abstractmethod
Calculate the metric value for given true and predicted labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
true
|
Data
|
True labels |
required |
predicted
|
Data
|
Predicted labels |
required |
Returns:
Type | Description |
---|---|
Reportable
|
Metric value |
Source code in src/flowcean/core/metric.py
19 20 21 22 23 24 25 26 27 28 29 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
ModelWithTransform(model, input_transform, output_transform)
dataclass
Report(entries)
A report containing reportables.
Initialize the report.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entries
|
dict[str, Reportable]
|
The report entries. |
required |
Source code in src/flowcean/core/report.py
15 16 17 18 19 20 21 |
|
__str__()
Return a string representation of the report.
Source code in src/flowcean/core/report.py
23 24 25 26 27 |
|
StopLearning
Bases: Exception
Stop learning.
This exception is raised when the learning process should stop.
ChainedTransforms(*transforms)
Bases: Transform
, FitOnce
, FitIncremetally
A transform that is a chain of other transforms.
Initialize the chained transforms.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transforms
|
Transform
|
The transforms to chain. |
()
|
Source code in src/flowcean/core/transform.py
169 170 171 172 173 174 175 176 177 178 |
|
FitIncremetally
Bases: ABC
A mixin for transforms that need to be fitted to data incrementally.
fit_incremental(data)
abstractmethod
Fit to the data incrementally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data
|
The data to fit to. |
required |
Source code in src/flowcean/core/transform.py
155 156 157 158 159 160 161 |
|
FitOnce
Bases: ABC
A mixin for transforms that need to be fitted to data once.
fit(data)
abstractmethod
Fit to the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data
|
The data to fit to. |
required |
Source code in src/flowcean/core/transform.py
143 144 145 146 147 148 149 |
|
Identity()
Bases: Transform
A transform that does nothing.
Initialize the identity transform.
Source code in src/flowcean/core/transform.py
211 212 213 |
|
Transform
Bases: ABC
Base class for all transforms.
apply(data)
abstractmethod
Apply the transform to data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data
|
The data to transform. |
required |
Returns:
Type | Description |
---|---|
Data
|
The transformed data. |
Source code in src/flowcean/core/transform.py
68 69 70 71 72 73 74 75 76 77 |
|
__call__(data)
Apply the transform to data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Data
|
The data to transform. |
required |
Returns:
Type | Description |
---|---|
Data
|
The transformed data. |
Source code in src/flowcean/core/transform.py
79 80 81 82 83 84 85 86 87 88 |
|
chain(other)
Chain this transform with other transforms.
This can be used to chain multiple transforms together. Chained transforms are applied left to right.
Example
chained_transform = TransformA().chain(TransformB())
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
Transform
|
The transforms to chain. |
required |
Returns:
Type | Description |
---|---|
Transform
|
A new Chain transform. |
Source code in src/flowcean/core/transform.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
__or__(other)
Shorthand for chaining transforms.
Example
chained_transform = TransformA() | TransformB()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
Transform
|
The transform to chain. |
required |
Returns:
Type | Description |
---|---|
Transform
|
A new Chain transform. |
Source code in src/flowcean/core/transform.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
inverse()
Get the inverse of the transform.
Returns:
Type | Description |
---|---|
Transform
|
The inverse of the transform. |
Source code in src/flowcean/core/transform.py
131 132 133 134 135 136 137 |
|
learn_active(environment, learner)
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. |
Source code in src/flowcean/core/strategies/active.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
deploy(environment, model, input_transforms=None, output_transforms=None)
Deploy a trained model to a custom environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
environment
|
ActiveEnvironment | IncrementalEnvironment
|
custom system environment |
required |
model
|
ModelWithTransform
|
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
|
Source code in src/flowcean/core/strategies/deploy.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
learn_incremental(environment, learner, inputs, outputs, input_transform=None, output_transform=None)
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. |
None
|
output_transform
|
Transform | 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. |
Source code in src/flowcean/core/strategies/incremental.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 66 67 68 69 |
|
evaluate_offline(model, environment, inputs, outputs, metrics)
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 |
---|---|---|---|
model
|
Model
|
The model to evaluate. |
required |
environment
|
OfflineEnvironment
|
The offline environment. |
required |
inputs
|
list[str]
|
The input feature names. |
required |
outputs
|
list[str]
|
The output feature names. |
required |
metrics
|
list[OfflineMetric]
|
The metrics to evaluate the model with. |
required |
Returns:
Type | Description |
---|---|
Report
|
The evaluation report. |
Source code in src/flowcean/core/strategies/offline.py
81 82 83 84 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 |
|
learn_offline(environment, learner, inputs, outputs, *, input_transform=None, output_transform=None)
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
|
Transform | 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. |
Source code in src/flowcean/core/strategies/offline.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|