-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_main_.py
More file actions
279 lines (249 loc) · 10.2 KB
/
test_main_.py
File metadata and controls
279 lines (249 loc) · 10.2 KB
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
270
271
272
273
274
275
276
277
278
279
from collections.abc import Collection
from pathlib import Path
from tempfile import TemporaryDirectory
import polars as pl
import pytest
from buildstock_fetch.building_ import Building
from buildstock_fetch.main_new import download_and_process_all, list_buildings
from buildstock_fetch.releases import RELEASES
from buildstock_fetch.types import FileType, ReleaseKey, normalize_upgrade_id
TEST_METADATA_MERGING_SKIP_RELEASES: set[ReleaseKey] = {"com_2025_amy2018_3"}
TEST_METADATA_MERGING_RELEASES = RELEASES.keys - TEST_METADATA_MERGING_SKIP_RELEASES
TEST_ANNUAL_LOAD_CURVE_MERGING_SKIP_RELEASES: set[ReleaseKey] = {"com_2025_amy2018_3"}
TEST_ANNUAL_LOAD_CURVE_MERGING_RELEASES = (
RELEASES.filter(file_types=["load_curve_annual"]).keys - TEST_ANNUAL_LOAD_CURVE_MERGING_SKIP_RELEASES
)
@pytest.fixture
def target_folder():
with TemporaryDirectory() as td:
yield Path(td)
def first_building_id_str(buildings: Collection[Building]) -> str:
building = next(iter(buildings))
return f"{building.release}-{building.upgrade}-{building.state}"
@pytest.mark.vcr
@pytest.mark.asyncio
@pytest.mark.network
@pytest.mark.parametrize(
["buildings"],
[
(buildings,)
for release in sorted(RELEASES.keys)
for state in ("NY",)
for upgrade in (
[normalize_upgrade_id("0")]
+ ([normalize_upgrade_id("5")] if normalize_upgrade_id("5") in RELEASES[release].upgrades else [])
)
if (buildings := list_buildings(release, state, upgrade, 5))
],
ids=first_building_id_str,
)
async def test_metadata_has_required_fields_and_exists_in_paths(target_folder: Path, buildings: Collection[Building]):
METADATA_COLUMNS = ["bldg_id", "upgrade", "in.", "upgrade.", "weight"]
NOT_METADATA_COLUMNS = ["out."]
await download_and_process_all(target_folder, buildings, ["metadata"])
filenames: set[Path] = set()
for building in buildings:
path = (
target_folder
/ building.release
/ "metadata"
/ f"state={building.state}"
/ f"upgrade={building.upgrade.zfill(2)}"
)
files = list(path.iterdir())
assert len(files) == 1
assert files[0].suffix == ".parquet"
filename = files[0]
lf = pl.scan_parquet(filename)
assert not lf.filter(pl.col("bldg_id") == building.id).collect().is_empty()
filenames.add(filename)
for filename in filenames:
lf = pl.scan_parquet(filename)
columns = lf.collect_schema().keys()
for required_col in METADATA_COLUMNS:
if required_col == "upgrade." and (
"upgrade=00" in str(filename)
or "res_2024_tmy3_1" in str(filename)
or ("2022" not in str(filename) and "res_2024" not in str(filename) and "res_2025" not in str(filename))
):
continue
assert any(c.startswith(required_col) for c in columns)
for alien_col in NOT_METADATA_COLUMNS:
assert not any(c.startswith(alien_col) for c in columns)
assert set(pl.read_parquet(list(filenames))["bldg_id"]) == {_.id for _ in buildings}
# @pytest.mark.vcr
@pytest.mark.asyncio
@pytest.mark.network
@pytest.mark.parametrize(
"buildings_partitioned",
[
[buildings[:5], buildings[-5:]]
for release in sorted(TEST_METADATA_MERGING_RELEASES)
for state in ("NY",)
for upgrade in (normalize_upgrade_id("0"),)
if (buildings := list_buildings(release, state, upgrade, 50))
],
ids=lambda _: first_building_id_str(_[0]),
)
async def test_metadata_merging(target_folder: Path, buildings_partitioned: list[Collection[Building]]):
METADATA_COLUMNS = ["bldg_id", "upgrade", "in.", "upgrade.", "weight"]
NOT_METADATA_COLUMNS = ["out."]
for building_chunk in buildings_partitioned:
await download_and_process_all(target_folder, building_chunk, ["metadata"])
buildings = [building for partition in buildings_partitioned for building in partition]
filenames: set[Path] = set()
for building in buildings:
path = (
target_folder
/ building.release
/ "metadata"
/ f"state={building.state}"
/ f"upgrade={building.upgrade.zfill(2)}"
)
files = list(path.iterdir())
assert len(files) == 1
assert files[0].suffix == ".parquet"
filename = files[0]
lf = pl.scan_parquet(filename)
assert not lf.filter(pl.col("bldg_id") == building.id).collect().is_empty()
filenames.add(filename)
for filename in filenames:
lf = pl.scan_parquet(filename)
columns = lf.collect_schema().keys()
for required_col in METADATA_COLUMNS:
if required_col == "upgrade." and (
"upgrade=00" in str(filename)
or "res_2024_tmy3_1" in str(filename)
or ("2022" not in str(filename) and "res_2024" not in str(filename) and "res_2025" not in str(filename))
):
continue
assert any(c.startswith(required_col) for c in columns)
for alien_col in NOT_METADATA_COLUMNS:
assert not any(c.startswith(alien_col) for c in columns)
assert set(pl.read_parquet(list(filenames))["bldg_id"]) == {_.id for _ in buildings}
RELEASES_WITH_LOAD_CURVES: list[ReleaseKey] = [
"com_2024_amy2018_1",
"com_2024_amy2018_2",
"res_2022_amy2012_1.1",
"res_2022_amy2012_1",
"res_2022_amy2018_1.1",
"res_2022_amy2018_1",
"res_2022_tmy3_1",
"res_2022_tmy3_1.1",
"res_2024_amy2018_2",
"res_2024_tmy3_2",
]
@pytest.mark.vcr
@pytest.mark.asyncio
@pytest.mark.network
@pytest.mark.parametrize(
["buildings"],
[
(buildings,)
for release in sorted(RELEASES.filter(file_types={"load_curve_annual"}).keys)
for state in ("NY",)
for upgrade in (normalize_upgrade_id("0"),)
if (buildings := list_buildings(release, state, upgrade, 5))
],
ids=first_building_id_str,
)
async def test_load_curve_annual(target_folder: Path, buildings: Collection[Building]):
ANNUAL_LOAD_CURVE_COLUMNS = ["bldg_id", "upgrade", "out.", "weight"]
NOT_ANNUAL_LOAD_CURVE_COLUMNS = ["in."]
await download_and_process_all(target_folder, buildings, ["load_curve_annual"])
filenames: set[Path] = set()
for building in buildings:
path = (
target_folder
/ building.release
/ "load_curve_annual"
/ f"state={building.state}"
/ f"upgrade={building.upgrade.zfill(2)}"
)
files = list(path.iterdir())
assert len(files) == 1
assert files[0].suffix == ".parquet"
filename = files[0]
lf = pl.scan_parquet(filename)
assert not lf.filter(pl.col("bldg_id") == building.id).collect().is_empty()
filenames.add(filename)
for filename in filenames:
lf = pl.scan_parquet(filename)
columns = lf.collect_schema().keys()
for required_col in ANNUAL_LOAD_CURVE_COLUMNS:
assert any(c.startswith(required_col) for c in columns)
for alien_col in NOT_ANNUAL_LOAD_CURVE_COLUMNS:
assert not any(c.startswith(alien_col) for c in columns)
assert set(pl.read_parquet(list(filenames))["bldg_id"]) == {_.id for _ in buildings}
@pytest.mark.vcr
@pytest.mark.asyncio
@pytest.mark.network
@pytest.mark.parametrize(
"buildings_partitioned",
[
[buildings[:5], buildings[-5:]]
for release in sorted(TEST_ANNUAL_LOAD_CURVE_MERGING_RELEASES)
for state in ("NY", "AL")
for upgrade in (normalize_upgrade_id("0"),)
if (buildings := list_buildings(release, state, upgrade, 50))
],
ids=lambda _: first_building_id_str(_[0]),
)
async def test_annual_load_curves_merging(target_folder: Path, buildings_partitioned: list[Collection[Building]]):
ANNUAL_LOAD_CURVE_COLUMNS = ["bldg_id", "upgrade", "out.", "weight"]
NOT_ANNUAL_LOAD_CURVE_COLUMNS = ["in."]
for building_chunk in buildings_partitioned:
await download_and_process_all(target_folder, building_chunk, ["load_curve_annual"])
buildings = [building for partition in buildings_partitioned for building in partition]
filenames: set[Path] = set()
for building in buildings:
path = (
target_folder
/ building.release
/ "load_curve_annual"
/ f"state={building.state}"
/ f"upgrade={building.upgrade.zfill(2)}"
)
files = list(path.iterdir())
assert len(files) == 1
assert files[0].suffix == ".parquet"
filename = files[0]
lf = pl.scan_parquet(filename)
assert not lf.filter(pl.col("bldg_id") == building.id).collect().is_empty()
filenames.add(filename)
for filename in filenames:
lf = pl.scan_parquet(filename)
columns = lf.collect_schema().keys()
for required_col in ANNUAL_LOAD_CURVE_COLUMNS:
assert any(c.startswith(required_col) for c in columns)
for alien_col in NOT_ANNUAL_LOAD_CURVE_COLUMNS:
assert not any(c.startswith(alien_col) for c in columns)
assert set(pl.read_parquet(list(filenames))["bldg_id"]) == {_.id for _ in buildings}
@pytest.mark.vcr
@pytest.mark.asyncio
@pytest.mark.network
@pytest.mark.parametrize(
["buildings"],
[
(buildings,)
for release in RELEASES_WITH_LOAD_CURVES
for state in ("NY",)
for upgrade in sorted(RELEASES[release].upgrades)[:2]
if (buildings := list_buildings(release, state, upgrade, 5))
],
ids=first_building_id_str,
)
async def test_load_curves(target_folder: Path, buildings: list[Building]):
file_types: list[FileType] = ["load_curve_15min", "load_curve_hourly", "load_curve_daily", "load_curve_monthly"]
await download_and_process_all(target_folder, buildings, file_types)
for building in buildings:
for file_type in file_types:
path = (
target_folder
/ building.release
/ file_type
/ f"state={building.state}"
/ f"upgrade={building.upgrade.zfill(2)}"
)
filenames = [n for n in path.iterdir() if n.suffix == ".parquet" and str(building.id) in n.name]
assert len(filenames) == 1