Spaces:
Sleeping
Sleeping
dn commited on
Commit ·
2ac3615
1
Parent(s): c55e963
normalize line endings to LF
Browse files- .gitattributes +1 -0
- shared/{api → pipelines}/pems.py +0 -0
- tests/pipelines/test_pems.py +336 -0
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
* text=auto eol=lf
|
shared/{api → pipelines}/pems.py
RENAMED
|
File without changes
|
tests/pipelines/test_pems.py
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
from shared.pipelines.pems import add_segment_length, add_within_year_se
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _base_row(**kwargs):
|
| 11 |
+
defaults = dict(
|
| 12 |
+
station=1000,
|
| 13 |
+
route=101,
|
| 14 |
+
direction="N",
|
| 15 |
+
hour=8,
|
| 16 |
+
year=2022,
|
| 17 |
+
type="ML",
|
| 18 |
+
abs_pm=10.0,
|
| 19 |
+
lanes=4,
|
| 20 |
+
days_observed=60,
|
| 21 |
+
avg_speed=60.0,
|
| 22 |
+
sd_speed=6.0,
|
| 23 |
+
avg_flow=1500.0,
|
| 24 |
+
sd_flow=200.0,
|
| 25 |
+
)
|
| 26 |
+
return {**defaults, **kwargs}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _df(*overrides):
|
| 30 |
+
rows = overrides if overrides else ({},)
|
| 31 |
+
return pd.DataFrame([_base_row(**r) for r in rows])
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class TestWithinYearSE:
|
| 35 |
+
def test_speed_se_formula(self):
|
| 36 |
+
# Arrange
|
| 37 |
+
df = _df(dict(sd_speed=6.0, days_observed=36))
|
| 38 |
+
|
| 39 |
+
# Act
|
| 40 |
+
out = add_within_year_se(df)
|
| 41 |
+
|
| 42 |
+
# Assert
|
| 43 |
+
assert out["se_speed"].iloc[0] == pytest.approx(1.0)
|
| 44 |
+
|
| 45 |
+
def test_flow_se_formula(self):
|
| 46 |
+
# Arrange
|
| 47 |
+
df = _df(dict(sd_flow=200.0, days_observed=100))
|
| 48 |
+
|
| 49 |
+
# Act
|
| 50 |
+
out = add_within_year_se(df)
|
| 51 |
+
|
| 52 |
+
# Assert
|
| 53 |
+
assert out["se_flow"].iloc[0] == pytest.approx(20.0)
|
| 54 |
+
|
| 55 |
+
def test_se_decreases_with_more_days(self):
|
| 56 |
+
# Arrange
|
| 57 |
+
df_few = _df(dict(sd_speed=6.0, days_observed=10))
|
| 58 |
+
df_many = _df(dict(sd_speed=6.0, days_observed=90))
|
| 59 |
+
|
| 60 |
+
# Act
|
| 61 |
+
se_few = add_within_year_se(df_few)["se_speed"].iloc[0]
|
| 62 |
+
se_many = add_within_year_se(df_many)["se_speed"].iloc[0]
|
| 63 |
+
|
| 64 |
+
# Assert
|
| 65 |
+
assert se_few > se_many
|
| 66 |
+
|
| 67 |
+
def test_days_observed_one_gives_se_equal_to_sd(self):
|
| 68 |
+
# Arrange
|
| 69 |
+
df = _df(dict(sd_speed=5.0, days_observed=1))
|
| 70 |
+
|
| 71 |
+
# Act
|
| 72 |
+
out = add_within_year_se(df)
|
| 73 |
+
v = out["se_speed"].iloc[0]
|
| 74 |
+
|
| 75 |
+
# Assert
|
| 76 |
+
assert math.isfinite(v)
|
| 77 |
+
assert v == pytest.approx(5.0)
|
| 78 |
+
|
| 79 |
+
def test_days_observed_zero_clipped_no_crash(self):
|
| 80 |
+
# Arrange
|
| 81 |
+
df = _df(dict(sd_speed=5.0, days_observed=0))
|
| 82 |
+
|
| 83 |
+
# Act
|
| 84 |
+
out = add_within_year_se(df)
|
| 85 |
+
|
| 86 |
+
# Assert
|
| 87 |
+
assert math.isfinite(out["se_speed"].iloc[0])
|
| 88 |
+
|
| 89 |
+
def test_missing_sd_flow_col_skipped(self):
|
| 90 |
+
# Arrange
|
| 91 |
+
df = _df().drop(columns=["sd_flow"])
|
| 92 |
+
|
| 93 |
+
# Act
|
| 94 |
+
out = add_within_year_se(df)
|
| 95 |
+
|
| 96 |
+
# Assert
|
| 97 |
+
assert "se_flow" not in out.columns
|
| 98 |
+
|
| 99 |
+
def test_missing_sd_speed_col_skipped(self):
|
| 100 |
+
# Arrange
|
| 101 |
+
df = _df().drop(columns=["sd_speed"])
|
| 102 |
+
|
| 103 |
+
# Act
|
| 104 |
+
out = add_within_year_se(df)
|
| 105 |
+
|
| 106 |
+
# Assert
|
| 107 |
+
assert "se_speed" not in out.columns
|
| 108 |
+
|
| 109 |
+
def test_multiple_rows_independent(self):
|
| 110 |
+
# Arrange
|
| 111 |
+
df = _df(
|
| 112 |
+
dict(sd_speed=4.0, days_observed=16), # 4 / sqrt(16) = 1.0
|
| 113 |
+
dict(sd_speed=9.0, days_observed=81), # 9 / sqrt(81) = 1.0
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Act
|
| 117 |
+
out = add_within_year_se(df)
|
| 118 |
+
|
| 119 |
+
# Assert
|
| 120 |
+
assert out["se_speed"].iloc[0] == pytest.approx(1.0)
|
| 121 |
+
assert out["se_speed"].iloc[1] == pytest.approx(1.0)
|
| 122 |
+
|
| 123 |
+
def test_both_output_columns_added(self):
|
| 124 |
+
# Arrange
|
| 125 |
+
df = _df()
|
| 126 |
+
|
| 127 |
+
# Act
|
| 128 |
+
out = add_within_year_se(df)
|
| 129 |
+
|
| 130 |
+
# Assert
|
| 131 |
+
assert "se_speed" in out.columns
|
| 132 |
+
assert "se_flow" in out.columns
|
| 133 |
+
|
| 134 |
+
def test_original_columns_preserved(self):
|
| 135 |
+
# Arrange
|
| 136 |
+
df = _df()
|
| 137 |
+
original_cols = list(df.columns)
|
| 138 |
+
|
| 139 |
+
# Act
|
| 140 |
+
out = add_within_year_se(df.copy())
|
| 141 |
+
|
| 142 |
+
# Assert
|
| 143 |
+
for col in original_cols:
|
| 144 |
+
assert col in out.columns
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
class TestSegmentLength:
|
| 148 |
+
def test_northbound_segment_lengths(self):
|
| 149 |
+
# Arrange
|
| 150 |
+
df = _df(
|
| 151 |
+
dict(station=1, abs_pm=10.0, direction="N"),
|
| 152 |
+
dict(station=2, abs_pm=12.5, direction="N"),
|
| 153 |
+
dict(station=3, abs_pm=15.0, direction="N"),
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
# Act
|
| 157 |
+
out = add_segment_length(df).sort_values("abs_pm").reset_index(drop=True)
|
| 158 |
+
|
| 159 |
+
# Assert
|
| 160 |
+
assert out["segment_length"].iloc[0] == pytest.approx(2.5)
|
| 161 |
+
assert out["segment_length"].iloc[1] == pytest.approx(2.5)
|
| 162 |
+
assert pd.isna(out["segment_length"].iloc[2])
|
| 163 |
+
|
| 164 |
+
def test_southbound_segment_lengths(self):
|
| 165 |
+
# Arrange
|
| 166 |
+
df = _df(
|
| 167 |
+
dict(station=1, abs_pm=15.0, direction="S"),
|
| 168 |
+
dict(station=2, abs_pm=12.5, direction="S"),
|
| 169 |
+
dict(station=3, abs_pm=10.0, direction="S"),
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# Act
|
| 173 |
+
out = add_segment_length(df).sort_values("abs_pm", ascending=False).reset_index(drop=True)
|
| 174 |
+
|
| 175 |
+
# Assert
|
| 176 |
+
assert out["segment_length"].iloc[0] == pytest.approx(2.5)
|
| 177 |
+
assert out["segment_length"].iloc[1] == pytest.approx(2.5)
|
| 178 |
+
assert pd.isna(out["segment_length"].iloc[2])
|
| 179 |
+
|
| 180 |
+
def test_eastbound_treated_like_northbound(self):
|
| 181 |
+
# Arrange
|
| 182 |
+
df = _df(
|
| 183 |
+
dict(station=1, abs_pm=5.0, direction="E", route=580),
|
| 184 |
+
dict(station=2, abs_pm=8.0, direction="E", route=580),
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# Act
|
| 188 |
+
out = add_segment_length(df).sort_values("abs_pm").reset_index(drop=True)
|
| 189 |
+
|
| 190 |
+
# Assert
|
| 191 |
+
assert out["segment_length"].iloc[0] == pytest.approx(3.0)
|
| 192 |
+
|
| 193 |
+
def test_westbound_treated_like_southbound(self):
|
| 194 |
+
# Arrange
|
| 195 |
+
df = _df(
|
| 196 |
+
dict(station=1, abs_pm=8.0, direction="W", route=580),
|
| 197 |
+
dict(station=2, abs_pm=5.0, direction="W", route=580),
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# Act
|
| 201 |
+
out = add_segment_length(df).sort_values("abs_pm", ascending=False).reset_index(drop=True)
|
| 202 |
+
|
| 203 |
+
# Assert
|
| 204 |
+
assert out["segment_length"].iloc[0] == pytest.approx(3.0)
|
| 205 |
+
|
| 206 |
+
def test_segment_length_always_non_negative(self):
|
| 207 |
+
# Arrange
|
| 208 |
+
df = _df(
|
| 209 |
+
dict(station=1, abs_pm=15.0, direction="S"),
|
| 210 |
+
dict(station=2, abs_pm=12.5, direction="S"),
|
| 211 |
+
dict(station=3, abs_pm=10.0, direction="S"),
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
# Act
|
| 215 |
+
out = add_segment_length(df)
|
| 216 |
+
|
| 217 |
+
# Assert
|
| 218 |
+
assert (out["segment_length"].dropna() >= 0).all()
|
| 219 |
+
|
| 220 |
+
def test_segment_computed_within_year(self):
|
| 221 |
+
# Arrange
|
| 222 |
+
df = _df(
|
| 223 |
+
dict(station=1, abs_pm=10.0, year=2020),
|
| 224 |
+
dict(station=2, abs_pm=12.5, year=2020),
|
| 225 |
+
dict(station=3, abs_pm=15.0, year=2020),
|
| 226 |
+
dict(station=1, abs_pm=10.0, year=2021),
|
| 227 |
+
dict(station=3, abs_pm=15.0, year=2021),
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
# Act
|
| 231 |
+
out = add_segment_length(df)
|
| 232 |
+
|
| 233 |
+
# Assert
|
| 234 |
+
seg_2020 = out[(out.station == 1) & (out.year == 2020)]["segment_length"].iloc[0]
|
| 235 |
+
seg_2021 = out[(out.station == 1) & (out.year == 2021)]["segment_length"].iloc[0]
|
| 236 |
+
assert seg_2020 == pytest.approx(2.5)
|
| 237 |
+
assert seg_2021 == pytest.approx(5.0)
|
| 238 |
+
|
| 239 |
+
def test_routes_do_not_bleed(self):
|
| 240 |
+
# Arrange
|
| 241 |
+
df = _df(
|
| 242 |
+
dict(station=1, route=101, abs_pm=10.0),
|
| 243 |
+
dict(station=2, route=280, abs_pm=11.0),
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
# Act
|
| 247 |
+
out = add_segment_length(df)
|
| 248 |
+
|
| 249 |
+
# Assert
|
| 250 |
+
assert out["segment_length"].isna().all()
|
| 251 |
+
|
| 252 |
+
def test_opposite_directions_do_not_bleed(self):
|
| 253 |
+
# Arrange
|
| 254 |
+
df = _df(
|
| 255 |
+
dict(station=1, direction="N", abs_pm=10.0),
|
| 256 |
+
dict(station=2, direction="S", abs_pm=12.5),
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
# Act
|
| 260 |
+
out = add_segment_length(df)
|
| 261 |
+
|
| 262 |
+
# Assert
|
| 263 |
+
assert out["segment_length"].isna().all()
|
| 264 |
+
|
| 265 |
+
def test_different_hours_do_not_bleed(self):
|
| 266 |
+
# Arrange
|
| 267 |
+
df = _df(
|
| 268 |
+
dict(station=1, hour=8, abs_pm=10.0),
|
| 269 |
+
dict(station=2, hour=17, abs_pm=12.5),
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
# Act
|
| 273 |
+
out = add_segment_length(df)
|
| 274 |
+
|
| 275 |
+
# Assert
|
| 276 |
+
assert out["segment_length"].isna().all()
|
| 277 |
+
|
| 278 |
+
def test_single_station_nan_segment(self):
|
| 279 |
+
# Arrange
|
| 280 |
+
df = _df()
|
| 281 |
+
|
| 282 |
+
# Act
|
| 283 |
+
out = add_segment_length(df)
|
| 284 |
+
|
| 285 |
+
# Assert
|
| 286 |
+
assert "segment_length" in out.columns
|
| 287 |
+
assert pd.isna(out["segment_length"].iloc[0])
|
| 288 |
+
|
| 289 |
+
def test_missing_abs_pm_column_skipped_gracefully(self):
|
| 290 |
+
# Arrange
|
| 291 |
+
df = pd.DataFrame([{k: v for k, v in _base_row().items() if k != "abs_pm"}])
|
| 292 |
+
|
| 293 |
+
# Act
|
| 294 |
+
out = add_segment_length(df)
|
| 295 |
+
|
| 296 |
+
# Assert
|
| 297 |
+
assert "segment_length" not in out.columns
|
| 298 |
+
|
| 299 |
+
def test_abs_pm_as_string_coerced(self):
|
| 300 |
+
# Arrange
|
| 301 |
+
df = _df(
|
| 302 |
+
dict(station=1, abs_pm="10.0"),
|
| 303 |
+
dict(station=2, abs_pm="12.5"),
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
# Act
|
| 307 |
+
out = add_segment_length(df).sort_values("abs_pm").reset_index(drop=True)
|
| 308 |
+
|
| 309 |
+
# Assert
|
| 310 |
+
assert out["segment_length"].iloc[0] == pytest.approx(2.5)
|
| 311 |
+
|
| 312 |
+
def test_output_columns_present(self):
|
| 313 |
+
# Arrange
|
| 314 |
+
df = _df(
|
| 315 |
+
dict(station=1, abs_pm=10.0),
|
| 316 |
+
dict(station=2, abs_pm=12.5),
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
# Act
|
| 320 |
+
out = add_segment_length(df)
|
| 321 |
+
|
| 322 |
+
# Assert
|
| 323 |
+
for col in ("travel_order_pm", "next_pm", "segment_length"):
|
| 324 |
+
assert col in out.columns
|
| 325 |
+
|
| 326 |
+
def test_original_columns_preserved(self):
|
| 327 |
+
# Arrange
|
| 328 |
+
df = _df(dict(station=1, abs_pm=10.0), dict(station=2, abs_pm=12.5))
|
| 329 |
+
original_cols = list(df.columns)
|
| 330 |
+
|
| 331 |
+
# Act
|
| 332 |
+
out = add_segment_length(df.copy())
|
| 333 |
+
|
| 334 |
+
# Assert
|
| 335 |
+
for col in original_cols:
|
| 336 |
+
assert col in out.columns
|