Skip to content

Commit

Permalink
Fix overlap problem and update numpy recs
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrrock2 committed Oct 28, 2023
1 parent e0d61f5 commit 726d61d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
43 changes: 26 additions & 17 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,32 @@ numpydoc==1.1.0; python_version >= "3.5" \
packaging==20.9; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.6" \
--hash=sha256:67714da7f7bc052e064859c05c595155bd1ee9f69f76557e21f051443c20947a \
--hash=sha256:5b327ac1320dc863dca72f4514ecc086f31186744b84a230374cc1fd776feae5
pandas==1.2.4; python_full_version >= "3.7.1" \
--hash=sha256:c601c6fdebc729df4438ec1f62275d6136a0dd14d332fc0e8ce3f7d2aadb4dd6 \
--hash=sha256:8d4c74177c26aadcfb4fd1de6c1c43c2bf822b3e0fc7a9b409eeaf84b3e92aaa \
--hash=sha256:b730add5267f873b3383c18cac4df2527ac4f0f0eed1c6cf37fcb437e25cf558 \
--hash=sha256:2cb7e8f4f152f27dc93f30b5c7a98f6c748601ea65da359af734dd0cf3fa733f \
--hash=sha256:2111c25e69fa9365ba80bbf4f959400054b2771ac5d041ed19415a8b488dc70a \
--hash=sha256:167693a80abc8eb28051fbd184c1b7afd13ce2c727a5af47b048f1ea3afefff4 \
--hash=sha256:612add929bf3ba9d27b436cc8853f5acc337242d6b584203f207e364bb46cb12 \
--hash=sha256:971e2a414fce20cc5331fe791153513d076814d30a60cd7348466943e6e909e4 \
--hash=sha256:68d7baa80c74aaacbed597265ca2308f017859123231542ff8a5266d489e1858 \
--hash=sha256:bd659c11a4578af740782288cac141a322057a2e36920016e0fc7b25c5a4b686 \
--hash=sha256:9db70ffa8b280bb4de83f9739d514cd0735825e79eef3a61d312420b9f16b758 \
--hash=sha256:298f0553fd3ba8e002c4070a723a59cdb28eda579f3e243bc2ee397773f5398b \
--hash=sha256:52d2472acbb8a56819a87aafdb8b5b6d2b3386e15c95bde56b281882529a7ded \
--hash=sha256:d0877407359811f7b853b548a614aacd7dea83b0c0c84620a9a643f180060950 \
--hash=sha256:2b063d41803b6a19703b845609c0b700913593de067b552a8b24dd8eeb8c9895 \
--hash=sha256:649ecab692fade3cbfcf967ff936496b0cfba0af00a55dfaacd82bdda5cb2279
pandas==2.1.2 \
--hash=sha256:021f09c15e1381e202d95d4a21ece8e7f2bf1388b6d7e9cae09dfe27bd2043d1 \
--hash=sha256:02954e285e8e2f4006b6f22be6f0df1f1c3c97adbb7ed211c6b483426f20d5c8 \
--hash=sha256:08d287b68fd28906a94564f15118a7ca8c242e50ae7f8bd91130c362b2108a81 \
--hash=sha256:24057459f19db9ebb02984c6fdd164a970b31a95f38e4a49cf7615b36a1b532c \
--hash=sha256:25c9976c17311388fcd953cb3d0697999b2205333f4e11e669d90ff8d830d429 \
--hash=sha256:3223f997b6d2ebf9c010260cf3d889848a93f5d22bb4d14cd32638b3d8bba7ad \
--hash=sha256:3f76280ce8ec216dde336e55b2b82e883401cf466da0fe3be317c03fb8ee7c7d \
--hash=sha256:52867d69a54e71666cd184b04e839cff7dfc8ed0cd6b936995117fdae8790b69 \
--hash=sha256:52897edc2774d2779fbeb6880d2cfb305daa0b1a29c16b91f531a18918a6e0f3 \
--hash=sha256:5aa6b86802e8cf7716bf4b4b5a3c99b12d34e9c6a9d06dad254447a620437931 \
--hash=sha256:5b40c9f494e1f27588c369b9e4a6ca19cd924b3a0e1ef9ef1a8e30a07a438f43 \
--hash=sha256:65177d1c519b55e5b7f094c660ed357bb7d86e799686bb71653b8a4803d8ff0d \
--hash=sha256:6ae6ffbd9d614c20d028c7117ee911fc4e266b4dca2065d5c5909e401f8ff683 \
--hash=sha256:7ad20d24acf3a0042512b7e8d8fdc2e827126ed519d6bd1ed8e6c14ec8a2c813 \
--hash=sha256:83c166b9bb27c1715bed94495d9598a7f02950b4749dba9349c1dd2cbf10729d \
--hash=sha256:851b5afbb0d62f6129ae891b533aa508cc357d5892c240c91933d945fff15731 \
--hash=sha256:8d0382645ede2fde352da2a885aac28ec37d38587864c0689b4b2361d17b1d4c \
--hash=sha256:a6cf8fcc8a63d333970b950a7331a30544cf59b1a97baf0a7409e09eafc1ac38 \
--hash=sha256:bbd98dcdcd32f408947afdb3f7434fade6edd408c3077bbce7bd840d654d92c6 \
--hash=sha256:d594e2ce51b8e0b4074e6644758865dc2bb13fd654450c1eae51201260a539f1 \
--hash=sha256:e78507adcc730533619de07bfdd1c62b2918a68cd4419ea386e28abf7f6a1e5c \
--hash=sha256:e7f12b2de0060b0b858cfec0016e7d980ae5bae455a1746bfcc70929100ee633 \
--hash=sha256:e90c95abb3285d06f6e4feedafc134306a8eced93cb78e08cf50e224d5ce22e2 \
--hash=sha256:eff794eeb7883c5aefb1ed572e7ff533ae779f6c6277849eab9e77986e352688 \
--hash=sha256:fc4944dc004ca6cc701dfa19afb8bdb26ad36b9bed5bcec617d2a11e9cae6902
pluggy==0.13.1; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.6" \
--hash=sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d \
--hash=sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0
Expand Down
26 changes: 22 additions & 4 deletions maup/indexed_geometries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pandas
import geopandas
# Added numpy import to handle output of STRtree query
import numpy
import warnings

