def test_two_categories_high_min_value(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy4.txt') df = read_data(path) twitter = TwitterAnomalyTrendinessDetector(freq='12H', is_multicategory=False) prediction = twitter.predict(df) assert len(prediction[prediction['prediction'] == 1]) == 0
def test_single_category(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy4.txt') df = read_data(path) one_category = df.loc[pd.IndexSlice[:, 'housing'], :].reset_index(level='category', drop=True) ma = STLTrendinessDetector(freq='12H', is_multicategory=False, anomaly_type='or') prediction = ma.predict(one_category) assert len(prediction[prediction['prediction'] == 1]) == 1
def test_read_data(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy.txt') df = read_data(path) index = df.index # assert index is pd.MultiIndex assert len(df) == 16 assert isinstance(df.index, pd.MultiIndex)
def test_two_categories(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy4.txt') df = read_data(path) ma = MovingAverageSeasonalTrendinessDetector(freq='12H', anomaly_type='or', lookback='5D') prediction = ma.predict(df) assert len(prediction[prediction['prediction'] == 1]) == 2
def test_two_categories_high_min_value(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy4.txt') df = read_data(path) ma = STLTrendinessDetector(freq='12H', anomaly_type='or', lookback='5D', min_value=2000) prediction = ma.predict(df) assert len(prediction[prediction['prediction'] == 1]) == 0
def test_y_None(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy.txt') df = read_data(path) X = df[['value']] y = None model = MockModel() models = [model] with pytest.raises(TypeError): eval_models(X, y, models, label_col_name='is_anomaly')
def test_real_model(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy.txt') df = read_data(path) model = MovingAverageSeasonalTrendinessDetector(is_multicategory=True, freq='12H') models = [model] X = df[['value']] y = df[['is_anomaly']] res = eval_models(X, y, models, label_col_name='is_anomaly') print(res)
def test_eval_models_all_false(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy.txt') df = read_data(path) df['is_anomaly'] = 0 model = MockModel() models = [model] X = df[['value']] y = df[['is_anomaly']] res = eval_models(X, y, models, label_col_name='is_anomaly') assert math.isnan(res['MockModel']['f1']) assert res['MockModel']['precision'] == 0 assert math.isnan(res['MockModel']['recall'])
def test_n_splits_big(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy2.txt') df = read_data(path) model = MockModel() models = [model] X = df[['value']] y = df[['is_anomaly']] res = eval_models_CV(X, y, models, n_splits=40000, verbose=True, label_col_name='is_anomaly') assert res['MockModel']['f1'] == 1.0 assert res['MockModel']['precision'] == 1.0 assert res['MockModel']['recall'] == 1.0
def test_eval_models_half_false(): path = os.path.join(THIS_DIR, os.pardir, 'tests/data/dummy2.txt') df = read_data(path) df['is_anomaly'] = 0 df.iloc[-1]['is_anomaly'] = 1 df.iloc[-2]['is_anomaly'] = 1 model = MockModel() models = [model] X = df[['value']] y = df[['is_anomaly']] res = eval_models(X, y, models, label_col_name='is_anomaly', window_size_for_metrics=0) assert res['MockModel']['precision'] == 0.5 assert res['MockModel']['recall'] == 1.0