Browse Source

Anti-segments in general model #142 (#174)

pull/1/head
Alexandr Velikiy 6 years ago committed by rozetko
parent
commit
d885b7ec22
  1. 30
      analytics/models/general_model.py

30
analytics/models/general_model.py

@ -18,8 +18,10 @@ class GeneralModel(Model):
super() super()
self.segments = [] self.segments = []
self.ipats = [] self.ipats = []
self.model_gen = []
self.state = { self.state = {
'convolve_max': 200, 'convolve_max': 240,
'convolve_min': 200,
'WINDOW_SIZE': 240, 'WINDOW_SIZE': 240,
} }
self.all_conv = [] self.all_conv = []
@ -27,6 +29,7 @@ class GeneralModel(Model):
def do_fit(self, dataframe: pd.DataFrame, segments: list) -> None: def do_fit(self, dataframe: pd.DataFrame, segments: list) -> None:
data = dataframe['value'] data = dataframe['value']
convolve_list = [] convolve_list = []
patterns_list = []
for segment in segments: for segment in segments:
if segment['labeled']: if segment['labeled']:
segment_from_index = utils.timestamp_to_index(dataframe, pd.to_datetime(segment['from'], unit='ms')) segment_from_index = utils.timestamp_to_index(dataframe, pd.to_datetime(segment['from'], unit='ms'))
@ -40,26 +43,37 @@ class GeneralModel(Model):
segment_data = data[x - self.state['WINDOW_SIZE'] : x + self.state['WINDOW_SIZE']] segment_data = data[x - self.state['WINDOW_SIZE'] : x + self.state['WINDOW_SIZE']]
segment_min = min(segment_data) segment_min = min(segment_data)
segment_data = segment_data - segment_min segment_data = segment_data - segment_min
convolve = scipy.signal.fftconvolve(segment_data, segment_data) patterns_list.append(segment_data)
convolve_list.append(max(convolve))
self.model_gen = utils.get_av_model(patterns_list)
for n in range(len(segments)): #labeled segments
labeled_data = data[self.ipats[n] - self.state['WINDOW_SIZE']: self.ipats[n] + self.state['WINDOW_SIZE'] + 1]
labeled_data = labeled_data - min(labeled_data)
auto_convolve = scipy.signal.fftconvolve(labeled_data, labeled_data)
convolve_data = scipy.signal.fftconvolve(labeled_data, self.model_gen)
convolve_list.append(max(auto_convolve))
convolve_list.append(max(convolve_data))
if len(convolve_list) > 0: if len(convolve_list) > 0:
self.state['convolve_max'] = float(max(convolve_list)) self.state['convolve_max'] = float(max(convolve_list))
else: else:
self.state['convolve_max'] = self.state['WINDOW_SIZE'] / 3 self.state['convolve_max'] = self.state['WINDOW_SIZE'] / 3
if len(convolve_list) > 0:
self.state['convolve_min'] = float(min(convolve_list))
else:
self.state['convolve_min'] = self.state['WINDOW_SIZE'] / 3
def do_predict(self, dataframe: pd.DataFrame) -> list: def do_predict(self, dataframe: pd.DataFrame) -> list:
data = dataframe['value'] data = dataframe['value']
pat_data = data[self.ipats[0] - self.state['WINDOW_SIZE']: self.ipats[0] + self.state['WINDOW_SIZE']] pat_data = self.model_gen
x = min(pat_data)
pat_data = pat_data - x
y = max(pat_data) y = max(pat_data)
for i in range(self.state['WINDOW_SIZE'] * 2, len(data)): for i in range(self.state['WINDOW_SIZE'] * 2, len(data)):
watch_data = data[i - self.state['WINDOW_SIZE'] * 2: i] watch_data = data[i - self.state['WINDOW_SIZE'] * 2: i]
w = min(watch_data) w = min(watch_data)
watch_data = watch_data - w watch_data = watch_data - w
conv = scipy.signal.fftconvolve(pat_data, watch_data) conv = scipy.signal.fftconvolve(watch_data, pat_data)
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)
@ -72,7 +86,7 @@ class GeneralModel(Model):
delete_list = [] delete_list = []
for val in segments: for val in segments:
if self.all_conv[val] < self.state['convolve_max'] * 0.8: if self.all_conv[val] < self.state['convolve_min'] * 0.8:
delete_list.append(val) delete_list.append(val)
for item in delete_list: for item in delete_list:

Loading…
Cancel
Save