Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alignment MLPro 1.9.5 #14

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 42 additions & 40 deletions src/mlpro_int_sklearn/wrappers/anomalydetectors/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
## -- 2024-05-07 1.2.0 SK Separation of particular algorithms into separate modules
## -- 2024-05-24 1.3.0 DA Refactoring
## -- 2024-11-27 1.4.0 DA Alignment with MLPro 1.9.2
## -- 2025-02-28 1.5.0 DA Correction and alignment with MLPro 1.9.5
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.4.0 (2024-11-27)
Ver. 1.5.0 (2025-02-28)

This module provides wrapper root classes from Scikit-learn to MLPro, specifically for anomaly detectors.

Expand All @@ -28,53 +29,53 @@

from mlpro.bf.various import Log
from mlpro.bf.streams import StreamTask, InstDict, InstTypeNew
from mlpro.oa.streams.tasks.anomalydetectors import *
from mlpro.oa.streams.tasks.anomalydetectors.anomalies import *
from mlpro.oa.streams.tasks.anomalydetectors.instancebased import AnomalyDetectorIBPG
from mlpro.oa.streams.tasks.anomalydetectors.anomalies.instancebased import PointAnomaly

from mlpro_int_sklearn.wrappers.basics import WrapperSklearn





## -------------------------------------------------------------------------------------------------
## -------------------------------------------------------------------------------------------------
class WrAnomalyDetectorSklearn2MLPro(AnomalyDetectorPAGA, WrapperSklearn):
class WrAnomalyDetectorSklearn2MLPro (AnomalyDetectorIBPG, WrapperSklearn):
"""
This is the base class for anomaly detection FOR anomaly detection algorithms which are wrapped
This is the base class for anomaly detection for anomaly detection algorithms which are wrapped
from Scikit-Learn ecosystem.

"""
C_TYPE = 'ScikitLearn Anomaly Detector'
C_NAME = 'ScikitLearn Anomlay Detector'

C_TYPE = 'Anomaly Detector (scikit-learn)'

## -------------------------------------------------------------------------------------------------
def __init__(self,
p_data_buffer = 20,
p_delay = 3,
p_group_anomaly_det = True,
p_name:str = None,
p_range_max = StreamTask.C_RANGE_THREAD,
p_ada : bool = True,
p_duplicate_data : bool = False,
p_visualize : bool = False,
p_logging=Log.C_LOG_ALL,
**p_kwargs):

super().__init__(p_group_anomaly_det=p_group_anomaly_det,
p_name = p_name,
p_range_max = p_range_max,
p_ada = p_ada,
p_duplicate_data = p_duplicate_data,
p_visualize = p_visualize,
p_logging = p_logging,
**p_kwargs)
def __init__( self,
p_data_buffer = 20,
p_delay = 3,
p_group_anomaly_det = True,
p_name:str = None,
p_range_max = StreamTask.C_RANGE_THREAD,
p_ada : bool = True,
p_duplicate_data : bool = False,
p_visualize : bool = False,
p_logging=Log.C_LOG_ALL,
**p_kwargs ):

AnomalyDetectorIBPG.__init__( self,
p_group_anomaly_det = p_group_anomaly_det,
p_name = p_name,
p_range_max = p_range_max,
p_ada = p_ada,
p_duplicate_data = p_duplicate_data,
p_visualize = p_visualize,
p_logging = p_logging,
**p_kwargs )

WrapperSklearn.__init__( self, p_logging = p_logging )

self.data_buffer = p_data_buffer
self.delay = p_delay
self.delay = p_delay
self.data_points = []
self.inst_value = 0
self._visualize = p_visualize
self.inst_value = 0
self._visualize = p_visualize


## -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -105,11 +106,12 @@ def _run(self, p_inst: InstDict ):
self.adapt(p_inst = { inst_id : ( inst_type, inst ) } )

if -1 in self.ano_scores:
anomaly = PointAnomaly( p_id=self._get_next_anomaly_id,
p_instances=[inst],
p_ano_scores=self.ano_scores,
p_visualize=self._visualize,
p_raising_object=self,
p_det_time=str(inst.tstamp) )
self._raise_anomaly_event(anomaly)
anomaly = PointAnomaly( p_id = self._get_next_anomaly_id(),
p_instances = [inst],
p_ano_scores = self.ano_scores,
p_visualize = self._visualize,
p_raising_object = self,
p_tstamp = inst.tstamp )

self._raise_anomaly_event( p_anomaly = anomaly )

2 changes: 1 addition & 1 deletion src/mlpro_int_sklearn/wrappers/anomalydetectors/isof.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

