Repository navigation
Expand file tree
/
Copy pathtest_basic.py
More file actions
248 lines (198 loc) · 8.21 KB
/
Copy pathtest_basic.py
File metadata and controls
248 lines (198 loc) · 8.21 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
"""Basic tests mirroring the R package examples."""
import numpy as np
import pandas as pd
import pytest
import pycpprouting as cr
@pytest.fixture
def edges():
"""Sample edges from R package examples."""
return pd.DataFrame({
"from": [0, 0, 1, 1, 2, 2, 3, 4, 4],
"to": [1, 3, 2, 4, 4, 5, 1, 3, 5],
"cost": [9, 2, 11, 3, 5, 12, 4, 1, 6],
})
@pytest.fixture
def directed_graph(edges):
return cr.makegraph(edges, directed=True)
@pytest.fixture
def undirected_graph(edges):
return cr.makegraph(edges, directed=False)
class TestMakegraph:
def test_basic_construction(self, directed_graph):
assert directed_graph["nbnode"] == 6
assert len(directed_graph["dict"]) == 6
assert directed_graph["data"].shape[0] == 9
def test_undirected_doubles_edges(self, undirected_graph):
# Undirected graph duplicates edges
assert undirected_graph["data"].shape[0] == 18
def test_negative_cost_raises(self):
df = pd.DataFrame({"from": [0], "to": [1], "cost": [-1]})
with pytest.raises(ValueError, match="Negative cost"):
cr.makegraph(df)
def test_wrong_columns_raises(self):
df = pd.DataFrame({"a": [0], "b": [1]})
with pytest.raises(ValueError, match="3 columns"):
cr.makegraph(df)
def test_with_coords(self, edges):
coords = pd.DataFrame({
"node": [0, 1, 2, 3, 4, 5],
"X": [2, 2, 2, 0, 0, 0],
"Y": [0, 2, 2, 0, 2, 4],
})
g = cr.makegraph(edges, directed=True, coords=coords)
assert g["coords"] is not None
def test_with_aux(self, edges):
g = cr.makegraph(edges, directed=True, aux=1.0)
assert g["attrib"]["aux"] is not None
assert len(g["attrib"]["aux"]) == 9
def test_with_traffic_params(self, edges):
g = cr.makegraph(edges, directed=True, alpha=0.15, beta=4, capacity=5)
assert g["attrib"]["alpha"] is not None
assert g["attrib"]["beta"] is not None
assert g["attrib"]["cap"] is not None
class TestTodf:
def test_roundtrip(self, edges, directed_graph):
df = cr.to_df(directed_graph)
assert list(df.columns) == ["from", "to", "dist"]
assert len(df) == 9
# Check original node IDs are restored
assert set(df["from"].astype(str)) <= {"0", "1", "2", "3", "4", "5"}
class TestDistancePair:
def test_basic(self, directed_graph):
d = cr.get_distance_pair(directed_graph, [0], [5])
assert len(d) == 1
# SP from 0 to 5: 0->1->4->5 = 9+3+6=18 or 0->3->1->4->5=2+4+3+6=15
# or 0->1->2->4->5=9+11+5+6=31, 0->1->2->5=9+11+12=32
# 0->3->1->2->4->5=2+4+11+5+6=28
# 0->3->1->4->5=2+4+3+6=15
assert d[0] == pytest.approx(15.0)
def test_same_node(self, directed_graph):
d = cr.get_distance_pair(directed_graph, [0], [0])
assert d[0] == pytest.approx(0.0)
def test_multiple_pairs(self, directed_graph):
d = cr.get_distance_pair(directed_graph, [0, 0], [1, 3])
assert len(d) == 2
assert d[0] == pytest.approx(6.0) # 0->3->1 = 2+4=6
assert d[1] == pytest.approx(2.0) # 0->3 = 2
def test_unreachable(self, directed_graph):
d = cr.get_distance_pair(directed_graph, [5], [0])
assert d[0] == np.inf
def test_dijkstra_algorithm(self, directed_graph):
d = cr.get_distance_pair(directed_graph, [0], [5], algorithm="Dijkstra")
assert d[0] == pytest.approx(15.0)
class TestDistanceMatrix:
def test_basic(self, directed_graph):
nodes = list(directed_graph["dict"]["ref"])
mat = cr.get_distance_matrix(directed_graph, [nodes[0]], nodes)
assert mat.shape == (1, 6)
def test_square_matrix(self, directed_graph):
nodes = list(directed_graph["dict"]["ref"])
mat = cr.get_distance_matrix(directed_graph, nodes, nodes)
assert mat.shape == (6, 6)
# Diagonal should be 0
for i in range(6):
assert mat[i, i] == pytest.approx(0.0)
class TestPathPair:
def test_basic(self, directed_graph):
paths = cr.get_path_pair(directed_graph, [0], [5])
key = "0_5"
assert key in paths
assert len(paths[key]) > 0
assert paths[key][0] == "0"
assert paths[key][-1] == "5"
def test_long_format(self, directed_graph):
df = cr.get_path_pair(directed_graph, [0], [5], long=True)
assert isinstance(df, pd.DataFrame)
assert list(df.columns) == ["from", "to", "node"]
def test_unreachable(self, directed_graph):
paths = cr.get_path_pair(directed_graph, [5], [0])
assert paths["5_0"] == []
class TestMultiPaths:
def test_basic(self, directed_graph):
nodes = ["0", "1", "5"]
paths = cr.get_multi_paths(directed_graph, nodes, nodes)
assert "0" in paths
assert "5" in paths["0"]
def test_long_format(self, directed_graph):
df = cr.get_multi_paths(directed_graph, ["0", "1"], ["4", "5"], long=True)
assert isinstance(df, pd.DataFrame)
class TestIsochrone:
def test_single_limit(self, directed_graph):
iso = cr.get_isochrone(directed_graph, "4", lim=1)
assert "4" in iso
# Node 3 is reachable from 4 with cost 1
assert "3" in iso["4"]
def test_multiple_limits(self, directed_graph):
iso = cr.get_isochrone(directed_graph, "4", lim=[1, 6])
assert "4" in iso
assert "1.0" in iso["4"] or "1" in iso["4"]
def test_long_format(self, directed_graph):
df = cr.get_isochrone(directed_graph, "4", lim=6, long=True)
assert isinstance(df, pd.DataFrame)
assert list(df.columns) == ["origin", "node", "lim"]
class TestDetour:
def test_basic(self, undirected_graph):
det = cr.get_detour(undirected_graph, [0], [5], extra=3)
assert "0_5" in det
def test_extra_required(self, directed_graph):
with pytest.raises(ValueError, match="No additional cost"):
cr.get_detour(directed_graph, [0], [5])
class TestCppSimplify:
def test_basic(self):
edges = pd.DataFrame({
"from": [1, 2, 3, 4, 5, 6, 7, 8],
"to": [0, 1, 2, 3, 6, 7, 8, 5],
"dist": [1, 1, 1, 1, 1, 1, 1, 1],
})
graph = cr.makegraph(edges, directed=True)
simp = cr.cpp_simplify(graph, rm_loop=True)
assert simp["nbnode"] <= graph["nbnode"]
class TestCppContract:
def test_basic(self, directed_graph):
contracted = cr.cpp_contract(directed_graph, silent=True)
assert "rank" in contracted
assert "shortcuts" in contracted
assert "original" in contracted
def test_contracted_distance(self, directed_graph):
contracted = cr.cpp_contract(directed_graph, silent=True)
d = cr.get_distance_pair(contracted, [0], [5])
d_orig = cr.get_distance_pair(directed_graph, [0], [5])
assert d[0] == pytest.approx(d_orig[0])
class TestAon:
def test_basic(self):
edges = pd.DataFrame({
"from": [0, 0, 1, 1, 2, 2, 3, 4, 4],
"to": [1, 3, 2, 4, 4, 5, 1, 3, 5],
"cost": [9, 2, 11, 3, 5, 12, 4, 1, 6],
})
graph = cr.makegraph(edges, directed=True)
trips = pd.DataFrame({
"from": [0, 1],
"to": [5, 5],
"flow": [10, 5],
})
aon = cr.get_aon(graph, trips["from"], trips["to"], trips["flow"], algorithm="d")
assert isinstance(aon, pd.DataFrame)
assert "flow" in aon.columns
assert aon["flow"].sum() > 0
class TestAssignTraffic:
def test_basic(self):
edges = pd.DataFrame({
"from": [0, 0, 1, 1, 2, 2, 3, 4, 4],
"to": [1, 3, 2, 4, 4, 5, 1, 3, 5],
"cost": [9, 2, 11, 3, 5, 12, 4, 1, 6],
})
graph = cr.makegraph(edges, directed=True, alpha=0.15, beta=4, capacity=5)
trips = pd.DataFrame({
"from": [0, 1],
"to": [5, 5],
"flow": [10, 5],
})
result = cr.assign_traffic(
graph, trips["from"], trips["to"], trips["flow"],
algorithm="fw", max_it=10, verbose=False,
)
assert "gap" in result
assert "iteration" in result
assert "data" in result
assert isinstance(result["data"], pd.DataFrame)