diff --git a/analytics/analytics/models/drop_model.py b/analytics/analytics/models/drop_model.py index 9857209..78d010b 100644 --- a/analytics/analytics/models/drop_model.py +++ b/analytics/analytics/models/drop_model.py @@ -42,7 +42,7 @@ class DropModel(Model): continue confidence = utils.find_confidence(segment_data) confidences.append(confidence) - segment_cent_index, drop_height, drop_length = utils.find_parameters(segment_data, segment_from_index, "drop") + segment_cent_index, drop_height, drop_length = utils.find_parameters(segment_data, segment_from_index, 'drop') drop_height_list.append(drop_height) drop_length_list.append(drop_length) self.idrops.append(segment_cent_index) @@ -59,7 +59,7 @@ class DropModel(Model): segment_from_index, segment_to_index, segment_data = utils.parse_segment(segment, dataframe) if len(segment_data) == 0: continue - segment_cent_index = utils.find_drop_parameters(segment_data, segment_from_index)[0] + segment_cent_index = utils.find_parameters(segment_data, segment_from_index, 'drop')[0] deleted_drop = utils.get_interval(data, segment_cent_index, self.state['WINDOW_SIZE']) deleted_drop = utils.subtract_min_without_nan(deleted_drop) del_conv_drop = scipy.signal.fftconvolve(deleted_drop, self.model_drop) diff --git a/analytics/analytics/models/jump_model.py b/analytics/analytics/models/jump_model.py index 5357ccf..77c9f88 100644 --- a/analytics/analytics/models/jump_model.py +++ b/analytics/analytics/models/jump_model.py @@ -1,7 +1,6 @@ from models import Model import utils -from utils.segments import parse_segment import numpy as np import pandas as pd import scipy.signal @@ -38,13 +37,13 @@ class JumpModel(Model): patterns_list = [] for segment in segments: if segment['labeled']: - segment_from_index, segment_to_index, segment_data = parse_segment(segment, dataframe) + segment_from_index, segment_to_index, segment_data = utils.parse_segment(segment, dataframe) percent_of_nans = segment_data.isnull().sum() / len(segment_data) if percent_of_nans > 0 or len(segment_data) == 0: continue confidence = utils.find_confidence(segment_data) confidences.append(confidence) - segment_cent_index, jump_height, jump_length = utils.find_parameters(segment_data, segment_from_index, "jump") + segment_cent_index, jump_height, jump_length = utils.find_parameters(segment_data, segment_from_index, 'jump') jump_height_list.append(jump_height) jump_length_list.append(jump_length) self.ijumps.append(segment_cent_index) @@ -61,7 +60,7 @@ class JumpModel(Model): segment_from_index, segment_to_index, segment_data = utils.parse_segment(segment, dataframe) if len(segment_data) == 0: continue - segment_cent_index = utils.find_jump_parameters(segment_data, segment_from_index)[0] + segment_cent_index = utils.find_parameters(segment_data, segment_from_index, 'jump')[0] deleted_jump = utils.get_interval(data, segment_cent_index, self.state['WINDOW_SIZE']) deleted_jump = utils.subtract_min_without_nan(labeled_jump) del_conv_jump = scipy.signal.fftconvolve(deleted_jump, self.model_jump) diff --git a/analytics/analytics/models/peak_model.py b/analytics/analytics/models/peak_model.py index f590372..a0c8561 100644 --- a/analytics/analytics/models/peak_model.py +++ b/analytics/analytics/models/peak_model.py @@ -52,7 +52,7 @@ class PeakModel(Model): del_conv_list = [] for segment in segments: if segment['deleted']: - segment_from_index, segment_to_index, segment_data = parse_segment(segment, dataframe) + segment_from_index, segment_to_index, segment_data = utils.parse_segment(segment, dataframe) if len(segment_data) == 0: continue del_max_index = segment_data.idxmax() diff --git a/analytics/tests/test_dataset.py b/analytics/tests/test_dataset.py index fa6d65f..77d35bd 100644 --- a/analytics/tests/test_dataset.py +++ b/analytics/tests/test_dataset.py @@ -23,6 +23,78 @@ class TestDataset(unittest.TestCase): model.fit(dataframe, segments, dict()) except ValueError: self.fail('Model {} raised unexpectedly'.format(model_name)) + + def test_peak_antisegments(self): + data_val = [1.0, 1.0, 1.0, 2.0, 3.0, 2.0, 1.0, 1.0, 1.0, 1.0, 5.0, 7.0, 5.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] + dataframe = create_dataframe(data_val) + segments = [{'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000010, 'to': 1523889000012, 'labeled': True, 'deleted': False}, + {'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000003, 'to': 1523889000005, 'labeled': False, 'deleted': True}] + + try: + model = models.PeakModel() + model_name = model.__class__.__name__ + model.fit(dataframe, segments, dict()) + except ValueError: + self.fail('Model {} raised unexpectedly'.format(model_name)) + + def test_jump_antisegments(self): + data_val = [1.0, 1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0, 1.0, 1.0, 1.0, 1.0, 9.0, 9.0, 9.0, 9.0, 9.0, 1.0, 1.0] + dataframe = create_dataframe(data_val) + segments = [{'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000010, 'to': 1523889000016, 'labeled': True, 'deleted': False}, + {'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000002, 'to': 1523889000008, 'labeled': False, 'deleted': True}] + + try: + model = models.JumpModel() + model_name = model.__class__.__name__ + model.fit(dataframe, segments, dict()) + except ValueError: + self.fail('Model {} raised unexpectedly'.format(model_name)) + + def test_trough_antisegments(self): + data_val = [9.0, 9.0, 9.0, 9.0, 7.0, 4.0, 7.0, 9.0, 9.0, 9.0, 5.0, 1.0, 5.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0] + dataframe = create_dataframe(data_val) + segments = [{'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000010, 'to': 1523889000012, 'labeled': True, 'deleted': False}, + {'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000003, 'to': 1523889000005, 'labeled': False, 'deleted': True}] + + try: + model = models.TroughModel() + model_name = model.__class__.__name__ + model.fit(dataframe, segments, dict()) + except ValueError: + self.fail('Model {} raised unexpectedly'.format(model_name)) + + def test_drop_antisegments(self): + data_val = [9.0, 9.0, 9.0, 9.0, 9.0, 5.0, 5.0, 5.0, 5.0, 9.0, 9.0, 9.0, 9.0, 1.0, 1.0, 1.0, 1.0, 1.0, 9.0, 9.0] + dataframe = create_dataframe(data_val) + segments = [{'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000010, 'to': 1523889000016, 'labeled': True, 'deleted': False}, + {'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000002, 'to': 1523889000008, 'labeled': False, 'deleted': True}] + + try: + model = models.DropModel() + model_name = model.__class__.__name__ + model.fit(dataframe, segments, dict()) + except ValueError: + self.fail('Model {} raised unexpectedly'.format(model_name)) + + def test_general_antisegments(self): + data_val = [1.0, 2.0, 1.0, 2.0, 5.0, 6.0, 3.0, 2.0, 1.0, 1.0, 8.0, 9.0, 8.0, 1.0, 2.0, 3.0, 2.0, 1.0, 1.0, 2.0] + dataframe = create_dataframe(data_val) + segments = [{'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000010, 'to': 1523889000012, 'labeled': True, 'deleted': False}, + {'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000003, 'to': 1523889000005, 'labeled': False, 'deleted': True}] + + try: + model = models.GeneralModel() + model_name = model.__class__.__name__ + model.fit(dataframe, segments, dict()) + except ValueError: + self.fail('Model {} raised unexpectedly'.format(model_name)) if __name__ == '__main__': unittest.main() + +def create_dataframe(data_val: list) -> pd.DataFrame: + data_ind = [1523889000000 + i for i in range(len(data_val))] + data = {'timestamp': data_ind, 'value': data_val} + dataframe = pd.DataFrame(data) + dataframe['timestamp'] = pd.to_datetime(dataframe['timestamp'], unit='ms') + return dataframe