-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aeed02b
commit 52b23c1
Showing
6 changed files
with
212 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
src/mlpro/oa/examples/howto_oa_pp_008_rearranger_deriver_normalizer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
## ------------------------------------------------------------------------------------------------- | ||
## -- Project : MLPro - A Synoptic Framework for Standardized Machine Learning Tasks | ||
## -- Package : mlpro.oa.examples | ||
## -- Module : howto_oa_pp_008_rearranger_deriver_normalizer.py | ||
## ------------------------------------------------------------------------------------------------- | ||
## -- History : | ||
## -- yyyy-mm-dd Ver. Auth. Description | ||
## -- 2023-02-12 1.0.0 DA Adapted from howto_bf_stream_task_deriver | ||
## ------------------------------------------------------------------------------------------------- | ||
|
||
""" | ||
Ver. 1.0.0 (2023-02-12) | ||
This module was adapted from howto_bf_streams_114_stream_task_deriver. It adds an online adaptive | ||
boundary detector and min/max normalizer task to the workflow. | ||
You will learn: | ||
1) How to set up an online adaptive stream workflow based on stream tasks. | ||
2) How to set up a stream scenario based on a stream and a processing stream workflow. | ||
3) How to add a task Deriver and how to extend the features. | ||
4) How to normalize even derived data based on a boundary detector | ||
5) How to run a stream scenario dark or with visualization. | ||
""" | ||
|
||
|
||
from mlpro.bf.streams.streams import * | ||
from mlpro.bf.streams.tasks import Rearranger, Deriver | ||
|
||
from mlpro.oa.streams import * | ||
from mlpro.oa.streams.tasks import BoundaryDetector, NormalizerMinMax | ||
|
||
|
||
|
||
## ------------------------------------------------------------------------------------------------- | ||
## ------------------------------------------------------------------------------------------------- | ||
class MyScenario (OAScenario): | ||
""" | ||
Example of a custom stream scenario including a stream and a stream workflow. See class | ||
mlpro.bf.streams.models.StreamScenario for further details and explanations. | ||
""" | ||
|
||
C_NAME = 'Demo Deriver' | ||
|
||
## ------------------------------------------------------------------------------------------------- | ||
def _setup(self, p_mode, p_visualize: bool, p_logging): | ||
|
||
# 1 Import a native stream from MLPro | ||
provider_mlpro = StreamProviderMLPro(p_logging=p_logging) | ||
stream = provider_mlpro.get_stream('DoubleSpiral2D', p_mode=p_mode, p_logging=p_logging) | ||
|
||
# 2 Set up a stream workflow | ||
workflow = StreamWorkflow( p_name='wf1', | ||
p_range_max=Task.C_RANGE_NONE, | ||
p_visualize=p_visualize, | ||
p_logging=logging ) | ||
|
||
# 2.1 Set up and add a rearranger task to reduce the feature and label space | ||
features = stream.get_feature_space().get_dims() | ||
features_new = [ ( 'F', features[0:1] ) ] | ||
|
||
task_rearranger = Rearranger( p_name='t1', | ||
p_range_max=Task.C_RANGE_THREAD, | ||
p_visualize=p_visualize, | ||
p_logging=p_logging, | ||
p_features_new=features_new ) | ||
|
||
workflow.add_task( p_task=task_rearranger ) | ||
|
||
# 2.2 Set up and add a deriver task to extend the feature and label space (1st derivative) | ||
features = task_rearranger._feature_space.get_dims() | ||
derived_feature = features[0] | ||
|
||
task_deriver_1 = Deriver( p_name='t2', | ||
p_range_max=Task.C_RANGE_THREAD, | ||
p_visualize=p_visualize, | ||
p_logging=p_logging, | ||
p_features=features, | ||
p_label=None, | ||
p_derived_feature=derived_feature, | ||
p_derived_label=None, | ||
p_order_derivative=1 ) | ||
|
||
workflow.add_task( p_task=task_deriver_1, p_pred_tasks=[task_rearranger] ) | ||
|
||
# 2.3 Set up and add a deriver task to extend the feature and label space (2nd derivative) | ||
features = task_deriver_1._feature_space.get_dims() | ||
derived_feature = features[0] | ||
|
||
task_deriver_2 = Deriver( p_name='t3', | ||
p_range_max=Task.C_RANGE_THREAD, | ||
p_visualize=p_visualize, | ||
p_logging=p_logging, | ||
p_features=features, | ||
p_label=None, | ||
p_derived_feature=derived_feature, | ||
p_derived_label=None, | ||
p_order_derivative=2 ) | ||
|
||
workflow.add_task( p_task=task_deriver_2, p_pred_tasks=[task_rearranger, task_deriver_1] ) | ||
|
||
# 2.4 Boundary detector | ||
task_bd = BoundaryDetector( p_name='t4', | ||
p_ada=True, | ||
p_visualize=True, | ||
p_logging=p_logging ) | ||
|
||
workflow.add_task( p_task = task_bd, p_pred_tasks=[task_deriver_2]) | ||
|
||
# # 2.5 MinMax-Normalizer | ||
task_norm_minmax = NormalizerMinMax( p_name='t5', | ||
p_ada=True, | ||
p_visualize=p_visualize, | ||
p_logging=p_logging ) | ||
|
||
task_bd.register_event_handler( p_event_id=BoundaryDetector.C_EVENT_ADAPTED, p_event_handler=task_norm_minmax.adapt_on_event ) | ||
|
||
workflow.add_task(p_task = task_norm_minmax, p_pred_tasks=[task_bd]) | ||
|
||
# 3 Return stream and workflow | ||
return stream, workflow | ||
|
||
|
||
|
||
|
||
# 1 Preparation of demo/unit test mode | ||
if __name__ == '__main__': | ||
# 1.1 Parameters for demo mode | ||
cycle_limit = 800 | ||
logging = Log.C_LOG_ALL | ||
visualize = True | ||
|
||
else: | ||
# 1.2 Parameters for internal unit test | ||
cycle_limit = 2 | ||
logging = Log.C_LOG_NOTHING | ||
visualize = False | ||
|
||
|
||
# 2 Instantiate the stream scenario | ||
myscenario = MyScenario( p_mode=Mode.C_MODE_SIM, | ||
p_cycle_limit=cycle_limit, | ||
p_visualize=visualize, | ||
p_logging=logging ) | ||
|
||
|
||
# 3 Reset and run own stream scenario | ||
myscenario.reset() | ||
|
||
if __name__ == '__main__': | ||
myscenario.init_plot( p_plot_settings=PlotSettings( p_view = PlotSettings.C_VIEW_ND, | ||
p_view_autoselect = False, | ||
p_step_rate = 2 ) ) | ||
input('Press ENTER to start stream processing...') | ||
|
||
myscenario.run() | ||
|
||
if __name__ == '__main__': | ||
input('Press ENTER to exit...') |