From 993cd7f35bee6b433437a86d3262d7ad361912fe Mon Sep 17 00:00:00 2001 From: rozetko Date: Thu, 14 Jun 2018 22:35:09 +0300 Subject: [PATCH] 172-make-peaks-detector-work --- analytics/pattern_detection_model.py | 11 +++++++---- analytics/peaks_detector.py | 10 ++-------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/analytics/pattern_detection_model.py b/analytics/pattern_detection_model.py index 7680d20..2a556ff 100644 --- a/analytics/pattern_detection_model.py +++ b/analytics/pattern_detection_model.py @@ -56,10 +56,13 @@ class PatternDetectionModel: start_index, stop_index = 0, len(dataframe) if len(segments) > 0: min_time, max_time = segments_box(segments) - start_index = dataframe[dataframe['timestamp'] >= min_time].index[0] - stop_index = dataframe[dataframe['timestamp'] > max_time].index[0] - start_index = max(start_index - window_size, 0) - stop_index = min(stop_index + window_size, len(dataframe)) + try: + start_index = dataframe[dataframe['timestamp'] >= min_time].index[0] + stop_index = dataframe[dataframe['timestamp'] > max_time].index[0] + start_index = max(start_index - window_size, 0) + stop_index = min(stop_index + window_size, len(dataframe)) + except IndexError: + pass dataframe = dataframe[start_index:stop_index] diff --git a/analytics/peaks_detector.py b/analytics/peaks_detector.py index 3ea1138..5d9f0c9 100644 --- a/analytics/peaks_detector.py +++ b/analytics/peaks_detector.py @@ -34,8 +34,6 @@ class PeaksDetector: array = array[window_size:-window_size] filtered = np.subtract(array, filtered) - import matplotlib.pyplot as plt - # filtered = np.convolve(array, step, mode='valid') # print(len(array)) # print(len(filtered)) @@ -53,12 +51,8 @@ class PeaksDetector: data = filtered data /= data.max() - #plt.plot(array[:1000]) - plt.plot(data[:1000]) - plt.show() - result = step_detect.find_steps(data, 0.1) - return [dataframe.index[x + window_size] for x in result] + return [(dataframe.index[x], dataframe.index[x + window_size]) for x in result] def save(self, model_filename): pass @@ -68,4 +62,4 @@ class PeaksDetector: def load(self, model_filename): pass # with open(model_filename, 'rb') as file: - # self.clf, self.scaler = pickle.load(file) \ No newline at end of file + # self.clf, self.scaler = pickle.load(file)