def config_with_default_periods(freq, frequency_end_of_week=None, frequency_step_hours=None, frequency_step_minutes=None): config = { "frequency_unit": freq, "time_column": "date", "target_columns": ["value1"], "long_format": False, "decomposition_model": "multiplicative", "expert": False } default_season_length = { "12M": 4, "6M": 2, "3M": 4, "M": 12, "W": 52, "D": 7, "B": 5, "H": 24, "min": 60 } if frequency_end_of_week: config["frequency_end_of_week"] = frequency_end_of_week elif frequency_step_hours: config["frequency_step_hours"] = frequency_step_hours elif frequency_step_minutes: config["frequency_step_minutes"] = frequency_step_minutes input_dataset_columns = ["value1", "value2", "country", "date"] config["season_length_{}".format(freq)] = default_season_length[freq] dku_config = STLConfig() dku_config.add_parameters(config, input_dataset_columns) return dku_config
def test_advanced_degrees(self, config, input_df, input_dataset_columns): config["additional_parameters_STL"] = { "seasonal_deg": "1", "trend_deg": "1", "low_pass_deg": "1" } dku_config = STLConfig() dku_config.add_parameters(config, input_dataset_columns) timeseries_preparator = TimeseriesPreparator(dku_config) df_prepared = timeseries_preparator.prepare_timeseries_dataframe( input_df) decomposition = STLDecomposition(dku_config) result_df = decomposition.fit(df_prepared) assert result_df.shape == (26, 6) np.testing.assert_array_equal( np.round(result_df.value1_trend.values, 4), np.array([ 547017.8314, 537486.722, 528097.1954, 518846.2605, 509728.8989, 500744.2034, 491895.324, 483188.5115, 474630.5299, 466256.2782, 458496.2869, 454985.6935, 453114.0625, 452740.149, 453810.1866, 456404.7768, 463218.9767, 470913.292, 478947.2522, 487217.229, 495684.7824, 504325.6079, 513126.1746, 522081.8564, 531195.1428, 540473.2835 ])) assert np.mean(result_df["value1_trend"]) == 492101.0195351211 assert np.mean(result_df["value1_seasonal"]) == 32625.652227975654 assert np.mean(result_df["value1_residuals"]) == -5345.248686173698 config["additional_parameters_STL"] = { "seasonal_deg": "1", "trend_deg": "0" } dku_config = STLConfig() dku_config.add_parameters(config, input_dataset_columns) timeseries_preparator = TimeseriesPreparator(dku_config) df_prepared = timeseries_preparator.prepare_timeseries_dataframe( input_df) decomposition = STLDecomposition(dku_config) result_df = decomposition.fit(df_prepared) assert result_df.shape == (26, 6) np.testing.assert_array_equal( np.round(result_df.value1_seasonal.values, 4), np.array([ 334926.5396, 363552.8324, 380642.7497, 151182.772, -168020.8919, -209675.4339, -276299.0916, -289677.7104, -278165.1873, -126041.2679, -8513.4181, 175315.4394, 425222.6624, 396736.9844, 290811.7923, 45628.9471, -110941.82, -272356.2149, -303391.2037, -338667.781, -295226.877, -106373.2845, 41186.7333, 274657.8578, 516720.1595, 432742.083 ])) assert np.mean(result_df["value1_trend"]) == 470658.0934271346 assert np.mean(result_df["value1_seasonal"]) == 40229.89887290871 assert np.mean(result_df["value1_residuals"]) == 8493.430776879803
def test_invalid_seasonal(self, advanced_config, input_dataset_columns): dku_config = STLConfig() advanced_config["seasonal_stl"] = 8 with pytest.raises(DSSParameterError) as odd_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "odd" in str(odd_err.value) advanced_config["seasonal_stl"] = 9.5 with pytest.raises(DSSParameterError) as double_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "type" in str(double_err.value) assert "int" in str(double_err.value) advanced_config["seasonal_stl"] = "string" with pytest.raises(DSSParameterError) as str_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "type" in str(str_err.value) assert "int" in str(str_err.value) advanced_config.pop("seasonal_stl") with pytest.raises(DSSParameterError) as str_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "required" in str(str_err.value)
def test_robust_params(self, advanced_config, input_dataset_columns): dku_config = STLConfig() advanced_config["robust_stl"] = False assert dku_config.add_parameters(advanced_config, input_dataset_columns) is None advanced_config["robust_stl"] = 42 with pytest.raises(DSSParameterError) as err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "bool" in str(err.value) advanced_config.pop("robust_stl") dku_config.add_parameters(advanced_config, input_dataset_columns) assert dku_config.robust_stl == False
def test_additional_parameters(self, advanced_config, input_dataset_columns): dku_config = STLConfig() advanced_config["additional_parameters_STL"] = {"invalid_key": ""} with pytest.raises(DSSParameterError) as str_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "keys" in str(str_err.value) dku_config = STLConfig() advanced_config["additional_parameters_STL"] = {"invalid_key": "5"} with pytest.raises(DSSParameterError) as str_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "keys" in str(str_err.value)
def test_advanced_params(self, advanced_config, input_dataset_columns): dku_config = STLConfig() assert dku_config.add_parameters(advanced_config, input_dataset_columns) is None
def test_add_parameters(self, basic_config, input_dataset_columns): dku_config = STLConfig() assert dku_config.add_parameters(basic_config, input_dataset_columns) is None
def test_advanced_smoothers(self, advanced_config, input_dataset_columns): dku_config = STLConfig() advanced_config["additional_parameters_STL"] = { "trend": "23", "low_pass": "******" } dku_config.add_parameters(advanced_config, input_dataset_columns) assert dku_config.additional_parameters_STL == { "trend": 23, "low_pass": 31 } dku_config = STLConfig() advanced_config["additional_parameters_STL"] = { "trend": "None", "low_pass": "******" } dku_config.add_parameters(advanced_config, input_dataset_columns) assert dku_config.additional_parameters_STL == { "trend": None, "low_pass": 31 } advanced_config["additional_parameters_STL"] = {"trend": ""} dku_config.add_parameters(advanced_config, input_dataset_columns) assert dku_config.additional_parameters_STL == {"trend": None} dku_config = STLConfig() advanced_config["additional_parameters_STL"] = {"trend": "2"} with pytest.raises(ValueError) as str_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "odd" in str(str_err.value) advanced_config["additional_parameters_STL"] = {"trend": "-2"} with pytest.raises(ValueError): _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "positive" in str(str_err.value) dku_config = STLConfig() advanced_config["additional_parameters_STL"] = {"trend": "5"} with pytest.raises(ValueError): _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "greater" in str(str_err.value) dku_config = STLConfig() advanced_config["additional_parameters_STL"] = {"trend": "string"} with pytest.raises(ValueError): _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "integer" in str(str_err.value)
def test_advanced_speed_jumps(self, advanced_config, input_dataset_columns): dku_config = STLConfig() advanced_config["additional_parameters_STL"] = { "seasonal_jump": "2", "trend_jump": "3", "low_pass_jump": "4" } dku_config.add_parameters(advanced_config, input_dataset_columns) assert dku_config.additional_parameters_STL == { "seasonal_jump": 2, "trend_jump": 3, "low_pass_jump": 4 } dku_config = STLConfig() advanced_config["additional_parameters_STL"] = { "seasonal_jump": "-1000" } with pytest.raises(ValueError) as str_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "positive integer" in str(str_err.value) dku_config = STLConfig() advanced_config["additional_parameters_STL"] = { "seasonal_jump": "string" } with pytest.raises(ValueError): _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "positive integer" in str(str_err.value) dku_config = STLConfig() advanced_config["additional_parameters_STL"] = {"seasonal_jump": ""} with pytest.raises(ValueError): _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "positive integer" in str(str_err.value)
def test_advanced_degrees(self, advanced_config, input_dataset_columns): dku_config = STLConfig() advanced_config["additional_parameters_STL"] = { "seasonal_deg": "1", "trend_deg": "1", "low_pass_deg": "1" } dku_config.add_parameters(advanced_config, input_dataset_columns) assert dku_config.additional_parameters_STL == { "seasonal_deg": 1, "trend_deg": 1, "low_pass_deg": 1 } dku_config = STLConfig() advanced_config["additional_parameters_STL"] = {"seasonal_deg": "1000"} with pytest.raises(ValueError) as str_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "must be equal to 0 or 1" in str(str_err.value) dku_config = STLConfig() advanced_config["additional_parameters_STL"] = { "seasonal_deg": "string" } with pytest.raises(ValueError) as str_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "must be equal to 0 or 1" in str(str_err.value) dku_config = STLConfig() advanced_config["additional_parameters_STL"] = {"low_pass_deg": ""} with pytest.raises(ValueError) as str_err: _ = dku_config.add_parameters(advanced_config, input_dataset_columns) assert "must be equal to 0 or 1" in str(str_err.value)
def dku_config(config, input_dataset_columns): dku_config = STLConfig() dku_config.add_parameters(config, input_dataset_columns) return dku_config
def test_advanced_smoothers(self, config, input_df, input_dataset_columns): config["decomposition_model"] = "additive" config["additional_parameters_STL"] = {"trend": "35", "low_pass": "******"} dku_config = STLConfig() dku_config.add_parameters(config, input_dataset_columns) timeseries_preparator = TimeseriesPreparator(dku_config) df_prepared = timeseries_preparator.prepare_timeseries_dataframe( input_df) decomposition = STLDecomposition(dku_config) result_df = decomposition.fit(df_prepared) assert result_df.shape == (26, 6) np.testing.assert_array_equal( np.round(result_df.value1_seasonal.values, 4), np.array([ 329279.6394, 360305.5117, 378691.0343, 151319.491, -166075.4661, -206300.4391, -272041.7161, -285356.053, -274969.4078, -125368.4261, -10804.3636, 173084.5489, 421640.9531, 393264.9995, 288207.4229, 42573.3565, -111402.3446, -270267.5348, -299889.3857, -334837.5864, -291850.134, -103986.6224, 42205.6726, 274027.7075, 515335.6499, 429183.6225 ])) assert np.mean(result_df["value1_trend"]) == 482542.4367257319 assert np.mean(result_df["value1_seasonal"]) == 40229.62038767122 assert np.mean(result_df["value1_residuals"]) == -3390.634036480091 config["additional_parameters_STL"] = { "trend": "2999999", "low_pass": "******" } dku_config = STLConfig() dku_config.add_parameters(config, input_dataset_columns) timeseries_preparator = TimeseriesPreparator(dku_config) df_prepared = timeseries_preparator.prepare_timeseries_dataframe( input_df) decomposition = STLDecomposition(dku_config) result_df = decomposition.fit(df_prepared) assert result_df.shape == (26, 6) assert np.mean(result_df["value1_trend"]) == 476077.5935197392 assert np.mean(result_df["value1_seasonal"]) == 43303.82955718398 assert np.mean(result_df["value1_residuals"]) == -3.134258664571322e-11 config["additional_parameters_STL"] = {"trend": ""} dku_config = STLConfig() dku_config.add_parameters(config, input_dataset_columns) timeseries_preparator = TimeseriesPreparator(dku_config) df_prepared = timeseries_preparator.prepare_timeseries_dataframe( input_df) decomposition = STLDecomposition(dku_config) result_df = decomposition.fit(df_prepared) assert result_df.shape == (26, 6) assert np.mean(result_df["value1_trend"]) == 492101.0195351211 assert np.mean(result_df["value1_seasonal"]) == 32625.652227975654 assert np.mean(result_df["value1_residuals"]) == -5345.248686173698 config["additional_parameters_STL"] = {"trend": "None"} dku_config = STLConfig() dku_config.add_parameters(config, input_dataset_columns) timeseries_preparator = TimeseriesPreparator(dku_config) df_prepared = timeseries_preparator.prepare_timeseries_dataframe( input_df) decomposition = STLDecomposition(dku_config) result_df = decomposition.fit(df_prepared) assert result_df.shape == (26, 6) assert np.mean(result_df["value1_trend"]) == 492101.0195351211 assert np.mean(result_df["value1_seasonal"]) == 32625.652227975654 assert np.mean(result_df["value1_residuals"]) == -5345.248686173698