diff --git a/analytics/models/trough_model.py b/analytics/models/trough_model.py index 54748f0..03ee6a9 100644 --- a/analytics/models/trough_model.py +++ b/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: @@ -55,6 +57,20 @@ class TroughModel(Model): convolve_trough = scipy.signal.fftconvolve(labeled_trough, self.model_trough) 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)) @@ -70,6 +86,16 @@ class TroughModel(Model): self.state['convolve_min'] = float(min(convolve_list)) 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'] @@ -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