Browse Source

Error name parse segment is not defined #315 (#322)

Fixed: Error name parse segment is not defined #315
pull/1/head
Alexandr Velikiy 6 years ago committed by Evgeny Smyshlyaev
parent
commit
5edc0b1977
  1. 4
      analytics/analytics/models/drop_model.py
  2. 7
      analytics/analytics/models/jump_model.py
  3. 2
      analytics/analytics/models/peak_model.py
  4. 72
      analytics/tests/test_dataset.py

4
analytics/analytics/models/drop_model.py

@ -42,7 +42,7 @@ class DropModel(Model):
continue continue
confidence = utils.find_confidence(segment_data) confidence = utils.find_confidence(segment_data)
confidences.append(confidence) 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_height_list.append(drop_height)
drop_length_list.append(drop_length) drop_length_list.append(drop_length)
self.idrops.append(segment_cent_index) 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) segment_from_index, segment_to_index, segment_data = utils.parse_segment(segment, dataframe)
if len(segment_data) == 0: if len(segment_data) == 0:
continue 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.get_interval(data, segment_cent_index, self.state['WINDOW_SIZE'])
deleted_drop = utils.subtract_min_without_nan(deleted_drop) deleted_drop = utils.subtract_min_without_nan(deleted_drop)
del_conv_drop = scipy.signal.fftconvolve(deleted_drop, self.model_drop) del_conv_drop = scipy.signal.fftconvolve(deleted_drop, self.model_drop)

7
analytics/analytics/models/jump_model.py

@ -1,7 +1,6 @@
from models import Model from models import Model
import utils import utils
from utils.segments import parse_segment
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import scipy.signal import scipy.signal
@ -38,13 +37,13 @@ class JumpModel(Model):
patterns_list = [] patterns_list = []
for segment in segments: for segment in segments:
if segment['labeled']: 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) percent_of_nans = segment_data.isnull().sum() / len(segment_data)
if percent_of_nans > 0 or len(segment_data) == 0: if percent_of_nans > 0 or len(segment_data) == 0:
continue continue
confidence = utils.find_confidence(segment_data) confidence = utils.find_confidence(segment_data)
confidences.append(confidence) 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_height_list.append(jump_height)
jump_length_list.append(jump_length) jump_length_list.append(jump_length)
self.ijumps.append(segment_cent_index) 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) segment_from_index, segment_to_index, segment_data = utils.parse_segment(segment, dataframe)
if len(segment_data) == 0: if len(segment_data) == 0:
continue 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.get_interval(data, segment_cent_index, self.state['WINDOW_SIZE'])
deleted_jump = utils.subtract_min_without_nan(labeled_jump) deleted_jump = utils.subtract_min_without_nan(labeled_jump)
del_conv_jump = scipy.signal.fftconvolve(deleted_jump, self.model_jump) del_conv_jump = scipy.signal.fftconvolve(deleted_jump, self.model_jump)

2
analytics/analytics/models/peak_model.py

@ -52,7 +52,7 @@ class PeakModel(Model):
del_conv_list = [] del_conv_list = []
for segment in segments: for segment in segments:
if segment['deleted']: 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: if len(segment_data) == 0:
continue continue
del_max_index = segment_data.idxmax() del_max_index = segment_data.idxmax()

72
analytics/tests/test_dataset.py

@ -23,6 +23,78 @@ class TestDataset(unittest.TestCase):
model.fit(dataframe, segments, dict()) model.fit(dataframe, segments, dict())
except ValueError: except ValueError:
self.fail('Model {} raised unexpectedly'.format(model_name)) 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__': if __name__ == '__main__':
unittest.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

Loading…
Cancel
Save