Browse Source

Add correlation in general model #437 (#440)

pull/1/head
Alexandr Velikiy 6 years ago committed by rozetko
parent
commit
3493e91235
  1. 24
      analytics/analytics/models/general_model.py

24
analytics/analytics/models/general_model.py

@ -6,10 +6,12 @@ import pandas as pd
import scipy.signal import scipy.signal
from scipy.fftpack import fft from scipy.fftpack import fft
from scipy.signal import argrelextrema from scipy.signal import argrelextrema
from scipy.stats.stats import pearsonr
import math import math
from scipy.stats import gaussian_kde from scipy.stats import gaussian_kde
from scipy.stats import norm from scipy.stats import norm
PEARSON_COEFF = 0.7
class GeneralModel(Model): class GeneralModel(Model):
@ -21,10 +23,11 @@ class GeneralModel(Model):
'convolve_max': 240, 'convolve_max': 240,
'convolve_min': 200, 'convolve_min': 200,
'WINDOW_SIZE': 0, 'WINDOW_SIZE': 0,
'conv_del_min': 100, 'conv_del_min': 0,
'conv_del_max': 120, 'conv_del_max': 0,
} }
self.all_conv = [] self.all_conv = []
self.all_corr = []
def get_model_type(self) -> (str, bool): def get_model_type(self) -> (str, bool):
model = 'general' model = 'general'
@ -67,14 +70,17 @@ class GeneralModel(Model):
raise ValueError('Labeled patterns must not be empty') raise ValueError('Labeled patterns must not be empty')
self.all_conv = [] self.all_conv = []
for i in range(self.state['WINDOW_SIZE'] * 2, len(data)): self.all_corr = []
watch_data = data[i - self.state['WINDOW_SIZE'] * 2: i] for i in range(self.state['WINDOW_SIZE'], len(data) - self.state['WINDOW_SIZE']):
watch_data = data[i - self.state['WINDOW_SIZE']: i + self.state['WINDOW_SIZE'] + 1]
watch_data = utils.subtract_min_without_nan(watch_data) watch_data = utils.subtract_min_without_nan(watch_data)
conv = scipy.signal.fftconvolve(watch_data, pat_data) conv = scipy.signal.fftconvolve(watch_data, pat_data)
correlation = pearsonr(watch_data, pat_data)
self.all_corr.append(correlation[0])
self.all_conv.append(max(conv)) self.all_conv.append(max(conv))
all_conv_peaks = utils.peak_finder(self.all_conv, self.state['WINDOW_SIZE'] * 2) all_conv_peaks = utils.peak_finder(self.all_conv, self.state['WINDOW_SIZE'] * 2)
all_corr_peaks = utils.peak_finder(self.all_corr, self.state['WINDOW_SIZE'] * 2)
filtered = self.__filter_detection(all_conv_peaks, data) filtered = self.__filter_detection(all_corr_peaks, data)
return set(item + self.state['WINDOW_SIZE'] for item in filtered) return set(item + self.state['WINDOW_SIZE'] for item in filtered)
def __filter_detection(self, segments: list, data: list): def __filter_detection(self, segments: list, data: list):
@ -84,7 +90,11 @@ class GeneralModel(Model):
for val in segments: for val in segments:
if self.all_conv[val] < self.state['convolve_min'] * 0.8: if self.all_conv[val] < self.state['convolve_min'] * 0.8:
delete_list.append(val) delete_list.append(val)
elif (self.all_conv[val] < self.state['conv_del_max'] * 1.02 and self.all_conv[val] > self.state['conv_del_min'] * 0.98): continue
if self.all_corr[val] < PEARSON_COEFF:
delete_list.append(val)
continue
if (self.all_conv[val] < self.state['conv_del_max'] * 1.02 and self.all_conv[val] > self.state['conv_del_min'] * 0.98):
delete_list.append(val) delete_list.append(val)
for item in delete_list: for item in delete_list:

Loading…
Cancel
Save