Skip to content

Commit

Permalink
Revert setup.cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
jenni-niels committed Jul 28, 2021
1 parent 6e7023a commit 8b8f82f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
4 changes: 3 additions & 1 deletion gerrychain/constraints/validity.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def population(partition):

return Bounds(population, bounds=bounds)


def within_percent_of_ideal_population_per_representative(
initial_partition, percent=0.01, pop_key="population"
):
Expand All @@ -83,7 +84,7 @@ def within_percent_of_ideal_population_per_representative(
Ideal population is defined as "total population / number of representatives."
:param initial_partition: Starting MultiMemberPartition from which to compute district
:param initial_partition: Starting MultiMemberPartition from which to compute district
information.
:param percent: (optional) Allowed percentage deviation. Default is 1%.
:param pop_key: (optional) The name of the population
Expand All @@ -102,6 +103,7 @@ def population_per_rep(partition):

return Bounds(population_per_rep, bounds=bounds)


def deviation_from_ideal(partition, attribute="population"):
"""Computes the deviation of the given ``attribute`` from exact equality
among parts of the partition. Usually ``attribute`` is the population, and
Expand Down
14 changes: 8 additions & 6 deletions gerrychain/partition/multi_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


class MultiMemberPartition(Partition):
"""A :class:`Partition` with district magnitude information included.
"""
A :class:`Partition` with district magnitude information included.
These additional data allows for districts of different scales (number of representatives)
to be properly balanced.
"""
Expand All @@ -15,9 +16,11 @@ def __init__(self, graph=None, assignment=None, updaters=None, magnitudes=None,
:param updaters: Dictionary of functions to track data about the partition.
The keys are stored as attributes on the partition class,
which the functions compute.
:param magnitudes (dict<Any, numeric>): Dictionary assigning districts to number of representatives
:param magnitudes (dict<Any, numeric>): Dictionary assigning districts to number of
representatives
"""
super().__init__(graph=graph, assignment=assignment, updaters=updaters, parent=parent, flips=flips)
super().__init__(graph=graph, assignment=assignment, updaters=updaters, parent=parent,
flips=flips)
if parent is None:
self._init_magnitudes(magnitudes)
else:
Expand All @@ -31,13 +34,12 @@ def _init_magnitudes(self, magnitudes):
"""
dist_ids = self.assignment.parts.keys()
self.magnitudes = {dist_id: 1 for dist_id in dist_ids}
if magnitudes != None:
if magnitudes is not None:
self.magnitudes = {**self.magnitudes, **magnitudes}


def _update_magnitudes_from_parent(self, magnitudes):
parent = self.parent
self.magnitudes = {**parent.magnitudes, **magnitudes}

def flip(self, flips, magnitudes):
return self.__class__(parent=self, flips=flips, magnitudes=magnitudes)
return self.__class__(parent=self, flips=flips, magnitudes=magnitudes)
4 changes: 2 additions & 2 deletions gerrychain/proposals/tree_proposals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def recom(
partition, pop_col, pop_target, epsilon, node_repeats=1, method=bipartition_tree,
partition, pop_col, pop_target, epsilon, node_repeats=1, method=bipartition_tree,
multimember=False,
):
"""ReCom proposal.
Expand Down Expand Up @@ -139,7 +139,7 @@ def __init__(self, pop_col, ideal_pop, epsilon, method=bipartition_tree_random,

def __call__(self, partition):
return recom(
partition, self.pop_col, self.ideal_pop, self.epsilon, method=self.method,
partition, self.pop_col, self.ideal_pop, self.epsilon, method=self.method,
multimember=self.multimember
)

Expand Down
19 changes: 10 additions & 9 deletions gerrychain/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ def bipartition_tree_random(


def recursive_tree_part(
graph, parts, pop_target, pop_col, epsilon, node_repeats=1, method=bipartition_tree, magnitudes=None
graph, parts, pop_target, pop_col, epsilon, node_repeats=1, method=bipartition_tree,
magnitudes=None
):
"""Uses :func:`~gerrychain.tree.bipartition_tree` recursively to partition a tree into
``len(parts)`` parts of population ``pop_target`` (within ``epsilon``). Can be used to
Expand Down Expand Up @@ -304,9 +305,11 @@ def recursive_tree_part(
debt = 0

for part in parts[:-1]:
part_mag = 1 if magnitudes == None else magnitudes[part]
min_pop = max(pop_target * part_mag * (1 - epsilon), pop_target * part_mag * (1 - epsilon) - debt)
max_pop = min(pop_target * part_mag * (1 + epsilon), pop_target * part_mag * (1 + epsilon) - debt)
part_mag = 1 if magnitudes is None else magnitudes[part]
min_pop = max(pop_target * part_mag * (1 - epsilon),
pop_target * part_mag * (1 - epsilon) - debt)
max_pop = min(pop_target * part_mag * (1 + epsilon),
pop_target * part_mag * (1 + epsilon) - debt)
nodes = method(
graph.subgraph(remaining_nodes),
pop_col=pop_col,
Expand All @@ -325,17 +328,15 @@ def recursive_tree_part(
debt += part_pop - pop_target * part_mag
remaining_nodes -= nodes

if magnitudes != None:
if magnitudes is not None:
new_magnitudes[part] = part_mag

# All of the remaining nodes go in the last part
for node in remaining_nodes:
flips[node] = parts[-1]

if magnitudes != None:
new_magnitudes[parts[-1]] = magnitudes[parts[-1]]

if magnitudes != None:
if magnitudes is not None:
new_magnitudes[parts[-1]] = magnitudes[parts[-1]]
return flips, new_magnitudes
else:
return flips
Expand Down

0 comments on commit 8b8f82f

Please sign in to comment.