Skip to content

Commit

Permalink
Merge pull request #22 from fhswf/oa/streams/ca
Browse files Browse the repository at this point in the history
OA-Streams: Introduction of Cluster's Size
  • Loading branch information
detlefarend committed May 27, 2024
2 parents f837e39 + 336e2f8 commit 6f87986
Show file tree
Hide file tree
Showing 31 changed files with 166 additions and 47 deletions.
9 changes: 5 additions & 4 deletions src/mlpro_int_river/wrappers/clusteranalyzers/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
## -- 2024-05-05 1.3.0 DA Alignment with MLPro 2
## -- 2024-05-07 1.4.0 DA Separation of particular algorithms into separate modules
## -- 2024-05-24 1.5.0 DA Alignment with MLPro 2
## -- 2024-05-25 1.5.1 SY Introduction of size as a property, Refactoring
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.5.0 (2024-05-24)
Ver. 1.5.1 (2024-05-25)
This module provides wrapper root classes from River to MLPro, specifically for cluster analyzers.
Expand Down Expand Up @@ -88,7 +89,7 @@ class WrClusterAnalyzerRiver2MLPro (WrapperRiver, ClusterAnalyzer):

C_TYPE = 'River Cluster Analyzer'

C_CLUSTER_PROPERTIES = [ cprop_centroid ]
C_CLUSTER_PROPERTIES = [ cprop_centroid, cprop_size ]

