Skip to content

Commit 3966328

Browse files
committed
Update doc strings for updaters
1 parent 6ee772b commit 3966328

8 files changed

+646
-107
lines changed

gerrychain/updaters/compactness.py

+127-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
from .flows import on_flow
44
from .cut_edges import on_edge_flow
5+
from typing import Dict, Set
56

67

7-
def boundary_nodes(partition, alias="boundary_nodes"):
8+
def boundary_nodes(partition, alias: str = "boundary_nodes") -> Set:
9+
"""
10+
:param partition: A partition of a Graph
11+
:type partition: :class:`~gerrychain.partition.Partition`
12+
:param alias: The name of the attribute that the boundary nodes are
13+
stored under.
14+
:type alias: str
15+
16+
:returns: The set of nodes in the partition that are on the boundary.
17+
:rtype: Set
18+
"""
819
if partition.parent:
920
return partition.parent[alias]
1021
return {
@@ -14,20 +25,55 @@ def boundary_nodes(partition, alias="boundary_nodes"):
1425
}
1526

1627

17-
def initialize_exterior_boundaries_as_a_set(partition):
28+
def initialize_exterior_boundaries_as_a_set(partition) -> Dict[int, Set]:
29+
"""
30+
:param partition: A partition of a Graph
31+
:type partition: :class:`~gerrychain.partition.Partition`
32+
33+
:returns: A dictionary mapping each part of a partition to the set of nodes
34+
in that part that are on the boundary.
35+
:rtype: Dict[int, Set]
36+
"""
1837
part_boundaries = collections.defaultdict(set)
1938
for node in partition["boundary_nodes"]:
2039
part_boundaries[partition.assignment.mapping[node]].add(node)
2140
return part_boundaries
2241

2342

2443
@on_flow(initialize_exterior_boundaries_as_a_set, alias="exterior_boundaries_as_a_set")
25-
def exterior_boundaries_as_a_set(partition, previous, inflow, outflow):
44+
def exterior_boundaries_as_a_set(partition, previous: Set, inflow: Set, outflow: Set) -> Set:
45+
"""
46+
Updater function that responds to the flow of nodes between different partitions.
47+
48+
:param partition: A partition of a Graph
49+
:type partition: :class:`~gerrychain.partition.Partition`
50+
:param previous: The previous set of exterior boundary nodes for a
51+
fixed part of the given partition.
52+
:type previous: Set
53+
:param inflow: The set of nodes that have flowed into the given part of the
54+
partition.
55+
:type inflow: Set
56+
:param outflow: The set of nodes that have flowed out of the given part of the
57+
partition.
58+
:type outflow: Set
59+
60+
:returns: The new set of exterior boundary nodes for the given part of the
61+
partition.
62+
:rtype: Set
63+
"""
2664
graph_boundary = partition["boundary_nodes"]
2765
return (previous | (inflow & graph_boundary)) - outflow
2866

2967

30-
def initialize_exterior_boundaries(partition):
68+
def initialize_exterior_boundaries(partition) -> Dict[int, float]:
69+
"""
70+
:param partition: A partition of a Graph
71+
:type partition: :class:`~gerrychain.partition.Partition`
72+
73+
:returns: A dictionary mapping each part of a partition to the total
74+
perimeter of the boundary nodes in that part.
75+
:rtype: Dict[int, float]
76+
"""
3177
graph_boundary = partition["boundary_nodes"]
3278
boundaries = collections.defaultdict(lambda: 0)
3379
for node in graph_boundary:
@@ -37,7 +83,26 @@ def initialize_exterior_boundaries(partition):
3783

3884

