flexmeasures.data.models.forecasting.utils

Functions

flexmeasures.data.models.forecasting.utils.data_to_bdf(data: DataFrame, horizon: int, probabilistic: bool, target_sensor: Sensor, sensor_to_save: Sensor, data_source: DataSource) BeliefsDataFrame

Converts a prediction DataFrame into a BeliefsDataFrame for saving to the database.

Parameters:
  • data – DataFrame containing predictions for different forecast horizons. If probabilistic forecasts are generated, data includes a component column, which encodes which quantile (cumulative probability) the row corresponds to.

  • horizon – Maximum forecast horizon in time-steps relative to the sensor’s resolution. For example, if the sensor resolution is 1 hour, a horizon of 48 represents a forecast horizon of 48 hours. Similarly, if the sensor resolution is 15 minutes, a horizon of 4*48 represents a forecast horizon of 48 hours.

  • probabilistic – Whether the forecasts are probabilistic or deterministic.

  • target_sensor – The Sensor object for which the predictions are made.

  • sensor_to_save – The Sensor object to save the forecasts to.

  • data_source – The DataSource object to attribute the forecasts to.

Returns:

A formatted BeliefsDataFrame ready for database insertion.

flexmeasures.data.models.forecasting.utils.floor_to_resolution(dt: datetime, resolution: timedelta) datetime
flexmeasures.data.models.forecasting.utils.negative_to_zero(x: ndarray) ndarray
flexmeasures.data.models.forecasting.utils.refresh_data_source(data_source: DataSource) DataSource

Refresh the potentially detached data source.

This avoids a sqlalchemy.exc.IntegrityError / psycopg2.errors.ForeignKeyViolation for the data source ID not being present in the data_source table.

Prefer looking up by ID when available: this sidesteps the attributes_hash mismatch that arises because PostgreSQL JSONB returns keys in alphabetical order, while the stored hash was originally computed from the Python insertion-order dict.