From 05a1ade3a3e2bde70ef2e7a5a3c150fa1255ac32 Mon Sep 17 00:00:00 2001 From: Normann Date: Wed, 8 Jan 2025 23:20:35 +0100 Subject: [PATCH] needed_prediction_hours fixed --- .../prediction/elecpriceakkudoktor.py | 18 +++++++++++------- tests/test_elecpriceakkudoktor.py | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/akkudoktoreos/prediction/elecpriceakkudoktor.py b/src/akkudoktoreos/prediction/elecpriceakkudoktor.py index a85d1de..486ed56 100644 --- a/src/akkudoktoreos/prediction/elecpriceakkudoktor.py +++ b/src/akkudoktoreos/prediction/elecpriceakkudoktor.py @@ -140,7 +140,7 @@ class ElecPriceAkkudoktor(ElecPriceProvider): def _update_data( self, force_update: Optional[bool] = False - ) -> None: # Tuple[np.ndarray, np.ndarray, np.ndarray]: # for debug main + ) -> None: # tuple[np.ndarray, np.ndarray, np.ndarray]: """Update forecast data in the ElecPriceDataRecord format. Retrieves data from Akkudoktor, maps each Akkudoktor field to the corresponding @@ -182,18 +182,21 @@ class ElecPriceAkkudoktor(ElecPriceProvider): amount_datasets = len(self.records) assert highest_orig_datetime # mypy fix + # some of our data is already in the future, so we need to predict less. If we got less data we increase the prediction hours + needed_prediction_hours = int( + self.config.prediction_hours + - ((highest_orig_datetime - self.start_datetime).total_seconds() // 3600) + ) if amount_datasets > 800: # we do the full ets with seasons of 1 week prediction = self._predict_ets( - history, seasonal_periods=168, prediction_hours=self.config.prediction_hours + history, seasonal_periods=168, prediction_hours=needed_prediction_hours ) elif amount_datasets > 168: # not enough data to do seasons of 1 week, but enough for 1 day prediction = self._predict_ets( - history, seasonal_periods=24, prediction_hours=self.config.prediction_hours + history, seasonal_periods=24, prediction_hours=needed_prediction_hours ) elif amount_datasets > 0: # not enough data for ets, do median - prediction = self._predict_median( - history, prediction_hours=self.config.prediction_hours - ) + prediction = self._predict_median(history, prediction_hours=needed_prediction_hours) else: logger.error("No data available for prediction") raise ValueError("No data available") @@ -229,7 +232,7 @@ def visualize_predictions( label="Predictions", color="red", ) - plt.title("Predictions vs True Values for ets") + plt.title("Predictions ets") plt.xlabel("Time") plt.ylabel("Price") plt.legend() @@ -238,6 +241,7 @@ def visualize_predictions( def main() -> None: + # Initialize ElecPriceAkkudoktor with required parameters elec_price_akkudoktor = ElecPriceAkkudoktor() history, history2, predictions = elec_price_akkudoktor._update_data() diff --git a/tests/test_elecpriceakkudoktor.py b/tests/test_elecpriceakkudoktor.py index f72870d..1517fd2 100644 --- a/tests/test_elecpriceakkudoktor.py +++ b/tests/test_elecpriceakkudoktor.py @@ -120,7 +120,9 @@ def test_update_data(mock_get, elecprice_provider, sample_akkudoktor_1_json, cac # Assert: Verify the result is as expected mock_get.assert_called_once() - assert len(elecprice_provider) == 96 + assert ( + len(elecprice_provider) == 73 + ) # we have 48 datasets in the api response, we want to know 48h into the future. The data we get has already 23h into the future so we need only 25h more. 48+25=73 # Assert we get prediction_hours prioce values by resampling np_price_array = elecprice_provider.key_to_array(