abcvoting.fileio

Read and write data to files.

Two data formats are supported: 1. the Preflib format (soi, toi, soc, toc, or cat), and 2. .abc.yaml files (more expressive than Preflib files).

abcvoting.fileio.ABC_YAML_VALID_KEYS = ['profile', 'num_cand', 'committeesize', 'compute', 'voter_weights', 'description']

Valid keys for .abc.yaml files.

exception abcvoting.fileio.MalformattedFileException

Malformatted file (Preflib or .abc.yaml).

abcvoting.fileio.abcvoting_to_pabutools(profile: Profile, committeesize: int)

Convert an abcvoting profile to a pabutools profile.

Parameters:
profileProfile

The abcvoting profile.

committeesizeint

The size of the committee.

Returns:
instancepabutools.election.Instance

The pabutools instance.

profilepabutools.election.ApprovalProfile

The pabutools approval profile.

abcvoting.fileio.get_file_names(dir_name, filename_extensions=None)

List all file names in a directory that fit the specified filename extensions.

Important

Not recursive, i.e., does not look into sub-directories!

Parameters:
dir_namestr

Path of directory to be searched for files.

filename_extensionslist of str, optional

File names must have one of these extensions.

Returns:
list of str

List of file names contained in the directory.

abcvoting.fileio.pabutools_to_abcvoting(pb_instance, pb_profile)

Convert an instance and a profile from the pabutools into a profile for abcvoting.

Parameters:
pb_instancepabutools.election.Instance

The pabutools instance.

pb_profilepabutools.election.AbstractApprovalProfile

The pabutools profile.

Returns:
abcvoting.preferences.Profile

The abcvoting profile.

abcvoting.fileio.read_abcvoting_yaml_file(filename)

Read contents of an abcvoting yaml file (ending with .abc.yaml).

Parameters:
filenamestr

File name of the .abc.yaml file.

Returns:
profileabcvoting.preferences.Profile

A profile.

committeesizeint or None

The desired committee size.

compute_instanceslist of dict

A list of compute instances, which are dictionaries.

Compute instances can be passed to Rule.compute.

datadict

The YAML data from filename.

abcvoting.fileio.read_pabulib_file(file_path)

Read the content of a pabulib file and returns the corresponding profile.

All projects are assumed to have the same cost and the size of the committee is equal to the budget limit of the instance (this does not always make sense).

Parameters:
file_pathstr

The file path.

Returns:
profileabcvoting.preferences.Profile

The profile.

committeesizeint

The size of the committee.

abcvoting.fileio.read_preflib_file(filename, top_ranks=None, setsize=None, use_weights=False)

Read a Preflib file (soi, toi, soc or toc).

Parameters:
filenamestr

Name of the Preflib file.

top_ranksint, default=1

The approval set consists of the top entries in each voter’s ranking in the Preflib instance. The parameter top_ranks determines how many ranks are joined to create the approval set. In case of of a Preflib category files, ranks correspond to categories.

It cannot be used if parameter setsize is used too.

setsizeint, optional

Minimum number of candidates that voters approve.

These candidates are taken from the top of ranking. In case of ties, more than setsize candidates are approved.

It cannot be used if parameter top_ranks is used too.

use_weightsbool, default=False

Use weights of voters instead of individual voters.

If False, treat vote count in Preflib file as the number of identical ballots, i.e., the number of voters that approve this set of candidates. If True, treat vote count as weight and use this weight in class Voter.

Returns:
abcvoting.preferences.Profile

Preference profile extracted from Preflib file.

abcvoting.fileio.read_preflib_files_from_dir(dir_name, top_ranks=None, setsize=None)

Read all Preflib files (soi, toi, soc or toc) in a given directory.

Parameters:
dir_namestr

Path of the directory to be searched for Preflib files.

top_ranksint, default=1

The approval set consists of the top entries in each voter’s ranking in the Preflib instance. The parameter top_ranks determines how many ranks are joined to create the approval set. In case of of a Preflib category files, ranks correspond to categories.

It cannot be used if parameter setsize is used too.

setsizeint

Minimum number of candidates that voters approve.

These candidates are taken from the top of ranking. In case of ties, more than setsize candidates are approved.

It cannot be used if parameter top_ranks is used too.

Returns:
dict

Dictionary with file names as keys and profiles (class abcvoting.preferences.Profile) as values.

abcvoting.fileio.write_abcvoting_instance_to_yaml_file(filename, profile, committeesize=None, compute_instances=None, description=None)

Write abcvoting instance to an abcvoting yaml file.

Parameters:
filenamestr

File name of the .abc.yaml file.

profileabcvoting.preferences.Profile

A profile.

committeesizeint, optional

The desired committee size.

compute_instanceslist of dict, optional

A list of compute instances, which are dictionaries.

Compute instances can be passed to Rule.compute.

descriptionstr, optional

An optional description of the data.

abcvoting.fileio.write_pabulib_file(profile, committeesize, file_path)

Write an abcvoting instance as a pabulib file.

Parameters:
profileProfile

The abcvoting profile.

committeesizeint

The size of the committee.

file_pathstr

The file path.

abcvoting.fileio.write_profile_to_preflib_cat_file(filepath, profile)

Write a profile to a Preflib category file (.cat).

Parameters:
filepathstr

File path of the Preflib file.

profileabcvoting.preferences.Profile

Profile to be written.

Returns:
None