Skip to content

Commit

Permalink
Merge pull request #965 from fhswf/oa/streams/cbad4
Browse files Browse the repository at this point in the history
Oa/streams/cbad4
  • Loading branch information
detlefarend committed May 9, 2024
2 parents 5c9e43b + 20638b6 commit d036d50
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 36 deletions.
31 changes: 18 additions & 13 deletions src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@
## -- 2024-02-25 1.1.0 SK Visualisation update
## -- 2024-04-10 1.2.0 DA/SK Refactoring
## -- 2024-04-11 1.3.0 DA Class Anomaly: extensions on methods update_plot_*
## -- 2024-05-07 1.3.1 SK Bug fix related to p_instances
## -- 2024-05-09 1.3.2 DA Bugfix in method Anomaly._update_plot()
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.3.0 (2024-04-11)
Ver. 1.3.2 (2024-05-09)
This module provides templates for anomaly detection to be used in the context of online adaptivity.
"""

from matplotlib.figure import Figure
from mlpro.bf.various import Id
from mlpro.bf.plot import Plottable, PlotSettings
from mlpro.bf.events import Event
from mlpro.bf.streams import Instance





## -------------------------------------------------------------------------------------------------
## -------------------------------------------------------------------------------------------------
class Anomaly (Id, Event, Plottable):
Expand All @@ -36,12 +39,14 @@ class Anomaly (Id, Event, Plottable):
Parameters
----------
p_id
Optional external id.
p_instances : Instance
List of instances. Default value = None.
p_ano_scores : list
List of anomaly scores of instances. Default = None.
p_visualize : bool
Boolean switch for visualisation. Default = False.
p_color : string
Color of the anomaly during visualization.
p_raising_object : object
Reference of the object raised. Default = None.
**p_kwargs
Further optional keyword arguments.
"""
Expand All @@ -57,7 +62,7 @@ class Anomaly (Id, Event, Plottable):

## -------------------------------------------------------------------------------------------------
def __init__(self,
p_instance : Instance = None,
p_instances: list[Instance] = None,
p_ano_scores : list = None,
p_visualize : bool = False,
p_raising_object : object = None,
Expand All @@ -69,13 +74,13 @@ def __init__(self,
p_tstamp=p_det_time, **p_kwargs)
Plottable.__init__( self, p_visualize = p_visualize )

self.instance : Instance = p_instance
self.instances : Instance | list[Instance] = p_instances
self.ano_scores = p_ano_scores


## -------------------------------------------------------------------------------------------------
def get_instance(self) -> Instance:
return self.instance
def get_instances(self) -> list[Instance]:
return self.instances


## -------------------------------------------------------------------------------------------------
Expand All @@ -89,7 +94,7 @@ def update_plot( self,
p_xlim = None,
p_ylim = None,
p_zlim = None,
**p_kwargs):
**p_kwargs ):

