Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop unchanged beliefs from BeliefsDataFrame before saving to db (#194)
To avoid databases from filling up with irrelevant information, only beliefs data representing *changed beliefs are saved*, and *unchanged beliefs are dropped*. * Create draft PR for #186 * Drop unchanged beliefs before saving BeliefsDataFrame to database * Add test for updating belief time * Add test for updating source * Add a probabilistic belief * Add workaround for groupby bug (copied from flexmeasures-entsoe) * Drop probabilistic beliefs in their entirety or not at all * Document tests * Use timely beliefs dev release * Use timely beliefs release * Changelog entry * Make dropping unchanged beliefs optional Co-authored-by: Flix6x <Flix6x@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
158 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import pandas as pd | ||
from timely_beliefs import utils as tb_utils | ||
|
||
from flexmeasures.api.common.utils.api_utils import save_to_db | ||
from flexmeasures.data.models.data_sources import DataSource | ||
from flexmeasures.data.models.time_series import Sensor | ||
|
||
|
||
def test_drop_unchanged_beliefs(setup_beliefs): | ||
"""Trying to save beliefs that are already in the database shouldn't raise an error. | ||
Even after updating the belief time, we expect to persist only the older belief time. | ||
""" | ||
|
||
# Set a reference for the number of beliefs stored and their belief times | ||
sensor = Sensor.query.filter_by(name="epex_da").one_or_none() | ||
bdf = sensor.search_beliefs() | ||
num_beliefs_before = len(bdf) | ||
belief_times_before = bdf.belief_times | ||
|
||
# See what happens when storing all existing beliefs verbatim | ||
save_to_db(bdf) | ||
|
||
# Verify that no new beliefs were saved | ||
bdf = sensor.search_beliefs() | ||
assert len(bdf) == num_beliefs_before | ||
|
||
# See what happens when storing all beliefs with their belief time updated | ||
bdf = tb_utils.replace_multi_index_level( | ||
bdf, "belief_time", bdf.belief_times + pd.Timedelta("1H") | ||
) | ||
save_to_db(bdf) | ||
|
||
# Verify that no new beliefs were saved | ||
bdf = sensor.search_beliefs() | ||
assert len(bdf) == num_beliefs_before | ||
assert list(bdf.belief_times) == list(belief_times_before) | ||
|
||
|
||
def test_do_not_drop_beliefs_copied_by_another_source(setup_beliefs): | ||
"""Trying to copy beliefs from one source to another should double the number of beliefs.""" | ||
|
||
# Set a reference for the number of beliefs stored | ||
sensor = Sensor.query.filter_by(name="epex_da").one_or_none() | ||
bdf = sensor.search_beliefs() | ||
num_beliefs_before = len(bdf) | ||
|
||
# See what happens when storing all belief with their source updated | ||
new_source = DataSource(name="Not Seita", type="demo script") | ||
bdf = tb_utils.replace_multi_index_level( | ||
bdf, "source", pd.Index([new_source] * num_beliefs_before) | ||
) | ||
save_to_db(bdf) | ||
|
||
# Verify that all the new beliefs were added | ||
bdf = sensor.search_beliefs() | ||
num_beliefs_after = len(bdf) | ||
assert num_beliefs_after == 2 * num_beliefs_before | ||
|
||
|
||
def test_do_not_drop_changed_probabilistic_belief(setup_beliefs): | ||
"""Trying to save a changed probabilistic belief should result in saving the whole belief. | ||
For example, given a belief that defines both cp=0.2 and cp=0.5, | ||
if that belief becomes more certain (e.g. cp=0.3 and cp=0.5), | ||
we expect to see the full new belief stored, rather than just the cp=0.3 value. | ||
""" | ||
|
||
# Set a reference for the number of beliefs stored | ||
sensor = Sensor.query.filter_by(name="epex_da").one_or_none() | ||
bdf = sensor.search_beliefs(source="Seita") | ||
num_beliefs_before = len(bdf) | ||
|
||
# See what happens when storing a belief with more certainty one hour later | ||
old_belief = bdf.loc[ | ||
( | ||
bdf.index.get_level_values("event_start") | ||
== pd.Timestamp("2021-03-28 16:00:00+00:00") | ||
) | ||
& ( | ||
bdf.index.get_level_values("belief_time") | ||
== pd.Timestamp("2021-03-28 14:00:00+00:00") | ||
) | ||
] | ||
new_belief = tb_utils.replace_multi_index_level( | ||
old_belief, "cumulative_probability", pd.Index([0.3, 0.5]) | ||
) | ||
new_belief = tb_utils.replace_multi_index_level( | ||
new_belief, "belief_time", new_belief.belief_times + pd.Timedelta("1H") | ||
) | ||
save_to_db(new_belief) | ||
|
||
# Verify that the whole probabilistic belief was added | ||
bdf = sensor.search_beliefs(source="Seita") | ||
num_beliefs_after = len(bdf) | ||
assert num_beliefs_after == num_beliefs_before + len(new_belief) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters