You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
5.6 KiB
138 lines
5.6 KiB
6 years ago
|
from models import Model, AnalyticUnitCache
|
||
6 years ago
|
|
||
6 years ago
|
import scipy.signal
|
||
|
from scipy.fftpack import fft
|
||
6 years ago
|
from scipy.signal import argrelextrema
|
||
6 years ago
|
from scipy.stats import gaussian_kde
|
||
7 years ago
|
|
||
6 years ago
|
import utils
|
||
6 years ago
|
import numpy as np
|
||
6 years ago
|
import pandas as pd
|
||
6 years ago
|
from typing import Optional
|
||
6 years ago
|
|
||
6 years ago
|
WINDOW_SIZE = 400
|
||
6 years ago
|
|
||
6 years ago
|
class DropModel(Model):
|
||
6 years ago
|
def __init__(self):
|
||
6 years ago
|
super()
|
||
7 years ago
|
self.segments = []
|
||
6 years ago
|
self.idrops = []
|
||
6 years ago
|
self.state = {
|
||
|
'confidence': 1.5,
|
||
6 years ago
|
'convolve_max': WINDOW_SIZE,
|
||
|
'DROP_HEIGHT': 1,
|
||
|
'DROP_LENGTH': 1,
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
def fit(self, dataframe: pd.DataFrame, segments: list, cache: Optional[AnalyticUnitCache]) -> AnalyticUnitCache:
|
||
|
if type(cache) is AnalyticUnitCache:
|
||
|
self.state = cache
|
||
6 years ago
|
self.segments = segments
|
||
6 years ago
|
|
||
6 years ago
|
data = dataframe['value']
|
||
6 years ago
|
confidences = []
|
||
6 years ago
|
convolve_list = []
|
||
6 years ago
|
drop_height_list = []
|
||
|
drop_length_list = []
|
||
6 years ago
|
for segment in segments:
|
||
|
if segment['labeled']:
|
||
6 years ago
|
segment_from_index = utils.timestamp_to_index(dataframe, pd.to_datetime(segment['from']))
|
||
|
segment_to_index = utils.timestamp_to_index(dataframe, pd.to_datetime(segment['to']))
|
||
6 years ago
|
|
||
6 years ago
|
segment_data = data[segment_from_index : segment_to_index + 1].reset_index(drop=True)
|
||
6 years ago
|
segment_min = min(segment_data)
|
||
|
segment_max = max(segment_data)
|
||
6 years ago
|
confidences.append(0.20 * (segment_max - segment_min))
|
||
|
flat_segment = segment_data.rolling(window=5).mean()
|
||
|
pdf = gaussian_kde(flat_segment.dropna())
|
||
|
x = np.linspace(flat_segment.dropna().min(), flat_segment.dropna().max(), len(flat_segment.dropna()))
|
||
|
y = pdf(x)
|
||
|
ax_list = []
|
||
|
for i in range(len(x)):
|
||
|
ax_list.append([x[i], y[i]])
|
||
|
ax_list = np.array(ax_list, np.float32)
|
||
|
antipeaks_kde = argrelextrema(np.array(ax_list), np.less)[0]
|
||
|
peaks_kde = argrelextrema(np.array(ax_list), np.greater)[0]
|
||
|
min_peak_index = peaks_kde[0]
|
||
|
max_peak_index = peaks_kde[1]
|
||
|
segment_median = ax_list[antipeaks_kde[0], 0]
|
||
|
segment_min_line = ax_list[min_peak_index, 0]
|
||
|
segment_max_line = ax_list[max_peak_index, 0]
|
||
|
#print(segment_min_line, segment_max_line)
|
||
|
drop_height = 0.95 * (segment_max_line - segment_min_line)
|
||
|
drop_height_list.append(drop_height)
|
||
6 years ago
|
drop_length = utils.find_drop_length(segment_data, segment_min_line, segment_max_line)
|
||
|
drop_length_list.append(drop_length)
|
||
6 years ago
|
cen_ind = utils.drop_intersection(flat_segment, segment_median) #finds all interseprions with median
|
||
|
drop_center = cen_ind[0]
|
||
6 years ago
|
segment_cent_index = drop_center - 5 + segment_from_index
|
||
6 years ago
|
self.idrops.append(segment_cent_index)
|
||
|
labeled_drop = data[segment_cent_index - WINDOW_SIZE : segment_cent_index + WINDOW_SIZE]
|
||
|
labeled_min = min(labeled_drop)
|
||
|
for value in labeled_drop:
|
||
|
value = value - labeled_min
|
||
|
|
||
6 years ago
|
convolve = scipy.signal.fftconvolve(labeled_drop, labeled_drop)
|
||
|
convolve_list.append(max(convolve))
|
||
|
|
||
6 years ago
|
if len(confidences) > 0:
|
||
6 years ago
|
self.state['confidence'] = float(min(confidences))
|
||
6 years ago
|
else:
|
||
6 years ago
|
self.state['confidence'] = 1.5
|
||
7 years ago
|
|
||
6 years ago
|
if len(convolve_list) > 0:
|
||
6 years ago
|
self.state['convolve_max'] = float(max(convolve_list))
|
||
6 years ago
|
else:
|
||
6 years ago
|
self.state['convolve_max'] = WINDOW_SIZE
|
||
|
|
||
|
if len(drop_height_list) > 0:
|
||
6 years ago
|
self.state['DROP_HEIGHT'] = int(min(drop_height_list))
|
||
6 years ago
|
else:
|
||
|
self.state['DROP_HEIGHT'] = 1
|
||
|
|
||
|
if len(drop_length_list) > 0:
|
||
6 years ago
|
self.state['DROP_LENGTH'] = int(max(drop_length_list))
|
||
6 years ago
|
else:
|
||
|
self.state['DROP_LENGTH'] = 1
|
||
7 years ago
|
|
||
6 years ago
|
return self.state
|
||
7 years ago
|
|
||
6 years ago
|
def do_predict(self, dataframe: pd.DataFrame):
|
||
6 years ago
|
data = dataframe['value']
|
||
6 years ago
|
possible_drops = utils.find_drop(data, self.state['DROP_HEIGHT'], self.state['DROP_LENGTH'] + 1)
|
||
6 years ago
|
filtered = self.__filter_prediction(possible_drops, data)
|
||
|
return [(dataframe['timestamp'][x - 1].value, dataframe['timestamp'][x + 1].value) for x in filtered]
|
||
6 years ago
|
|
||
6 years ago
|
def __filter_prediction(self, segments: list, data: list):
|
||
6 years ago
|
delete_list = []
|
||
6 years ago
|
variance_error = int(0.004 * len(data))
|
||
|
if variance_error > 50:
|
||
|
variance_error = 50
|
||
|
|
||
6 years ago
|
for i in range(1, len(segments)):
|
||
|
if segments[i] < segments[i - 1] + variance_error:
|
||
|
delete_list.append(segments[i])
|
||
|
for item in delete_list:
|
||
|
segments.remove(item)
|
||
|
delete_list = []
|
||
6 years ago
|
|
||
|
if len(segments) == 0 or len(self.idrops) == 0 :
|
||
|
segments = []
|
||
|
return segments
|
||
|
pattern_data = data[self.idrops[0] - WINDOW_SIZE : self.idrops[0] + WINDOW_SIZE]
|
||
6 years ago
|
for segment in segments:
|
||
6 years ago
|
if segment > WINDOW_SIZE and segment < (len(data) - WINDOW_SIZE):
|
||
|
convol_data = data[segment - WINDOW_SIZE : segment + WINDOW_SIZE]
|
||
6 years ago
|
conv = scipy.signal.fftconvolve(pattern_data, convol_data)
|
||
6 years ago
|
if conv[WINDOW_SIZE*2] > self.state['convolve_max'] * 1.2 or conv[WINDOW_SIZE*2] < self.state['convolve_max'] * 0.8:
|
||
6 years ago
|
delete_list.append(segment)
|
||
|
else:
|
||
6 years ago
|
delete_list.append(segment)
|
||
6 years ago
|
for item in delete_list:
|
||
|
segments.remove(item)
|
||
6 years ago
|
#print(segments)
|
||
|
for idrop in self.idrops:
|
||
|
segments.append(idrop)
|
||
6 years ago
|
|
||
|
return segments
|