Skip to content

Commit

Permalink
Refact: StreamTask, OATask - order of processing/adaption on new/obso…
Browse files Browse the repository at this point in the history
…lete instances #988
  • Loading branch information
syamrajsatheesh committed May 22, 2024
1 parent 6eb606b commit 1fc11a3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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


Expand Down
19 changes: 14 additions & 5 deletions src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
20 changes: 13 additions & 7 deletions src/mlpro/oa/streams/tasks/anomalydetectors/anomalies/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion src/mlpro/oa/streams/tasks/anomalydetectors/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

11 changes: 9 additions & 2 deletions src/mlpro/oa/streams/tasks/anomalydetectors/paga_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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 = []
Expand Down

0 comments on commit 1fc11a3

Please sign in to comment.