2
2
3
3
from .flows import on_flow
4
4
from .cut_edges import on_edge_flow
5
+ from typing import Dict , Set
5
6
6
7
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
+ """
8
19
if partition .parent :
9
20
return partition .parent [alias ]
10
21
return {
@@ -14,20 +25,55 @@ def boundary_nodes(partition, alias="boundary_nodes"):
14
25
}
15
26
16
27
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
+ """
18
37
part_boundaries = collections .defaultdict (set )
19
38
for node in partition ["boundary_nodes" ]:
20
39
part_boundaries [partition .assignment .mapping [node ]].add (node )
21
40
return part_boundaries
22
41
23
42
24
43
@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
+ """
26
64
graph_boundary = partition ["boundary_nodes" ]
27
65
return (previous | (inflow & graph_boundary )) - outflow
28
66
29
67
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
+ """
31
77
graph_boundary = partition ["boundary_nodes" ]
32
78
boundaries = collections .defaultdict (lambda : 0 )
33
79
for node in graph_boundary :
@@ -37,7 +83,26 @@ def initialize_exterior_boundaries(partition):
37
83
38
84
39
85
@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
+ """
41
106
graph_boundary = partition ["boundary_nodes" ]
42
107
added_perimeter = sum (
43
108
partition .graph .nodes [node ]["boundary_perim" ]
@@ -51,6 +116,14 @@ def exterior_boundaries(partition, previous, inflow, outflow):
51
116
52
117
53
118
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
+ """
54
127
return {
55
128
part : sum (
56
129
partition .graph .edges [edge ]["shared_perim" ]
@@ -61,7 +134,27 @@ def initialize_interior_boundaries(partition):
61
134
62
135
63
136
@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
+ """
65
158
added_perimeter = sum (
66
159
partition .graph .edges [edge ]["shared_perim" ] for edge in new_edges
67
160
)
@@ -71,21 +164,44 @@ def interior_boundaries(partition, previous, new_edges, old_edges):
71
164
return previous + added_perimeter - removed_perimeter
72
165
73
166
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
+ """
75
173
return partition .flips
76
174
77
175
78
- def perimeter_of_part (partition , part ) :
176
+ def perimeter_of_part (partition , part : int ) -> float :
79
177
"""
80
178
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
83
192
"""
84
193
exterior_perimeter = partition ["exterior_boundaries" ][part ]
85
194
interior_perimeter = partition ["interior_boundaries" ][part ]
86
195
87
196
return exterior_perimeter + interior_perimeter
88
197
89
198
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
+ """
91
207
return {part : perimeter_of_part (partition , part ) for part in partition .parts }
0 commit comments