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.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.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_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_profile_to_preflib_cat_file(filepath, profile)

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

Parameters:
filenamestr

File name of the Preflib file.

profileabcvoting.preferences.Profile

Profile to be written.

Returns:
None