from shapely.prepared import prep
from shapely.strtree import STRtree
Expand All @@ -21,17 +23,20 @@ def __init__(self, geometries):


def query(self, geometry):
# IMPORTANT: When "geometry" is multi-part, this query will return a
# (2 x n) array instead of a (1 x n) array, so it's safest to flatten the query
# output before proceeding.
relevant_index_array = self.spatial_index.query(geometry)
relevant_indices = [*set(numpy.ndarray.flatten(relevant_index_array))]
relevant_geometries = self.geometries.iloc[relevant_indices]
return relevant_geometries

def intersections(self, geometry):
def intersections(self, geometry):
relevant_geometries = self.query(geometry)
intersections = relevant_geometries.intersection(geometry)
return intersections[-(intersections.is_empty | intersections.isna())]

def covered_by(self, container):
def covered_by(self, container):
relevant_geometries = self.query(container)
prepared_container = prep(container)

Expand All @@ -41,7 +46,7 @@ def covered_by(self, container):
selected_geometries = relevant_geometries.apply(prepared_container.covers)
return relevant_geometries[selected_geometries]

def assign(self, targets):
def assign(self, targets):
target_geometries = get_geometries(targets)
groups = [
self.covered_by(container).apply(lambda x: container_index)
Expand All @@ -50,7 +55,20 @@ def assign(self, targets):
)
]
if groups:
return pandas.concat(groups).reindex(self.index)
groups = [group for group in groups if len(group) > 0]
groups_concat = pandas.concat(groups)
# No reindexing allowed with a non-unique Index,
# so we need to find and remove repetitions. (This only happens when the
# targets have overlaps and some source is completely covered by more
# than one target.)
# Any that get removed here will be randomly assigned to one of the
# covering units at the assign_by_area step by maup.assign.
groups_concat_index_list = list(groups_concat.index)
seen = set()
bad_indices = list(set([x for x in groups_concat_index_list if x in seen or seen.add(x)]))
if len(bad_indices)>0:
groups_concat = groups_concat.drop(bad_indices)
return groups_concat.reindex(self.index)
else:
return geopandas.GeoSeries()

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exclude = ["examples/", "tests/"]
[tool.poetry.dependencies]
python = "^3.8.0"
numpy = "^1.20.3"
pandas = "^1.2.4"
pandas = "^2.1.2"
geopandas = "^0.12.2"
Shapely = "^2.0.1"
tqdm = "^4.61.1"
Expand Down
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import geopandas as gp
import pytest
from shapely.geometry import Polygon
import pandas as pd
import maup

CRS = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
Expand Down Expand Up @@ -89,8 +90,9 @@ def square_mostly_in_top_left():
def squares_some_neat_some_overlapping(
square_mostly_in_top_left, squares_within_four_square_grid
):
result = squares_within_four_square_grid.append(
square_mostly_in_top_left, ignore_index=True
result = pd.concat(
[squares_within_four_square_grid, square_mostly_in_top_left],
ignore_index=True,
)
result.crs = CRS
return result
Expand Down

0 comments on commit 726d61d

Please sign in to comment.