From eee4576f83f85fc6e1683a7f7b1c26d5267ba7cb Mon Sep 17 00:00:00 2001 From: Markus Utke Date: Fri, 22 Sep 2023 14:43:13 +0200 Subject: [PATCH] added support for more edge case elections --- pabutools/analysis/category.py | 4 ++++ pabutools/analysis/profileproperties.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/pabutools/analysis/category.py b/pabutools/analysis/category.py index a473e9b4..78b832ae 100644 --- a/pabutools/analysis/category.py +++ b/pabutools/analysis/category.py @@ -66,6 +66,10 @@ def category_proportionality( app_total_cost += project.cost for category in project.categories: app_cost_per_category[category] += project.cost + if app_total_cost == 0: + raise ValueError( + "Category proportionality can only be computed for instances with at least one non-empty ballot." + ) for category in categories: proportional_app_cost_per_category[category] += ( app_cost_per_category[category] diff --git a/pabutools/analysis/profileproperties.py b/pabutools/analysis/profileproperties.py index 4aafbaa7..9e029985 100644 --- a/pabutools/analysis/profileproperties.py +++ b/pabutools/analysis/profileproperties.py @@ -52,6 +52,8 @@ def median_ballot_length(instance: Instance, profile: AbstractProfile) -> int: The median length of the ballots in the profile. """ + if profile.num_ballots() == 0: + return 0 ballot_lengths = np.zeros(profile.num_ballots()) index = 0 for ballot in profile: @@ -100,6 +102,8 @@ def median_ballot_cost(instance: Instance, profile: AbstractProfile) -> Number: The median cost of the ballots in the profile. """ + if profile.num_ballots() == 0: + return 0 ballot_costs = np.zeros(profile.num_ballots()) index = 0 for ballot in profile: @@ -148,6 +152,8 @@ def median_approval_score( The median approval score of projects. """ + if len(instance) == 0: + return 0 return float( np.median([frac(profile.approval_score(project)) for project in instance]) ) @@ -190,6 +196,8 @@ def median_total_score(instance: Instance, profile: AbstractCardinalProfile) -> The median score assigned to a project. """ + if len(instance) == 0: + return 0 return float( np.median([frac(profile.total_score(project)) for project in instance]) )