-
Notifications
You must be signed in to change notification settings - Fork 29
/
pandas_reporter.py
269 lines (206 loc) · 10.5 KB
/
pandas_reporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from __future__ import annotations
from typing import Any, Union, Dict
from datetime import datetime, timedelta
from copy import deepcopy, copy
from flask import current_app
import timely_beliefs as tb
import pandas as pd
from flexmeasures.data.models.reporting import Reporter
from flexmeasures.data.schemas.reporting.pandas_reporter import (
PandasReporterConfigSchema,
PandasReporterParametersSchema,
)
from flexmeasures.data.models.time_series import Sensor
from flexmeasures.utils.time_utils import server_now
class PandasReporter(Reporter):
"""This reporter applies a series of pandas methods on"""
__version__ = "1"
__author__ = "Seita"
_config_schema = PandasReporterConfigSchema()
_parameters_schema = PandasReporterParametersSchema()
input: list[str] = None
transformations: list[dict[str, Any]] = None
final_df_output: str = None
data: Dict[str, Union[tb.BeliefsDataFrame, pd.DataFrame]] = None
def fetch_data(
self,
start: datetime,
end: datetime,
input: dict,
resolution: timedelta | None = None,
belief_time: datetime | None = None,
):
"""
Fetches the time_beliefs from the database
"""
self.data = {}
for input_search_parameters in input:
_input_search_parameters = input_search_parameters.copy()
sensor: Sensor = _input_search_parameters.pop("sensor", None)
name = _input_search_parameters.pop("name", f"sensor_{sensor.id}")
# using start / end instead of event_starts_after/event_ends_before when not defined
event_starts_after = _input_search_parameters.pop(
"event_starts_after", start
)
event_ends_before = _input_search_parameters.pop("event_ends_before", end)
resolution = _input_search_parameters.pop("resolution", resolution)
belief_time = _input_search_parameters.pop("belief_time", belief_time)
bdf = sensor.search_beliefs(
event_starts_after=event_starts_after,
event_ends_before=event_ends_before,
resolution=resolution,
beliefs_before=belief_time,
**_input_search_parameters,
)
# store data source as local variable
for source in bdf.sources.unique():
self.data[f"source_{source.id}"] = source
# store BeliefsDataFrame as local variable
self.data[name] = bdf
def _compute_report(self, **kwargs) -> tb.BeliefsDataFrame:
"""
This method applies the transformations and outputs the dataframe
defined in `final_df_output` field of the report_config.
"""
# report configuration
start: datetime = kwargs.get("start")
end: datetime = kwargs.get("end")
input: dict = kwargs.get("input")
output_sensor: Sensor | None = kwargs.get("sensor")
resolution: timedelta | None = kwargs.get("resolution", None)
belief_time: datetime | None = kwargs.get("belief_time", None)
if resolution is None:
resolution = self.sensor.event_resolution
# fetch sensor data
self.fetch_data(start, end, input, resolution, belief_time)
if belief_time is None:
belief_time = server_now()
# apply pandas transformations to the dataframes in `self.data`
self._apply_transformations()
output = kwargs.get("output", [])
if len(output) == 0 and output_sensor is None:
raise ValueError(
"No output sensor defined. At least define an output sensor in the `sensor` or `output` fields in `parameters`."
)
if len(output) == 0:
output = [
{
"name": self._config["required_output"][0]["name"],
"sensor": output_sensor,
}
]
results = []
for output_description in output:
result = copy(output_description)
# TODO: use sensor to store multiple outputs
# sensor = output_description["sensor"]
name = output_description["name"]
output_data = self.data[name]
if isinstance(output_data, tb.BeliefsDataFrame):
# if column is missing, use the first column
column = output_description.get("column", output_data.columns[0])
output_data = output_data.rename(columns={column: "event_value"})[
["event_value"]
]
output_data = self._clean_belief_dataframe(output_data, belief_time)
elif isinstance(output_data, tb.BeliefsSeries):
output_data = self._clean_belief_series(output_data, belief_time)
result["data"] = output_data
results.append(result)
return results[0].get("data")
def _clean_belief_series(
self, belief_series: tb.BeliefsSeries, belief_time: datetime
) -> tb.BeliefsDataFrame:
belief_series = belief_series.to_frame("event_value")
belief_series["belief_time"] = belief_time
belief_series["cumulative_probability"] = 0.5
belief_series["source"] = self.data_source
belief_series = belief_series.set_index(
["belief_time", "source", "cumulative_probability"], append=True
)
return belief_series
def _clean_belief_dataframe(
self, bdf: tb.BeliefsDataFrame, belief_time: datetime
) -> tb.BeliefsDataFrame:
# filing the missing indexes with default values:
# belief_time=belief_time, cummulative_probability=0.5, source=data_source
if "belief_time" not in bdf.index.names:
bdf["belief_time"] = [belief_time] * len(bdf)
bdf = bdf.set_index("belief_time", append=True)
if "cumulative_probability" not in bdf.index.names:
bdf["cumulative_probability"] = [0.5] * len(bdf)
bdf = bdf.set_index("cumulative_probability", append=True)
if "source" not in bdf.index.names:
bdf["source"] = [self.data_source] * len(bdf)
bdf = bdf.set_index("source", append=True)
bdf = bdf.reorder_levels(tb.BeliefsDataFrame().index.names)
return bdf
def get_object_or_literal(self, value: Any, method: str) -> Any:
"""This method allows using the dataframes as inputs of the Pandas methods that
are run in the transformations. Make sure that they have been created before accessed.
This works by putting the symbol `@` in front of the name of the dataframe that we want to reference.
For instance, to reference the dataframe test_df, which lives in self.data, we would do `@test_df`.
This functionality is disabled for methods `eval`and `query` to avoid interfering their internal behaviour
given that they also use `@` to allow using local variables.
Example:
>>> self.get_object_or_literal(["@df_wind", "@df_solar"], "sum")
[<BeliefsDataFrame for Wind Turbine sensor>, <BeliefsDataFrame for Solar Panel sensor>]
"""
if method in ["eval", "query"]:
if isinstance(value, str) and value.startswith("@"):
current_app.logger.debug(
"Cannot reference objects in self.data using the method eval or query. That is because these methods use the symbol `@` to make reference to local variables."
)
return value
if isinstance(value, str) and value.startswith("@"):
value = value.replace("@", "")
return self.data[value]
if isinstance(value, list):
return [self.get_object_or_literal(v, method) for v in value]
return value
def _process_pandas_args(self, args: list, method: str) -> list:
"""This method applies the function get_object_or_literal to all the arguments
to detect where to replace a string "@<object-name>" with the actual object stored in `self.data["<object-name>"]`.
"""
for i in range(len(args)):
args[i] = self.get_object_or_literal(args[i], method)
return args
def _process_pandas_kwargs(self, kwargs: dict, method: str) -> dict:
"""This method applies the function get_object_or_literal to all the keyword arguments
to detect where to replace a string "@<object-name>" with the actual object stored in `self.data["<object-name>"]`.
"""
for k, v in kwargs.items():
kwargs[k] = self.get_object_or_literal(v, method)
return kwargs
def _apply_transformations(self):
"""Convert the series using the given list of transformation specs, which is called in the order given.
Each transformation specs should include a 'method' key specifying a method name of a Pandas DataFrame.
Optionally, 'args' and 'kwargs' keys can be specified to pass on arguments or keyword arguments to the given method.
All data exchange is made through the dictionary `self.data`. The superclass Reporter already fetches BeliefsDataFrames of
the sensors and saves them in the self.data dictionary fields `sensor_<sensor_id>`. In case you need to perform complex operations on dataframes, you can
split the operations in several steps and saving the intermediate results using the parameters `df_input` and `df_output` for the
input and output dataframes, respectively.
Example:
The example below converts from hourly meter readings in kWh to electricity demand in kW.
transformations = [
{"method": "diff"},
{"method": "shift", "kwargs": {"periods": -1}},
{"method": "head", "args": [-1]},
],
"""
previous_df = None
for _transformation in self._config.get("transformations"):
transformation = deepcopy(_transformation)
df_input = transformation.get(
"df_input", previous_df
) # default is using the previous transformation output
df_output = transformation.get(
"df_output", df_input
) # default is OUTPUT = INPUT.method()
method = transformation.get("method")
args = self._process_pandas_args(transformation.get("args", []), method)
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