Skip to content

Commit 1598303

Browse files
committed
Generate BACI data sample for testing
1 parent fcfff6c commit 1598303

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

message_ix_models/tools/cepii.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,28 @@ def baci_data_from_files(
166166

167167
log.info(f"{len(result)} observations")
168168
return result
169+
170+
171+
if __name__ == "__main__": # pragma: no cover
172+
from tqdm import tqdm
173+
174+
from message_ix_models.util import random_sample_from_file
175+
176+
print("Generate test data for BACI")
177+
178+
# - Fetch (if necessary) and unpack (if necessary) the BACI data archive.
179+
# - Select only the data files.
180+
paths = filter(lambda p: p.name.startswith("BACI"), fetch(**SOURCE["CEPII_BACI"]))
181+
182+
# Target for test data files
183+
target_dir = path_fallback("cepii-baci", where="test")
184+
185+
# Fraction of data to retain
186+
frac = 0.001
187+
188+
for file in tqdm(paths):
189+
# - Read data, sample, and replace with random values.
190+
# - Write to the test data directory.
191+
random_sample_from_file(file, frac, cols=["q", "v"], na_values=[""]).to_csv(
192+
target_dir.joinpath(file.name), float_format="%.3f", index=False
193+
)

0 commit comments

Comments
 (0)