From dc5465cb7f92b3ef5d15d77d8fb0dc0d632d41c1 Mon Sep 17 00:00:00 2001 From: tomaszraducha Date: Mon, 23 May 2022 13:12:20 +0200 Subject: [PATCH] few review corrections --- configuration/config.py | 11 +++++------ configuration/tests/configuration_tests.py | 4 ++-- electoral_sys/seat_assignment.py | 10 +++++----- scripts/binom_approx.py | 6 +++--- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/configuration/config.py b/configuration/config.py index ddfec5f..9f768f5 100644 --- a/configuration/config.py +++ b/configuration/config.py @@ -78,7 +78,7 @@ class Config: # and assigns all seats based on the results in the whole country; # it can be, however, a first-past-the-post system with one district, # or majoritarian voting, if a proper seat allocation rule is provided - 'country-wide_system': es.single_district_voting, + 'countrywide_system': es.single_district_voting, # a system with electoral districts as specified by 'district_sizes', either PR or first-past-the-post # what depends on the seat assignment method and number of seats per district, @@ -240,11 +240,10 @@ def f(n, g): # Determine the number of states if self.num_parties < 2: raise ValueError('The simulation needs at least two states') - else: - self.all_states = generate_state_labels(self.num_parties) - self.zealot_state = self.all_states[0] - self.not_zealot_state = self.all_states[-1] - self.suffix = ''.join(['_parties_', str(self.num_parties), self.suffix]) + self.all_states = generate_state_labels(self.num_parties) + self.zealot_state = self.all_states[0] + self.not_zealot_state = self.all_states[-1] + self.suffix = ''.join(['_parties_', str(self.num_parties), self.suffix]) if self.mass_media is None: self.mass_media = 1.0 / self.num_parties # the symmetric case with no propaganda diff --git a/configuration/tests/configuration_tests.py b/configuration/tests/configuration_tests.py index 16443ff..def6f71 100644 --- a/configuration/tests/configuration_tests.py +++ b/configuration/tests/configuration_tests.py @@ -109,7 +109,7 @@ def test_config_basic_class_attributes(self): # might be silly to test whether some attributes exist, but it might help to think through future changes # and all the parser actions should be defined as class attributes of Config, so instances know they have them self.maxDiff = None - self.assertIn('country-wide_system', Config.voting_systems.keys()) + self.assertIn('countrywide_system', Config.voting_systems.keys()) self.assertIn('main_district_system', Config.voting_systems.keys()) for action in parser._actions: if action.dest != 'help': @@ -153,7 +153,7 @@ def test_config_attributes_values_alternative_sys(self): config = Config(input_parser, ArgumentDict()) self.assertListEqual(list(config.voting_systems.keys()), - ['country-wide_system', 'main_district_system', 'one', 'two', 'three']) + ['countrywide_system', 'main_district_system', 'one', 'two', 'three']) self.assertDictEqual(config.alternative_systems[0], {'name': 'one', 'type': 'basic', 'seat_rule': 'hare', 'threshold': 0.2, 'seat_alloc_function': seat_assignment_rules['hare'], 'seats': [2], diff --git a/electoral_sys/seat_assignment.py b/electoral_sys/seat_assignment.py index fefa64b..3e9da67 100644 --- a/electoral_sys/seat_assignment.py +++ b/electoral_sys/seat_assignment.py @@ -157,7 +157,7 @@ def imperiali_method(total_seats, votes=None, **kwargs): ########################################################### -def largest_reminder_formula(quota, total_seats, votes): +def largest_remainder_formula(quota, total_seats, votes): """ General formula for the largest reminder seat assigning methods. After providing the proper quota it can compute the seat assigment @@ -194,7 +194,7 @@ def hare_quota(total_seats, votes=None, total_votes=None, **kwargs): :return: seat assignment, a dict of a form {party_code: number_of_seats} """ quota = Decimal(total_votes) / total_seats - return largest_reminder_formula(quota, total_seats, votes) + return largest_remainder_formula(quota, total_seats, votes) def droop_quota(total_seats, votes=None, total_votes=None, **kwargs): @@ -206,7 +206,7 @@ def droop_quota(total_seats, votes=None, total_votes=None, **kwargs): :return: seat assignment, a dict of a form {party_code: number_of_seats} """ quota = Decimal(1 + floor(Decimal(total_votes) / (total_seats + 1))) - return largest_reminder_formula(quota, total_seats, votes) + return largest_remainder_formula(quota, total_seats, votes) def exact_droop_quota(total_seats, votes=None, total_votes=None, **kwargs): @@ -219,7 +219,7 @@ def exact_droop_quota(total_seats, votes=None, total_votes=None, **kwargs): :return: seat assignment, a dict of a form {party_code: number_of_seats} """ quota = Decimal(total_votes) / (total_seats + 1) - assignment = largest_reminder_formula(quota, total_seats, votes) + assignment = largest_remainder_formula(quota, total_seats, votes) if sum(assignment.values()) > total_seats: log.warning('The exact Droop quota assigned more seats than available! Increasing the quota to avoid it.') return droop_quota(total_seats, votes=votes, total_votes=total_votes) @@ -237,7 +237,7 @@ def imperiali_quota(total_seats, votes=None, total_votes=None, **kwargs): :return: seat assignment, a dict of a form {party_code: number_of_seats} """ quota = Decimal(total_votes) / (total_seats + 2) - assignment = largest_reminder_formula(quota, total_seats, votes) + assignment = largest_remainder_formula(quota, total_seats, votes) if sum(assignment.values()) > total_seats: log.warning('The Imperiali quota assigned more seats than available! Using the Droop quota.') return exact_droop_quota(total_seats, votes=votes, total_votes=total_votes) diff --git a/scripts/binom_approx.py b/scripts/binom_approx.py index 7c91eac..f530ff7 100644 --- a/scripts/binom_approx.py +++ b/scripts/binom_approx.py @@ -51,7 +51,7 @@ def get_binom_hist(config_args, m, system): @return: histogram bar sizes for a given setting in a binomial approximation. (numpy.array) """ p = 0.5 * (1.0 - config_args.epsilon) + config_args.epsilon * config_args.mass_media # effective state 'a' probability - if system == 'country-wide_system': + if system == 'countrywide_system': eff_N = config_args.n - config_args.n_zealots # number of non-zealot voters sub_density = st.binom(eff_N, p).pmf(np.arange(eff_N + 1)) # density for single voters density = np.zeros(config_args.n + 1) # density including zealots @@ -61,7 +61,7 @@ def get_binom_hist(config_args, m, system): n_d = config_args.n / config_args.q # number of voters per district n_z = config_args.n_zealots / config_args.q # average number of zealots per district - # In contrast to the 'country-wide_system' case, here we only approximate the effect of zealots. + # In contrast to the 'countrywide_system' case, here we only approximate the effect of zealots. # Otherwise, we would need to sum over all possible combinations. if n_d % 2: p_d = 1.0 - st.binom(n_d - n_z, p).cdf(n_d // 2 - n_z) # probability of winning in a district @@ -131,7 +131,7 @@ def plot_hist_with_binom_approx(distribution, m, hist, density, suffix, colors=( except (OSError, IOError) as e: raise IOError("Data for the given configuration was not found, " "run main.py with the same configuration first.") - for system in ['country-wide_system', 'main_district_system']: + for system in ['countrywide_system', 'main_district_system']: distribution = convert_to_distributions(res[system]) hist, density = get_binom_hist(cfg, m, system)