Browse Source

Jumpdetector -> JumpDetector & all_mins in JumpDetector.__predict

pull/1/head
Alexey Velikiy 6 years ago
parent
commit
e797e183a5
  1. 2
      analytics/detectors/__init__.py
  2. 1
      analytics/detectors/general_detector/general_detector.py
  3. 54
      analytics/detectors/jump_detector.py
  4. 8
      analytics/detectors/pattern_detector.py
  5. 21
      analytics/utils/__init__.py

2
analytics/detectors/__init__.py

@ -2,4 +2,4 @@ from detectors.general_detector import GeneralDetector
from detectors.pattern_detector import PatternDetector from detectors.pattern_detector import PatternDetector
from detectors.peaks_detector import PeaksDetector from detectors.peaks_detector import PeaksDetector
from detectors.step_detector import StepDetector from detectors.step_detector import StepDetector
from detectors.jump_detector import Jumpdetector from detectors.jump_detector import JumpDetector

1
analytics/detectors/general_detector/general_detector.py

@ -10,7 +10,6 @@ import os.path
import json import json
NANOSECONDS_IN_MS = 1000000 NANOSECONDS_IN_MS = 1000000
logger = logging.getLogger('analytic_toolset') logger = logging.getLogger('analytic_toolset')

54
analytics/detectors/jump_detector.py

@ -7,41 +7,13 @@ from scipy.signal import argrelextrema
import math import math
class Jumpdetector: class JumpDetector:
def __init__(self): def __init__(self):
self.segments = [] self.segments = []
self.confidence = 1.5 self.confidence = 1.5
self.convolve_max = 120 self.convolve_max = 120
def intersection_segment(self, data, median):
cen_ind = []
for i in range(1, len(data)-1):
if data[i - 1] < median and data[i + 1] > median:
cen_ind.append(i)
del_ind = []
for i in range(1,len(cen_ind)):
if cen_ind[i] == cen_ind[i - 1] + 1:
del_ind.append(i - 1)
del_ind = del_ind[::-1]
for i in del_ind:
del cen_ind[i]
return cen_ind
def logistic_sigmoid(self, x , y, alpha, height):
distribution = []
for i in range(x, y):
F = 1 * height / (1 + math.exp(-i * alpha))
distribution.append(F)
return distribution
def alpha_finder(self, data):
"""
поиск альфы для логистической сигмоиды
"""
pass
async def fit(self, dataframe, segments): async def fit(self, dataframe, segments):
#self.alpha_finder() #self.alpha_finder()
data = dataframe['value'] data = dataframe['value']
@ -64,10 +36,10 @@ class Jumpdetector:
min_line = ax_list[min_peak, 0] min_line = ax_list[min_peak, 0]
max_line = ax_list[max_peak, 0] max_line = ax_list[max_peak, 0]
sigm_heidht = max_line - min_line sigm_heidht = max_line - min_line
pat_sigm = logistic_sigmoid(-120, 120, 1, sigm_heidht) pat_sigm = utils.logistic_sigmoid(-120, 120, 1, sigm_heidht)
for i in range(0, len(pat_sigm)): for i in range(0, len(pat_sigm)):
pat_sigm[i] = pat_sigm[i] + min_line pat_sigm[i] = pat_sigm[i] + min_line
cen_ind = self.intersection_segment(flat_segment, mids[0]) cen_ind = utils.intersection_segment(flat_segment, mids[0])
c = [] c = []
for i in range(len(cen_ind)): for i in range(len(cen_ind)):
x = cen_ind[i] x = cen_ind[i]
@ -105,26 +77,20 @@ class Jumpdetector:
else: else:
self.convolve_max = 120 # макс метрика свертки равна отступу(120), вау! self.convolve_max = 120 # макс метрика свертки равна отступу(120), вау!
def logistic_sigmoid(x1, x2, alpha, height): async def predict(self, dataframe):
distribution = []
for i in range(x, y):
F = 1 * height / (1 + math.exp(-i * alpha))
distribution.append(F)
return distribution
def predict(self, dataframe):
data = dataframe['value'] data = dataframe['value']
result = self.__predict(data) result = await self.__predict(data)
result.sort() result.sort()
if len(self.segments) > 0: if len(self.segments) > 0:
result = [segment for segment in result if not utils.is_intersect(segment, self.segments)] result = [segment for segment in result if not utils.is_intersect(segment, self.segments)]
return result return result
def __predict(self, data): async def __predict(self, data):
window_size = 24 window_size = 24
all_max_flatten_data = data.rolling(window=window_size).mean() all_max_flatten_data = data.rolling(window=window_size).mean()
all_mins = argrelextrema(np.array(all_max_flatten_data), np.less)[0]
extrema_list = [] extrema_list = []
# добавить все пересечения экспоненты со сглаженным графиком # добавить все пересечения экспоненты со сглаженным графиком
@ -172,3 +138,9 @@ class Jumpdetector:
(self.confidence, self.convolve_max) = pickle.load(file) (self.confidence, self.convolve_max) = pickle.load(file)
except: except:
pass pass
def alpha_finder(self, data):
"""
поиск альфы для логистической сигмоиды
"""
pass

8
analytics/detectors/pattern_detector.py

@ -17,12 +17,12 @@ logger = logging.getLogger('analytic_toolset')
def resolve_detector_by_pattern(pattern): def resolve_detector_by_pattern(pattern):
if pattern == "peak": if pattern == 'peak':
return detectors.PeaksDetector() return detectors.PeaksDetector()
if pattern == "drop": if pattern == 'drop':
return detectors.StepDetector() return detectors.StepDetector()
if pattern == "jump": if pattern == 'jump':
return detectors.Jumpdetector() return detectors.JumpDetector()
raise ValueError('Unknown pattern "%s"' % pattern) raise ValueError('Unknown pattern "%s"' % pattern)

21
analytics/utils/__init__.py

@ -59,3 +59,24 @@ def segments_box(segments):
min_time = pd.to_datetime(min_time, unit='ms') min_time = pd.to_datetime(min_time, unit='ms')
max_time = pd.to_datetime(max_time, unit='ms') max_time = pd.to_datetime(max_time, unit='ms')
return min_time, max_time return min_time, max_time
def intersection_segment(data, median):
cen_ind = []
for i in range(1, len(data)-1):
if data[i - 1] < median and data[i + 1] > median:
cen_ind.append(i)
del_ind = []
for i in range(1,len(cen_ind)):
if cen_ind[i] == cen_ind[i - 1] + 1:
del_ind.append(i - 1)
del_ind = del_ind[::-1]
for i in del_ind:
del cen_ind[i]
return cen_ind
def logistic_sigmoid(self, x1, x2, alpha, height):
distribution = []
for i in range(x1, x2):
F = 1 * height / (1 + math.exp(-i * alpha))
distribution.append(F)
return distribution
Loading…
Cancel
Save