## -------------------------------------------------------------------------------------------------
## -------------------------------------------------------------------------------------------------
class WrSklearnIsolationForest2MLPro(WrAnomalyDetectorSklearn2MLPro):
class WrSklearnIsolationForest2MLPro (WrAnomalyDetectorSklearn2MLPro):
C_NAME = 'Isolation Forest Anomaly Detector'
C_TYPE = 'Anomaly Detector'

Expand Down
107 changes: 54 additions & 53 deletions src/mlpro_int_sklearn/wrappers/anomalydetectors/ocsvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
## -- 2024-05-07 1.2.0 SK Separation of particular algorithms into separate modules
## -- 2024-05-24 1.3.0 DA Refactoring
## -- 2024-11-27 1.4.0 DA Alignment with MLPro 1.9.2
## -- 2025-02-17 1.5.0 DA Alignment with MLPro 1.9.5 and minor corrections
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.4.0 (2024-11-27)
Ver. 1.5.0 (2025-02-17)

This module provides wrapper functionalities to incorporate OneClass SVM algorithm of the
Scikit-learn ecosystem.
Expand All @@ -43,62 +44,62 @@

## -------------------------------------------------------------------------------------------------
## -------------------------------------------------------------------------------------------------
class WrSklearnOneClassSVM2MLPro(WrAnomalyDetectorSklearn2MLPro):
C_NAME = 'One Class SVM Anomaly Detector'
C_TYPE = 'Anomaly Detector'
class WrSklearnOneClassSVM2MLPro (WrAnomalyDetectorSklearn2MLPro):

C_NAME = 'One Class SVM'

## -------------------------------------------------------------------------------------------------
def __init__(self,
p_data_buffer = 20,
p_delay = 3,
p_kernel = 'rbf', #['linear', 'poly', 'rbf', 'sigmoid', 'precomputed']
p_nu = 0.01,
p_degree : int = 3,
p_gamma : float = 'scale', #['scale', 'auto'] or float
p_coef : float = 0,
p_tolerance : float = 0.001,
p_shrinking : bool = True,
p_cache_size : float = 200,
p_verbose : bool = False,
p_group_anomaly_det = True,
p_name:str = None,
p_range_max = StreamTask.C_RANGE_THREAD,
p_ada : bool = True,
p_duplicate_data : bool = False,
p_visualize : bool = False,
p_logging=Log.C_LOG_ALL,
**p_kwargs):

super().__init__(p_data_buffer = p_data_buffer,
p_delay = p_delay,
p_group_anomaly_det=p_group_anomaly_det,
p_name = p_name,
p_range_max = p_range_max,
p_ada = p_ada,
p_duplicate_data = p_duplicate_data,
p_visualize = p_visualize,
p_logging = p_logging,
**p_kwargs)
def __init__( self,
p_data_buffer = 20,
p_delay = 3,
p_kernel = 'rbf', #['linear', 'poly', 'rbf', 'sigmoid', 'precomputed']
p_nu = 0.01,
p_degree : int = 3,
p_gamma : float = 'scale', #['scale', 'auto'] or float
p_coef : float = 0,
p_tolerance : float = 0.001,
p_shrinking : bool = True,
p_cache_size : float = 200,
p_verbose : bool = False,
p_group_anomaly_det = True,
p_name:str = None,
p_range_max = StreamTask.C_RANGE_THREAD,
p_ada : bool = True,
p_duplicate_data : bool = False,
p_visualize : bool = False,
p_logging = Log.C_LOG_ALL,
**p_kwargs ):

super().__init__( p_data_buffer = p_data_buffer,
p_delay = p_delay,
p_group_anomaly_det=p_group_anomaly_det,
p_name = p_name,
p_range_max = p_range_max,
p_ada = p_ada,
p_duplicate_data = p_duplicate_data,
p_visualize = p_visualize,
p_logging = p_logging,
**p_kwargs )

self.kernel = p_kernel
self.nu = p_nu
self.degree = p_degree
self.gamma = p_gamma
self.coef = p_coef
self.tol = p_tolerance
self.shrinking = p_shrinking
self.kernel = p_kernel
self.nu = p_nu
self.degree = p_degree
self.gamma = p_gamma
self.coef = p_coef
self.tol = p_tolerance
self.shrinking = p_shrinking
self.cache_size = p_cache_size
self.verbose = p_verbose

self.svm = OCSVM(kernel=self.kernel,
nu=self.nu,
degree=self.degree,
gamma=self.gamma,
coef0=self.coef,
tol=self.tol,
shrinking=self.shrinking,
cache_size=self.cache_size,
verbose=self.verbose)
self.verbose = p_verbose

self.svm = OCSVM( kernel=self.kernel,
nu=self.nu,
degree=self.degree,
gamma=self.gamma,
coef0=self.coef,
tol=self.tol,
shrinking=self.shrinking,
cache_size=self.cache_size,
verbose=self.verbose )


# -------------------------------------------------------------------------------------------------
Expand Down