Skip to content

Commit

Permalink
lint and remove compat
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfleis committed Nov 22, 2023
1 parent 423082b commit 0601a86
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 136 deletions.
4 changes: 2 additions & 2 deletions momepy/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _get_links(self):

p2.append(item)

self.result = list(zip(range(len(p1)), p1, p2))
self.result = list(zip(range(len(p1)), p1, p2, strict=True))

for a in self.result:
n = a[0]
Expand Down Expand Up @@ -371,7 +371,7 @@ def _list_to_tuple(line):
def _list_to_pairs(in_list):
"""Split a line at every point."""
tmp_list = [list(point) for point in in_list]
return [list(pair) for pair in zip(tmp_list, tmp_list[1:])]
return [list(pair) for pair in zip(tmp_list, tmp_list[1:], strict=True)]


def _compute_angle(point1, point2):
Expand Down
6 changes: 3 additions & 3 deletions momepy/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def __init__(
end_markers = []

lengths = shapely.length(lines)
for ix, (line, length) in enumerate(zip(lines, lengths)):
for ix, (line, length) in enumerate(zip(lines, lengths, strict=True)):
pts = shapely.line_interpolate_point(
line, np.linspace(0, length, num=int((length) // distance))
)
Expand All @@ -560,7 +560,7 @@ def __init__(
ids += [ix] * 2

ticks = []
for num, (pt, end) in enumerate(zip(list_points, end_markers), 1):
for num, (pt, end) in enumerate(zip(list_points, end_markers, strict=True), 1):
if end:
ticks.append([pt, pt])
ticks.append([pt, pt])
Expand All @@ -587,7 +587,7 @@ def __init__(

min_distances = []
min_inds = []
for dis, ind in zip(dist_per_res, inp_per_res):
for dis, ind in zip(dist_per_res, inp_per_res, strict=True):
min_distances.append(np.min(dis))
min_inds.append(ind[np.argmin(dis)])

Expand Down
8 changes: 6 additions & 2 deletions momepy/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def _dist(a, b):

bboxes = shapely.minimum_rotated_rectangle(gdf.geometry)
for geom, bbox in tqdm(
zip(gdf.geometry, bboxes), total=gdf.shape[0], disable=not verbose
zip(gdf.geometry, bboxes, strict=True),
total=gdf.shape[0],
disable=not verbose,
):
if geom.geom_type in ["Polygon", "MultiPolygon", "LinearRing"]:
bbox = list(bbox.exterior.coords)
Expand Down Expand Up @@ -793,7 +795,9 @@ def __init__(
print("Spatial weights ready...") if verbose else None

self.sw = spatial_weights
patches = dict(zip(gdf[unique_id], spatial_weights.component_labels))
patches = dict(
zip(gdf[unique_id], spatial_weights.component_labels, strict=True)
)

for uid in tqdm(
self.id,
Expand Down
2 changes: 1 addition & 1 deletion momepy/diversity.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def p(n, sum_n):
counts.update(data.value_counts())
else:
sample_bins = mc.UserDefined(data, bins)
counts = dict(zip(bins, sample_bins.counts))
counts = dict(zip(bins, sample_bins.counts, strict=True))

return -sum(p(n, sum(counts.values())) for n in counts.values() if n != 0)

Expand Down
60 changes: 18 additions & 42 deletions momepy/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import numpy as np
import pandas as pd
import shapely
from packaging.version import Version
from scipy.spatial import Voronoi
from shapely.geometry.base import BaseGeometry
from shapely.ops import polygonize
Expand All @@ -25,8 +24,6 @@
"get_network_ratio",
]

GPD_10 = Version(gpd.__version__) >= Version("0.10")


def buffered_limit(gdf, buffer=100):
"""
Expand Down Expand Up @@ -241,7 +238,7 @@ def __init__(
n_chunks,
)
else:
if isinstance(limit, (gpd.GeoSeries, gpd.GeoDataFrame)):
if isinstance(limit, gpd.GeoSeries | gpd.GeoDataFrame):
limit = limit.unary_union

bounds = shapely.bounds(limit)
Expand Down Expand Up @@ -275,10 +272,7 @@ def _morphological_tessellation(
objects.loc[mask, objects.geometry.name] = objects[mask].buffer(
-shrink, cap_style=2, join_style=2
)
if GPD_10:
objects = objects.reset_index(drop=True).explode(ignore_index=True)
else:
objects = objects.reset_index(drop=True).explode().reset_index(drop=True)
objects = objects.reset_index(drop=True).explode(ignore_index=True)
objects = objects.set_index(unique_id)

print("Generating input point array...") if verbose else None
Expand Down Expand Up @@ -331,7 +325,7 @@ def _dense_point_array(self, geoms, distance, index):
else:
lines = geoms
lengths = shapely.length(lines)
for ix, line, length in zip(index, lines, lengths):
for ix, line, length in zip(index, lines, lengths, strict=True):
if length > distance: # some polygons might have collapsed
pts = shapely.line_interpolate_point(
line,
Expand Down Expand Up @@ -621,45 +615,28 @@ def __init__(self, tessellation, edges, buildings, id_name, unique_id):
gpd.GeoDataFrame(geometry=edges.buffer(0.001)),
how="difference",
)
cut = cut.explode(ignore_index=True) if GPD_10 else cut.explode()

cut = cut.explode(ignore_index=True)
weights = libpysal.weights.Queen.from_dataframe(cut, silence_warnings=True)
cut["component"] = weights.component_labels
buildings_c = buildings.copy()
buildings_c.geometry = buildings_c.representative_point() # make points
if GPD_10:
centroids_temp_id = gpd.sjoin(
buildings_c,
cut[[cut.geometry.name, "component"]],
how="left",
predicate="within",
)
else:
centroids_temp_id = gpd.sjoin(
buildings_c,
cut[[cut.geometry.name, "component"]],
how="left",
op="within",
)
centroids_temp_id = gpd.sjoin(
buildings_c,
cut[[cut.geometry.name, "component"]],
how="left",
predicate="within",
)

cells_copy = tessellation[[unique_id, tessellation.geometry.name]].merge(
centroids_temp_id[[unique_id, "component"]], on=unique_id, how="left"
)
if GPD_10:
blocks = cells_copy.dissolve(by="component").explode(ignore_index=True)
else:
blocks = (
cells_copy.dissolve(by="component").explode().reset_index(drop=True)
)
blocks = cells_copy.dissolve(by="component").explode(ignore_index=True)
blocks[id_name] = range(len(blocks))
blocks = blocks[[id_name, blocks.geometry.name]]

if GPD_10:
centroids_w_bl_id2 = gpd.sjoin(
buildings_c, blocks, how="left", predicate="within"
)
else:
centroids_w_bl_id2 = gpd.sjoin(buildings_c, blocks, how="left", op="within")
centroids_w_bl_id2 = gpd.sjoin(
buildings_c, blocks, how="left", predicate="within"
)

self.buildings_id = centroids_w_bl_id2[id_name]

Expand Down Expand Up @@ -823,7 +800,7 @@ def get_node_id(
edges = edges.set_index(edge_id)
centroids = objects.centroid
for eid, centroid in tqdm(
zip(objects[edge_id], centroids),
zip(objects[edge_id], centroids, strict=True),
total=objects.shape[0],
disable=not verbose,
):
Expand All @@ -844,7 +821,9 @@ def get_node_id(

elif edge_keys is not None and edge_values is not None:
for edge_i, edge_r, geom in tqdm(
zip(objects[edge_keys], objects[edge_values], objects.geometry),
zip(
objects[edge_keys], objects[edge_values], objects.geometry, strict=True
),
total=objects.shape[0],
disable=not verbose,
):
Expand Down Expand Up @@ -903,9 +882,6 @@ def get_network_ratio(df, edges, initial_buffer=500):
4 [26] [1]
"""

if not GPD_10:
raise ImportError("`get_network_ratio` requires geopandas 0.10 or newer.")

(df_ix, edg_ix), dist = edges.sindex.nearest(
df.geometry, max_distance=initial_buffer, return_distance=True
)
Expand Down
54 changes: 11 additions & 43 deletions momepy/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import numpy as np
import pandas as pd
import shapely
from packaging.version import Version
from scipy.signal import find_peaks
from scipy.stats import gaussian_kde
from shapely.geometry import LineString, Point
Expand All @@ -30,9 +29,6 @@
"FaceArtifacts",
]

GPD_10 = Version(gpd.__version__) >= Version("0.10")
GPD_09 = Version(gpd.__version__) >= Version("0.9")


def preprocess(
buildings, size=30, compactness=0.2, islands=True, loops=2, verbose=True
Expand Down Expand Up @@ -80,11 +76,7 @@ def preprocess(
GeoDataFrame containing preprocessed geometry
"""
blg = buildings.copy()
if GPD_10:
blg = blg.explode(ignore_index=True)
else:
blg = blg.explode()
blg.reset_index(drop=True, inplace=True)
blg = blg.explode(ignore_index=True)
for loop in range(0, loops):
print("Loop", loop + 1, f"out of {loops}.") if verbose else None
blg.reset_index(inplace=True, drop=True)
Expand Down Expand Up @@ -182,13 +174,10 @@ def remove_false_nodes(gdf):
momepy.extend_lines
momepy.close_gaps
"""
if isinstance(gdf, (gpd.GeoDataFrame, gpd.GeoSeries)):
if isinstance(gdf, gpd.GeoDataFrame | gpd.GeoSeries):
# explode to avoid MultiLineStrings
# reset index due to the bug in GeoPandas explode
if GPD_10:
df = gdf.reset_index(drop=True).explode(ignore_index=True)
else:
df = gdf.reset_index(drop=True).explode().reset_index(drop=True)
df = gdf.reset_index(drop=True).explode(ignore_index=True)

# get underlying shapely geometry
geom = df.geometry.array
Expand Down Expand Up @@ -242,10 +231,7 @@ def remove_false_nodes(gdf):

# remove incorrect geometries and append fixed versions
df = df.drop(merge)
if GPD_10:
final = gpd.GeoSeries(new).explode(ignore_index=True)
else:
final = gpd.GeoSeries(new).explode().reset_index(drop=True)
final = gpd.GeoSeries(new).explode(ignore_index=True)
if isinstance(gdf, gpd.GeoDataFrame):
return pd.concat(
[
Expand Down Expand Up @@ -472,10 +458,7 @@ def extend_lines(gdf, tolerance, target=None, barrier=None, extension=0):
"""
# explode to avoid MultiLineStrings
# reset index due to the bug in GeoPandas explode
if GPD_10:
df = gdf.reset_index(drop=True).explode(ignore_index=True)
else:
df = gdf.reset_index(drop=True).explode().reset_index(drop=True)
df = gdf.reset_index(drop=True).explode(ignore_index=True)

if target is None:
target = df
Expand Down Expand Up @@ -758,10 +741,7 @@ def _selecting_rabs_from_poly(
rab["rab_diameter"] = rab[["deltax", "deltay"]].max(axis=1)

# selecting the adjacent areas that are of smaller than itself
if GPD_10:
rab_adj = gpd.sjoin(gdf, rab, predicate="intersects")
else:
rab_adj = gpd.sjoin(gdf, rab, op="intersects")
rab_adj = gpd.sjoin(gdf, rab, predicate="intersects")

area_right = area_col + "_right"
area_left = area_col + "_left"
Expand Down Expand Up @@ -870,16 +850,10 @@ def _selecting_incoming_lines(rab_multipolygons, edges, angle_threshold=0):
is used to select the line to be extended further.
"""
# selecting the lines that are touching but not covered by
if GPD_10:
touching = gpd.sjoin(edges, rab_multipolygons, predicate="touches")
edges_idx, rabs_idx = rab_multipolygons.sindex.query_bulk(
edges.geometry, predicate="covered_by"
)
else:
touching = gpd.sjoin(edges, rab_multipolygons, op="touches")
edges_idx, rabs_idx = rab_multipolygons.sindex.query_bulk(
edges.geometry, op="covered_by"
)
touching = gpd.sjoin(edges, rab_multipolygons, predicate="touches")
edges_idx, rabs_idx = rab_multipolygons.sindex.query_bulk(
edges.geometry, predicate="covered_by"
)
idx_drop = edges.index.take(edges_idx)
touching_idx = touching.index
ls = list(set(touching_idx) - set(idx_drop))
Expand Down Expand Up @@ -1035,12 +1009,6 @@ def roundabout_simplification(
GeoDataFrame with an updated geometry and an additional
column labeling modified edges.
"""
if not GPD_09:
raise ImportError(
"`roundabout_simplification` requires geopandas 0.9.0 or newer. "
f"Your current version is {gpd.__version__}."
)

if len(edges[edges.geom_type != "LineString"]) > 0:
raise TypeError(
"Only LineString geometries are allowed. "
Expand Down Expand Up @@ -1223,7 +1191,7 @@ def __init__(
# find index (in linspace) of highest peak
highest_peak_index = self.peaks[highest_peak_listindex]
# define all possible peak ranges fitting our definition
peak_bounds = list(zip(self.peaks, self.peaks[1:]))
peak_bounds = list(zip(self.peaks, self.peaks[1:], strict=True))
peak_bounds_accepted = [b for b in peak_bounds if highest_peak_index in b]
# find all valleys that lie between two peaks
valleys_accepted = [
Expand Down
9 changes: 0 additions & 9 deletions momepy/tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

import momepy as mm

# https://github.com/geopandas/geopandas/issues/2282
GPD_REGR = Version("0.10.2") < Version(gpd.__version__) < Version("0.11")
GPD_10 = Version(gpd.__version__) >= Version("0.10")


class TestElements:
def setup_method(self):
Expand Down Expand Up @@ -152,7 +148,6 @@ def test_get_network_id_duplicate(self):
buildings_id = mm.get_network_id(self.df_buildings, self.df_streets, "nID")
assert not buildings_id.isna().any()

@pytest.mark.skipif(GPD_REGR, reason="regression in geopandas")
def test_get_node_id(self):
nx = mm.gdf_to_nx(self.df_streets)
nodes, edges = mm.nx_to_gdf(nx)
Expand All @@ -162,8 +157,6 @@ def test_get_node_id(self):
ids = mm.get_node_id(self.df_buildings, nodes, edges, "nodeID", "nID")
assert not ids.isna().any()

@pytest.mark.skipif(GPD_REGR, reason="regression in geopandas")
@pytest.mark.skipif(not GPD_10, reason="requires sindex.nearest")
def test_get_node_id_ratio(self):
nx = mm.gdf_to_nx(self.df_streets)
nodes, edges = mm.nx_to_gdf(nx)
Expand Down Expand Up @@ -219,7 +212,6 @@ def test_enclosures(self):
encl = mm.enclosures(self.df_streets, limit=gpd.GeoSeries([limit]), clip=True)
assert len(encl) == 18

@pytest.mark.skipif(not GPD_10, reason="requires sindex.nearest")
def test_get_network_ratio(self):
convex_hull = self.df_streets.unary_union.convex_hull
enclosures = mm.enclosures(self.df_streets, limit=gpd.GeoSeries([convex_hull]))
Expand All @@ -246,7 +238,6 @@ def test_get_network_ratio(self):
for i, idx in enumerate(expected_tail):
assert sorted(links2.edgeID_keys.tail(5).iloc[i]) == sorted(idx)

@pytest.mark.skipif(GPD_10, reason="requires sindex.nearest")
def test_get_network_ratio_error(self):
convex_hull = self.df_streets.unary_union.convex_hull
enclosures = mm.enclosures(self.df_streets, limit=gpd.GeoSeries([convex_hull]))
Expand Down
3 changes: 0 additions & 3 deletions momepy/tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import momepy as mm

NX_26 = Version(nx.__version__) < Version("2.6")


class TestGraph:
def setup_method(self):
Expand Down Expand Up @@ -171,7 +169,6 @@ def test_mean_nodes(self):
== edge
)

@pytest.mark.skipif(NX_26, reason="networkx<2.6 has a bug")
def test_clustering(self):
net = mm.clustering(self.network)
check = 0.05555555555555555
Expand Down
Loading

0 comments on commit 0601a86

Please sign in to comment.