|
| 1 | +import random |
| 2 | +from fractions import Fraction |
1 | 3 | from itertools import permutations |
| 4 | +from pathlib import Path |
2 | 5 | from typing import Literal, cast |
3 | 6 |
|
| 7 | +import pandas as pd |
4 | 8 | import pytest |
5 | 9 |
|
6 | 10 | from votekit.ballot import RankBallot, ScoreBallot |
|
25 | 29 | tiebroken_ranking, |
26 | 30 | validate_score_vector, |
27 | 31 | ) |
| 32 | +from votekit.utils.common_utils import _mentions_from_ballots, _mentions_from_df |
| 33 | + |
| 34 | +CSV_DIR = Path(__file__).resolve().parents[1] / "data" / "csv" |
28 | 35 |
|
29 | 36 | profile_no_ties = RankProfile( |
30 | 37 | ballots=( |
|
42 | 49 | ) |
43 | 50 | ) |
44 | 51 |
|
| 52 | +profile_with_duplicates = RankProfile( |
| 53 | + ballots=( |
| 54 | + RankBallot(ranking=tuple(map(frozenset, [{"A"}, {"B"}, {"B"}])), weight=1), |
| 55 | + RankBallot(ranking=tuple(map(frozenset, [{"A"}, {"B"}, {"C"}])), weight=1 / 2), |
| 56 | + RankBallot(ranking=tuple(map(frozenset, [{"B"}, {"B"}, {"B"}])), weight=3), |
| 57 | + ) |
| 58 | +) |
| 59 | + |
45 | 60 | profile_with_missing = RankProfile( |
46 | 61 | ballots=( |
47 | 62 | RankBallot(ranking=tuple(map(frozenset, [{"A", "B"}, {"D"}])), weight=1), |
@@ -246,13 +261,133 @@ def test_fpv_errors(): |
246 | 261 | first_place_votes(cast(RankProfile, ScoreProfile(ballots=(ScoreBallot(scores={"A": 3}),)))) |
247 | 262 |
|
248 | 263 |
|
249 | | -def test_mentions(): |
| 264 | +def test_mentions_from_ballots(): |
| 265 | + correct = {"A": 9 / 2, "B": 9 / 2, "C": 7 / 2} |
| 266 | + test = _mentions_from_ballots(profile_no_ties) |
| 267 | + assert correct == test |
| 268 | + assert isinstance(test["A"], float) |
| 269 | + |
| 270 | + |
| 271 | +def test_mentions_from_ballots_with_ties(): |
| 272 | + correct = {"A": 9 / 2, "B": 9 / 2, "C": 7 / 2} |
| 273 | + test = _mentions_from_ballots(profile_with_ties) |
| 274 | + assert correct == test |
| 275 | + assert isinstance(test["A"], float) |
| 276 | + |
| 277 | + |
| 278 | +def test_mentions_from_ballots_with_duplicates(): |
| 279 | + correct = {"A": 3 / 2, "B": 23 / 2, "C": 1 / 2} |
| 280 | + test = _mentions_from_ballots(profile_with_duplicates) |
| 281 | + assert correct == test |
| 282 | + assert isinstance(test["A"], float) |
| 283 | + |
| 284 | + |
| 285 | +def test_mentions_from_df(): |
| 286 | + correct = {"A": 9 / 2, "B": 9 / 2, "C": 7 / 2} |
| 287 | + test = _mentions_from_df(profile_no_ties) |
| 288 | + assert correct == test |
| 289 | + assert isinstance(test["A"], float) |
| 290 | + |
| 291 | + |
| 292 | +def test_mentions_from_df_with_ties(): |
250 | 293 | correct = {"A": 9 / 2, "B": 9 / 2, "C": 7 / 2} |
251 | | - test = mentions(profile_no_ties) |
| 294 | + test = _mentions_from_df(profile_with_ties) |
| 295 | + assert correct == test |
| 296 | + assert isinstance(test["A"], float) |
| 297 | + |
| 298 | + |
| 299 | +def test_mentions_from_df_with_duplicates(): |
| 300 | + correct = {"A": 3 / 2, "B": 23 / 2, "C": 1 / 2} |
| 301 | + test = _mentions_from_df(profile_with_duplicates) |
252 | 302 | assert correct == test |
253 | 303 | assert isinstance(test["A"], float) |
254 | 304 |
|
255 | 305 |
|
| 306 | +@pytest.mark.slow |
| 307 | +def test_fast_and_slow_mentions_are_same(): |
| 308 | + profile = cast(RankProfile, RankProfile.from_csv(CSV_DIR / "albany_profile.csv")) |
| 309 | + assert _mentions_from_ballots(profile) == _mentions_from_df(profile) |
| 310 | + |
| 311 | + |
| 312 | +def test_mentions_zero_weight_ballots(): |
| 313 | + # "A" appears only on a zero-weight ballot, so it is not in profile.candidates; both |
| 314 | + # mentions paths must skip it rather than raise KeyError |
| 315 | + profile = RankProfile( |
| 316 | + ballots=( |
| 317 | + RankBallot(ranking=tuple(map(frozenset, [{"A"}])), weight=0), |
| 318 | + RankBallot(ranking=tuple(map(frozenset, [{"B"}])), weight=2), |
| 319 | + ) |
| 320 | + ) |
| 321 | + correct = {"B": 2.0} |
| 322 | + assert _mentions_from_ballots(profile) == correct |
| 323 | + assert _mentions_from_df(profile) == correct |
| 324 | + |
| 325 | + |
| 326 | +def test_mentions_from_df_returns_python_floats(): |
| 327 | + assert all(type(v) is float for v in _mentions_from_df(profile_no_ties).values()) |
| 328 | + |
| 329 | + |
| 330 | +def test_mentions_no_ranking_ballot_raises_in_both_paths(): |
| 331 | + # an unranked ballot materializes with ranking=None, so both paths must raise the same error |
| 332 | + profile = RankProfile(ballots=(RankBallot(weight=2), RankBallot(ranking=({"A"},), weight=1))) |
| 333 | + with pytest.raises(TypeError, match="Ballots must have rankings."): |
| 334 | + _mentions_from_df(profile) |
| 335 | + with pytest.raises(TypeError, match="Ballots must have rankings."): |
| 336 | + _mentions_from_ballots(profile) |
| 337 | + |
| 338 | + |
| 339 | +def test_mentions_all_unranked_profile_raises_in_both_paths(): |
| 340 | + # every ballot has ranking=None, so the profile has no ranking columns at all |
| 341 | + profile = RankProfile(ballots=(RankBallot(weight=2),)) |
| 342 | + with pytest.raises(TypeError, match="Ballots must have rankings."): |
| 343 | + _mentions_from_df(profile) |
| 344 | + with pytest.raises(TypeError, match="Ballots must have rankings."): |
| 345 | + _mentions_from_ballots(profile) |
| 346 | + |
| 347 | + |
| 348 | +def test_mentions_empty_frozenset_ranking_consistent(): |
| 349 | + # a present-but-empty frozenset ranking is not ranking=None; neither path should raise |
| 350 | + profile = RankProfile(ballots=(RankBallot(ranking=(frozenset(),), weight=1),)) |
| 351 | + assert _mentions_from_df(profile) == {} |
| 352 | + assert _mentions_from_ballots(profile) == {} |
| 353 | + |
| 354 | + |
| 355 | +def test_mentions_from_df_duplicate_ballot_index(): |
| 356 | + # profiles built from a df keep the given index; duplicate labels must not break the df path |
| 357 | + df = RankProfile(ballots=(RankBallot(ranking=({"A"}, {"B"}), weight=1),)).df |
| 358 | + profile = RankProfile(df=pd.concat([df, df]), candidates=("A", "B"), max_ranking_length=2) |
| 359 | + correct = {"A": 2.0, "B": 2.0} |
| 360 | + assert _mentions_from_df(profile) == correct |
| 361 | + assert _mentions_from_ballots(profile) == correct |
| 362 | + |
| 363 | + |
| 364 | +def _random_mentions_profile(rng: random.Random) -> RankProfile: |
| 365 | + # samples every dimension that has produced a path divergence: ties, duplicate candidates, |
| 366 | + # short ballots, zero and Fraction weights, mixed str/int candidates |
| 367 | + cands = ["A", "B", 1, 2, "C"][: rng.randint(2, 5)] |
| 368 | + ballots = [] |
| 369 | + for _ in range(rng.randint(1, 8)): |
| 370 | + pool = [rng.choice(cands) for _ in range(rng.randint(1, len(cands)))] |
| 371 | + ranking = [] |
| 372 | + while pool: |
| 373 | + n = rng.randint(1, min(2, len(pool))) |
| 374 | + ranking.append(frozenset(pool[:n])) |
| 375 | + pool = pool[n:] |
| 376 | + weight = rng.choice([0, 1, 3, 1 / 2, Fraction(1, 3)]) |
| 377 | + ballots.append(RankBallot(ranking=tuple(ranking), weight=weight)) |
| 378 | + # sometimes omit the explicit candidate list so profile.candidates can exclude candidates |
| 379 | + # that appear only on zero-weight ballots |
| 380 | + if rng.random() < 0.5: |
| 381 | + return RankProfile(ballots=tuple(ballots), max_ranking_length=len(cands)) |
| 382 | + return RankProfile(ballots=tuple(ballots), candidates=cands, max_ranking_length=len(cands)) |
| 383 | + |
| 384 | + |
| 385 | +@pytest.mark.parametrize("seed", range(25)) |
| 386 | +def test_mentions_paths_stay_in_sync(seed): |
| 387 | + profile = _random_mentions_profile(random.Random(seed)) |
| 388 | + assert _mentions_from_ballots(profile) == _mentions_from_df(profile) |
| 389 | + |
| 390 | + |
256 | 391 | def test_mentions_errors(): |
257 | 392 | with pytest.raises(TypeError, match="Profile must be of type RankProfile"): |
258 | 393 | mentions(cast(RankProfile, ScoreProfile(ballots=(ScoreBallot(scores={"A": 3}),)))) |
|
0 commit comments