-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_export.py
More file actions
120 lines (87 loc) · 2.9 KB
/
Copy pathtest_export.py
File metadata and controls
120 lines (87 loc) · 2.9 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
"""Tests for single-run exports and the run CLI (roadmap #5)."""
from __future__ import annotations
import importlib.util
import json
from pathlib import Path
import pandas as pd
from spatial_market_lockin import ModelConfig, SpatialMarketModel
from spatial_market_lockin.export import export_run
REPO_ROOT = Path(__file__).resolve().parents[1]
def _small_run() -> SpatialMarketModel:
model = SpatialMarketModel(
ModelConfig(seed=42, steps=15, grid_size=10, num_buyers=20, num_sellers=4)
)
model.run()
return model
def test_export_writes_all_files(tmp_path: Path) -> None:
model = _small_run()
out = export_run(model, tmp_path / "run")
for name in (
"model_timeseries.csv",
"transactions.csv",
"final_buyers.csv",
"final_sellers.csv",
"run_metadata.json",
):
assert (out / name).exists()
def test_transactions_csv_matches_ledger(tmp_path: Path) -> None:
model = _small_run()
out = export_run(model, tmp_path)
df = pd.read_csv(out / "transactions.csv")
assert len(df) == len(model.transactions)
assert list(df.columns) == [
"tick",
"buyer_id",
"seller_id",
"buyer_generation",
"price",
"quantity",
"cost",
"x",
"y",
]
def test_final_sellers_csv_matches_living_sellers(tmp_path: Path) -> None:
model = _small_run()
out = export_run(model, tmp_path)
df = pd.read_csv(out / "final_sellers.csv")
assert len(df) == len(model.alive_sellers)
def test_metadata_json_has_config_and_summary(tmp_path: Path) -> None:
model = _small_run()
out = export_run(model, tmp_path)
metadata = json.loads((out / "run_metadata.json").read_text())
assert metadata["config"]["seed"] == 42
assert metadata["config"]["steps"] == 15
assert "switch_rate" in metadata["summary"]
assert "never_transacted" in metadata["summary"]
def test_export_creates_nested_output_dir(tmp_path: Path) -> None:
model = _small_run()
out = export_run(model, tmp_path / "a" / "b" / "run")
assert out.is_dir()
assert (out / "transactions.csv").exists()
def _load_run_model_main():
spec = importlib.util.spec_from_file_location(
"run_model_cli", REPO_ROOT / "scripts" / "run_model.py"
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module.main
def test_cli_runs_and_writes_outputs(tmp_path: Path) -> None:
main = _load_run_model_main()
main(
[
"--seed",
"1",
"--steps",
"5",
"--grid-size",
"8",
"--num-buyers",
"10",
"--num-sellers",
"3",
"--output-dir",
str(tmp_path / "cli"),
]
)
assert (tmp_path / "cli" / "model_timeseries.csv").exists()
assert (tmp_path / "cli" / "run_metadata.json").exists()