Skip to content

Commit

Permalink
add flag check_output_resolution and get the minimum resolution of th…
Browse files Browse the repository at this point in the history
…e input data from the min of the input sensors event resolutions instead of the output sensors.

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
  • Loading branch information
victorgarcia98 committed Mar 11, 2024
1 parent 2f3ea42 commit ff64744
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions flexmeasures/data/models/reporting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Reporter(DataGenerator):
_parameters_schema = ReporterParametersSchema()
_config_schema = ReporterConfigSchema()

def _compute(self, **kwargs) -> List[Dict[str, Any]]:
def _compute(self, check_output_resolution=True, **kwargs) -> List[Dict[str, Any]]:
"""This method triggers the creation of a new report.
The same object can generate multiple reports with different start, end, resolution
Expand All @@ -32,7 +32,7 @@ def _compute(self, **kwargs) -> List[Dict[str, Any]]:

for result in results:
# checking that the event_resolution of the output BeliefDataFrame is equal to the one of the output sensor
assert (
assert check_output_resolution or (
result["sensor"].event_resolution == result["data"].event_resolution
), f"The resolution of the results ({result['data'].event_resolution}) should match that of the output sensor ({result['sensor'].event_resolution}, ID {result['sensor'].id})."

Expand Down
33 changes: 25 additions & 8 deletions flexmeasures/data/models/reporting/pandas_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ def _compute_report(self, **kwargs) -> list[dict[str, Any]]:

resolution: timedelta | None = kwargs.get("resolution", None)
belief_time: datetime | None = kwargs.get("belief_time", None)
belief_horizon: timedelta | None = kwargs.get("belief_horizon", None)
output: list[dict[str, Any]] = kwargs.get("output")

# by default, use the minimum resolution among the output sensors
# by default, use the minimum resolution among the input sensors
if resolution is None:
resolution = min([o["sensor"].event_resolution for o in output])
resolution = min([i["sensor"].event_resolution for i in input])

# fetch sensor data
self.fetch_data(start, end, input, resolution, belief_time)
Expand All @@ -117,10 +118,14 @@ def _compute_report(self, **kwargs) -> list[dict[str, Any]]:
output_data = output_data.rename(columns={column: "event_value"})[
["event_value"]
]
output_data = self._clean_belief_dataframe(output_data, belief_time)
output_data = self._clean_belief_dataframe(
output_data, belief_time, belief_horizon
)

elif isinstance(output_data, tb.BeliefsSeries):
output_data = self._clean_belief_series(output_data, belief_time)
output_data = self._clean_belief_series(
output_data, belief_time, belief_horizon
)

result["data"] = output_data

Expand All @@ -129,11 +134,16 @@ def _compute_report(self, **kwargs) -> list[dict[str, Any]]:
return results

def _clean_belief_series(
self, belief_series: tb.BeliefsSeries, belief_time: datetime
self,
belief_series: tb.BeliefsSeries,
belief_time: datetime | None = None,
belief_horizon: timedelta | None = None,
) -> tb.BeliefsDataFrame:
"""Create a BeliefDataFrame from a BeliefsSeries creating the necessary indexes."""

belief_series = belief_series.to_frame("event_value")
if belief_horizon is not None:
belief_time = belief_series["event_start"] + belief_horizon
belief_series["belief_time"] = belief_time
belief_series["cumulative_probability"] = 0.5
belief_series["source"] = self.data_source
Expand All @@ -144,13 +154,21 @@ def _clean_belief_series(
return belief_series

def _clean_belief_dataframe(
self, bdf: tb.BeliefsDataFrame, belief_time: datetime
self,
bdf: tb.BeliefsDataFrame,
belief_time: datetime | None = None,
belief_horizon: timedelta | None = None,
) -> tb.BeliefsDataFrame:
"""Add missing indexes to build a proper BeliefDataFrame."""

# filing the missing indexes with default values:
if "belief_time" not in bdf.index.names:
bdf["belief_time"] = [belief_time] * len(bdf)
if belief_horizon is not None:
belief_time = bdf["event_start"] + belief_horizon
else:
belief_time = [belief_time] * len(bdf)
bdf["belief_time"] = belief_time

bdf = bdf.set_index("belief_time", append=True)

if "cumulative_probability" not in bdf.index.names:
Expand Down Expand Up @@ -249,7 +267,6 @@ def _apply_transformations(self):
kwargs = self._process_pandas_kwargs(
transformation.get("kwargs", {}), method
)

self.data[df_output] = getattr(self.data[df_input], method)(*args, **kwargs)

previous_df = df_output

0 comments on commit ff64744

Please sign in to comment.