epymorph 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- epymorph/__init__.py +7 -0
- epymorph/__main__.py +16 -0
- epymorph/adrio/__init__.py +0 -0
- epymorph/adrio/acs5.py +1378 -0
- epymorph/adrio/adrio.py +953 -0
- epymorph/adrio/cdc.py +1826 -0
- epymorph/adrio/commuting_flows.py +283 -0
- epymorph/adrio/csv.py +499 -0
- epymorph/adrio/humidity.py +206 -0
- epymorph/adrio/lodes.py +509 -0
- epymorph/adrio/numpy.py +160 -0
- epymorph/adrio/prism.py +345 -0
- epymorph/adrio/processing.py +1221 -0
- epymorph/adrio/soda.py +602 -0
- epymorph/adrio/us_tiger.py +289 -0
- epymorph/adrio/validation.py +382 -0
- epymorph/attribute.py +614 -0
- epymorph/cache.py +680 -0
- epymorph/cli/__init__.py +0 -0
- epymorph/cli/cache.py +78 -0
- epymorph/cli/main.py +29 -0
- epymorph/compartment_model.py +1873 -0
- epymorph/data/__init__.py +1 -0
- epymorph/data/ipm/__init__.py +22 -0
- epymorph/data/ipm/no.py +14 -0
- epymorph/data/ipm/pei.py +40 -0
- epymorph/data/ipm/seirs.py +50 -0
- epymorph/data/ipm/sirh.py +55 -0
- epymorph/data/ipm/sirs.py +48 -0
- epymorph/data/ipm/sparsemod.py +96 -0
- epymorph/data/mm/__init__.py +22 -0
- epymorph/data/mm/centroids.py +76 -0
- epymorph/data/mm/flat.py +61 -0
- epymorph/data/mm/icecube.py +50 -0
- epymorph/data/mm/no.py +29 -0
- epymorph/data/mm/pei.py +105 -0
- epymorph/data/mm/sparsemod.py +73 -0
- epymorph/data/pei.py +424 -0
- epymorph/data_shape.py +593 -0
- epymorph/data_type.py +185 -0
- epymorph/data_usage.py +258 -0
- epymorph/database.py +1301 -0
- epymorph/error.py +195 -0
- epymorph/event.py +196 -0
- epymorph/geography/__init__.py +0 -0
- epymorph/geography/custom.py +93 -0
- epymorph/geography/scope.py +457 -0
- epymorph/geography/us_census.py +1056 -0
- epymorph/geography/us_geography.py +502 -0
- epymorph/geography/us_tiger.py +1083 -0
- epymorph/initializer.py +786 -0
- epymorph/kit.py +107 -0
- epymorph/log/__init__.py +0 -0
- epymorph/log/file.py +128 -0
- epymorph/log/messaging.py +139 -0
- epymorph/log/movement.py +228 -0
- epymorph/movement_model.py +347 -0
- epymorph/parameter_fitting/__init__.py +0 -0
- epymorph/parameter_fitting/distribution.py +68 -0
- epymorph/parameter_fitting/dynamics.py +19 -0
- epymorph/parameter_fitting/filter/__init__.py +0 -0
- epymorph/parameter_fitting/filter/base_filter.py +50 -0
- epymorph/parameter_fitting/filter/particle.py +32 -0
- epymorph/parameter_fitting/filter/particle_filter.py +296 -0
- epymorph/parameter_fitting/likelihood.py +54 -0
- epymorph/parameter_fitting/output.py +73 -0
- epymorph/parameter_fitting/particlefilter_simulation.py +175 -0
- epymorph/parameter_fitting/perturbation.py +19 -0
- epymorph/parameter_fitting/utils/__init__.py +0 -0
- epymorph/parameter_fitting/utils/data_loader.py +97 -0
- epymorph/parameter_fitting/utils/epymorph_simulation.py +119 -0
- epymorph/parameter_fitting/utils/observations.py +63 -0
- epymorph/parameter_fitting/utils/parameter_estimation.py +49 -0
- epymorph/parameter_fitting/utils/params_perturb.py +43 -0
- epymorph/parameter_fitting/utils/particle_initializer.py +95 -0
- epymorph/parameter_fitting/utils/resampler.py +258 -0
- epymorph/parameter_fitting/utils/utils.py +101 -0
- epymorph/params.py +438 -0
- epymorph/py.typed +0 -0
- epymorph/rume.py +871 -0
- epymorph/simulation.py +991 -0
- epymorph/simulator/__init__.py +0 -0
- epymorph/simulator/basic/__init__.py +0 -0
- epymorph/simulator/basic/basic_simulator.py +171 -0
- epymorph/simulator/basic/ipm_exec.py +437 -0
- epymorph/simulator/basic/mm_exec.py +244 -0
- epymorph/simulator/basic/output.py +213 -0
- epymorph/simulator/world.py +149 -0
- epymorph/simulator/world_list.py +274 -0
- epymorph/strata.py +24 -0
- epymorph/sympy_shim.py +57 -0
- epymorph/time.py +1197 -0
- epymorph/tools/__init__.py +0 -0
- epymorph/tools/data.py +297 -0
- epymorph/tools/ipm_diagram.py +208 -0
- epymorph/tools/out_map.py +551 -0
- epymorph/tools/out_plot.py +423 -0
- epymorph/tools/out_table.py +582 -0
- epymorph/util.py +1036 -0
- epymorph-1.0.0.dist-info/METADATA +57 -0
- epymorph-1.0.0.dist-info/RECORD +104 -0
- epymorph-1.0.0.dist-info/WHEEL +5 -0
- epymorph-1.0.0.dist-info/entry_points.txt +2 -0
- epymorph-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Encodes the geographic system made up of US Census delineations.
|
|
3
|
+
This system comprises a set of perfectly-nested granularities,
|
|
4
|
+
and a structured ID system for labeling all delineations
|
|
5
|
+
(sometimes loosely called FIPS codes or GEOIDs).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
from abc import ABC, abstractmethod
|
|
10
|
+
from typing import Iterable, Literal, Self
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
from numpy.typing import NDArray
|
|
14
|
+
from typing_extensions import override
|
|
15
|
+
|
|
16
|
+
from epymorph.error import GeographyError
|
|
17
|
+
from epymorph.util import prefix
|
|
18
|
+
|
|
19
|
+
CensusGranularityName = Literal["state", "county", "tract", "block group", "block"]
|
|
20
|
+
"""Type alias: the name of a supported Census granularity."""
|
|
21
|
+
|
|
22
|
+
CENSUS_HIERARCHY = ("state", "county", "tract", "block group", "block")
|
|
23
|
+
"""The granularities in hierarchy order (largest to smallest)."""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class CensusGranularity(ABC):
|
|
27
|
+
"""
|
|
28
|
+
Each `CensusGranularity` instance defines a set of utility functions for working
|
|
29
|
+
with GEOIDs of that granularity, as well as inspecting and manipulating the
|
|
30
|
+
granularity hierarchy itself.
|
|
31
|
+
|
|
32
|
+
In typical usage, you will not construct this class directly, but use the
|
|
33
|
+
`CensusGranularity.of(granularity)` static method to obtain an instance, or import
|
|
34
|
+
one of the singleton instances: `STATE`, `COUNTY`, `TRACT`, `BLOCK_GROUP`, or
|
|
35
|
+
`BLOCK`.
|
|
36
|
+
|
|
37
|
+
Parameters
|
|
38
|
+
----------
|
|
39
|
+
name :
|
|
40
|
+
The granularity.
|
|
41
|
+
length :
|
|
42
|
+
The number of digits in the GEOIDs for this granularity.
|
|
43
|
+
match_pattern :
|
|
44
|
+
A regex pattern matching GEOIDs for this granularity.
|
|
45
|
+
extract_pattern :
|
|
46
|
+
A regex pattern with match groups for extracting this granularity
|
|
47
|
+
segment from a GEOID of an equal or finer granularity.
|
|
48
|
+
decompose_pattern :
|
|
49
|
+
A regex pattern with match groups for decomposing a GEOID of this
|
|
50
|
+
granularity into segments.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
_name: CensusGranularityName
|
|
54
|
+
_index: int
|
|
55
|
+
_length: int
|
|
56
|
+
_match_pattern: re.Pattern[str]
|
|
57
|
+
"""The pattern used for matching GEOIDs of this granularity."""
|
|
58
|
+
_extract_pattern: re.Pattern[str]
|
|
59
|
+
"""The pattern used for extracting GEOIDs of this granularity or smaller."""
|
|
60
|
+
_decompose_pattern: re.Pattern[str]
|
|
61
|
+
"""The pattern used for decomposing GEOIDs of this granularity."""
|
|
62
|
+
|
|
63
|
+
def __init__(
|
|
64
|
+
self,
|
|
65
|
+
name: CensusGranularityName,
|
|
66
|
+
length: int,
|
|
67
|
+
match_pattern: str,
|
|
68
|
+
extract_pattern: str,
|
|
69
|
+
decompose_pattern: str,
|
|
70
|
+
):
|
|
71
|
+
self._name = name
|
|
72
|
+
self._index = CENSUS_HIERARCHY.index(name)
|
|
73
|
+
self._length = length
|
|
74
|
+
self._match_pattern = re.compile(match_pattern)
|
|
75
|
+
self._extract_pattern = re.compile(extract_pattern)
|
|
76
|
+
self._decompose_pattern = re.compile(decompose_pattern)
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def name(self) -> CensusGranularityName:
|
|
80
|
+
"""The name of the granularity this class models."""
|
|
81
|
+
return self._name
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def length(self) -> int:
|
|
85
|
+
"""The number of digits in a GEOID of this granularity."""
|
|
86
|
+
return self._length
|
|
87
|
+
|
|
88
|
+
# TODO: test operators
|
|
89
|
+
def __lt__(self, other: Self) -> bool:
|
|
90
|
+
return self._index < other._index
|
|
91
|
+
|
|
92
|
+
def __gt__(self, other: Self) -> bool:
|
|
93
|
+
return self._index > other._index
|
|
94
|
+
|
|
95
|
+
def __le__(self, other: Self) -> bool:
|
|
96
|
+
return self._index <= other._index
|
|
97
|
+
|
|
98
|
+
def __ge__(self, other: Self) -> bool:
|
|
99
|
+
return self._index >= other._index
|
|
100
|
+
|
|
101
|
+
def __eq__(self, other) -> bool:
|
|
102
|
+
if not isinstance(other, CensusGranularity):
|
|
103
|
+
return False
|
|
104
|
+
return self._index == other._index
|
|
105
|
+
|
|
106
|
+
# TODO: remove?
|
|
107
|
+
def is_nested(self, outer: CensusGranularityName) -> bool:
|
|
108
|
+
"""
|
|
109
|
+
Test whether this granularity is nested inside (or equal to)
|
|
110
|
+
the given granularity.
|
|
111
|
+
|
|
112
|
+
Parameters
|
|
113
|
+
----------
|
|
114
|
+
outer :
|
|
115
|
+
The other granularity to consider.
|
|
116
|
+
|
|
117
|
+
Returns
|
|
118
|
+
-------
|
|
119
|
+
:
|
|
120
|
+
True if this granularity is inside or the same as `other`.
|
|
121
|
+
"""
|
|
122
|
+
return CENSUS_HIERARCHY.index(outer) <= CENSUS_HIERARCHY.index(self.name)
|
|
123
|
+
|
|
124
|
+
def matches(self, geoid: str) -> bool:
|
|
125
|
+
"""
|
|
126
|
+
Test whether the given GEOID matches this granularity exactly.
|
|
127
|
+
For example: "04" matches state granularity but not county granularity.
|
|
128
|
+
|
|
129
|
+
Parameters
|
|
130
|
+
----------
|
|
131
|
+
geoid :
|
|
132
|
+
The GEOID to test.
|
|
133
|
+
|
|
134
|
+
Returns
|
|
135
|
+
-------
|
|
136
|
+
:
|
|
137
|
+
True if the GEOID given matches this granularity.
|
|
138
|
+
"""
|
|
139
|
+
return self._match_pattern.match(geoid) is not None
|
|
140
|
+
|
|
141
|
+
def extract(self, geoid: str) -> str:
|
|
142
|
+
"""
|
|
143
|
+
Extract this level of granularity's GEOID segment, if the given GEOID is of
|
|
144
|
+
this granularity or smaller.
|
|
145
|
+
|
|
146
|
+
Parameters
|
|
147
|
+
----------
|
|
148
|
+
geoid :
|
|
149
|
+
The GEOID to operate on.
|
|
150
|
+
|
|
151
|
+
Returns
|
|
152
|
+
-------
|
|
153
|
+
:
|
|
154
|
+
The segment of the GEOID that matches this granularity.
|
|
155
|
+
|
|
156
|
+
Raises
|
|
157
|
+
------
|
|
158
|
+
GeographyError
|
|
159
|
+
If the GEOID is unsuitable or poorly formatted.
|
|
160
|
+
"""
|
|
161
|
+
if (m := self._extract_pattern.match(geoid)) is not None:
|
|
162
|
+
return m[1]
|
|
163
|
+
else:
|
|
164
|
+
msg = f"Unable to extract {self._name} info from ID {id}; check its format."
|
|
165
|
+
raise GeographyError(msg)
|
|
166
|
+
|
|
167
|
+
def truncate(self, geoid: str) -> str:
|
|
168
|
+
"""
|
|
169
|
+
Truncate the given GEOID to this level of granularity.
|
|
170
|
+
If the given GEOID is for a granularity larger than this level,
|
|
171
|
+
the GEOID will be returned unchanged.
|
|
172
|
+
|
|
173
|
+
Parameters
|
|
174
|
+
----------
|
|
175
|
+
geoid :
|
|
176
|
+
The GEOID to operate on.
|
|
177
|
+
|
|
178
|
+
Returns
|
|
179
|
+
-------
|
|
180
|
+
:
|
|
181
|
+
The truncated GEOID.
|
|
182
|
+
"""
|
|
183
|
+
return geoid[: self.length]
|
|
184
|
+
|
|
185
|
+
def truncate_unique(self, geoids: Iterable[str]) -> Iterable[str]:
|
|
186
|
+
"""
|
|
187
|
+
Truncate an Iterable of GEOIDs to this level of granularity, returning only
|
|
188
|
+
unique entries without changing the ordering of entries.
|
|
189
|
+
|
|
190
|
+
Parameters
|
|
191
|
+
----------
|
|
192
|
+
geoids :
|
|
193
|
+
The list of GEOIDs to operate on.
|
|
194
|
+
|
|
195
|
+
Returns
|
|
196
|
+
-------
|
|
197
|
+
:
|
|
198
|
+
The unique values contained in `geoids` after truncation
|
|
199
|
+
to this level of granularity.
|
|
200
|
+
"""
|
|
201
|
+
n = self.length
|
|
202
|
+
seen = set[str]()
|
|
203
|
+
for g in geoids:
|
|
204
|
+
curr = g[:n]
|
|
205
|
+
if curr not in seen:
|
|
206
|
+
yield curr
|
|
207
|
+
seen.add(curr)
|
|
208
|
+
|
|
209
|
+
def _decompose(self, geoid: str) -> re.Match[str]:
|
|
210
|
+
"""
|
|
211
|
+
Decompose a GEOID as a regex match.
|
|
212
|
+
|
|
213
|
+
Raises
|
|
214
|
+
------
|
|
215
|
+
GeographyError
|
|
216
|
+
If the match fails.
|
|
217
|
+
"""
|
|
218
|
+
match = self._decompose_pattern.match(geoid)
|
|
219
|
+
if match is None:
|
|
220
|
+
msg = (
|
|
221
|
+
f"Unable to decompose {self.name} info from ID {id}; check its format."
|
|
222
|
+
)
|
|
223
|
+
raise GeographyError(msg)
|
|
224
|
+
return match
|
|
225
|
+
|
|
226
|
+
@abstractmethod
|
|
227
|
+
def decompose(self, geoid: str) -> tuple[str, ...]:
|
|
228
|
+
"""
|
|
229
|
+
Decompose a GEOID into a tuple containing all of its granularity component IDs.
|
|
230
|
+
The GEOID must match this granularity exactly.
|
|
231
|
+
|
|
232
|
+
Parameters
|
|
233
|
+
----------
|
|
234
|
+
geoid :
|
|
235
|
+
The GEOID to operate on.
|
|
236
|
+
|
|
237
|
+
Returns
|
|
238
|
+
-------
|
|
239
|
+
:
|
|
240
|
+
A tuple as long as the number of granularity segments used for this
|
|
241
|
+
level of granularity.
|
|
242
|
+
|
|
243
|
+
Raises
|
|
244
|
+
------
|
|
245
|
+
GeographyError
|
|
246
|
+
If the GEOID does not match this granularity exactly.
|
|
247
|
+
"""
|
|
248
|
+
|
|
249
|
+
def grouped(self, sorted_geoids: NDArray[np.str_]) -> dict[str, NDArray[np.str_]]:
|
|
250
|
+
"""
|
|
251
|
+
Group a list of GEOIDs by this level of granularity.
|
|
252
|
+
|
|
253
|
+
Requires that the GEOID array has been sorted!
|
|
254
|
+
|
|
255
|
+
Parameters
|
|
256
|
+
----------
|
|
257
|
+
sorted_geoids :
|
|
258
|
+
The GEOIDs to group, as a sorted array.
|
|
259
|
+
|
|
260
|
+
Returns
|
|
261
|
+
-------
|
|
262
|
+
:
|
|
263
|
+
A dictionary where the keys represent the unique groups
|
|
264
|
+
present and the values are all GEOIDs contained in each group.
|
|
265
|
+
"""
|
|
266
|
+
group_prefix = prefix(self.length)(sorted_geoids)
|
|
267
|
+
uniques, splits = np.unique(group_prefix, return_index=True)
|
|
268
|
+
grouped = np.split(sorted_geoids, splits[1:])
|
|
269
|
+
return dict(zip(uniques, grouped))
|
|
270
|
+
|
|
271
|
+
@staticmethod
|
|
272
|
+
def of(name: CensusGranularityName) -> "CensusGranularity":
|
|
273
|
+
"""
|
|
274
|
+
Get a CensusGranularity instance by name.
|
|
275
|
+
|
|
276
|
+
Parameters
|
|
277
|
+
----------
|
|
278
|
+
name :
|
|
279
|
+
The name of the granularity.
|
|
280
|
+
|
|
281
|
+
Returns
|
|
282
|
+
-------
|
|
283
|
+
:
|
|
284
|
+
The CensusGranularity instance, whose type matches the named granularity.
|
|
285
|
+
"""
|
|
286
|
+
match name:
|
|
287
|
+
case "state":
|
|
288
|
+
return STATE
|
|
289
|
+
case "county":
|
|
290
|
+
return COUNTY
|
|
291
|
+
case "tract":
|
|
292
|
+
return TRACT
|
|
293
|
+
case "block group":
|
|
294
|
+
return BLOCK_GROUP
|
|
295
|
+
case "block":
|
|
296
|
+
return BLOCK
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
class State(CensusGranularity):
|
|
300
|
+
"""State-level utility functions."""
|
|
301
|
+
|
|
302
|
+
def __init__(self):
|
|
303
|
+
super().__init__(
|
|
304
|
+
name="state",
|
|
305
|
+
length=2,
|
|
306
|
+
match_pattern=r"^\d{2}$",
|
|
307
|
+
extract_pattern=r"^(\d{2})\d*$",
|
|
308
|
+
decompose_pattern=r"^(\d{2})$",
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
@override
|
|
312
|
+
def decompose(self, geoid: str) -> tuple[str]:
|
|
313
|
+
"""
|
|
314
|
+
Decompose a GEOID into a tuple containing the state ID.
|
|
315
|
+
The GEOID must be a state GEOID.
|
|
316
|
+
|
|
317
|
+
Parameters
|
|
318
|
+
----------
|
|
319
|
+
geoid :
|
|
320
|
+
The GEOID to operate on.
|
|
321
|
+
|
|
322
|
+
Returns
|
|
323
|
+
-------
|
|
324
|
+
:
|
|
325
|
+
A tuple of state ID.
|
|
326
|
+
|
|
327
|
+
Raises
|
|
328
|
+
------
|
|
329
|
+
GeographyError
|
|
330
|
+
If the GEOID does not match this granularity exactly.
|
|
331
|
+
"""
|
|
332
|
+
m = self._decompose(geoid)
|
|
333
|
+
return (m[1],)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
class County(CensusGranularity):
|
|
337
|
+
"""County-level utility functions."""
|
|
338
|
+
|
|
339
|
+
def __init__(self):
|
|
340
|
+
super().__init__(
|
|
341
|
+
name="county",
|
|
342
|
+
length=5,
|
|
343
|
+
match_pattern=r"^\d{5}$",
|
|
344
|
+
extract_pattern=r"^\d{2}(\d{3})\d*$",
|
|
345
|
+
decompose_pattern=r"^(\d{2})(\d{3})$",
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
@override
|
|
349
|
+
def decompose(self, geoid: str) -> tuple[str, str]:
|
|
350
|
+
"""
|
|
351
|
+
Decompose a GEOID into a tuple containing the state and county ID.
|
|
352
|
+
The GEOID must be a county GEOID.
|
|
353
|
+
|
|
354
|
+
Parameters
|
|
355
|
+
----------
|
|
356
|
+
geoid :
|
|
357
|
+
The GEOID to operate on.
|
|
358
|
+
|
|
359
|
+
Returns
|
|
360
|
+
-------
|
|
361
|
+
:
|
|
362
|
+
A tuple of state and county ID.
|
|
363
|
+
|
|
364
|
+
Raises
|
|
365
|
+
------
|
|
366
|
+
GeographyError
|
|
367
|
+
If the GEOID does not match this granularity exactly.
|
|
368
|
+
"""
|
|
369
|
+
m = self._decompose(geoid)
|
|
370
|
+
return (m[1], m[2])
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
class Tract(CensusGranularity):
|
|
374
|
+
"""Census-tract-level utility functions."""
|
|
375
|
+
|
|
376
|
+
def __init__(self):
|
|
377
|
+
super().__init__(
|
|
378
|
+
name="tract",
|
|
379
|
+
length=11,
|
|
380
|
+
match_pattern=r"^\d{11}$",
|
|
381
|
+
extract_pattern=r"^\d{5}(\d{6})\d*$",
|
|
382
|
+
decompose_pattern=r"^(\d{2})(\d{3})(\d{6})$",
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
@override
|
|
386
|
+
def decompose(self, geoid: str) -> tuple[str, str, str]:
|
|
387
|
+
"""
|
|
388
|
+
Decompose a GEOID into a tuple containing the state, county, and tract ID.
|
|
389
|
+
The GEOID must be a tract GEOID.
|
|
390
|
+
|
|
391
|
+
Parameters
|
|
392
|
+
----------
|
|
393
|
+
geoid :
|
|
394
|
+
The GEOID to operate on.
|
|
395
|
+
|
|
396
|
+
Returns
|
|
397
|
+
-------
|
|
398
|
+
:
|
|
399
|
+
A tuple of state, county, and tract ID.
|
|
400
|
+
|
|
401
|
+
Raises
|
|
402
|
+
------
|
|
403
|
+
GeographyError
|
|
404
|
+
If the GEOID does not match this granularity exactly.
|
|
405
|
+
"""
|
|
406
|
+
m = self._decompose(geoid)
|
|
407
|
+
return (m[1], m[2], m[3])
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
class BlockGroup(CensusGranularity):
|
|
411
|
+
"""Block-group-level utility functions."""
|
|
412
|
+
|
|
413
|
+
def __init__(self):
|
|
414
|
+
super().__init__(
|
|
415
|
+
name="block group",
|
|
416
|
+
length=12,
|
|
417
|
+
match_pattern=r"^\d{12}$",
|
|
418
|
+
extract_pattern=r"^\d{11}(\d)\d*$",
|
|
419
|
+
decompose_pattern=r"^(\d{2})(\d{3})(\d{6})(\d)$",
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
@override
|
|
423
|
+
def decompose(self, geoid: str) -> tuple[str, str, str, str]:
|
|
424
|
+
"""
|
|
425
|
+
Decompose a GEOID into a tuple containing the state, county, tract, and
|
|
426
|
+
block group ID. The GEOID must be a block group GEOID.
|
|
427
|
+
|
|
428
|
+
Parameters
|
|
429
|
+
----------
|
|
430
|
+
geoid :
|
|
431
|
+
The GEOID to operate on.
|
|
432
|
+
|
|
433
|
+
Returns
|
|
434
|
+
-------
|
|
435
|
+
:
|
|
436
|
+
A tuple of state, county, tract, and block group ID.
|
|
437
|
+
|
|
438
|
+
Raises
|
|
439
|
+
------
|
|
440
|
+
GeographyError
|
|
441
|
+
If the GEOID does not match this granularity exactly.
|
|
442
|
+
"""
|
|
443
|
+
m = self._decompose(geoid)
|
|
444
|
+
return (m[1], m[2], m[3], m[4])
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
class Block(CensusGranularity):
|
|
448
|
+
"""Block-level utility functions."""
|
|
449
|
+
|
|
450
|
+
def __init__(self):
|
|
451
|
+
super().__init__(
|
|
452
|
+
name="block",
|
|
453
|
+
length=15,
|
|
454
|
+
match_pattern=r"^\d{15}$",
|
|
455
|
+
extract_pattern=r"^\d{11}(\d{4})$",
|
|
456
|
+
decompose_pattern=r"^(\d{2})(\d{3})(\d{6})(\d{4})$",
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
@override
|
|
460
|
+
def decompose(self, geoid: str) -> tuple[str, str, str, str, str]:
|
|
461
|
+
"""
|
|
462
|
+
Decompose a GEOID into a tuple containing the state, county, tract,
|
|
463
|
+
block group, and block ID. The GEOID must be a block GEOID.
|
|
464
|
+
Note that block IDs are kind of strange -- the block group ID
|
|
465
|
+
is a single digit, but the block ID also includes this digit.
|
|
466
|
+
Thus, the returned tuple will include this digit in both of the last two parts.
|
|
467
|
+
|
|
468
|
+
Parameters
|
|
469
|
+
----------
|
|
470
|
+
geoid :
|
|
471
|
+
The GEOID to operate on.
|
|
472
|
+
|
|
473
|
+
Returns
|
|
474
|
+
-------
|
|
475
|
+
:
|
|
476
|
+
A tuple of state, county, tract, block group, and block ID.
|
|
477
|
+
|
|
478
|
+
Raises
|
|
479
|
+
------
|
|
480
|
+
GeographyError
|
|
481
|
+
If the GEOID does not match this granularity exactly.
|
|
482
|
+
"""
|
|
483
|
+
# The block group ID is the first digit of the block ID,
|
|
484
|
+
# but the block ID also includes this digit.
|
|
485
|
+
m = self._decompose(geoid)
|
|
486
|
+
return (m[1], m[2], m[3], m[4][0], m[4])
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
# Singletons for the CensusGranularity classes.
|
|
490
|
+
STATE = State()
|
|
491
|
+
"""A singleton `State` instance."""
|
|
492
|
+
COUNTY = County()
|
|
493
|
+
"""A singleton `County` instance."""
|
|
494
|
+
TRACT = Tract()
|
|
495
|
+
"""A singleton `Tract` instance."""
|
|
496
|
+
BLOCK_GROUP = BlockGroup()
|
|
497
|
+
"""A singleton `BlockGroup` instance."""
|
|
498
|
+
BLOCK = Block()
|
|
499
|
+
"""A singleton `Block` instance."""
|
|
500
|
+
|
|
501
|
+
CENSUS_GRANULARITY = (STATE, COUNTY, TRACT, BLOCK_GROUP, BLOCK)
|
|
502
|
+
"""`CensusGranularity` singletons in hierarchy order."""
|