Skip to content

Commit cc6bfb2

Browse files
committed
Fix warnings in tests
1 parent 93768fd commit cc6bfb2

File tree

8 files changed

+81
-47
lines changed

8 files changed

+81
-47
lines changed

maup/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import geopandas
22
from .adjacencies import adjacencies
3-
from .assign import assign
3+
from .assign import assign, AssigmentWarning
44
from .indexed_geometries import IndexedGeometries
55
from .intersections import intersections, prorate
66
from .repair import (
@@ -24,9 +24,10 @@
2424
"`geopandas.options.use_pygeos = False` before importing your shapefile."
2525
)
2626

27-
__version__ = "2.0.2"
27+
__version__ = "2.0.3"
2828
__all__ = [
2929
"adjacencies",
30+
"AssigmentWarning",
3031
"assign",
3132
"IndexedGeometries",
3233
"intersections",

maup/assign.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
from .crs import require_same_crs
77

88

9+
class AssigmentWarning(UserWarning):
10+
"""Warning raised when some source geometries are not assigned to any target."""
11+
12+
913
@require_same_crs
1014
def assign(sources, targets):
1115
"""Assign source geometries to targets. A source is assigned to the
@@ -25,7 +29,10 @@ def assign(sources, targets):
2529
# Warn here if there are still unassigned source geometries.
2630
unassigned = sources[assignment.isna()]
2731
if len(unassigned): # skip if done
28-
warnings.warn("Warning: Some units in the source geometry were unassigned.")
32+
warnings.warn(
33+
"Warning: Some units in the source geometry were unassigned.",
34+
AssigmentWarning,
35+
)
2936

3037
return assignment.astype(targets.index.dtype, errors="ignore")
3138

maup/repair.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def absorb_by_shared_perimeter(
497497

498498
# This difference in indices is expected since not all target geometries may have sources
499499
# to absorb, so it would be nice to remove this warning.
500-
result = targets.union(sources_to_absorb)
500+
result = targets.union(sources_to_absorb, align=True)
501501

502502
# The .union call only returns the targets who had a corresponding
503503
# source to absorb. Now we fill in all of the unchanged targets.

poetry.lock

Lines changed: 36 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ geopandas = "^1.1.1"
3030
Shapely = "^2.1.1"
3131
tqdm = "^4.67.1"
3232
pyproj = ">=3.7.2"
33+
pyogrio = "^0.11.1"
3334

3435
[tool.poetry.group.dev.dependencies]
3536
pytest = "^8.4.1"
@@ -42,6 +43,7 @@ sphinx-copybutton = "^0.5.2"
4243
sphinx-rtd-theme = "^3.0.2"
4344
black = "^25.1.0"
4445
ipykernel = "^6.30.1"
46+
python-dateutil = "^2.9.0.post0"
4547

4648
[build-system]
4749
requires = ["poetry-core>=2.0.0"]

tests/test_assign.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import geopandas
22
import pandas
33
from numpy import nan
4+
import pytest
45

56
from maup import assign
6-
from maup.assign import assign_by_area, assign_by_covering
7+
from maup.assign import assign_by_area, assign_by_covering, AssigmentWarning
78

89

910
def test_assign_assigns_geometries_when_they_nest_neatly(
@@ -147,7 +148,12 @@ def test_example_case():
147148
blocks.to_crs("EPSG:5070", inplace=True)
148149
precincts.to_crs("EPSG:5070", inplace=True)
149150
columns = ["TOTPOP", "BVAP", "WVAP", "HISP"]
150-
assignment = assign(blocks, precincts)
151+
152+
with pytest.warns(
153+
AssigmentWarning, match="Some units in the source geometry were unassigned."
154+
):
155+
assignment = assign(blocks, precincts)
156+
151157
precincts[columns] = blocks[columns].groupby(assignment).sum()
152158
assert (precincts[columns] > 0).sum().sum() > len(precincts)
153159
for col in columns: # fails because it does not neatly cover

tests/test_prorate.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pandas
33
import pytest
44

5-
from maup import assign, intersections, prorate, normalize
5+
from maup import assign, intersections, prorate, normalize, AssigmentWarning
66

77

88
@pytest.fixture
@@ -85,8 +85,13 @@ def test_example_case():
8585
# like boundary intersections, which we do not want to include in
8686
# our proration.
8787
pieces = intersections(old_precincts, new_precincts, area_cutoff=0)
88-
# Weight by prorated population from blocks
89-
weights = blocks["TOTPOP"].groupby(assign(blocks, pieces)).sum()
88+
89+
with pytest.warns(
90+
AssigmentWarning, match="Some units in the source geometry were unassigned."
91+
):
92+
# Weight by prorated population from blocks
93+
weights = blocks["TOTPOP"].groupby(assign(blocks, pieces)).sum()
94+
9095
weights = normalize(weights, level=0)
9196
# Use blocks to estimate population of each piece
9297
new_precincts[columns] = prorate(pieces, old_precincts[columns], weights=weights)

tests/test_repair.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import geopandas
22
import maup
33
from maup.repair import count_overlaps, autorepair, quick_repair
4+
from maup.assign import AssigmentWarning
45
import pytest
56

67
# These tests are losely based off the test_example_case in test_prorate.py
@@ -66,7 +67,13 @@ def test_crop_to():
6667

6768
# Calculate without cropping
6869
pieces = maup.intersections(old_precincts, new_precincts, area_cutoff=0)
69-
weights = blocks["TOTPOP"].groupby(maup.assign(blocks, pieces)).sum()
70+
71+
with pytest.warns(
72+
AssigmentWarning, match="Some units in the source geometry were unassigned."
73+
):
74+
# Weight by prorated population from blocks
75+
weights = blocks["TOTPOP"].groupby(maup.assign(blocks, pieces)).sum()
76+
7077
weights = maup.normalize(weights, level=0)
7178
new_precincts[columns] = maup.prorate(
7279
pieces, old_precincts[columns], weights=weights
@@ -76,7 +83,13 @@ def test_crop_to():
7683
old_precincts["geometries"] = maup.crop_to(old_precincts, new_precincts)
7784
new_precincts_cropped = new_precincts.copy()
7885
pieces = maup.intersections(old_precincts, new_precincts_cropped, area_cutoff=0)
79-
weights = blocks["TOTPOP"].groupby(maup.assign(blocks, pieces)).sum()
86+
87+
with pytest.warns(
88+
AssigmentWarning, match="Some units in the source geometry were unassigned."
89+
):
90+
# Weight by prorated population from blocks
91+
weights = blocks["TOTPOP"].groupby(maup.assign(blocks, pieces)).sum()
92+
8093
weights = maup.normalize(weights, level=0)
8194
new_precincts_cropped[columns] = maup.prorate(
8295
pieces, old_precincts[columns], weights=weights

0 commit comments

Comments
 (0)