Browse Source

Drops pattern ValueError: All elements of patterns_list should have same length #414 (#418)

pull/1/head
Alexandr Velikiy 6 years ago committed by rozetko
parent
commit
22fe2cf6b7
  1. 5
      analytics/analytics/models/model.py
  2. 32
      analytics/analytics/utils/common.py
  3. 22
      analytics/tests/test_dataset.py
  4. 14
      analytics/tests/test_utils.py

5
analytics/analytics/models/model.py

@ -79,11 +79,6 @@ class Model(ABC):
self.state['WINDOW_SIZE'] = math.ceil(max_length / 2) if max_length else 0
model, model_type = self.get_model_type()
learning_info = self.get_parameters_from_segments(dataframe, labeled, deleted, model, model_type)
if self.state.get('pattern_center') and self.state.get('pattern_model'):
for center in self.state['pattern_center']:
aligned_segment = utils.get_interval(data, center, self.state['WINDOW_SIZE'])
aligned_segment = utils.subtract_min_without_nan(aligned_segment)
learning_info['patterns_list'].append(aligned_segment)
self.do_fit(dataframe, labeled, deleted, learning_info)
return self.state

32
analytics/analytics/utils/common.py

@ -80,21 +80,23 @@ def ar_mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
def get_av_model(patterns_list):
if len(patterns_list) == 0:
return []
x = len(patterns_list[0])
if len(patterns_list) > 1 and len(patterns_list[1]) != x:
raise ValueError('All elements of patterns_list should have same length')
model_pat = []
for i in range(x):
av_val = []
for val in patterns_list:
if type(val) == pd.Series:
val = val.values
av_val.append(val[i])
model_pat.append(ar_mean(av_val))
return model_pat
if not patterns_list: return []
patterns_list = get_same_length(patterns_list)
value_list = list(map(list, zip(*patterns_list)))
return list(map(ar_mean, value_list))
def get_same_length(patterns_list):
for index in range(len(patterns_list)):
if type(patterns_list[index]) == pd.Series:
patterns_list[index] = patterns_list[index].tolist()
patterns_list = list(filter(None, patterns_list))
max_length = max(map(len, patterns_list))
for pat in patterns_list:
if len(pat) < max_length:
length_difference = max_length - len(pat)
added_values = list(0 for _ in range(length_difference))
pat.extend(added_values)
return patterns_list
def close_filtering(pattern_list, win_size):
if len(pattern_list) == 0:

22
analytics/tests/test_dataset.py

@ -229,11 +229,31 @@ class TestDataset(unittest.TestCase):
}
data_val = [1.0, 1.0, 1.0, 4.0, 4.0, 0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 4.0, 4.0, 4.0, 4.0]
dataframe = create_dataframe(data_val)
segments = [{'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000010, 'to': 1523889000012, 'labeled': True, 'deleted': False}]
segments = [{'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 152388900009, 'to': 1523889000013, 'labeled': True, 'deleted': False}]
model = models.JumpModel()
result = model.fit(dataframe, segments, cache)
self.assertEqual(len(result['pattern_center']), 3)
def test_models_for_pattern_model_cache(self):
cache = {
'pattern_center': [4, 12],
'pattern_model': [],
'confidence': 2,
'convolve_max': 8,
'convolve_min': 7,
'WINDOW_SIZE': 2,
'conv_del_min': 0,
'conv_del_max': 0,
}
data_val = [5.0, 5.0, 5.0, 5.0, 1.0, 1.0, 1.0, 1.0, 9.0, 9.0, 9.0, 9.0, 0, 0, 0, 0, 0, 0, 6.0, 6.0, 6.0, 1.0, 1.0, 1.0, 1.0, 1.0]
dataframe = create_dataframe(data_val)
segments = [{'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000019, 'to': 1523889000024, 'labeled': True, 'deleted': False}]
try:
model = models.DropModel()
model_name = model.__class__.__name__
model.fit(dataframe, segments, cache)
except ValueError:
self.fail('Model {} raised unexpectedly'.format(model_name))
if __name__ == '__main__':
unittest.main()

14
analytics/tests/test_utils.py

@ -129,7 +129,12 @@ class TestUtils(unittest.TestCase):
patterns_list = []
result = []
self.assertEqual(utils.get_av_model(patterns_list), result)
def test_get_av_model_normal_data(self):
patterns_list = [[1, 1, 1], [2, 2, 2],[3,3,3]]
result = [2.0, 2.0, 2.0]
self.assertEqual(utils.get_av_model(patterns_list), result)
def test_find_jump_nan_data(self):
data = [np.NaN, np.NaN, np.NaN, np.NaN]
data = pd.Series(data)
@ -207,6 +212,13 @@ class TestUtils(unittest.TestCase):
utils.get_convolve(segemnts, av_model, data, window_size)
except ValueError:
self.fail('Method get_convolve raised unexpectedly')
def test_get_av_model_for_different_length(self):
patterns_list = [[1.0, 1.0, 2.0], [4.0, 4.0], [2.0, 2.0, 2.0], [3.0, 3.0], []]
try:
utils.get_av_model(patterns_list)
except ValueError:
self.fail('Method get_convolve raised unexpectedly')
if __name__ == '__main__':
unittest.main()

Loading…
Cancel
Save