Ejemplo n.º 1
0
    def test_three_value_segment(self):
        data_val = [
            1.0, 1.0, 1.0, 1.0, 1.0, 5.0, 2.0, 5.0, 5.0, 1.0, 1.0, 1.0, 1.0,
            9.0, 9.0, 9.0, 9.0, 2.0, 3.0, 4.0, 5.0, 4.0, 2.0, 1.0, 3.0, 4.0
        ]
        dataframe = create_dataframe(data_val)
        segments = [{
            '_id': 'Esl7uetLhx4lCqHa',
            'analyticUnitId': 'opnICRJwOmwBELK8',
            'from': 1523889000004,
            'to': 1523889000006,
            'labeled': True,
            'deleted': False
        }]
        segments = [Segment.from_json(segment) for segment in segments]

        model_instances = [
            models.GeneralModel(),
            models.PeakModel(),
        ]
        try:
            for model in model_instances:
                model_name = model.__class__.__name__
                model.state = model.get_state(None)
                model.fit(dataframe, segments, 'test')
        except ValueError:
            self.fail('Model {} raised unexpectedly'.format(model_name))
Ejemplo n.º 2
0
    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
        }]
        segments = [Segment.from_json(segment) for segment in segments]

        try:
            model = models.GeneralModel()
            model_name = model.__class__.__name__
            model.state = model.get_state(None)
            model.fit(dataframe, segments, 'test')
        except ValueError:
            self.fail('Model {} raised unexpectedly'.format(model_name))
Ejemplo n.º 3
0
 def test_general_for_two_labeling(self):
     data_val = [1.0, 2.0, 5.0, 2.0, 1.0, 1.0, 3.0, 6.0, 4.0, 2.0, 1.0, 0, 0]
     dataframe = create_dataframe(data_val)
     segments = [{'_id': 'Esl7uetLhx4lCqHa', 'analyticUnitId': 'opnICRJwOmwBELK8', 'from': 1523889000001, 'to': 1523889000003, 'labeled': True, 'deleted': False}]
     model = models.GeneralModel()
     model.fit(dataframe, segments, dict())
     result = len(data_val) + 1
     for _ in range(2):
         model.do_detect(dataframe)
         max_pattern_index = max(model.do_detect(dataframe))
         self.assertLessEqual(max_pattern_index, result)
Ejemplo n.º 4
0
def resolve_model_by_pattern(pattern: str) -> models.Model:
    if pattern == 'GENERAL':
        return models.GeneralModel()
    if pattern == 'PEAK':
        return models.PeakModel()
    if pattern == 'TROUGH':
        return models.TroughModel()
    if pattern == 'DROP':
        return models.DropModel()
    if pattern == 'JUMP':
        return models.JumpModel()
    if pattern == 'CUSTOM':
        return models.CustomModel()
    raise ValueError('Unknown pattern "%s"' % pattern)
Ejemplo n.º 5
0
 def test_problem_data_for_random_model(self):
     problem_data = [
         2.0, 3.0, 3.0, 3.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
         2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0,
         5.0, 5.0, 5.0, 5.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0,
         3.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0,
         3.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0,
         2.0, 2.0, 2.0, 6.0, 7.0, 8.0, 8.0, 4.0, 2.0, 2.0, 3.0, 3.0, 3.0,
         4.0, 4.0, 4.0, 4.0, 3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 2.0, 2.0, 2.0,
         2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 2.0, 2.0,
         2.0, 2.0, 3.0, 4.0, 4.0, 4.0, 4.0, 4.0, 6.0, 5.0, 4.0, 4.0, 3.0,
         3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 2.0, 3.0, 3.0, 2.0,
         2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 8.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
         2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0
     ]
     data = create_dataframe(problem_data)
     cache = {
         'patternCenter': [5, 50],
         'patternModel': [],
         'windowSize': 2,
         'convolveMin': 0,
         'convolveMax': 0,
         'convDelMin': 0,
         'convDelMax': 0,
     }
     max_ws = 20
     iteration = 1
     for ws in range(1, max_ws):
         for _ in range(iteration):
             pattern_model = create_random_model(ws)
             convolve = scipy.signal.fftconvolve(pattern_model,
                                                 pattern_model)
             cache['windowSize'] = ws
             cache['patternModel'] = pattern_model
             cache['convolveMin'] = max(convolve)
             cache['convolveMax'] = max(convolve)
             try:
                 model = models.GeneralModel()
                 model.state = model.get_state(cache)
                 model_name = model.__class__.__name__
                 model.detect(data, 'test')
             except ValueError:
                 self.fail(
                     'Model {} raised unexpectedly with av_model {} and window size {}'
                     .format(model_name, pattern_model, ws))
Ejemplo n.º 6
0
    def test_models_with_corrupted_dataframe(self):
        data = [[1523889000000 + i, float('nan')] for i in range(10)]
        dataframe = pd.DataFrame(data, columns=['timestamp', 'value'])
        segments = []

        model_instances = [
            models.JumpModel(),
            models.DropModel(),
            models.GeneralModel(),
            models.PeakModel(),
            models.TroughModel()
        ]
        try:
            for model in model_instances:
                model_name = model.__class__.__name__
                model.fit(dataframe, segments, dict())
        except ValueError:
            self.fail('Model {} raised unexpectedly'.format(model_name))
Ejemplo n.º 7
0
    def test_models_with_corrupted_dataframe(self):
        data = [[1523889000000 + i, float('nan')] for i in range(10)]
        dataframe = pd.DataFrame(data, columns=['timestamp', 'value'])
        segments = []

        model_instances = [
            models.JumpModel(),
            models.DropModel(),
            models.GeneralModel(),
            models.PeakModel(),
            models.TroughModel()
        ]

        for model in model_instances:
            model_name = model.__class__.__name__
            model.state = model.get_state(None)
            with self.assertRaises(AssertionError):
                model.fit(dataframe, segments, 'test')