## -------------------------------------------------------------------------------------------------
def __init__( self,
Expand Down Expand Up @@ -145,13 +146,13 @@ def _adapt(self, p_inst_new : Instance) -> bool:

# update MLPro clusters from river
self.get_clusters()
self._update_clusters()
self._update_clusters(input_data)

return True


## -------------------------------------------------------------------------------------------------
def _update_clusters(self):
def _update_clusters(self, input_data):
"""
This method is to update the centroids of each introduced cluster.
"""
Expand Down
17 changes: 14 additions & 3 deletions src/mlpro_int_river/wrappers/clusteranalyzers/clustream.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
## -- 2024-04-30 1.2.0 DA Alignment with MLPro 2
## -- 2024-05-05 1.3.0 DA Alignment with MLPro 2
## -- 2024-05-07 1.4.0 DA Separated to own module
## -- 2024-05-25 1.4.1 SY Introduction of size as a property
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.4.0 (2024-05-07)
Ver. 1.4.1 (2024-05-25)
This module provides a wrapper class for the CluStream algorithm provided by River.
Expand Down Expand Up @@ -154,14 +156,23 @@ def __init__(self,


## -------------------------------------------------------------------------------------------------
def _update_clusters(self):
def _update_clusters(self, input_data):
"""
This method is to update the centroids of each introduced cluster.
"""

updated_cls = self._river_algo.predict_one(input_data)

for x in self._river_algo.centers.keys():
related_cluster = self._clusters[x]
related_cluster.centroid.value = list(self._river_algo.centers[x].values())

if x == updated_cls:
act_size = related_cluster.size.value
if act_size is not None:
related_cluster.size.value = act_size+1
else:
related_cluster.size.value = 1


## -------------------------------------------------------------------------------------------------
Expand All @@ -180,7 +191,7 @@ def get_clusters(self) -> dict[Cluster]:
try:
related_cluster = self._clusters[x]
except:
related_cluster = self._cls_cluster(p_id=x, p_visualize=self.get_visualization())
related_cluster = self._cls_cluster(p_id=x, p_properties=self.C_CLUSTER_PROPERTIES, p_visualize=self.get_visualization())

if self.get_visualization():
related_cluster.init_plot(p_figure = self._figure, p_plot_settings=self._plot_settings)
Expand Down
18 changes: 13 additions & 5 deletions src/mlpro_int_river/wrappers/clusteranalyzers/dbstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
## -- 2024-04-30 1.2.0 DA Alignment with MLPro 2
## -- 2024-05-05 1.3.0 DA Alignment with MLPro 2
## -- 2024-05-07 1.4.0 DA Separated to own module
## -- 2024-05-25 1.4.1 SY Introduction of size as a property
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.4.0 (2024-05-07)
Ver. 1.4.1 (2024-05-25)
This module provides a wrapper class for the DBStream algorithm provided by River.
Expand Down Expand Up @@ -105,8 +106,6 @@ class WrRiverDBStream2MLPro (WrClusterAnalyzerRiver2MLPro):

C_TYPE = 'River Cluster Analyzer DBSTREAM'

C_CLUSTER_PROPERTIES = [ cprop_centroid ]

## -------------------------------------------------------------------------------------------------
def __init__(self,
p_name:str = None,
Expand Down Expand Up @@ -138,14 +137,23 @@ def __init__(self,


## -------------------------------------------------------------------------------------------------
def _update_clusters(self):
def _update_clusters(self, input_data):
"""
This method is to update the centroids of each introduced cluster.
"""

for _, (key, val) in enumerate(self._river_algo.micro_clusters.items()):
updated_cls = self._river_algo.predict_one(input_data)

for x, (key, val) in enumerate(self._river_algo.micro_clusters.items()):
related_cluster = self._clusters[id(val)]
related_cluster.centroid.value = list(self._river_algo.centers[key].values())

if x == updated_cls:
act_size = related_cluster.size.value
if act_size is not None:
related_cluster.size.value = act_size+1
else:
related_cluster.size.value = 1


## -------------------------------------------------------------------------------------------------
Expand Down
17 changes: 13 additions & 4 deletions src/mlpro_int_river/wrappers/clusteranalyzers/denstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
## -- 2024-04-30 1.2.0 DA Alignment with MLPro 2
## -- 2024-05-05 1.3.0 DA Alignment with MLPro 2
## -- 2024-05-07 1.4.0 DA Separated to own module
## -- 2024-05-25 1.4.1 SY Introduction of size as a property
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.4.0 (2024-05-07)
Ver. 1.4.1 (2024-05-25)
This module provides a wrapper class for the DenStream algorithm provided by River.
Expand Down Expand Up @@ -142,21 +143,29 @@ def __init__(self,


## -------------------------------------------------------------------------------------------------
def _update_clusters(self):
def _update_clusters(self, input_data):
"""
This method is to update the centroids of each introduced cluster.
"""

if self._river_algo.n_clusters != 0:
updated_cls = self._river_algo.predict_one(input_data)

for val in self._river_algo.p_micro_clusters.values():
related_cluster = self._clusters[id(val)]

list_center = []
for _, (_, val_center) in enumerate(val.x.items()):
for x, (_, val_center) in enumerate(val.x.items()):
list_center.append(val_center)

try:
related_cluster.centroid.value = list_center
related_cluster.centroid.value = list_center
if x == updated_cls:
act_size = related_cluster.size.value
if act_size is not None:
related_cluster.size.value = act_size+1
else:
related_cluster.size.value = 1
except:
pass

Expand Down
15 changes: 13 additions & 2 deletions src/mlpro_int_river/wrappers/clusteranalyzers/kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
## -- 2024-04-30 1.2.0 DA Alignment with MLPro 2
## -- 2024-05-05 1.3.0 DA Alignment with MLPro 2
## -- 2024-05-07 1.4.0 DA Separated to own module
## -- 2024-05-25 1.4.1 SY Introduction of size as a property
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.4.0 (2024-05-07)
Ver. 1.4.1 (2024-05-25)
This module provides a wrapper class for the KMeans algorithm provided by River.
Expand Down Expand Up @@ -133,16 +134,26 @@ def __init__(self,


## -------------------------------------------------------------------------------------------------
def _update_clusters(self):
def _update_clusters(self, input_data):
"""
This method is to update the centroids of each introduced cluster.
"""

updated_cls = self._river_algo.predict_one(input_data)

for x in self._river_algo.centers.keys():
related_cluster = self._clusters[x]
list_center = []
for y in range(len(self._river_algo.centers[x])):
list_center.append(self._river_algo.centers[x][y+1])
related_cluster.centroid.value = list_center

if x == updated_cls:
act_size = related_cluster.size.value
if act_size is not None:
related_cluster.size.value = act_size+1
else:
related_cluster.size.value = 1


## -------------------------------------------------------------------------------------------------
Expand Down
14 changes: 12 additions & 2 deletions src/mlpro_int_river/wrappers/clusteranalyzers/streamkmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
## -- 2024-04-30 1.2.0 DA Alignment with MLPro 2
## -- 2024-05-05 1.3.0 DA Alignment with MLPro 2
## -- 2024-05-07 1.4.0 DA Separated to own module
## -- 2024-05-25 1.4.1 SY Introduction of size as a property
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.4.0 (2024-05-07)
Ver. 1.4.1 (2024-05-25)
This module provides a wrapper class for the STREAMKMeans algorithm provided by River.
Expand Down Expand Up @@ -140,16 +141,25 @@ def __init__(self,


## -------------------------------------------------------------------------------------------------
def _update_clusters(self):
def _update_clusters(self, input_data):
"""
This method is to update the centroids of each introduced cluster.
"""

updated_cls = self._river_algo.predict_one(input_data)

for x in self._river_algo.centers.keys():
try:
related_cluster = self._clusters[x]
list_center = []
for y in range(len(self._river_algo.centers[x])):
list_center.append(self._river_algo.centers[x][y+1])
if x == updated_cls:
act_size = related_cluster.size.value
if act_size is not None:
related_cluster.size.value = act_size+1
else:
related_cluster.size.value = 1
try:
related_cluster.centroid.value = list_center
except:
Expand Down
4 changes: 3 additions & 1 deletion test/howtos/oa/howto_oa_ca_001_run_kmeans_2d_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
## -- 2023-12-28 1.1.0 DA Exchange of benchmark stream and number of clouds
## -- 2024-04-30 1.2.0 DA Alignment with MLPro 2
## -- 2024-05-05 1.3.0 DA Alignment with MLPro 2
## -- 2024-05-27 1.3.1 SY Printing clusters' sizes
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.3.0 (2024-05-05)
Ver. 1.3.1 (2024-05-27)
This module demonstrates online cluster analysis of static 2D random point clouds using the wrapped
River implementation of stream algorithm KMeans. To this regard, the systematics of sub-framework
Expand Down Expand Up @@ -129,6 +130,7 @@ def _setup(self, p_mode, p_ada: bool, p_visualize: bool, p_logging):
myscenario.log(Log.C_LOG_TYPE_I, 'Number of clusters: ', number_of_clusters)
for x in range(number_of_clusters):
myscenario.log(Log.C_LOG_TYPE_I, 'Center of Cluster ', str(x+1), ': ', list(clusters[x].centroid.value))
myscenario.log(Log.C_LOG_TYPE_I, 'Size of Cluster ', str(x+1), ': ', clusters[x].size.value)
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')

Expand Down
4 changes: 3 additions & 1 deletion test/howtos/oa/howto_oa_ca_002_run_kmeans_2d_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
## -- 2023-12-28 1.1.0 DA Exchange of benchmark stream and number of clouds
## -- 2024-01-05 1.1.1 SY Replace algorithm to StreamKMeans
## -- 2024-04-30 1.2.0 DA Alignment with MLPro 2
## -- 2024-05-27 1.2.1 SY Printing clusters' sizes
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.2.0 (2024-04-30)
Ver. 1.2.1 (2024-05-27)
This module demonstrates online cluster analysis of dynamic 2D random point clouds using the wrapped
River implementation of stream algorithm KMeans. To this regard, the systematics of sub-framework
Expand Down Expand Up @@ -131,6 +132,7 @@ def _setup(self, p_mode, p_ada: bool, p_visualize: bool, p_logging):
myscenario.log(Log.C_LOG_TYPE_I, 'Number of clusters: ', number_of_clusters)
for x in range(number_of_clusters):
myscenario.log(Log.C_LOG_TYPE_I, 'Center of Cluster ', str(x+1), ': ', list(clusters[x].centroid.value))
myscenario.log(Log.C_LOG_TYPE_I, 'Size of Cluster ', str(x+1), ': ', clusters[x].size.value)
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
## -- 2023-12-21 1.1.1 SY Refactoring
## -- 2024-02-02 1.2.0 SY Parameters Optimization
## -- 2024-04-30 1.3.0 DA Alignment with MLPro 2
## -- 2024-05-27 1.3.1 SY Printing clusters' sizes
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.3.0 (2024-04-30)
Ver. 1.3.1 (2024-05-27)
This module demonstrates online cluster analysis of normalized static 2D random point clouds using the wrapped
River implementation of stream algorithm KMeans. To this regard, the systematics of sub-framework
Expand Down Expand Up @@ -158,6 +159,7 @@ def _setup(self, p_mode, p_ada: bool, p_visualize: bool, p_logging):
myscenario.log(Log.C_LOG_TYPE_I, 'Number of clusters: ', number_of_clusters)
for x in range(number_of_clusters):
myscenario.log(Log.C_LOG_TYPE_I, 'Center of Cluster ', str(x+1), ': ', list(clusters[x].centroid.value))
myscenario.log(Log.C_LOG_TYPE_I, 'Size of Cluster ', str(x+1), ': ', clusters[x].size.value)
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
## -- - Add window to the workflow
## -- 2024-02-02 1.1.0 SY Parameters Optimization
## -- 2024-04-30 1.2.0 DA Alignment with MLPro 2
## -- 2024-05-27 1.2.1 SY Printing clusters' sizes
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.2.0 (2024-04-30)
Ver. 1.2.1 (2024-05-27)
This module demonstrates online cluster analysis of normalized dynamic 2D random point clouds using the wrapped
River implementation of stream algorithm KMeans. To this regard, the systematics of sub-framework
Expand Down Expand Up @@ -165,6 +166,7 @@ def _setup(self, p_mode, p_ada: bool, p_visualize: bool, p_logging):
myscenario.log(Log.C_LOG_TYPE_I, 'Number of clusters: ', number_of_clusters)
for x in range(number_of_clusters):
myscenario.log(Log.C_LOG_TYPE_I, 'Center of Cluster ', str(x+1), ': ', list(clusters[x].centroid.value))
myscenario.log(Log.C_LOG_TYPE_I, 'Size of Cluster ', str(x+1), ': ', clusters[x].size.value)
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')

Expand Down
4 changes: 3 additions & 1 deletion test/howtos/oa/howto_oa_ca_005_run_kmeans_3d_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
## -- 2023-12-28 1.1.0 DA Exchange of benchmark stream and number of clouds
## -- 2023-12-29 1.2.0 DA Adjustments on cloud sizes and weights
## -- 2024-04-30 1.3.0 DA Alignment with MLPro 2
## -- 2024-05-27 1.3.1 SY Printing clusters' sizes
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.3.0 (2024-04-30)
Ver. 1.3.1 (2024-05-27)
This module demonstrates online cluster analysis of static 3D random point clouds using the wrapped
River implementation of stream algorithm KMeans. To this regard, the systematics of sub-framework
Expand Down Expand Up @@ -132,6 +133,7 @@ def _setup(self, p_mode, p_ada: bool, p_visualize: bool, p_logging):
myscenario.log(Log.C_LOG_TYPE_I, 'Number of clusters: ', number_of_clusters)
for x in range(number_of_clusters):
myscenario.log(Log.C_LOG_TYPE_I, 'Center of Cluster ', str(x+1), ': ', list(clusters[x].centroid.value))
myscenario.log(Log.C_LOG_TYPE_I, 'Size of Cluster ', str(x+1), ': ', clusters[x].size.value)
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')

Expand Down
4 changes: 3 additions & 1 deletion test/howtos/oa/howto_oa_ca_006_run_kmeans_3d_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
## -- 2023-12-28 1.1.0 DA Exchange of benchmark stream and number of clouds
## -- 2024-01-05 1.1.1 SY Replace algorithm to StreamKMeans
## -- 2024-04-30 1.2.0 DA Alignment with MLPro 2
## -- 2024-05-27 1.2.1 SY Printing clusters' sizes
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.2.0 (2024-04-30)
Ver. 1.2.1 (2024-05-27)
This module demonstrates online cluster analysis of dynamic 3D random point clouds using the wrapped
River implementation of stream algorithm KMeans. To this regard, the systematics of sub-framework
Expand Down Expand Up @@ -132,6 +133,7 @@ def _setup(self, p_mode, p_ada: bool, p_visualize: bool, p_logging):
myscenario.log(Log.C_LOG_TYPE_I, 'Number of clusters: ', number_of_clusters)
for x in range(number_of_clusters):
myscenario.log(Log.C_LOG_TYPE_I, 'Center of Cluster ', str(x+1), ': ', list(clusters[x].centroid.value))
myscenario.log(Log.C_LOG_TYPE_I, 'Size of Cluster ', str(x+1), ': ', clusters[x].size.value)
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')
myscenario.log(Log.C_LOG_TYPE_I, '-------------------------------------------------------')

Expand Down
Loading

0 comments on commit 6f87986

Please sign in to comment.