return super().update_plot( p_axlimits_changed = p_axlimits_changed,
p_xlim = p_xlim,
Expand All @@ -102,7 +107,7 @@ def update_plot( self,
def _update_plot_2d( self,
p_settings: PlotSettings,
p_axlimits_changed : bool,
P_xlim,
p_xlim,
p_ylim,
**p_kwargs ):
pass
Expand All @@ -112,7 +117,7 @@ def _update_plot_2d( self,
def _update_plot_3d( self,
p_settings: PlotSettings,
p_axlimits_changed : bool,
P_xlim,
p_xlim,
p_ylim,
p_zlim,
**p_kwargs ):
Expand Down
16 changes: 9 additions & 7 deletions src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
## -- 2023-11-21 1.0.1 SK Time Stamp update
## -- 2024-02-25 1.1.0 SK Visualisation update
## -- 2024-04-10 1.2.0 DA/SK Refactoring
## -- 2024-05-07 1.2.1 SK Bug fix on groupanomaly visualisation
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.2.0 (2024-04-10)
Ver. 1.2.1 (2024-05-07)
This module provides templates for anomaly detection to be used in the context of online adaptivity.
"""
Expand All @@ -39,7 +40,7 @@ class GroupAnomaly (Anomaly):

## -------------------------------------------------------------------------------------------------
def __init__(self,
p_instances : Instance = None,
p_instances : list[Instance] = None,
p_ano_scores : list = None,
p_visualize : bool = False,
p_raising_object : object = None,
Expand All @@ -53,6 +54,7 @@ def __init__(self,

self.instances = p_instances
p_ano_scores = p_ano_scores
self.plot_update = True


## -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -80,22 +82,22 @@ def _update_plot_nd(self, p_settings: PlotSettings, **p_kwargs):
color (str): Color of the shaded region.
alpha (float): Transparency of the shaded region (default is 0.5).
"""
super()._update_plot_nd(p_settings, **p_kwargs)
if not self.plot_update: return

label = self.C_NAME[0]
x1 = self.get_instance()[0].get_id()
x2 = self.get_instance()[-1].get_id()
x1 = self.get_instances()[0].get_id()
x2 = self.get_instances()[-1].get_id()
a=[]
b=[]
for instance in self.get_instance():
for instance in self.get_instances():
a.append(instance.get_feature_data().get_values())
for x in a:
b.extend(x)
y1 = min(b)
y2 = max(b)

if self._rect is None:
self._rect = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=0, edgecolor='none', facecolor='yellow', alpha=0.3)
self._rect = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=1, edgecolor='black', facecolor='yellow', alpha=0.5)
self._plot_rectangle = p_settings.axes.add_patch(self._rect)
self._plot_rectangle_t = p_settings.axes.text((x1+x2)/2, 0, label, color='b' )

Expand Down
15 changes: 8 additions & 7 deletions src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
## -- 2024-02-25 1.1.0 SK Visualisation update
## -- 2024-04-10 1.2.0 DA/SK Refactoring
## -- 2024-04-16 1.3.0 DA Finished visualisation
## -- 2024-05-07 1.3.1 SK Bug fix related to p_instances
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.3.0 (2024-04-16)
Ver. 1.3.1 (2024-05-07)
This module provides templates for anomaly detection to be used in the context of online adaptivity.
"""
Expand Down Expand Up @@ -42,22 +43,22 @@ class PointAnomaly (Anomaly):

## -------------------------------------------------------------------------------------------------
def __init__(self,
p_instance : Instance = None,
p_instances : list[Instance] = None,
p_ano_scores : list = None,
p_visualize : bool = False,
p_raising_object : object = None,
p_det_time : str = None,
p_deviation : float=None,
**p_kwargs):

super().__init__( p_instance=p_instance,
super().__init__( p_instances=p_instances,
p_ano_scores=p_ano_scores,
p_visualize=p_visualize,
p_raising_object=p_raising_object,
p_det_time=p_det_time,
**p_kwargs )

self.instance = p_instance
self.instances = p_instances
self.ano_scores = p_ano_scores


Expand Down Expand Up @@ -88,7 +89,7 @@ def _update_plot_2d(self, p_settings: PlotSettings, p_axlimits_changed: bool, p_

if ( self._plot_line_x1 is not None ) and not p_axlimits_changed: return

inst = self.get_instance()
inst = self.get_instances()[-1]
feature_values = inst.get_feature_data().get_values()

len_x = ( p_xlim[1] - p_xlim[0] ) * self.C_PLOT_CH_SIZE / 2
Expand Down Expand Up @@ -130,7 +131,7 @@ def _update_plot_3d(self, p_settings: PlotSettings, p_axlimits_changed: bool, p_

if ( self._plot_line_x1 is not None ) and not p_axlimits_changed: return

inst = self.get_instance()
inst = self.get_instances()[-1]
feature_values = inst.get_feature_data().get_values()

len_x = ( p_xlim[1] - p_xlim[0] ) * self.C_PLOT_CH_SIZE / 2
Expand Down Expand Up @@ -190,7 +191,7 @@ def _update_plot_nd(self, p_settings: PlotSettings, p_axlimits_changed: bool, p_

if ( self._plot_line is not None ) and not p_axlimits_changed: return

inst_id = self.get_instance().get_id()
inst_id = self.get_instances()[-1].get_id()
xpos = [inst_id, inst_id]

if self._plot_line is None:
Expand Down
22 changes: 13 additions & 9 deletions src/mlpro/oa/streams/tasks/anomalydetectors/paga_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
## -- 2023-09-12 1.0.0 SK Release
## -- 2023-11-21 1.0.1 SK Time Stamp update
## -- 2024-02-25 1.1.0 SK Visualisation update
## -- 2024-04-10 1.2.0 DA/SK Refactoring
## -- 2024-04-10 1.2.0 DA/SK
## -- 2024-05-07 1.2.1 SK Bug fix on groupanomaly visualisation
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.2.0 (2024-04-10)
Ver. 1.2.1 (2024-05-07)
This module provides templates for anomaly detection to be used in the context of online adaptivity.
"""

from mlpro.oa.streams.basics import *
from mlpro.oa.streams.tasks.anomalydetectors.anomalies import GroupAnomaly
from mlpro.oa.streams.tasks.anomalydetectors.basics import AnomalyDetector
from mlpro.oa.streams.tasks.anomalydetectors.anomalies import *



Expand Down Expand Up @@ -58,8 +59,8 @@ def __init__(self,
p_logging = p_logging,
**p_kwargs)

self.group_anomalies = []
self.group_anomalies_instances = []
self.group_anomalies : list[Anomaly] = []
self.group_anomalies_instances : list[Instance] = []
self.group_ano_scores = []
self.group_anomaly_det = p_group_anomaly_det

Expand All @@ -82,12 +83,12 @@ def _buffer_anomaly(self, p_anomaly):

if self.group_anomaly_det:
self.group_anomalies.append(p_anomaly)
self.group_anomalies_instances.append(p_anomaly.get_instance()[-1])
self.group_anomalies_instances.append(p_anomaly.get_instances()[-1])
self.group_ano_scores.append(p_anomaly.get_ano_scores())

if len(self.group_anomalies_instances) > 1:

if int(p_anomaly.get_instance()[-1].get_id()) - 1 == int(self.group_anomalies_instances[-2].get_id()):
if int(p_anomaly.get_instances()[-1].get_id()) - 1 == int(self.group_anomalies_instances[-2].get_id()):

if len(self.group_anomalies_instances) == 3:

Expand All @@ -97,7 +98,7 @@ def _buffer_anomaly(self, p_anomaly):
anomaly = GroupAnomaly(p_instances=self.group_anomalies_instances,
p_ano_scores=self.group_ano_scores, p_visualize=self._visualize,
p_raising_object=self,
p_det_time=str(p_anomaly.get_instance()[-1].get_tstamp()))
p_det_time=str(p_anomaly.get_instances()[-1].get_tstamp()))
anomaly.set_id( p_id = self._get_next_anomaly_id() )
self._anomalies[anomaly.get_id()] = anomaly
self.group_anomalies = []
Expand All @@ -115,11 +116,14 @@ def _buffer_anomaly(self, p_anomaly):
return p_anomaly

else:
for anomaly in self.group_anomalies:
if isinstance(anomaly, GroupAnomaly):
anomaly.plot_update = False
self.group_anomalies = []
self.group_anomalies_instances = []
self.group_ano_scores = []
self.group_anomalies.append(p_anomaly)
self.group_anomalies_instances.append(p_anomaly.get_instance()[-1])
self.group_anomalies_instances.append(p_anomaly.get_instances()[-1])
self.group_ano_scores.append(p_anomaly.get_ano_scores())
p_anomaly.set_id( p_id = self._get_next_anomaly_id() )
self._anomalies[p_anomaly.get_id()] = p_anomaly
Expand Down

0 comments on commit d036d50

Please sign in to comment.