Browse Source

Anti-segments in troughs model #142 (#171)

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

30
analytics/models/trough_model.py

@ -23,6 +23,8 @@ class TroughModel(Model):
'convolve_max': 570000,
'convolve_min': 530000,
'WINDOW_SIZE': 240,
'conv_del_min': 54000,
'conv_del_max': 55000,
}
def do_fit(self, dataframe: pd.DataFrame, segments: list) -> None:
@ -56,6 +58,20 @@ class TroughModel(Model):
convolve_list.append(max(auto_convolve))
convolve_list.append(max(convolve_trough))
del_conv_list = []
for segment in segments:
if segment['deleted']:
segment_from_index = utils.timestamp_to_index(dataframe, pd.to_datetime(segment['from'], unit='ms'))
segment_to_index = utils.timestamp_to_index(dataframe, pd.to_datetime(segment['to'], unit='ms'))
segment_data = data[segment_from_index: segment_to_index + 1]
if len(segment_data) == 0:
continue
del_min_index = segment_data.idxmin()
deleted_trough = data[del_min_index - self.state['WINDOW_SIZE']: del_min_index + self.state['WINDOW_SIZE'] + 1]
deleted_trough = deleted_trough - min(deleted_trough)
del_conv_trough = scipy.signal.fftconvolve(deleted_trough, self.model_trough)
del_conv_list.append(max(del_conv_trough))
if len(confidences) > 0:
self.state['confidence'] = float(min(confidences))
else:
@ -71,6 +87,16 @@ class TroughModel(Model):
else:
self.state['convolve_min'] = self.state['WINDOW_SIZE']
if len(del_conv_list) > 0:
self.state['conv_del_min'] = float(min(del_conv_list))
else:
self.state['conv_del_min'] = self.state['WINDOW_SIZE']
if len(del_conv_list) > 0:
self.state['conv_del_max'] = float(max(del_conv_list))
else:
self.state['conv_del_max'] = self.state['WINDOW_SIZE']
def do_predict(self, dataframe: pd.DataFrame):
data = dataframe['value']
window_size = int(len(data)/SMOOTHING_COEFF) #test ws on flat data
@ -102,7 +128,7 @@ class TroughModel(Model):
if len(segments) == 0 or len(self.itroughs) == 0 :
segments = []
return segments
pattern_data = self.model_peak
pattern_data = self.model_trough
for segment in segments:
if segment > self.state['WINDOW_SIZE']:
convol_data = data[segment - self.state['WINDOW_SIZE'] : segment + self.state['WINDOW_SIZE'] + 1]
@ -110,6 +136,8 @@ class TroughModel(Model):
conv = scipy.signal.fftconvolve(convol_data, pattern_data)
if max(conv) > self.state['convolve_max'] * 1.1 or max(conv) < self.state['convolve_min'] * 0.9:
delete_list.append(segment)
if max(conv) < self.state['conv_del_max'] * 1.02 and max(conv) > self.state['conv_del_min'] * 0.98:
delete_list.append(segment)
else:
delete_list.append(segment)
# TODO: implement filtering

Loading…
Cancel
Save