From 1fc11a3ba7d14bed4c417197cdc98a3f60dda039 Mon Sep 17 00:00:00 2001 From: syam47 Date: Wed, 22 May 2024 17:31:29 +0200 Subject: [PATCH] Refact: StreamTask, OATask - order of processing/adaption on new/obsolete instances #988 --- .../anomalydetectors/anomalies/basics.py | 6 +++--- .../tasks/anomalydetectors/anomalies/group.py | 19 +++++++++++++----- .../tasks/anomalydetectors/anomalies/point.py | 20 ++++++++++++------- .../streams/tasks/anomalydetectors/basics.py | 9 ++++++++- .../tasks/anomalydetectors/paga_detectors.py | 11 ++++++++-- 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/basics.py b/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/basics.py index f065e5061..aaec46e4f 100644 --- a/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/basics.py +++ b/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/basics.py @@ -63,7 +63,7 @@ class Anomaly (Id, Event, Plottable): ## ------------------------------------------------------------------------------------------------- def __init__(self, - p_instances: InstDict = None, + p_instances: list[InstDict] = None, p_ano_scores : list = None, p_visualize : bool = False, p_raising_object : object = None, @@ -75,12 +75,12 @@ def __init__(self, p_tstamp=p_det_time, **p_kwargs) Plottable.__init__( self, p_visualize = p_visualize ) - self.instances : InstDict = p_instances + self.instances : list[InstDict] = p_instances self.ano_scores = p_ano_scores ## ------------------------------------------------------------------------------------------------- - def get_instances(self) -> list[Instance]: + def get_instances(self) -> list[InstDict]: return self.instances diff --git a/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/group.py b/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/group.py index f1d270cdb..37cc07ecd 100644 --- a/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/group.py +++ b/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/group.py @@ -41,7 +41,7 @@ class GroupAnomaly (Anomaly): ## ------------------------------------------------------------------------------------------------- def __init__(self, - p_instances : InstDict = None, + p_instances : list[InstDict] = None, p_ano_scores : list = None, p_visualize : bool = False, p_raising_object : object = None, @@ -53,7 +53,7 @@ def __init__(self, p_visualize=p_visualize, p_raising_object=p_raising_object, p_det_time=p_det_time, **p_kwargs) - self.instances = p_instances + self.instances : list[InstDict] = p_instances p_ano_scores = p_ano_scores self.plot_update = True @@ -86,12 +86,21 @@ def _update_plot_nd(self, p_settings: PlotSettings, **p_kwargs): if not self.plot_update: return label = self.C_NAME[0] - x1 = self.get_instances()[0].get_id() - x2 = self.get_instances()[-1].get_id() + + + x1 : Instance = None + x2 : Instance = None + (inst_type, x1) = self.get_instances()[0].values()[-1] + (inst_type, x2) = self.get_instances()[-1].values()[-1] + + x1 = x1.get_id() + x2 = x2.get_id() a=[] b=[] for instance in self.get_instances(): - a.append(instance.get_feature_data().get_values()) + inst : Instance = None + (inst_type, inst) = instance.values()[-1] + a.append(inst.get_feature_data().get_values()) for x in a: b.extend(x) y1 = min(b) diff --git a/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/point.py b/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/point.py index e7c8812f5..4682ee46e 100644 --- a/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/point.py +++ b/src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/point.py @@ -44,7 +44,7 @@ class PointAnomaly (Anomaly): ## ------------------------------------------------------------------------------------------------- def __init__(self, - p_instances : InstDict = None, + p_instances : list[InstDict] = None, p_ano_scores : list = None, p_visualize : bool = False, p_raising_object : object = None, @@ -59,7 +59,7 @@ def __init__(self, p_det_time=p_det_time, **p_kwargs ) - self.instances = p_instances + self.instances : list[InstDict] = p_instances self.ano_scores = p_ano_scores @@ -90,8 +90,9 @@ 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_instances()[-1] - feature_values = inst.get_feature_data().get_values() + inst : Instance = None + (inst_type, inst) = self.get_instances()[-1].values()[-1] + feature_values = inst.get_feature_data().get_values() len_x = ( p_xlim[1] - p_xlim[0] ) * self.C_PLOT_CH_SIZE / 2 len_y = ( p_ylim[1] - p_ylim[0] ) * self.C_PLOT_CH_SIZE / 2 @@ -132,8 +133,9 @@ 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_instances()[-1] - feature_values = inst.get_feature_data().get_values() + inst : Instance = None + (inst_type, inst) = self.get_instances()[-1].values()[-1] + feature_values = inst.get_feature_data().get_values() len_x = ( p_xlim[1] - p_xlim[0] ) * self.C_PLOT_CH_SIZE / 2 len_y = ( p_ylim[1] - p_ylim[0] ) * self.C_PLOT_CH_SIZE / 2 @@ -192,7 +194,11 @@ 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_instances()[-1].get_id() + + inst : Instance = None + (inst_type, inst) = self.get_instances()[-1].values()[-1] + + inst_id = inst.get_id() xpos = [inst_id, inst_id] if self._plot_line is None: diff --git a/src/mlpro/oa/streams/tasks/anomalydetectors/basics.py b/src/mlpro/oa/streams/tasks/anomalydetectors/basics.py index bc4a10533..e3b1cc8fc 100644 --- a/src/mlpro/oa/streams/tasks/anomalydetectors/basics.py +++ b/src/mlpro/oa/streams/tasks/anomalydetectors/basics.py @@ -208,6 +208,13 @@ def _renormalize(self, p_normalizer: Normalizer): p_normalizer : Normalizer Normalizer object to be applied on task-specific """ + anomaly : Anomaly = None for anomaly in self._anomalies.values(): - anomaly.get_instance().renormalize( p_normalizer=p_normalizer) + instances : list[InstDict] = None + instances = anomaly.get_instances() + + for item in instances: + (inst_id, inst) = item.values()[-1] + inst.remormalize( p_normalizer=p_normalizer) + diff --git a/src/mlpro/oa/streams/tasks/anomalydetectors/paga_detectors.py b/src/mlpro/oa/streams/tasks/anomalydetectors/paga_detectors.py index 362a920dd..4fffe48dc 100644 --- a/src/mlpro/oa/streams/tasks/anomalydetectors/paga_detectors.py +++ b/src/mlpro/oa/streams/tasks/anomalydetectors/paga_detectors.py @@ -88,7 +88,14 @@ def _buffer_anomaly(self, p_anomaly): if len(self.group_anomalies_instances) > 1: - if int(p_anomaly.get_instances()[-1].get_id()) - 1 == int(self.group_anomalies_instances[-2].get_id()): + inst_1 : Instance = None + inst_2 : Instance = None + (inst_type, inst_2) = self.group_anomalies_instances[-1].values()[-1] + second = inst_2.get_id() + (inst_type, inst_1) = self.group_anomalies_instances[-2].values()[-1] + first = inst_1.get_id() + + if int(second) - 1 == int(first): if len(self.group_anomalies_instances) == 3: @@ -98,7 +105,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_instances()[-1].get_tstamp())) + p_det_time=str(inst_2.get_tstamp())) anomaly.set_id( p_id = self._get_next_anomaly_id() ) self._anomalies[anomaly.get_id()] = anomaly self.group_anomalies = []