Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
detlefarend committed Feb 12, 2023
1 parent aeed02b commit 52b23c1
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 35 deletions.
36 changes: 19 additions & 17 deletions src/mlpro/bf/examples/howto_bf_streams_114_stream_task_deriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
## -- 2023-02-02 0.0.0 SY Creation
## -- 2023-02-05 1.0.0 SY First version release
## -- 2023-02-07 1.1.0 SY Change the dataset to doublespiral2d
## -- 2023-02-12 1.2.0 DA New plot parameter p_view_autoselect
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.2.0 (2023-02-07)
Ver. 1.2.0 (2023-02-12)
This module demonstrates the principles of stream processing with MLPro. To this regard, a stream of
a stream provider is combined with a stream workflow to a stream scenario. The workflow consists of
Expand Down Expand Up @@ -94,14 +95,14 @@ def _setup(self, p_mode, p_visualize: bool, p_logging):
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 )
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] )

Expand All @@ -110,14 +111,14 @@ def _setup(self, p_mode, p_visualize: bool, p_logging):
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 )
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] )

Expand Down Expand Up @@ -153,6 +154,7 @@ def _setup(self, p_mode, p_visualize: bool, p_logging):

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...')

Expand Down
6 changes: 4 additions & 2 deletions src/mlpro/bf/mt.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
## -- 2022-12-29 1.7.0 DA Refactoring of plot settings
## -- 2022-12-30 1.7.1 DA Bugfix in method Task._get_plot_host_tag()
## -- 2023-01-01 1.8.0 DA Refactoring of plot settings
## -- 2023-02-12 1.9.0 DA New plot parameter p_view_autoselect
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.8.0 (2023-01-01)
Ver. 1.9.0 (2023-02-12)
This module provides classes for multitasking with optional interprocess communication (IPC) based
on shared objects. Multitasking in MLPro combines multrithreading and multiprocessing and simplifies
Expand Down Expand Up @@ -963,7 +964,8 @@ def init_plot( self,
p_plot_depth = ps.plot_depth,
p_detail_level = ps.detail_level,
p_force_fg = ps.force_fg,
p_id=task_ax_id )
p_id=task_ax_id,
p_view_autoselect = ps.view_autoselect )

else:
# Task plots embedded in the predecessor/workflow figure/subplot
Expand Down
33 changes: 19 additions & 14 deletions src/mlpro/bf/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
## -- 2023-01-01 2.7.0 DA Class Plottable: introduction of update step rate
## -- 2023-01-04 2.8.0 DA Class PlotSettings: new parameters p_horizon, p_force_fg
## -- 2023-02-02 2.8.1 MRD Disable Tkinter backend for macos https://bugs.python.org/issue46573
## -- 2023-02-12 2.9.0 DA Class PlotSettings: new parameter p_view_autoselect
## -------------------------------------------------------------------------------------------------

"""
Ver. 2.8.1 (2023-02-02)
Ver. 2.9.0 (2023-02-12)
This module provides various classes related to data plotting.
"""
Expand Down Expand Up @@ -95,6 +96,8 @@ class PlotSettings:
Optional boolean flag. If True, the releated window is kept in foreground. Default = True.
p_id : int
Optional unique id of the subplot within the figure. Default = 1.
p_view_autoselect : bool
If True, the final view is automatically selected during runtime. Default = True.
p_kwargs : dict
Further optional named parameters.
"""
Expand All @@ -119,24 +122,26 @@ def __init__( self,
p_detail_level : int = 0,
p_force_fg : bool = True,
p_id : int = 1,
p_view_autoselect : bool = True,
**p_kwargs ):

if p_view not in self.C_VALID_VIEWS:
raise ParamError('Wrong value for parameter p_view. See class mlpro.bf.plot.SubPlotSettings for more details.')

self.view = p_view
self.axes = p_axes
self.pos_x = p_pos_x
self.pos_y = p_pos_y
self.size_x = p_size_x
self.size_y = p_size_y
self.step_rate = p_step_rate
self.horizon = p_horizon
self.plot_depth = p_plot_depth
self.detail_level = p_detail_level
self.force_fg = p_force_fg
self.id = p_id
self.kwargs = p_kwargs.copy()
self.view = p_view
self.axes = p_axes
self.pos_x = p_pos_x
self.pos_y = p_pos_y
self.size_x = p_size_x
self.size_y = p_size_y
self.step_rate = p_step_rate
self.horizon = p_horizon
self.plot_depth = p_plot_depth
self.detail_level = p_detail_level
self.force_fg = p_force_fg
self.id = p_id
self.view_autoselect = p_view_autoselect
self.kwargs = p_kwargs.copy()



Expand Down
7 changes: 5 additions & 2 deletions src/mlpro/bf/streams/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@
## -- - Refactoring of plotting
## -- - incorporation of new plot parameter p_horizon
## -- 2023-01-05 1.0.1 DA Refactoring of method StreamShared.get_instances()
## -- 2023-02-12 1.1.0 DA Class StreamTask: implementation of plot parameter view_autoselect
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.0.1 (2023-01-05)
Ver. 1.1.0 (2023-02-12)
This module provides classes for standardized stream processing.
"""
Expand Down Expand Up @@ -975,7 +976,9 @@ def update_plot( self,
try:
self._plot_view_finalized
except:
self._finalize_plot_view(p_inst_ref=inst_ref)
if self._plot_settings.view_autoselect:
self._finalize_plot_view(p_inst_ref=inst_ref)

self._plot_view_finalized = True

Task.update_plot(self, p_inst_new=inst_new, p_inst_del=inst_del, **p_kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from mlpro.bf.streams.streams import *
from mlpro.bf.streams.tasks import Window, Rearranger
from mlpro.oa.streams import *
from mlpro.oa.streams.tasks import *



Expand Down
164 changes: 164 additions & 0 deletions src/mlpro/oa/examples/howto_oa_pp_008_rearranger_deriver_normalizer.py
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...')

0 comments on commit 52b23c1

Please sign in to comment.