-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_union_city.py
107 lines (94 loc) · 3.88 KB
/
test_union_city.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
import os
from unittest.mock import MagicMock
import sys
import pytest
_EXAMPLES_DIR = r"examples"
def test_example_download():
# If (and only if) Emme is not installed, replace INRO libraries with MagicMock
try:
import inro.emme.database.emmebank
except ModuleNotFoundError:
sys.modules["inro.emme.database.emmebank"] = MagicMock()
sys.modules["inro.emme.network"] = MagicMock()
sys.modules["inro.emme.database.scenario"] = MagicMock()
sys.modules["inro.emme.database.matrix"] = MagicMock()
sys.modules["inro.emme.network.node"] = MagicMock()
sys.modules["inro.emme.desktop.app"] = MagicMock()
sys.modules["inro"] = MagicMock()
sys.modules["inro.modeller"] = MagicMock()
import shutil
from tm2py.examples import get_example
name = "UnionCity"
example_dir = os.path.join(os.getcwd(), _EXAMPLES_DIR)
union_city_root = os.path.join(example_dir, name)
if os.path.exists(union_city_root):
shutil.rmtree(union_city_root)
get_example(
example_name="UnionCity", example_subdir=_EXAMPLES_DIR, root_dir=os.getcwd()
)
# default retrieval_url points to Union City example on box
# check that the root union city folder exists
assert os.path.isdir(os.path.join(example_dir, name))
# check some expected files exists
files_to_check = [
os.path.join("inputs", "hwy", "tolls.csv"),
os.path.join("inputs", "nonres", "2035_fromOAK.csv"),
os.path.join("inputs", "landuse", "maz_data.csv"),
os.path.join("emme_project", "mtc_emme.emp"),
os.path.join("emme_project", "Database_highway", "emmebank"),
]
for file_name in files_to_check:
assert os.path.exists(
os.path.join(example_dir, name, file_name)
), f"get_example failed, missing {file_name}"
# check zip file was removed
assert not (os.path.exists(os.path.join(example_dir, name, "test_data.zip")))
@pytest.mark.skipci
def test_highway():
from tm2py.controller import RunController
from tm2py.examples import get_example
import openmatrix as _omx
union_city_root = os.path.join(os.getcwd(), _EXAMPLES_DIR, "UnionCity")
get_example(
example_name="UnionCity", example_subdir=_EXAMPLES_DIR, root_dir=os.getcwd()
)
controller = RunController(
[
os.path.join(_EXAMPLES_DIR, r"scenario_config.toml"),
os.path.join(_EXAMPLES_DIR, r"model_config.toml"),
],
run_dir=union_city_root
)
controller.run()
root = os.path.join(controller.run_dir, r"skim_matrices\highway")
ref_root = os.path.join(controller.run_dir, r"ref_skim_matrices\highway")
open_files = []
file_names = [name for name in os.listdir(root) if name.endswith(".omx")]
different_skims = []
try:
for name in file_names:
skims = _omx.open_file(os.path.join(root, name))
open_files.append(skims)
ref_skims = _omx.open_file(os.path.join(ref_root, name))
open_files.append(ref_skims)
for key in skims.list_matrices():
data = skims[key].read()
ref_data = ref_skims[key].read()
if not (data == ref_data).all():
different_skims.append(key)
finally:
for f in open_files:
f.close()
assert (
len(different_skims) == 0
), f"there are {len(different_skims)} different skims: {','.join(different_skims)}"
count_different_lines = 0
with open(os.path.join(root, "HWYSKIM_MAZMAZ_DA.csv")) as data:
with open(os.path.join(ref_root, "HWYSKIM_MAZMAZ_DA.csv")) as ref_data:
for line in data:
ref_line = next(ref_data)
if ref_line != line:
count_different_lines = 1
assert (
count_different_lines == 0
), f"HWYSKIM_MAZMAZ_DA.csv differs on {count_different_lines} lines"