Skip to content

Commit 5ad73d0

Browse files
committed
More fixes from Chris's comments
1 parent 811021b commit 5ad73d0

12 files changed

+153
-96
lines changed

gerrychain/constraints/validity.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ def districts_within_tolerance(
157157

158158

159159
def refuse_new_splits(partition_county_field: str) -> Callable[[Partition], bool]:
160-
"""Refuse all proposals that split a county that was previous unsplit.
160+
"""
161+
Refuse all proposals that split a county that was previous unsplit.
161162
162163
:param partition_county_field: Name of field for county information generated by
163164
:func:`.county_splits`.

gerrychain/graph/graph.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ def from_file(
131131
132132
:param filename: Path to the shapefile / GeoPackage / GeoJSON / etc.
133133
:type filename: str
134-
:param adjacency: The adjacency type to use ("rook" or "queen"). Defaults is "rook"
134+
:param adjacency: The adjacency type to use ("rook" or "queen"). Default is "rook"
135135
:type adjacency: str, optional
136136
:param cols_to_add: The names of the columns that you want to
137-
add to the graph as node attributes. Default is None
137+
add to the graph as node attributes. Default is None.
138138
:type cols_to_add: Optional[List[str]], optional
139139
:param reproject: Whether to reproject to a UTM projection before
140140
creating the graph. Default is False.
@@ -187,13 +187,13 @@ def from_geodataframe(
187187
:param dataframe: The GeoDateFrame to convert
188188
:type dataframe: :class:`geopandas.GeoDataFrame`
189189
:param adjacency: The adjacency type to use ("rook" or "queen").
190-
Default is "rook"
190+
Default is "rook".
191191
:type adjacency: str, optional
192192
:param cols_to_add: The names of the columns that you want to
193193
add to the graph as node attributes. Default is None.
194194
:type cols_to_add: Optional[List[str]], optional
195195
:param reproject: Whether to reproject to a UTM projection before
196-
creating the graph. Default is ``True``.
196+
creating the graph. Default is ``False``.
197197
:type reproject: bool, optional
198198
:param ignore_errors: Whether to ignore all invalid geometries and
199199
attept to create the graph anyway. Default is ``False``.
@@ -275,7 +275,7 @@ def edge_indices(self):
275275
def add_data(self, df: pd.DataFrame,
276276
columns: Optional[Iterable[str]] = None) -> None:
277277
"""
278-
Add columns of a DataFrame to a graph as node attributes using
278+
Add columns of a DataFrame to a graph as node attributes
279279
by matching the DataFrame's index to node ids.
280280
281281
:param df: Dataframe containing given columns.

gerrychain/partition/assignment.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import defaultdict
22
from collections.abc import Mapping
3-
from typing import Dict, Union, Optional, DefaultDict, Set
3+
from typing import Dict, Union, Optional, DefaultDict, Set, Type
44
from ..graph import Graph
55

66
import pandas
@@ -22,14 +22,14 @@ class Assignment(Mapping):
2222
'mapping'
2323
]
2424

25-
def __init__(self, parts: Dict, mapping: Dict = None, validate: bool = True) -> None:
25+
def __init__(self, parts: Dict, mapping: Optional[Dict] = None, validate: bool = True) -> None:
2626
"""
27-
:param parts: dictionary mapping partition assignments to sets or
28-
frozensets of nodes
27+
:param parts: Dictionary mapping partition assignments frozensets of nodes.
2928
:type parts: Dict
30-
:param mapping: dictionary mapping nodes to partition assignments
31-
:type mapping: Dict, optional
32-
:param validate: whether to validate the assignment
29+
:param mapping: Dictionary mapping nodes to partition assignments.
30+
Default is None.
31+
:type mapping: Optional[Dict], optional
32+
:param validate: Whether to validate the assignment. Default is True.
3333
:type validate: bool, optional
3434
3535
:returns: None
@@ -163,6 +163,7 @@ def get_assignment(part_assignment: Union[str, Dict, Assignment],
163163
:class:`Assignment` object corresponding to the desired assignment.
164164
:type part_assignment: str
165165
:param graph: The graph from which to extract the assignment.
166+
Default is None.
166167
:type graph: Optional[Graph], optional
167168
168169
:returns: An :class:`Assignment` object containing the assignment
@@ -190,16 +191,16 @@ def get_assignment(part_assignment: Union[str, Dict, Assignment],
190191
raise TypeError("Assignment must be a dict or a node attribute key")
191192

192193

193-
def level_sets(mapping: Dict, container: Set = set) -> DefaultDict:
194+
def level_sets(mapping: Dict, container: Type[Set] = set) -> DefaultDict:
194195
"""
195196
Inverts a dictionary. ``{key: value}`` becomes
196197
``{value: <container of keys that map to value>}``.
197198
198199
:param mapping: A dictionary to invert. Keys and values can be of any type.
199200
:type mapping: Dict
200201
:param container: A container type used to collect keys that map to the same value.
201-
By default, it is a set.
202-
:type container: Set
202+
By default, the container type is ``set``.
203+
:type container: Type[Set], optional
203204
204205
:return: A dictionary where each key is a value from the original dictionary,
205206
and the corresponding value is a container (by default, a set) of keys from

gerrychain/partition/partition.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ class Partition:
1414
the first layer of computations at each step in the Markov chain - basic
1515
aggregations and calculations that we want to optimize.
1616
17-
:ivar gerrychain.Graph graph: The underlying graph.
18-
:ivar gerrychain.Assignment assignment: Maps node IDs to district IDs.
19-
:ivar dict parts: Maps district IDs to the set of nodes in that district.
20-
:ivar dict subgraphs: Maps district IDs to the induced subgraph of that district.
17+
:ivar graph: The underlying graph.
18+
:type graph: :class:`~gerrychain.Graph`
19+
:ivar assignment: Maps node IDs to district IDs.
20+
:type assignment: :class:`~gerrychain.assignment.Assignment`
21+
:ivar parts: Maps district IDs to the set of nodes in that district.
22+
:type parts: Dict
23+
:ivar subgraphs: Maps district IDs to the induced subgraph of that district.
24+
:type subgraphs: Dict
2125
"""
2226
__slots__ = (
2327
'graph',

gerrychain/partition/subgraphs.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class SubgraphView:
1212
1313
:ivar graph: The parent graph from which subgraphs are derived.
1414
:type graph: Graph
15-
:ivar parts: A dictionary mapping keys to subsets of nodes in the graph.
15+
:ivar parts: A list-of-lists dictionary (so a dict with key values indicated by
16+
the list index) mapping keys to subsets of nodes in the graph.
1617
:type parts: List[List[Any]]
1718
:ivar subgraphs_cache: Cache to store subgraph views for quick access.
1819
:type subgraphs_cache: Dict
@@ -28,7 +29,7 @@ def __init__(self, graph: Graph, parts: List[List[Any]]) -> None:
2829
:param graph: The parent graph from which subgraphs are derived.
2930
:type graph: Graph
3031
:param parts: A list of lists of nodes corresponding the different
31-
parts of the partition of the graph
32+
parts of the partition of the graph.
3233
:type parts: List[List[Any]]
3334
3435
:returns: None

gerrychain/proposals/proposals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def propose_random_flip(partition: Partition) -> Partition:
9595

9696
def slow_reversible_propose_bi(partition: Partition) -> Partition:
9797
"""
98-
Proposes a random boundary flip from the partition in a reversible fasion
98+
Proposes a random boundary flip from the partition in a reversible fashion
9999
for bipartitions by selecting a boundary node at random and uniformly picking
100100
one of its neighboring parts. For k-partitions this is not uniform since there
101101
might be multiple parts next to a single node.

gerrychain/proposals/tree_proposals.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
_bipartition_tree_random_all, uniform_spanning_tree,
99
find_balanced_edge_cuts_memoization,
1010
)
11-
from typing import Callable, Optional, Dict
11+
from typing import Callable, Optional, Dict, Union
1212

1313

1414
def recom(
1515
partition: Partition,
1616
pop_col: str,
17-
pop_target: float,
17+
pop_target: Union[int, float],
1818
epsilon: float,
1919
node_repeats: int = 1,
2020
weight_dict: Optional[Dict] = None,
@@ -53,8 +53,9 @@ def recom(
5353
:param pop_col: The name of the population column.
5454
:type pop_col: str
5555
:param pop_target: The target population for each district.
56-
:type pop_target: float
57-
:param epsilon: The epsilon value for population deviation.
56+
:type pop_target: Union[int,float]
57+
:param epsilon: The epsilon value for population deviation as a percentage of the
58+
target population.
5859
:type epsilon: float
5960
:param node_repeats: The number of times to repeat the bipartitioning step. Default is 1.
6061
:type node_repeats: int, optional
@@ -96,7 +97,7 @@ def recom(
9697
def reversible_recom(
9798
partition: Partition,
9899
pop_col: str,
99-
pop_target: float,
100+
pop_target: Union[int, float],
100101
epsilon: float,
101102
balance_edge_fn: Callable = find_balanced_edge_cuts_memoization,
102103
M: int = 1,
@@ -116,8 +117,9 @@ def reversible_recom(
116117
:param pop_col: The name of the population column.
117118
:type pop_col: str
118119
:param pop_target: The target population for each district.
119-
:type pop_target: float
120-
:param epsilon: The epsilon value for population deviation.
120+
:type pop_target: Union[int,float]
121+
:param epsilon: The epsilon value for population deviation as a percentage of the
122+
target population.
121123
:type epsilon: float
122124
:param balance_edge_fn: The balance edge function. Default is
123125
find_balanced_edge_cuts_memoization.
@@ -211,15 +213,16 @@ class ReCom:
211213

212214
def __init__(self,
213215
pop_col: str,
214-
ideal_pop: int,
216+
ideal_pop: Union[int, float],
215217
epsilon: float,
216218
method: Callable = bipartition_tree_random):
217219
"""
218220
:param pop_col: The name of the column in the partition that contains the population data.
219221
:type pop_col: str
220222
:param ideal_pop: The ideal population for each district.
221-
:type ideal_pop: float
222-
:param epsilon: The maximum allowable deviation from the ideal population.
223+
:type ideal_pop: Union[int,float]
224+
:param epsilon: The epsilon value for population deviation as a percentage of the
225+
target population.
223226
:type epsilon: float
224227
:param method: The method used for bipartitioning the tree.
225228
Defaults to `bipartition_tree_random`.

gerrychain/updaters/compactness.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def boundary_nodes(partition, alias: str = "boundary_nodes") -> Set:
1010
:param partition: A partition of a Graph
1111
:type partition: :class:`~gerrychain.partition.Partition`
1212
:param alias: The name of the attribute that the boundary nodes are
13-
stored under.
14-
:type alias: str
13+
stored under. Default is 'boundary_nodes'.
14+
:type alias: str, optional
1515
1616
:returns: The set of nodes in the partition that are on the boundary.
1717
:rtype: Set
@@ -166,6 +166,9 @@ def interior_boundaries(partition, previous: Set, new_edges: Set, old_edges: Set
166166

167167
def flips(partition) -> Dict:
168168
"""
169+
:param partition: A partition of a Graph
170+
:type partition: :class:`~gerrychain.partition.Partition`
171+
169172
:returns: The flips that were made to get from the parent partition to the
170173
given partition.
171174
:rtype: Dict

0 commit comments

Comments
 (0)