Browse Source

detectors to folder

pull/1/head
Alexey Velikiy 7 years ago
parent
commit
bb3a9b47f6
  1. 0
      analytics/detectors/__init__.py
  2. 6
      analytics/detectors/general_detector.py
  3. 17
      analytics/detectors/pattern_detection_model.py
  4. 4
      analytics/detectors/peaks_detector.py
  5. 0
      analytics/detectors/step_detect.py
  6. 0
      analytics/detectors/step_detector.py
  7. 6
      analytics/worker.py

0
analytics/detectors/__init__.py

6
analytics/anomaly_model.py → 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

17
analytics/pattern_detection_model.py → 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)

4
analytics/peaks_detector.py → 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:

0
analytics/step_detect.py → analytics/detectors/step_detect.py

0
analytics/step_detector.py → analytics/detectors/step_detector.py

6
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

Loading…
Cancel
Save