Feat/allow int candidates#370
Open
graceg571 wants to merge 10 commits into
Open
Conversation
c7d7185 to
ac74d1d
Compare
peterrrock2
requested changes
Jun 22, 2026
peterrrock2
left a comment
Collaborator
There was a problem hiding this comment.
- Consistency note: "Candidate can be a str or int" or "Candidate can strings, integers, or a mix of both"
- Fix roundtrip on CSV with mixed types
|
|
||
| Args: | ||
| scores (dict[str, float]): Mutable score dict, modified in place. | ||
| scores (dict[str | int, float]): Mutable score dict, modified in place. |
Collaborator
There was a problem hiding this comment.
Test this with a mixture of candidate types
Comment on lines
+196
to
+205
| cand_subset: list[Candidate] | ||
| | tuple[Candidate] | ||
| | set[Candidate] | ||
| | list[str] | ||
| | set[str] | ||
| | list[int] | ||
| | set[int], | ||
| pairwise_dict: dict[tuple[Candidate, Candidate], tuple[float, float]] | ||
| | dict[tuple[str, str], tuple[float, float]] | ||
| | dict[tuple[int, int], tuple[float, float]], |
Comment on lines
33
to
46
| def comention_above(i: Candidate, j: Candidate, ballot: RankBallot) -> bool: | ||
| """ | ||
| Takes candidates i,j and returns True if i >= j in the ranking. | ||
| Requires that the ballot has a ranking. | ||
|
|
||
|
|
||
| Args: | ||
| i (str): Candidate name. | ||
| j (str): Candidate name. | ||
| i (Candidate): Candidate name. | ||
| Candidates can be strings, integers, or mix of both. | ||
| j (Candidate): Candidate name. | ||
| Candidates can be strings, integers, or mix of both. | ||
| ballot (RankBallot): RankBallot. | ||
|
|
||
| Returns: |
Collaborator
There was a problem hiding this comment.
While we are here, maybe rename these?
|
|
||
| Returns: | ||
| dict[str, dict[str, float]]: Data dictionary for ``multi_bar_plot``. | ||
| dict[str, dict[str | int, float]]: Data dictionary for ``multi_bar_plot``. |
|
|
||
| Returns: | ||
| dict[str, float]: | ||
| dict[str | int, float]: |
Comment on lines
+388
to
+393
| def df(self) -> pd.DataFrame: | ||
| """ | ||
| Compute the dataframe as a cached property. | ||
| The dataframe is internally stored with candidate ids. | ||
| The dataframe will be translated to original candidate names. | ||
| """ |
Collaborator
There was a problem hiding this comment.
Docstrings are a part of the Public API. Keep internal details in # NOTE comments.
Comment on lines
+1111
to
+1113
| Compute the dataframe as a cached property. | ||
| The dataframe is internally stored with candidate ids. | ||
| The dataframe will be translated to original candidate names. |
Collaborator
There was a problem hiding this comment.
Why do we need that extra set? Investigate, please.
Collaborator
There was a problem hiding this comment.
Check collisions. Also add checks for instantiating from df.
…imultaneous veto and ranked pairs to mixed cand testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses #359
This PR allows integer and string candidates as input to rank and score ballots. The user can now input a ranking or score using candidates "1" and 1, and each will be treated as separate candidates with a warning raised to let the user know that's the case.
RankProfileandScoreProfilestore an internal df of the ballots using candidate integer IDs instead of candidate names to provide a uniform internal representation regardless of candidate type which will be helpful for our eventual move to Rust. If the df attribute is accessed, the internal df will be translated to its candidate names and cached for future references.Why
Scottish election data labels their candidates as integers and stores a mapping of their integer IDs to candidate names. Likewise, it is common within math literature to use integers to represent candidates. This change will align better with the Scottish election data and math literature as well as allow users to more easily input candidates to ballots without casting all to strings.
We could have cast all integers to strings or enforced no mixing of candidate types, but decided against it: election result candidate names would be misaligned with the ballots the user created, and with the internal df there is no need to constrain candidate types across an entire ballot or preference profile.
Changes
types.py: addedCandidate : TypeAlias = str | intandCandidateFloatDictLikealias. All type annotations are updated to useCandidateacross the codebase.ballot.py:RankBallotandScoreBallotnow acceptCandidatein rankings and scores. Warning raised when string and integer candidates collide (e.g. "1" and 1), treated as separate candidatespref_profile.py:RankProfileandScoreProfilemaintain and internal_dfwith the candidate integer IDs as the values or column names respectively. The publicdfproperty translates back to candidate names and is cached._candidatesand_candidates_caststore the candidate integer IDs to be able to search the internal_df.candidate_id_mapis the mapping of candidate names to their integer IDs.id_candidate_mapis the mapping of candidate integer IDs to their names. These maps allow translation both ways.Candidatetypes as input and propagateCandidatetypes to internal functionsTesting
test_ScoreBallot.py/test_RankBallot.py: new tests for int and mixed str/int candidates at the ballot level, including collision warningtest_rank_pp.py/test_score_pp.py: new tests for int and mixed str/int candidate profilestest_rank_pp_df.py/test_score_pp_df.py: new tests verifying internal_dfuses integer IDs and the publicdftranslates back to candidate names of mixed typestest_mixed_candidates.py: fuzz tests running Borda, STV, and Plurality elections and allutils.pyfunctions over profiles with mixed str/int candidates at varying ballot counts (10, 1000, 10000)