Skip to content

Commit

Permalink
Merge pull request #96 from cdonnay/read_csv
Browse files Browse the repository at this point in the history
add new arguments
  • Loading branch information
jgibson517 authored Sep 21, 2023
2 parents 5e5b9cd + de2b825 commit 2cd0800
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/VoteKit.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/votekit/cvr_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@

def load_csv(
fpath: str,
rank_cols: list[int] = [],
*,
weight_col: Optional[int] = None,
delimiter: Optional[str] = None,
id_col: Optional[int] = None,
) -> PreferenceProfile:
"""
Given a file path, loads cvr with ranks as columns and voters as rows
Given a file path, loads cast vote records (cvr) with ranks as columns and voters as rows
(empty cells are treated as None)
Args:
fpath: Path to cvr file
rank_cols: list of column indexes that contain rankings, indexing starts from 0,
in order from top to bottom rank.
Default implies that all columns contain rankings.
weight_col: The column position for ballot weights
if parsing Scottish elections like cvrs
delimiter: The character that breaks up rows
Expand All @@ -34,7 +38,7 @@ def load_csv(
DataError: If the voter id column has duplicate values
Returns:
A preference schedule that represents all the ballots in the elction
A preference profile that represents all the ballots in the election
"""
if not os.path.isfile(fpath):
raise FileNotFoundError(f"File with path {fpath} cannot be found")
Expand All @@ -55,6 +59,12 @@ def load_csv(
if id_col is not None and not df.iloc[:, id_col].is_unique:
raise DataError(f"Duplicate value(s) in column at index {id_col}")

if rank_cols:
if id_col is not None:
df = df.iloc[:, rank_cols+[id_col]]
else:
df = df.iloc[:, rank_cols]

ranks = list(df.columns)
if id_col is not None:
ranks.remove(df.columns[id_col])
Expand All @@ -63,6 +73,8 @@ def load_csv(

for group, group_df in grouped:
ranking = [{None} if pd.isnull(c) else {c} for c in group]


voters = None
if id_col is not None:
voters = set(group_df.iloc[:, id_col])
Expand Down
10 changes: 4 additions & 6 deletions src/votekit/elections/election_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,18 @@ def __init__(
self.transfer = transfer
self.seats = seats
self.tiebreak = tiebreak
self.threshold = self.get_threshold(quota)
self.quota = quota.lower()
self.threshold = self.get_threshold()

# can cache since it will not change throughout rounds
def get_threshold(self, quota: str) -> int:
def get_threshold(self) -> int:
"""
Calculates threshold required for election
Args:
quota: Type of quota formula
Returns:
Value of the threshold
"""
quota = quota.lower()
quota = self.quota
if quota == "droop":
return int(self._profile.num_ballots() / (self.seats + 1) + 1)
elif quota == "hare":
Expand Down

0 comments on commit 2cd0800

Please sign in to comment.