Skip to content

Commit 7537165

Browse files
committed
Add .iea_web.fuzz_data() and tests
1 parent 5aeb5e2 commit 7537165

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

message_ix_models/tests/test_tools.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ def test_generate_code_lists(test_context, tmp_path):
3333

3434
# generate_code_lists() runs
3535
iea_web.generate_code_lists(tmp_path)
36+
37+
38+
def test_fuzz_data(test_context, tmp_path):
39+
# fuzz_data() runs
40+
iea_web.fuzz_data(target_path=tmp_path)

message_ix_models/tools/iea_web.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
from pathlib import Path
44

5+
import numpy as np
56
import pandas as pd
67
import yaml
78
from iam_units import registry
@@ -24,9 +25,9 @@
2425

2526
#: File name containing data.
2627
FILE = "WBAL_12052022124930839.csv"
27-
FILE = "cac5fa90-en.zip"
28+
# FILE = "cac5fa90-en.zip"
2829

29-
NROWS = 1e7
30+
NROWS = 1e6
3031

3132

3233
def _read(base_path=None, **kwargs) -> pd.DataFrame:
@@ -142,3 +143,18 @@ def _check1(value):
142143
cl_path.write_text(yaml.dump(data))
143144

144145

146+
def fuzz_data(base_path=None, target_path=None):
147+
"""Generate a fuzzed subset of the data for testing."""
148+
df = _read(base_path)
149+
150+
# - Reduce the data by only taking 2 periods for each (flow, product, country).
151+
# - Replace the actual values with random.
152+
df = (
153+
df.groupby(["FLOW", "PRODUCT", "COUNTRY"])
154+
.take([0, -1])
155+
.reset_index(drop=True)
156+
.assign(Value=lambda df: np.random.rand(len(df)))
157+
)
158+
159+
# TODO write to file
160+
# path = (target_path or package_data_path("iea")).joinpath(f"fuzzed-{FILE}")

0 commit comments

Comments
 (0)