-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_queries.py
217 lines (197 loc) · 7.04 KB
/
test_queries.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
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
import pytest
import pytz
import timely_beliefs as tb
from flexmeasures.data.models.assets import Asset, Power
from flexmeasures.data.queries.utils import (
multiply_dataframe_with_deterministic_beliefs,
simplify_index,
)
@pytest.mark.parametrize(
"query_start, query_end, num_values",
[
(
datetime(2015, 1, 1, tzinfo=pytz.utc),
datetime(2015, 1, 2, tzinfo=pytz.utc),
96,
),
(datetime(2015, 1, 1, tzinfo=pytz.utc), None, 96),
(None, datetime(2015, 1, 2, tzinfo=pytz.utc), 96),
(None, None, 96),
(
datetime(2015, 1, 1, tzinfo=pytz.utc),
datetime(2015, 1, 1, 12, tzinfo=pytz.utc),
48,
),
(None, datetime(2015, 1, 1, 12, tzinfo=pytz.utc), 48),
# (
# datetime(1957, 1, 1, tzinfo=pytz.utc),
# datetime(1957, 1, 2, tzinfo=pytz.utc),
# 0,
# ), # test empty BeliefsDataFrame # todo: uncomment when this if fixed: https://github.com/pandas-dev/pandas/issues/30517
],
)
def test_collect_power(db, app, query_start, query_end, num_values):
wind_device_1 = Asset.query.filter_by(name="wind-asset-1").one_or_none()
data = Power.query.filter(Power.asset_id == wind_device_1.id).all()
print(data)
bdf: tb.BeliefsDataFrame = Power.collect(
wind_device_1.name, (query_start, query_end)
)
print(bdf)
assert (
bdf.index.names[0] == "event_start"
) # first index level of collect function should be event_start, so that df.loc[] refers to event_start
assert pd.api.types.is_timedelta64_dtype(
bdf.index.get_level_values("belief_horizon")
) # dtype of belief_horizon is timedelta64[ns], so the minimum horizon on an empty BeliefsDataFrame is NaT instead of NaN
assert len(bdf) == num_values
for v1, v2 in zip(bdf.values, data):
assert abs(v1[0] - v2.value) < 10 ** -6
@pytest.mark.parametrize(
"query_start, query_end, resolution, num_values",
[
(
datetime(2015, 1, 1, tzinfo=pytz.utc),
datetime(2015, 1, 2, tzinfo=pytz.utc),
timedelta(minutes=15),
96,
),
(
datetime(2015, 1, 1, tzinfo=pytz.utc),
datetime(2015, 1, 2, tzinfo=pytz.utc),
timedelta(minutes=30),
48,
),
(
datetime(2015, 1, 1, tzinfo=pytz.utc),
datetime(2015, 1, 2, tzinfo=pytz.utc),
"30min",
48,
),
(
datetime(2015, 1, 1, tzinfo=pytz.utc),
datetime(2015, 1, 2, tzinfo=pytz.utc),
"PT45M",
32,
),
],
)
def test_collect_power_resampled(
db, app, query_start, query_end, resolution, num_values
):
wind_device_1 = Asset.query.filter_by(name="wind-asset-1").one_or_none()
bdf: tb.BeliefsDataFrame = Power.collect(
wind_device_1.name, (query_start, query_end), resolution=resolution
)
print(bdf)
assert len(bdf) == num_values
def test_multiplication():
df1 = pd.DataFrame(
[[30.0, timedelta(hours=3)]],
index=pd.date_range(
"2000-01-01 10:00", "2000-01-01 15:00", freq="1h", closed="left"
),
columns=["event_value", "belief_horizon"],
)
df2 = pd.DataFrame(
[[10.0, timedelta(hours=1)]],
index=pd.date_range(
"2000-01-01 13:00", "2000-01-01 18:00", freq="1h", closed="left"
),
columns=["event_value", "belief_horizon"],
)
df = multiply_dataframe_with_deterministic_beliefs(df1, df2)
df_compare = pd.concat(
[
pd.DataFrame(
[[np.nan, timedelta(hours=3)]],
index=pd.date_range(
"2000-01-01 10:00", "2000-01-01 13:00", freq="1h", closed="left"
),
columns=["event_value", "belief_horizon"],
),
pd.DataFrame(
[[300.0, timedelta(hours=1)]],
index=pd.date_range(
"2000-01-01 13:00", "2000-01-01 15:00", freq="1h", closed="left"
),
columns=["event_value", "belief_horizon"],
),
pd.DataFrame(
[[np.nan, timedelta(hours=1)]],
index=pd.date_range(
"2000-01-01 15:00", "2000-01-01 18:00", freq="1h", closed="left"
),
columns=["event_value", "belief_horizon"],
),
],
axis=0,
)
pd.testing.assert_frame_equal(df, df_compare)
def test_multiplication_with_one_empty_dataframe():
df1 = pd.DataFrame(
[],
columns=["event_value", "belief_horizon"],
)
# set correct types
df1["event_value"] = pd.to_numeric(df1["event_value"])
df1["belief_horizon"] = pd.to_timedelta(df1["belief_horizon"])
df2 = pd.DataFrame(
[[10.0, timedelta(hours=1)]],
index=pd.date_range(
"2000-01-01 13:00", "2000-01-01 18:00", freq="1h", closed="left"
),
columns=["event_value", "belief_horizon"],
)
df_compare = pd.DataFrame(
[[np.nan, timedelta(hours=1)]],
index=pd.date_range(
"2000-01-01 13:00", "2000-01-01 18:00", freq="1h", closed="left"
),
columns=["event_value", "belief_horizon"],
)
# set correct types
df_compare["event_value"] = pd.to_numeric(df_compare["event_value"])
df_compare["belief_horizon"] = pd.to_timedelta(df_compare["belief_horizon"])
df = multiply_dataframe_with_deterministic_beliefs(df1, df2)
pd.testing.assert_frame_equal(df, df_compare)
def test_multiplication_with_both_empty_dataframe():
df1 = pd.DataFrame(
[],
columns=["event_value", "belief_horizon"],
)
# set correct types
df1["event_value"] = pd.to_numeric(df1["event_value"])
df1["belief_horizon"] = pd.to_timedelta(df1["belief_horizon"])
df2 = pd.DataFrame(
[],
columns=["event_value", "belief_horizon"],
)
# set correct types
df2["event_value"] = pd.to_numeric(df2["event_value"])
df2["belief_horizon"] = pd.to_timedelta(df2["belief_horizon"])
df_compare = pd.DataFrame(
[],
columns=["event_value", "belief_horizon"],
)
# set correct types
df_compare["event_value"] = pd.to_numeric(df_compare["event_value"])
df_compare["belief_horizon"] = pd.to_timedelta(df_compare["belief_horizon"])
df = multiply_dataframe_with_deterministic_beliefs(df1, df2)
pd.testing.assert_frame_equal(df, df_compare)
def test_simplify_index():
"""Check whether simplify_index retains the event resolution."""
wind_device_1 = Asset.query.filter_by(name="wind-asset-1").one_or_none()
bdf: tb.BeliefsDataFrame = Power.collect(
wind_device_1.name,
(
datetime(2015, 1, 1, tzinfo=pytz.utc),
datetime(2015, 1, 2, tzinfo=pytz.utc),
),
resolution=timedelta(minutes=15),
)
df = simplify_index(bdf)
assert df.event_resolution == timedelta(minutes=15)