Browse Source

Analytic types (#573)

pull/1/head
Alexey Velikiy 6 years ago committed by Evgeny Smyshlyaev
parent
commit
194a8b9de6
  1. 14
      analytics/analytics/analytic_types/__init__.py
  2. 1
      analytics/analytics/analytic_types/data_bucket.py
  3. 3
      analytics/analytics/analytic_unit_manager.py
  4. 1
      analytics/analytics/buckets/__init__.py
  5. 5
      analytics/analytics/detectors/pattern_detector.py
  6. 14
      analytics/analytics/models/general_model.py
  7. 6
      analytics/analytics/models/model.py

14
analytics/analytics/analytic_types/__init__.py

@ -0,0 +1,14 @@
"""
It is the place where we put all classes and types
which is common for all of the code.
For example, if you write someting which is used
in analytic_unit_manager, it should be here.
If you create something spicific which is used only in one place,
like PatternDetectionCache, then it should not be here.
"""
from analytic_types.data_bucket import DataBucket
AnalyticUnitId = str

1
analytics/analytics/buckets/data_bucket.py → analytics/analytics/analytic_types/data_bucket.py

@ -1,5 +1,6 @@
import pandas as pd import pandas as pd
class DataBucket(object): class DataBucket(object):
data: pd.DataFrame data: pd.DataFrame

3
analytics/analytics/analytic_unit_manager.py

@ -4,14 +4,13 @@ import traceback
from concurrent.futures import Executor, ThreadPoolExecutor from concurrent.futures import Executor, ThreadPoolExecutor
from analytic_unit_worker import AnalyticUnitWorker from analytic_unit_worker import AnalyticUnitWorker
from analytic_types import AnalyticUnitId
import detectors import detectors
from models import ModelCache from models import ModelCache
logger = log.getLogger('AnalyticUnitManager') logger = log.getLogger('AnalyticUnitManager')
AnalyticUnitId = str
def get_detector_by_type( def get_detector_by_type(
detector_type: str, analytic_unit_type: str, analytic_unit_id: AnalyticUnitId detector_type: str, analytic_unit_type: str, analytic_unit_id: AnalyticUnitId

1
analytics/analytics/buckets/__init__.py

@ -1 +0,0 @@
from buckets.data_bucket import DataBucket

5
analytics/analytics/detectors/pattern_detector.py

@ -8,9 +8,10 @@ import pandas as pd
from typing import Optional, Generator from typing import Optional, Generator
from detectors import Detector from detectors import Detector
from buckets import DataBucket from analytic_types import DataBucket
from models import ModelCache from models import ModelCache
from utils import convert_pd_timestamp_to_ms from utils import convert_pd_timestamp_to_ms
from analytic_types import AnalyticUnitId
logger = logging.getLogger('PATTERN_DETECTOR') logger = logging.getLogger('PATTERN_DETECTOR')
@ -31,7 +32,7 @@ def resolve_model_by_pattern(pattern: str) -> models.Model:
return models.CustomModel() return models.CustomModel()
raise ValueError('Unknown pattern "%s"' % pattern) raise ValueError('Unknown pattern "%s"' % pattern)
AnalyticUnitId = str
class PatternDetector(Detector): class PatternDetector(Detector):
MIN_BUCKET_SIZE = 150 MIN_BUCKET_SIZE = 150

14
analytics/analytics/models/general_model.py

@ -11,6 +11,8 @@ import math
from scipy.stats import gaussian_kde from scipy.stats import gaussian_kde
from scipy.stats import norm from scipy.stats import norm
import logging import logging
from analytic_types import AnalyticUnitId
PEARSON_FACTOR = 0.7 PEARSON_FACTOR = 0.7
@ -39,8 +41,8 @@ class GeneralModel(Model):
center_ind = start + math.ceil((end - start) / 2) center_ind = start + math.ceil((end - start) / 2)
return center_ind return center_ind
def do_fit(self, dataframe: pd.DataFrame, labeled_segments: list, deleted_segments: list, learning_info: dict, AnalyticUnitId: str) -> None: def do_fit(self, dataframe: pd.DataFrame, labeled_segments: list, deleted_segments: list, learning_info: dict, id: AnalyticUnitId) -> None:
logging.debug('Start method do_fit for analytic unit: {}'.format(AnalyticUnitId)) logging.debug('Start method do_fit for analytic unit: {}'.format(id))
data = utils.cut_dataframe(dataframe) data = utils.cut_dataframe(dataframe)
data = data['value'] data = data['value']
last_pattern_center = self.state.get('pattern_center', []) last_pattern_center = self.state.get('pattern_center', [])
@ -61,10 +63,10 @@ class GeneralModel(Model):
self.state['convolve_min'], self.state['convolve_max'] = utils.get_min_max(convolve_list, self.state['WINDOW_SIZE'] / 3) self.state['convolve_min'], self.state['convolve_max'] = utils.get_min_max(convolve_list, self.state['WINDOW_SIZE'] / 3)
self.state['conv_del_min'], self.state['conv_del_max'] = utils.get_min_max(del_conv_list, self.state['WINDOW_SIZE']) self.state['conv_del_min'], self.state['conv_del_max'] = utils.get_min_max(del_conv_list, self.state['WINDOW_SIZE'])
logging.debug('Method do_fit completed correctly for analytic unit: {}'.format(AnalyticUnitId)) logging.debug('Method do_fit completed correctly for analytic unit: {}'.format(id))
def do_detect(self, dataframe: pd.DataFrame, AnalyticUnitId: str) -> List[int]: def do_detect(self, dataframe: pd.DataFrame, id: AnalyticUnitId) -> List[int]:
logging.debug('Start method do_detect for analytic unit: {}'.format(AnalyticUnitId)) logging.debug('Start method do_detect for analytic unit: {}'.format(id))
data = utils.cut_dataframe(dataframe) data = utils.cut_dataframe(dataframe)
data = data['value'] data = data['value']
pat_data = self.state.get('pattern_model', []) pat_data = self.state.get('pattern_model', [])
@ -76,7 +78,7 @@ class GeneralModel(Model):
all_corr_peaks = utils.find_peaks(all_corr, window_size * 2) all_corr_peaks = utils.find_peaks(all_corr, window_size * 2)
filtered = self.__filter_detection(all_corr_peaks, data) filtered = self.__filter_detection(all_corr_peaks, data)
filtered = list(filtered) filtered = list(filtered)
logging.debug('Method do_detect completed correctly for analytic unit: {}'.format(AnalyticUnitId)) logging.debug('Method do_detect completed correctly for analytic unit: {}'.format(id))
return [(item, item + window_size * 2) for item in filtered] return [(item, item + window_size * 2) for item in filtered]
def __filter_detection(self, segments: Generator[int, None, None], data: pd.Series) -> Generator[int, None, None]: def __filter_detection(self, segments: Generator[int, None, None], data: pd.Series) -> Generator[int, None, None]:

6
analytics/analytics/models/model.py

@ -6,6 +6,7 @@ from typing import Optional
import pandas as pd import pandas as pd
import math import math
import logging import logging
from analytic_types import AnalyticUnitId
ModelCache = dict ModelCache = dict
@ -62,9 +63,8 @@ class Model(ABC):
@abstractmethod @abstractmethod
def get_model_type(self) -> (str, bool): def get_model_type(self) -> (str, bool):
pass pass
# TODO: id: str -> id: AnalyticUnitId in all models def fit(self, dataframe: pd.DataFrame, segments: list, id: AnalyticUnitId, cache: Optional[ModelCache]) -> ModelCache:
def fit(self, dataframe: pd.DataFrame, segments: list, id: str, cache: Optional[ModelCache]) -> ModelCache:
logging.debug('Start method fit for analytic unit {}'.format(id)) logging.debug('Start method fit for analytic unit {}'.format(id))
data = dataframe['value'] data = dataframe['value']
if cache != None and len(cache) > 0: if cache != None and len(cache) > 0:

Loading…
Cancel
Save