|
|
@ -12,7 +12,7 @@ from scipy.stats import norm |
|
|
|
from typing import Optional |
|
|
|
from typing import Optional |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WINDOW_SIZE = 350 |
|
|
|
WINDOW_SIZE = 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GeneralModel(Model): |
|
|
|
class GeneralModel(Model): |
|
|
@ -41,12 +41,11 @@ class GeneralModel(Model): |
|
|
|
segment_data = data[segment_from_index: segment_to_index + 1] |
|
|
|
segment_data = data[segment_from_index: segment_to_index + 1] |
|
|
|
if len(segment_data) == 0: |
|
|
|
if len(segment_data) == 0: |
|
|
|
continue |
|
|
|
continue |
|
|
|
self.ipats.append(segment_from_index + int((segment_to_index - segment_from_index) / 2)) |
|
|
|
x = segment_from_index + int((segment_to_index - segment_from_index) / 2) |
|
|
|
|
|
|
|
self.ipats.append(x) |
|
|
|
|
|
|
|
segment_data = data[x - WINDOW_SIZE : x + WINDOW_SIZE] |
|
|
|
segment_min = min(segment_data) |
|
|
|
segment_min = min(segment_data) |
|
|
|
segment_data = segment_data - segment_min |
|
|
|
segment_data = segment_data - segment_min |
|
|
|
segment_max = max(segment_data) |
|
|
|
|
|
|
|
segment_data = segment_data / segment_max |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
convolve = scipy.signal.fftconvolve(segment_data, segment_data) |
|
|
|
convolve = scipy.signal.fftconvolve(segment_data, segment_data) |
|
|
|
convolve_list.append(max(convolve)) |
|
|
|
convolve_list.append(max(convolve)) |
|
|
|
|
|
|
|
|
|
|
@ -57,35 +56,29 @@ class GeneralModel(Model): |
|
|
|
|
|
|
|
|
|
|
|
return self.state |
|
|
|
return self.state |
|
|
|
|
|
|
|
|
|
|
|
def do_predict(self, dataframe: pd.DataFrame): |
|
|
|
def do_predict(self, dataframe: pd.DataFrame) -> list: |
|
|
|
data = dataframe['value'] |
|
|
|
data = dataframe['value'] |
|
|
|
pat_data = data[self.ipats[0] - WINDOW_SIZE: self.ipats[0] + WINDOW_SIZE] |
|
|
|
pat_data = data[self.ipats[0] - WINDOW_SIZE: self.ipats[0] + WINDOW_SIZE] |
|
|
|
x = min(pat_data) |
|
|
|
x = min(pat_data) |
|
|
|
pat_data = pat_data - x |
|
|
|
pat_data = pat_data - x |
|
|
|
y = max(pat_data) |
|
|
|
y = max(pat_data) |
|
|
|
pat_data = pat_data / y |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(WINDOW_SIZE * 2, len(data)): |
|
|
|
for i in range(WINDOW_SIZE * 2, len(data)): |
|
|
|
watch_data = data[i - WINDOW_SIZE * 2: i] |
|
|
|
watch_data = data[i - WINDOW_SIZE * 2: i] |
|
|
|
w = min(watch_data) |
|
|
|
w = min(watch_data) |
|
|
|
watch_data = watch_data - w |
|
|
|
watch_data = watch_data - w |
|
|
|
r = max(watch_data) |
|
|
|
|
|
|
|
if r < y: |
|
|
|
|
|
|
|
watch_data = watch_data / y |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
watch_data = watch_data / r |
|
|
|
|
|
|
|
conv = scipy.signal.fftconvolve(pat_data, watch_data) |
|
|
|
conv = scipy.signal.fftconvolve(pat_data, watch_data) |
|
|
|
self.all_conv.append(max(conv)) |
|
|
|
self.all_conv.append(max(conv)) |
|
|
|
all_conv_peaks = utils.peak_finder(self.all_conv, WINDOW_SIZE * 2) |
|
|
|
all_conv_peaks = utils.peak_finder(self.all_conv, WINDOW_SIZE * 2) |
|
|
|
|
|
|
|
|
|
|
|
filtered = self.__filter_prediction(all_conv_peaks, data) |
|
|
|
filtered = self.__filter_prediction(all_conv_peaks, data) |
|
|
|
|
|
|
|
filtered = set(item + WINDOW_SIZE for item in filtered) |
|
|
|
# TODO: convert from ns to ms more proper way (not dividing by 10^6) |
|
|
|
# TODO: convert from ns to ms more proper way (not dividing by 10^6) |
|
|
|
return [(dataframe['timestamp'][x - 1].value / 1000000, dataframe['timestamp'][x + 1].value / 1000000) for x in filtered] |
|
|
|
return [(dataframe['timestamp'][x - 1].value / 1000000, dataframe['timestamp'][x + 1].value / 1000000) for x in filtered] |
|
|
|
|
|
|
|
|
|
|
|
def __filter_prediction(self, segments: list, data: list): |
|
|
|
def __filter_prediction(self, segments: list, data: list): |
|
|
|
if len(segments) == 0 or len(self.ipats) == 0: |
|
|
|
if len(segments) == 0 or len(self.ipats) == 0: |
|
|
|
segments = [] |
|
|
|
return [] |
|
|
|
return segments |
|
|
|
|
|
|
|
delete_list = [] |
|
|
|
delete_list = [] |
|
|
|
|
|
|
|
|
|
|
|
for val in segments: |
|
|
|
for val in segments: |
|
|
@ -95,4 +88,4 @@ class GeneralModel(Model): |
|
|
|
for item in delete_list: |
|
|
|
for item in delete_list: |
|
|
|
segments.remove(item) |
|
|
|
segments.remove(item) |
|
|
|
|
|
|
|
|
|
|
|
return segments |
|
|
|
return set(segments) |
|
|
|