Interfaces to Other Packages¶
The pref_voting.interfaces modules convert between pref_voting election objects and the election objects of two other voting packages:
VoteKit (MGGG Redistricting Lab): ranked ballots (with ties and truncation) and score ballots.
abcvoting (Lackner et al.): approval-based committee (ABC) voting.
This makes it possible, for instance, to generate profiles with VoteKit’s ballot generators and analyze them with pref_voting’s voting methods, to run VoteKit’s elections on pref_voting profiles, or to apply abcvoting’s committee rules to pref_voting approval profiles.
The correspondence between the main election objects:
pref_voting |
VoteKit |
abcvoting |
|---|---|---|
|
|
— |
|
|
— |
|
|
— |
|
— |
|
Both packages are optional dependencies: they are only imported when a conversion function is called, and a plain installation of pref_voting is unchanged. To use the interfaces, install the extras:
uv pip install "pref_voting[votekit]" # VoteKit (requires Python >= 3.11)
uv pip install "pref_voting[abcvoting]" # abcvoting
uv pip install "pref_voting[interfaces]" # both
Weights. VoteKit ballots and abcvoting voters carry (possibly fractional) weights, while pref_voting profiles count voters with integer counts. Integer-valued weights are converted directly; fractional weights raise a ValueError unless scale_weights=True is passed, in which case all weights are multiplied by a common denominator (an exact rescaling that preserves the relative weights of the ballots but changes the total number of voters).
All of the conversions are also available as methods on the profile classes: Profile.to_votekit(), ProfileWithTies.to_votekit() / ProfileWithTies.from_votekit(), and GradeProfile.to_votekit() / GradeProfile.from_votekit() / GradeProfile.to_abcvoting() / GradeProfile.from_abcvoting().
For a walkthrough of all the conversions, see the notebook examples/interfaces_demo.ipynb.
VoteKit¶
- pref_voting.interfaces.votekit_interface.to_votekit_profile(profile)[source]¶
Convert a
ProfileorProfileWithTiesto a VoteKitPreferenceProfileof ranked ballots.Candidates are named using the profile’s
cmap. For aProfileWithTies, tied candidates are placed in the same position of the VoteKit ranking, unranked candidates are omitted from the ballot, and gaps in the ranks are compressed (only the relative order of the ranks matters in aProfileWithTies).- Parameters:
profile (Profile or ProfileWithTies) – The profile to convert.
- Returns:
A VoteKit
PreferenceProfileof ranked ballots.
- pref_voting.interfaces.votekit_interface.votekit_to_profile_with_ties(vk_profile, scale_weights=False)[source]¶
Convert a VoteKit
PreferenceProfileof ranked ballots to aProfileWithTies.The candidates of the resulting profile are the candidate names (strings) of the VoteKit profile. Candidates tied in a position of a VoteKit ranking are assigned the same rank; candidates missing from a ballot are unranked; skipped positions (empty frozensets) are preserved as gaps in the ranks.
- Parameters:
vk_profile – A VoteKit
PreferenceProfileof ranked ballots.scale_weights (bool, optional) – If True, fractional ballot weights are scaled to integers by clearing denominators; otherwise fractional weights raise a
ValueError.
- Returns:
A
ProfileWithTies.
- pref_voting.interfaces.votekit_interface.grade_profile_to_votekit(gprofile)[source]¶
Convert a
GradeProfilewith numeric grades to a VoteKitPreferenceProfileof score ballots.Candidates are named using the profile’s
cmap.Warning
VoteKit does not store scores of 0: a candidate assigned the grade 0 becomes an unscored candidate in the VoteKit profile. In particular, for approval ballots (grades 0 and 1), the explicit 0s are not preserved when converting to VoteKit and back.
- Parameters:
gprofile (GradeProfile) – A grade profile with numeric grades.
- Returns:
A VoteKit
PreferenceProfileof score ballots.
- pref_voting.interfaces.votekit_interface.votekit_to_grade_profile(vk_profile, grades=None, scale_weights=False)[source]¶
Convert a VoteKit
PreferenceProfileof score ballots to aGradeProfile.The candidates of the resulting profile are the candidate names (strings) of the VoteKit profile. Since a VoteKit profile does not carry a grade scale, the grades of the resulting profile default to the set of score values that appear in the profile; pass
gradesto specify the intended scale instead. Integer-valued scores (VoteKit stores scores as floats) are converted to ints. Candidates without a score on a ballot are ungraded on that ballot.- Parameters:
vk_profile – A VoteKit
PreferenceProfileof score ballots.grades (list, optional) – The grades of the resulting profile. If provided, every score in the profile must be one of these grades.
scale_weights (bool, optional) – If True, fractional ballot weights are scaled to integers by clearing denominators; otherwise fractional weights raise a
ValueError.
- Returns:
A
GradeProfile.
abcvoting¶
- pref_voting.interfaces.abcvoting_interface.to_abcvoting_profile(gprofile)[source]¶
Convert a
GradeProfilewith grades 0 and 1 to an abcvotingProfile.The candidates
0, ..., num_cand - 1of the abcvoting profile are the indices of the (sorted) candidates of the grade profile, and the candidate names of the abcvoting profile are given by the grade profile’scmap. Each ballot type contributes one abcvoting voter, whose approval set is the set of candidates assigned the grade 1 and whose weight is the ballot count. Useabcvoting.preferences.Profile.convert_to_unit_weights()on the result if unit weights are needed.- Parameters:
gprofile (GradeProfile) – A grade profile with grades 0 and 1.
- Returns:
An abcvoting
Profile.
- pref_voting.interfaces.abcvoting_interface.abcvoting_to_grade_profile(abc_profile, scale_weights=False)[source]¶
Convert an abcvoting
Profileto aGradeProfilewith grades 0 and 1.The candidates of the grade profile are the integers
0, ..., num_cand - 1, with thecand_namesof the abcvoting profile as thecmap. Each voter contributes one ballot assigning the grade 1 to each approved candidate and the grade 0 to every other candidate, with the voter’s weight as the ballot count.- Parameters:
abc_profile – An abcvoting
Profile.scale_weights (bool, optional) – If True, fractional voter weights are scaled to integers by clearing denominators; otherwise fractional weights raise a
ValueError.
- Returns:
A
GradeProfilewith grades 0 and 1.