diff --git a/analytics/detectors/__init__.py b/analytics/detectors/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/analytics/anomaly_model.py b/analytics/detectors/general_detector.py similarity index 99% rename from analytics/anomaly_model.py rename to analytics/detectors/general_detector.py index 0c3ba72..10caea1 100644 --- a/analytics/anomaly_model.py +++ b/analytics/detectors/general_detector.py @@ -1,11 +1,11 @@ -import os.path from data_provider import DataProvider from data_preprocessor import data_preprocessor -import json import pandas as pd import logging from urllib.parse import urlparse import config +import os.path +import json logger = logging.getLogger('analytic_toolset') @@ -18,7 +18,7 @@ def anomalies_to_timestamp(anomalies): return anomalies -class AnomalyModel: +class GeneralDetector: def __init__(self, anomaly_name): self.anomaly_name = anomaly_name diff --git a/analytics/pattern_detection_model.py b/analytics/detectors/pattern_detection_model.py similarity index 91% rename from analytics/pattern_detection_model.py rename to analytics/detectors/pattern_detection_model.py index b735b82..a78ec13 100644 --- a/analytics/pattern_detection_model.py +++ b/analytics/detectors/pattern_detection_model.py @@ -1,4 +1,8 @@ +from detectors.step_detector import StepDetector +from detectors.peaks_detector import PeaksDetector + from data_provider import DataProvider + import logging from urllib.parse import urlparse import os.path @@ -24,9 +28,9 @@ def segments_box(segments): class PatternDetectionModel: - def __init__(self, analytic_unit_id, pattern): + def __init__(self, analytic_unit_id, pattern_type): self.analytic_unit_id = analytic_unit_id - self.pattern = pattern + self.pattern_type = pattern_type self.__load_anomaly_config() @@ -46,10 +50,10 @@ class PatternDetectionModel: self.data_prov = DataProvider(datasource, target, dataset_filename) self.model = None - self.__load_model(pattern) + self.__load_model(pattern_type) def learn(self, segments): - self.model = self.__create_model(self.pattern) + self.model = self.__create_model(self.pattern_type) window_size = 200 dataframe = self.data_prov.get_dataframe() @@ -94,11 +98,10 @@ class PatternDetectionModel: def __create_model(self, pattern): if pattern == "peaks": - from peaks_detector import PeaksDetector return PeaksDetector() if pattern == "jumps" or pattern == "drops": - from step_detector import StepDetector return StepDetector(pattern) + raise ValueError('Unknown pattern "%s"' % pattern) def __load_anomaly_config(self): with open(os.path.join(config.ANALYTIC_UNITS_FOLDER, self.analytic_unit_id + ".json"), 'r') as config_file: @@ -111,7 +114,7 @@ class PatternDetectionModel: def __load_model(self, pattern): logger.info("Load model '%s'" % self.analytic_unit_id) - model_filename = os.path.join(config.MODELS_FOLDER, self.pattern + ".m") + model_filename = os.path.join(config.MODELS_FOLDER, self.pattern_type + ".m") if os.path.exists(model_filename): self.model = self.__create_model(pattern) self.model.load(model_filename) diff --git a/analytics/peaks_detector.py b/analytics/detectors/peaks_detector.py similarity index 98% rename from analytics/peaks_detector.py rename to analytics/detectors/peaks_detector.py index 5d9f0c9..25b86e7 100644 --- a/analytics/peaks_detector.py +++ b/analytics/detectors/peaks_detector.py @@ -1,6 +1,8 @@ +import detectors.step_detect + from scipy import signal import numpy as np -import step_detect + class PeaksDetector: diff --git a/analytics/step_detect.py b/analytics/detectors/step_detect.py similarity index 100% rename from analytics/step_detect.py rename to analytics/detectors/step_detect.py diff --git a/analytics/step_detector.py b/analytics/detectors/step_detector.py similarity index 100% rename from analytics/step_detector.py rename to analytics/detectors/step_detector.py diff --git a/analytics/worker.py b/analytics/worker.py index 6741e5e..e237f9e 100644 --- a/analytics/worker.py +++ b/analytics/worker.py @@ -1,6 +1,6 @@ import config -from anomaly_model import AnomalyModel -from pattern_detection_model import PatternDetectionModel +from detectors.general_detector import GeneralDetector +from detectors.pattern_detection_model import PatternDetectionModel import queue import threading import json @@ -104,7 +104,7 @@ class Worker(object): def get_model(self, analytic_unit_id, pattern): if analytic_unit_id not in self.models_cache: if pattern.find('general') != -1: - model = AnomalyModel(analytic_unit_id) + model = GeneralDetector(analytic_unit_id) else: model = PatternDetectionModel(analytic_unit_id, pattern) self.models_cache[analytic_unit_id] = model