Skip to content

Commit

Permalink
fix: better implementation and moved to timely-beliefs
Browse files Browse the repository at this point in the history
Signed-off-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
Flix6x committed Mar 12, 2024
1 parent 6e87bf5 commit a314ad1
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 14 deletions.
9 changes: 6 additions & 3 deletions flexmeasures/data/models/planning/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def _prepare(self, skip_validation: bool = False) -> tuple: # noqa: C901
query_window=(start, end),
resolution=resolution,
beliefs_before=belief_time,
to_instantaneous=True,
as_instantaneous_events=True,
boundary_policy="first",
)
if isinstance(soc_minima, Sensor):
soc_minima = get_continuous_series_sensor_or_quantity(
Expand All @@ -192,7 +193,8 @@ def _prepare(self, skip_validation: bool = False) -> tuple: # noqa: C901
query_window=(start, end),
resolution=resolution,
beliefs_before=belief_time,
to_instantaneous=True,
as_instantaneous_events=True,
boundary_policy="max",
)
if isinstance(soc_maxima, Sensor):
soc_maxima = get_continuous_series_sensor_or_quantity(
Expand All @@ -202,7 +204,8 @@ def _prepare(self, skip_validation: bool = False) -> tuple: # noqa: C901
query_window=(start, end),
resolution=resolution,
beliefs_before=belief_time,
to_instantaneous=True,
as_instantaneous_events=True,
boundary_policy="min",
)

device_constraints[0] = add_storage_constraints(
Expand Down
1 change: 1 addition & 0 deletions flexmeasures/data/models/planning/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ def add_soc_targets(db, add_battery_assets, setup_sources) -> dict[str, Sensor]:
("soc-targets (1h)", timedelta(minutes=60), timedelta(minutes=0)),
("soc-targets (15min)", timedelta(minutes=15), timedelta(minutes=0)),
("soc-targets (15min lagged)", timedelta(minutes=15), timedelta(minutes=5)),
("soc-targets (5min)", timedelta(minutes=5), timedelta(minutes=0)),
("soc-targets (instantaneous)", timedelta(minutes=0), timedelta(minutes=0)),
]

Expand Down
7 changes: 7 additions & 0 deletions flexmeasures/data/models/planning/tests/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,13 @@ def test_battery_storage_efficiency_sensor(
"14:00:00",
"15:00:00",
),
# A value defined in a finer resolution is downsampled to match the power sensor resolution.
# Only a single value coincides with the power sensor resolution.
(
"soc-targets (5min)",
"14:00:00",
"14:15:00", # todo: change to "14:00:00" after timely-beliefs makes it possible to resample to a certain frequency and event resolution simultaneously
),
# A simple case, SOC constraint sensor in the sample resolution as the power sensor.
(
"soc-targets (15min)",
Expand Down
16 changes: 10 additions & 6 deletions flexmeasures/data/models/planning/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ def get_series_from_quantity_or_sensor(
query_window: tuple[datetime, datetime],
resolution: timedelta,
beliefs_before: datetime | None = None,
to_instantaneous: bool = False,
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.
Expand Down Expand Up @@ -357,16 +358,17 @@ def get_series_from_quantity_or_sensor(
event_starts_after=query_window[0],
event_ends_before=query_window[1],
resolution=resolution,
# resolution=resolution if not to_instantaneous else timedelta(0), # NotImplementedError in timely-beliefs
# resolution=resolution if not boundary_policy else timedelta(0), # NotImplementedError in timely-beliefs
# frequency=resolution,
beliefs_before=beliefs_before,
most_recent_beliefs_only=True,
one_deterministic_belief_per_event=True,
)
if as_instantaneous_events:
bdf = bdf.resample_events(timedelta(0), boundary_policy=boundary_policy)
time_series = simplify_index(bdf).reindex(index).squeeze()
time_series = convert_units(time_series, quantity_or_sensor.unit, unit)

if to_instantaneous and not quantity_or_sensor.event_resolution == timedelta(0):
time_series = time_series.ffill(limit=1)
else:
raise TypeError(
f"quantity_or_sensor {quantity_or_sensor} should be a pint Quantity or timely-beliefs Sensor"
Expand All @@ -384,7 +386,8 @@ def get_continuous_series_sensor_or_quantity(
beliefs_before: datetime | None = None,
fallback_attribute: str | None = None,
max_value: float | int | pd.Series = np.nan,
to_instantaneous: bool = False,
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`.
Expand Down Expand Up @@ -412,7 +415,8 @@ def get_continuous_series_sensor_or_quantity(
query_window=query_window,
resolution=resolution,
beliefs_before=beliefs_before,
to_instantaneous=to_instantaneous,
as_instantaneous_events=as_instantaneous_events,
boundary_policy=boundary_policy,
)

# Apply upper limit
Expand Down
2 changes: 1 addition & 1 deletion requirements/3.10/app.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ tabulate==0.9.0
# via -r requirements/app.in
threadpoolctl==3.2.0
# via scikit-learn
timely-beliefs[forecast]==2.1.0
timely-beliefs[forecast]==2.2.0
# via -r requirements/app.in
timetomodel==0.7.3
# via -r requirements/app.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/3.11/app.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ tabulate==0.9.0
# via -r requirements/app.in
threadpoolctl==3.2.0
# via scikit-learn
timely-beliefs[forecast]==2.1.0
timely-beliefs[forecast]==2.2.0
# via -r requirements/app.in
timetomodel==0.7.3
# via -r requirements/app.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/3.8/app.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ tabulate==0.9.0
# via -r requirements/app.in
threadpoolctl==3.2.0
# via scikit-learn
timely-beliefs[forecast]==2.1.0
timely-beliefs[forecast]==2.2.0
# via -r requirements/app.in
timetomodel==0.7.3
# via -r requirements/app.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/3.9/app.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ tabulate==0.9.0
# via -r requirements/app.in
threadpoolctl==3.2.0
# via scikit-learn
timely-beliefs[forecast]==2.1.0
timely-beliefs[forecast]==2.2.0
# via -r requirements/app.in
timetomodel==0.7.3
# via -r requirements/app.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/app.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pyomo>=5.6
tabulate
timetomodel>=0.7.3
# significantly faster db queries
timely-beliefs[forecast]>=2.1
timely-beliefs[forecast]>=2.2
python-dotenv
# a backport, not needed in Python3.8
importlib_metadata
Expand Down

0 comments on commit a314ad1

Please sign in to comment.