3985
@on_flow(initialize_exterior_boundaries, alias="exterior_boundaries")
40-
def exterior_boundaries(partition, previous, inflow, outflow):
86+
def exterior_boundaries(partition, previous: Set, inflow: Set, outflow: Set) -> Dict:
87+
"""
88+
Updater function that responds to the flow of nodes between different partitions.
89+
90+
:param partition: A partition of a Graph
91+
:type partition: :class:`~gerrychain.partition.Partition`
92+
:param previous: The previous set of exterior boundary nodes for a
93+
fixed part of the given partition.
94+
:type previous: Set
95+
:param inflow: The set of nodes that have flowed into the given part of the
96+
partition.
97+
:type inflow: Set
98+
:param outflow: The set of nodes that have flowed out of the given part of the
99+
partition.
100+
:type outflow: Set
101+
102+
:returns: A dict mapping each part of the partition to the new exterior
103+
boundary of that part.
104+
:rtype: Dict
105+
"""
41106
graph_boundary = partition["boundary_nodes"]
42107
added_perimeter = sum(
43108
partition.graph.nodes[node]["boundary_perim"]
@@ -51,6 +116,14 @@ def exterior_boundaries(partition, previous, inflow, outflow):
51116

52117

53118
def initialize_interior_boundaries(partition):
119+
"""
120+
:param partition: A partition of a Graph
121+
:type partition: :class:`~gerrychain.partition.Partition`
122+
123+
:returns: A dictionary mapping each part of a partition to the total
124+
perimeter the given part shares with other parts.
125+
:rtype: Dict[int, float]
126+
"""
54127
return {
55128
part: sum(
56129
partition.graph.edges[edge]["shared_perim"]
@@ -61,7 +134,27 @@ def initialize_interior_boundaries(partition):
61134

62135

63136
@on_edge_flow(initialize_interior_boundaries, alias="interior_boundaries")
64-
def interior_boundaries(partition, previous, new_edges, old_edges):
137+
def interior_boundaries(partition, previous: Set, new_edges: Set, old_edges: Set) -> Dict:
138+
"""
139+
Updater function that responds to the flow of nodes between different partitions.
140+
141+
:param partition: A partition of a Graph
142+
:type partition: :class:`~gerrychain.partition.Partition`
143+
:param previous: The previous set of exterior boundary nodes for a
144+
fixed part of the given partition.
145+
:type previous: Set
146+
:param new_edges: The set of edges that have flowed into the given part of the
147+
partition.
148+
:type new_edges: Set
149+
:param old_edges: The set of edges that have flowed out of the given part of the
150+
partition.
151+
:type old_edges: Set
152+
153+
154+
:returns: A dict mapping each part of the partition to the new interior
155+
boundary of that part.
156+
:rtype: Dict
157+
"""
65158
added_perimeter = sum(
66159
partition.graph.edges[edge]["shared_perim"] for edge in new_edges
67160
)
@@ -71,21 +164,44 @@ def interior_boundaries(partition, previous, new_edges, old_edges):
71164
return previous + added_perimeter - removed_perimeter
72165

73166

74-
def flips(partition):
167+
def flips(partition) -> Dict:
168+
"""
169+
:returns: The flips that were made to get from the parent partition to the
170+
given partition.
171+
:rtype: Dict
172+
"""
75173
return partition.flips
76174

77175

78-
def perimeter_of_part(partition, part):
176+
def perimeter_of_part(partition, part: int) -> float:
79177
"""
80178
Totals up the perimeter of the part in the partition.
81-
Requires that 'boundary_perim' be a node attribute, 'shared_perim' be an edge
82-
attribute, 'cut_edges' be an updater, and 'exterior_boundaries' be an updater.
179+
180+
.. Warning::
181+
182+
Requires that 'boundary_perim' be a node attribute, 'shared_perim' be an edge
183+
attribute, 'cut_edges' be an updater, and 'exterior_boundaries' be an updater.
184+
185+
:param partition: A partition of a Graph
186+
:type partition: :class:`~gerrychain.partition.Partition`
187+
:param part: The id of the part of the partition whose perimeter we want to compute.
188+
:type part: int
189+
190+
:returns: The perimeter of the desired part.
191+
:rtype: float
83192
"""
84193
exterior_perimeter = partition["exterior_boundaries"][part]
85194
interior_perimeter = partition["interior_boundaries"][part]
86195

87196
return exterior_perimeter + interior_perimeter
88197

89198

90-
def perimeter(partition):
199+
def perimeter(partition) -> Dict[int, float]:
200+
"""
201+
:param partition: A partition of a Graph
202+
:type partition: :class:`~gerrychain.partition.Partition`
203+
204+
:returns: A dictionary mapping each part of a partition to its perimeter.
205+
:rtype: Dict[int, float]
206+
"""
91207
return {part: perimeter_of_part(partition, part) for part in partition.parts}

gerrychain/updaters/county_splits.py

+51-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
11
import collections
22
from enum import Enum
3+
from typing import Callable, Dict
4+
35

46
CountyInfo = collections.namedtuple("CountyInfo", "split nodes contains")
7+
"""
8+
A named tuple to store county split information.
9+
10+
:param split: The county split status. Makes use of
11+
:class:`.CountySplit` enum to compute.
12+
:type split: int
13+
:param nodes: The nodes that are contained in the county.
14+
:type nodes: List
15+
:param contains: The assignment IDs that are contained in the county.
16+
:type contains: Set
17+
"""
518

619

720
class CountySplit(Enum):
21+
"""
22+
Enum to track county splits in a partition.
23+
24+
:cvar NOT_SPLIT: The county is not split.
25+
:cvar NEW_SPLIT: The county is split in the current partition.
26+
:cvar OLD_SPLIT: The county is split in the parent partition.
27+
"""
828
NOT_SPLIT = 0
929
NEW_SPLIT = 1
1030
OLD_SPLIT = 2
1131

1232

13-
def county_splits(partition_name, county_field_name):
14-
"""Track county splits.
33+
def county_splits(partition_name: str, county_field_name: str) -> Callable:
34+
"""
35+
Update that allows for the tracking of county splits.
1536
1637
:param partition_name: Name that the :class:`.Partition` instance will store.
38+
:type partition_name: str
1739
:param county_field_name: Name of county ID field on the graph.
18-
:return: The tracked data is a dictionary keyed on the county ID. The
40+
:type county_field_name: str
41+
42+
:returns: The tracked data is a dictionary keyed on the county ID. The
1943
stored values are tuples of the form `(split, nodes, seen)`.
2044
`split` is a :class:`.CountySplit` enum, `nodes` is a list of
2145
node IDs, and `seen` is a list of assignment IDs that are
2246
contained in the county.
47+
:rtype: Callable
2348
"""
2449

2550
def _get_county_splits(partition):
@@ -28,8 +53,29 @@ def _get_county_splits(partition):
2853
return _get_county_splits
2954

3055

31-
def compute_county_splits(partition, county_field, partition_field):
32-
"""Track nodes in counties and information about their splitting."""
56+
def compute_county_splits(partition,
57+
county_field: str,
58+
partition_field: str
59+
) -> Dict[str, CountyInfo]:
60+
"""
61+
Track nodes in counties and information about their splitting.
62+
63+
:param partition: The partition object to compute county splits for.
64+
:type partition: :class:`~gerrychain.partition.Partition`
65+
:param county_field: Name of county ID field on the graph.
66+
:type county_field: str
67+
:param partition_field: Name of the attribute in the graph
68+
that stores the partition information. The county
69+
split information will be computed with respect to this
70+
division of the graph.
71+
:type partition_field: str
72+
73+
:returns: A dict containing the information on how counties changed
74+
between the parent and child partitions. If there is no parent
75+
partition, then only the OLD_SPLIT and NOT_SPLIT values will be
76+
used.
77+
:rtype: Dict[str, CountyInfo]
78+
"""
3379

3480
# Create the initial county data containers.
3581
if not partition.parent:

0 commit comments

Comments
 (0)