|
|
@ -1,5 +1,7 @@ |
|
|
|
import numpy as np |
|
|
|
import numpy as np |
|
|
|
import pickle |
|
|
|
import pickle |
|
|
|
|
|
|
|
import scipy.signal |
|
|
|
|
|
|
|
from scipy.fftpack import fft |
|
|
|
from scipy.signal import argrelextrema |
|
|
|
from scipy.signal import argrelextrema |
|
|
|
|
|
|
|
|
|
|
|
def is_intersect(target_segment, segments): |
|
|
|
def is_intersect(target_segment, segments): |
|
|
@ -22,21 +24,34 @@ class StepDetector: |
|
|
|
self.pattern = pattern |
|
|
|
self.pattern = pattern |
|
|
|
self.segments = [] |
|
|
|
self.segments = [] |
|
|
|
self.confidence = 1.5 |
|
|
|
self.confidence = 1.5 |
|
|
|
|
|
|
|
self.convolve_max = 570000 |
|
|
|
|
|
|
|
|
|
|
|
def fit(self, dataframe, segments): |
|
|
|
def fit(self, dataframe, segments): |
|
|
|
data = dataframe['value'] |
|
|
|
data = dataframe['value'] |
|
|
|
confidences = [] |
|
|
|
confidences = [] |
|
|
|
|
|
|
|
convolve_list = [] |
|
|
|
for segment in segments: |
|
|
|
for segment in segments: |
|
|
|
if segment['labeled']: |
|
|
|
if segment['labeled']: |
|
|
|
segment_data = data[segment['start'] : segment['finish'] + 1] |
|
|
|
segment_data = data[segment['start'] : segment['finish'] + 1] |
|
|
|
segment_min = min(segment_data) |
|
|
|
segment_min = min(segment_data) |
|
|
|
segment_max = max(segment_data) |
|
|
|
segment_max = max(segment_data) |
|
|
|
confidences.append(0.24 * (segment_max - segment_min)) |
|
|
|
confidences.append(0.24 * (segment_max - segment_min)) |
|
|
|
|
|
|
|
flat_segment = segment_data.rolling(window=5).mean() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
segment_min_index = flat_segment.idxmin() - 5 |
|
|
|
|
|
|
|
labeled_drop = data[segment_min_index - 60 : segment_min_index + 60] |
|
|
|
|
|
|
|
convolve = scipy.signal.fftconvolve(labeled_drop, labeled_drop) |
|
|
|
|
|
|
|
convolve_list.append(max(convolve)) |
|
|
|
|
|
|
|
|
|
|
|
if len(confidences) > 0: |
|
|
|
if len(confidences) > 0: |
|
|
|
self.confidence = min(confidences) |
|
|
|
self.confidence = min(confidences) |
|
|
|
else: |
|
|
|
else: |
|
|
|
self.confidence = 1.5 |
|
|
|
self.confidence = 1.5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(convolve_list) > 0: |
|
|
|
|
|
|
|
self.convolve_max = max(convolve_list) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
self.convolve_max = 570000 |
|
|
|
|
|
|
|
|
|
|
|
def predict(self, dataframe): |
|
|
|
def predict(self, dataframe): |
|
|
|
data = dataframe['value'] |
|
|
|
data = dataframe['value'] |
|
|
@ -66,15 +81,22 @@ class StepDetector: |
|
|
|
|
|
|
|
|
|
|
|
def __filter_prediction(self, segments, all_max_flatten_data): |
|
|
|
def __filter_prediction(self, segments, all_max_flatten_data): |
|
|
|
delete_list = [] |
|
|
|
delete_list = [] |
|
|
|
for i in segments: |
|
|
|
variance_error = int(0.004 * len(all_max_flatten_data)) |
|
|
|
new_data = all_max_flatten_data[i-50:i+250] |
|
|
|
if variance_error > 200: |
|
|
|
min_value = 100 |
|
|
|
variance_error = 200 |
|
|
|
for val in new_data: |
|
|
|
for i in range(1, len(segments)): |
|
|
|
if val < min_value: |
|
|
|
if segments[i] < segments[i - 1] + variance_error: |
|
|
|
min_value = val |
|
|
|
delete_list.append(segments[i]) |
|
|
|
if all_max_flatten_data[i] > min_value: |
|
|
|
for item in delete_list: |
|
|
|
delete_list.append(i) |
|
|
|
segments.remove(item) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete_list = [] |
|
|
|
|
|
|
|
pattern_data = all_max_flatten_data[segments[0] - 60 : segments[0] + 60] |
|
|
|
|
|
|
|
for segment in segments: |
|
|
|
|
|
|
|
convol_data = all_max_flatten_data[segment - 60 : segment + 60] |
|
|
|
|
|
|
|
conv = scipy.signal.fftconvolve(pattern_data, convol_data) |
|
|
|
|
|
|
|
if max(conv) > self.convolve_max * 1.05: |
|
|
|
|
|
|
|
delete_list.append(segment) |
|
|
|
for item in delete_list: |
|
|
|
for item in delete_list: |
|
|
|
segments.remove(item) |
|
|
|
segments.remove(item) |
|
|
|
|
|
|
|
|
|
|
@ -82,11 +104,11 @@ class StepDetector: |
|
|
|
|
|
|
|
|
|
|
|
def save(self, model_filename): |
|
|
|
def save(self, model_filename): |
|
|
|
with open(model_filename, 'wb') as file: |
|
|
|
with open(model_filename, 'wb') as file: |
|
|
|
pickle.dump((self.confidence), file) |
|
|
|
pickle.dump((self.confidence, self.convolve_max), file) |
|
|
|
|
|
|
|
|
|
|
|
def load(self, model_filename): |
|
|
|
def load(self, model_filename): |
|
|
|
try: |
|
|
|
try: |
|
|
|
with open(model_filename, 'rb') as file: |
|
|
|
with open(model_filename, 'rb') as file: |
|
|
|
self.confidence = pickle.load(file) |
|
|
|
(self.confidence, self.convolve_max) = pickle.load(file) |
|
|
|
except: |
|
|
|
except: |
|
|
|
pass |
|
|
|
pass |
|
|
|