flexmeasures.data.models.planning.utils

Functions

flexmeasures.data.models.planning.utils.add_tiny_price_slope(prices: DataFrame, col_name: str = 'event_value', d: float = 0.001) DataFrame

Add tiny price slope to col_name to represent e.g. inflation as a simple linear price increase. This is meant to break ties, when multiple time slots have equal prices, in favour of acting sooner. We penalise the future with at most d times the price spread (1 per thousand by default).

flexmeasures.data.models.planning.utils.fallback_charging_policy(sensor: Sensor, device_constraints: DataFrame, start: datetime, end: datetime, resolution: timedelta) Series

This fallback charging policy is to just start charging or discharging, or do neither, depending on the first target state of charge and the capabilities of the Charge Point. Note that this ignores any cause of the infeasibility and, while probably a decent policy for Charge Points, should not be considered a robust policy for other asset types.

flexmeasures.data.models.planning.utils.get_continuous_series_sensor_or_quantity(quantity_or_sensor: Sensor | ur.Quantity | None, actuator: Sensor | Asset, unit: ur.Quantity | str, query_window: tuple[datetime, datetime], resolution: timedelta, beliefs_before: datetime | None = None, fallback_attribute: str | None = None, max_value: float | int | pd.Series = nan, as_instantaneous_events: bool = False, boundary_policy: str | None = None) pd.Series

Creates a time series from a quantity or sensor within a specified window, falling back to a given fallback_attribute and making sure no values exceed max_value.

Parameters:
  • quantity_or_sensor – The quantity or sensor containing the data.

  • actuator – The actuator from which relevant defaults are retrieved.

  • unit – The desired unit of the data.

  • query_window – The time window (start, end) to query the data.

  • resolution – The resolution or time interval for the data.

  • beliefs_before – Timestamp for prior beliefs or knowledge.

  • fallback_attribute – Attribute serving as a fallback default in case no quantity or sensor is given.

  • max_value – Maximum value (also replacing NaN values).

  • as_instantaneous_events – optionally, convert to instantaneous events, in which case the passed resolution is interpreted as the desired frequency of the data.

Returns:

time series data with missing values handled based on the chosen method.

flexmeasures.data.models.planning.utils.get_market(sensor: Sensor) Sensor

Get market sensor from the sensor’s attributes.

flexmeasures.data.models.planning.utils.get_power_values(query_window: tuple[datetime, datetime], resolution: timedelta, beliefs_before: datetime | None, sensor: Sensor) np.ndarray

Get measurements or forecasts of an inflexible device represented by a power or energy sensor as an array of power values in MW.

If the requested schedule lies in the future, the returned data will consist of (the most recent) forecasts (if any exist). If the requested schedule lies in the past, the returned data will consist of (the most recent) measurements (if any exist). The latter amounts to answering “What if we could have scheduled under perfect foresight?”.

Parameters:
  • query_window – datetime window within which events occur (equal to the scheduling window)

  • resolution – timedelta used to resample the forecasts to the resolution of the schedule

  • beliefs_before – datetime used to indicate we are interested in the state of knowledge at that time

  • sensor – power sensor representing an energy flow out of the device

Returns:

power measurements or forecasts (consumption is positive, production is negative)

flexmeasures.data.models.planning.utils.get_prices(query_window: tuple[datetime, datetime], resolution: timedelta, beliefs_before: datetime | None, price_sensor: Sensor | None = None, sensor: Sensor | None = None, allow_trimmed_query_window: bool = True) tuple[pd.DataFrame, tuple[datetime, datetime]]

Check for known prices or price forecasts.

If so allowed, the query window is trimmed according to the available data. If not allowed, prices are extended to the edges of the query window: - The first available price serves as a naive backcast. - The last available price serves as a naive forecast.

flexmeasures.data.models.planning.utils.get_quantity_from_attribute(entity: Asset | Sensor, attribute: str, unit: str | ur.Quantity) ur.Quantity

Get the value (in the given unit) of a quantity stored as an entity attribute.

Parameters:
  • entity – The entity (sensor or asset) containing the attribute to retrieve the value from.

  • attribute – The attribute name to extract the value from.

  • unit – The unit in which the value should be returned.

Returns:

The retrieved quantity or the provided default.

flexmeasures.data.models.planning.utils.get_series_from_quantity_or_sensor(quantity_or_sensor: Sensor | ur.Quantity, unit: ur.Quantity | str, query_window: tuple[datetime, datetime], resolution: timedelta, beliefs_before: datetime | None = None, as_instantaneous_events: bool = True, boundary_policy: str | None = None) pd.Series

Get a time series given a quantity or sensor defined on a time window.

Parameters:
  • quantity_or_sensor – A pint Quantity or timely-beliefs Sensor, measuring e.g. power capacity or efficiency.

  • unit – Unit of the output data.

  • query_window – Tuple representing the start and end of the requested data.

  • resolution – Time resolution of the requested data.

  • beliefs_before – Optional datetime used to indicate we are interested in the state of knowledge at that time.

  • as_instantaneous_events – Optionally, convert to instantaneous events, in which case the passed resolution is interpreted as the desired frequency of the data.

Returns:

Pandas Series with the requested time series data.

flexmeasures.data.models.planning.utils.idle_after_reaching_target(schedule: Series, target: Series, initial_state: float = 0) Series

Stop planned (dis)charging after target is reached (or constraint is met).

flexmeasures.data.models.planning.utils.initialize_df(columns: list[str], start: datetime, end: datetime, resolution: timedelta, inclusive: str = 'left') DataFrame
flexmeasures.data.models.planning.utils.initialize_index(start: date | datetime | str, end: date | datetime | str, resolution: timedelta | str, inclusive: str = 'left') pd.DatetimeIndex
flexmeasures.data.models.planning.utils.initialize_series(data: pd.Series | list[float] | np.ndarray | float | None, start: datetime, end: datetime, resolution: timedelta, inclusive: str = 'left') pd.Series
flexmeasures.data.models.planning.utils.nanmin_of_series_and_value(s: pd.Series, value: float | pd.Series) pd.Series

Perform a nanmin between a Series and a float.