Skip to content

Commit

Permalink
Merge pull request #1092 from gboeing/sim
Browse files Browse the repository at this point in the history
linting and fixes
  • Loading branch information
gboeing authored Dec 7, 2023
2 parents 94b4bef + a5c6ad4 commit 15ba6fb
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:
types_or: [markdown, yaml]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.6"
rev: "v0.1.7"
hooks:
- id: ruff
args: [--fix]
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- under-the-hood code clean-up (#1092)

## 1.8.0 (2023-11-30)

- formally support Python 3.12 (#1082)
Expand Down
5 changes: 3 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import sys
from pathlib import Path

# go up two levels from /docs/source to the package root
sys.path.insert(0, str(Path().resolve().parent.parent))
# go up two levels from current working dir (/docs/source) to package root
pkg_root_path = str(Path.cwd().parent.parent)
sys.path.insert(0, pkg_root_path)

author = "Geoff Boeing"
copyright = "2016-2023, Geoff Boeing" # noqa: A001
Expand Down
10 changes: 2 additions & 8 deletions osmnx/_overpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,7 @@ def _make_overpass_settings():
-------
string
"""
if settings.memory is None:
maxsize = ""
else:
maxsize = f"[maxsize:{settings.memory}]"
maxsize = "" if settings.memory is None else f"[maxsize:{settings.memory}]"
return settings.overpass_settings.format(timeout=settings.timeout, maxsize=maxsize)


Expand Down Expand Up @@ -301,10 +298,7 @@ def _download_overpass_network(polygon, network_type, custom_filter):
"""
# create a filter to exclude certain kinds of ways based on the requested
# network_type, if provided, otherwise use custom_filter
if custom_filter is not None:
osm_filter = custom_filter
else:
osm_filter = _get_osm_filter(network_type)
osm_filter = custom_filter if custom_filter is not None else _get_osm_filter(network_type)

# create overpass settings string
overpass_settings = _make_overpass_settings()
Expand Down
7 changes: 2 additions & 5 deletions osmnx/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ def add_edge_lengths(G, precision=None, edges=None):
stacklevel=2,
)

if edges is None:
uvk = tuple(G.edges)
else:
uvk = edges
uvk = tuple(G.edges) if edges is None else edges

# extract edge IDs and corresponding coordinates from their nodes
x = G.nodes(data="x")
Expand Down Expand Up @@ -381,7 +378,7 @@ def nearest_edges(G, X, Y, interpolate=None, return_dist=False):

# interpolate points along edges to index with k-d tree or ball tree
uvk_xy = []
for uvk, geom in zip(geoms.index, geoms.values):
for uvk, geom in zip(geoms.index, geoms.to_numpy()):
uvk_xy.extend((uvk, xy) for xy in utils_geo.interpolate_points(geom, interpolate))
labels, xy = zip(*uvk_xy)
vertices = pd.DataFrame(xy, index=labels, columns=["x", "y"])
Expand Down
11 changes: 6 additions & 5 deletions osmnx/elevation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Add node elevations from raster files or web APIs, and calculate edge grades."""

import multiprocessing as mp
import time
from hashlib import sha1
Expand Down Expand Up @@ -94,7 +95,7 @@ def _query_raster(nodes, filepath, band):
"""
# must open raster file here: cannot pickle it to pass in multiprocessing
with rasterio.open(filepath) as raster:
values = np.array(tuple(raster.sample(nodes.values, band)), dtype=float).squeeze()
values = np.array(tuple(raster.sample(nodes.to_numpy(), band)), dtype=float).squeeze()
values[values == raster.nodata] = np.nan
return zip(nodes.index, values)

Expand Down Expand Up @@ -264,10 +265,10 @@ def add_node_elevations_google(
raise InsufficientResponseError(err_msg)

# add elevation as an attribute to the nodes
df = pd.DataFrame(node_points, columns=["node_points"])
df["elevation"] = [result["elevation"] for result in results]
df["elevation"] = df["elevation"].round(precision)
nx.set_node_attributes(G, name="elevation", values=df["elevation"].to_dict())
df_elev = pd.DataFrame(node_points, columns=["node_points"])
df_elev["elevation"] = [result["elevation"] for result in results]
df_elev["elevation"] = df_elev["elevation"].round(precision)
nx.set_node_attributes(G, name="elevation", values=df_elev["elevation"].to_dict())
utils.log(f"Added elevation data from {domain!r} to all nodes.")

return G
Expand Down
2 changes: 1 addition & 1 deletion osmnx/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ def _filter_gdf_by_polygon_and_tags(gdf, polygon, tags):
gdf = gdf[polygon_filter & combined_tag_filter].copy()

# remove columns of all nulls (created by discarded component features)
gdf.dropna(axis="columns", how="all", inplace=True)
gdf = gdf.dropna(axis="columns", how="all")

# multi-index gdf by element_type and osmid then return
idx_cols = ["element_type", "osmid"]
Expand Down
14 changes: 4 additions & 10 deletions osmnx/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,10 @@ def _plot_folium(gdf, m, popup_attribute, tiles, zoom, fit_bounds, **kwargs):
m = folium.Map(location=centroid, zoom_start=zoom, tiles=tiles)

# identify the geometry and popup columns
if popup_attribute is None:
attrs = ["geometry"]
else:
attrs = ["geometry", popup_attribute]
attrs = ["geometry"] if popup_attribute is None else ["geometry", popup_attribute]

# add each edge to the map
for vals in gdf[attrs].values:
for vals in gdf[attrs].to_numpy():
params = dict(zip(["geom", "popup_val"], vals))
pl = _make_folium_polyline(**params, **kwargs)
pl.add_to(m)
Expand Down Expand Up @@ -207,11 +204,8 @@ def _make_folium_polyline(geom, popup_val=None, **kwargs):
locations = [(lat, lon) for lon, lat in geom.coords]

# create popup if popup_val is not None
if popup_val is None:
popup = None
else:
# folium doesn't interpret html, so can't do newlines without iframe
popup = folium.Popup(html=json.dumps(popup_val))
# folium doesn't interpret html, so can't do newlines without iframe
popup = None if popup_val is None else folium.Popup(html=json.dumps(popup_val))

# create a folium polyline with attributes
return folium.PolyLine(locations=locations, popup=popup, **kwargs)
5 changes: 1 addition & 4 deletions osmnx/geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ def _geocode_query_to_gdf(query, which_result, by_osmid):
gdf : geopandas.GeoDataFrame
a GeoDataFrame with one row containing the result of geocoding
"""
if which_result is None:
limit = 50
else:
limit = which_result
limit = 50 if which_result is None else which_result

results = _nominatim._download_nominatim_element(query, by_osmid=by_osmid, limit=limit)

Expand Down
17 changes: 5 additions & 12 deletions osmnx/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ def save_graph_geopackage(G, filepath=None, encoding="utf-8", directed=False):
None
"""
# default filepath if none was provided
if filepath is None:
filepath = Path(settings.data_folder) / "graph.gpkg"
else:
filepath = Path(filepath)
filepath = Path(settings.data_folder) / "graph.gpkg" if filepath is None else Path(filepath)

# if save folder does not already exist, create it
filepath.parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -94,10 +91,9 @@ def save_graph_shapefile(G, filepath=None, encoding="utf-8", directed=False):
)

# default filepath if none was provided
if filepath is None:
filepath = Path(settings.data_folder) / "graph_shapefile"
else:
filepath = Path(filepath)
filepath = (
Path(settings.data_folder) / "graph_shapefile" if filepath is None else Path(filepath)
)

# if save folder does not already exist, create it (shapefiles
# get saved as set of files)
Expand Down Expand Up @@ -141,10 +137,7 @@ def save_graphml(G, filepath=None, gephi=False, encoding="utf-8"):
None
"""
# default filepath if none was provided
if filepath is None:
filepath = Path(settings.data_folder) / "graph.graphml"
else:
filepath = Path(filepath)
filepath = Path(settings.data_folder) / "graph.graphml" if filepath is None else Path(filepath)

# if save folder does not already exist, create it
filepath.parent.mkdir(parents=True, exist_ok=True)
Expand Down
18 changes: 8 additions & 10 deletions osmnx/osm_xml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Read/write .osm formatted XML files."""

import bz2
import xml.sax
from pathlib import Path
Expand Down Expand Up @@ -232,10 +233,7 @@ def _save_graph_xml(
None
"""
# default filepath if none was provided
if filepath is None:
filepath = Path(settings.data_folder) / "graph.osm"
else:
filepath = Path(filepath)
filepath = Path(settings.data_folder) / "graph.osm" if filepath is None else Path(filepath)

# if save folder does not already exist, create it
filepath.parent.mkdir(parents=True, exist_ok=True)
Expand All @@ -255,7 +253,7 @@ def _save_graph_xml(
)

# rename columns per osm specification
gdf_nodes.rename(columns={"x": "lon", "y": "lat"}, inplace=True)
gdf_nodes = gdf_nodes.rename(columns={"x": "lon", "y": "lat"})
gdf_nodes["lon"] = gdf_nodes["lon"].round(precision)
gdf_nodes["lat"] = gdf_nodes["lat"].round(precision)
gdf_nodes = gdf_nodes.reset_index().rename(columns={"osmid": "id"})
Expand All @@ -277,7 +275,7 @@ def _save_graph_xml(
# misc. string replacements to meet OSM XML spec
if "oneway" in gdf_edges.columns:
# fill blank oneway tags with default (False)
gdf_edges.loc[pd.isnull(gdf_edges["oneway"]), "oneway"] = oneway
gdf_edges.loc[pd.isna(gdf_edges["oneway"]), "oneway"] = oneway
gdf_edges.loc[:, "oneway"] = gdf_edges["oneway"].astype(str)
gdf_edges.loc[:, "oneway"] = (
gdf_edges["oneway"].str.replace("False", "no").replace("True", "yes")
Expand Down Expand Up @@ -466,7 +464,7 @@ def _append_edges_xml_tree(root, gdf_edges, edge_attrs, edge_tags, edge_tag_aggs
root : ElementTree.Element
XML tree with edges appended
"""
gdf_edges.reset_index(inplace=True)
gdf_edges = gdf_edges.reset_index()
if merge_edges:
for _, all_way_edges in gdf_edges.groupby("id"):
first = all_way_edges.iloc[0].dropna().astype(str)
Expand Down Expand Up @@ -518,11 +516,11 @@ def _get_unique_nodes_ordered_from_way(df_way_edges):
design schema.
"""
G = nx.MultiDiGraph()
df_way_edges.reset_index(inplace=True)
all_nodes = list(df_way_edges["u"].values) + list(df_way_edges["v"].values)
df_way_edges.reset_index(inplace=True) # noqa: PD002
all_nodes = list(df_way_edges["u"].to_numpy()) + list(df_way_edges["v"].to_numpy())

G.add_nodes_from(all_nodes)
G.add_edges_from(df_way_edges[["u", "v"]].values)
G.add_edges_from(df_way_edges[["u", "v"]].to_numpy())

# copy nodes into new graph
H = utils_graph.get_largest_component(G, strongly=False)
Expand Down
23 changes: 7 additions & 16 deletions osmnx/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,7 @@ def plot_figure_ground(
et_flat.append(et)

# lookup corresponding width for each edge type in flat list
edge_widths = [
street_widths[et] if et in street_widths else default_width for et in et_flat
]
edge_widths = [street_widths.get(et, default_width) for et in et_flat]

# node diameter should equal largest edge width to make joints
# perfectly smooth. alternatively use min(?) to prevent
Expand Down Expand Up @@ -748,14 +746,10 @@ def plot_orientation(
# width: make bars fill the circumference without gaps or overlaps
width = 2 * np.pi / num_bins

# radius: how long to make each bar
# radius: how long to make each bar. set bar length so either the bar area
# (ie, via sqrt) or the bar height is proportional to the bin's frequency
bin_frequency = bin_counts / bin_counts.sum()
if area:
# set bar length so area is proportional to frequency
radius = np.sqrt(bin_frequency)
else:
# set bar length so height is proportional to frequency
radius = bin_frequency
radius = np.sqrt(bin_frequency) if area else bin_frequency

# create ax (if necessary) then set N at top and go clockwise
if ax is None:
Expand Down Expand Up @@ -840,14 +834,14 @@ def _get_colors_by_value(vals, num_bins, cmap, start, stop, na_color, equal_size
normalizer = colors.Normalize(full_min, full_max)
scalar_mapper = cm.ScalarMappable(normalizer, colormaps[cmap])
color_series = vals.map(scalar_mapper.to_rgba)
color_series.loc[pd.isnull(vals)] = na_color
color_series.loc[pd.isna(vals)] = na_color

else:
# otherwise, bin values then assign colors to bins
cut_func = pd.qcut if equal_size else pd.cut
bins = cut_func(vals, num_bins, labels=range(num_bins))
bin_colors = get_colors(num_bins, cmap, start, stop)
color_list = [bin_colors[b] if pd.notnull(b) else na_color for b in bins]
color_list = [bin_colors[b] if pd.notna(b) else na_color for b in bins]
color_series = pd.Series(color_list, index=bins.index)

return color_series
Expand Down Expand Up @@ -885,10 +879,7 @@ def _save_and_show(fig, ax, save=False, show=True, close=True, filepath=None, dp

if save:
# default filepath, if none provided
if filepath is None:
filepath = Path(settings.imgs_folder) / "image.png"
else:
filepath = Path(filepath)
filepath = Path(settings.imgs_folder) / "image.png" if filepath is None else Path(filepath)

# if save folder does not already exist, create it
filepath.parent.mkdir(parents=True, exist_ok=True)
Expand Down
2 changes: 1 addition & 1 deletion osmnx/simplification.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _build_path(G, endpoint, endpoint_successor, endpoints):
# where a one-way street turns into a two-way here, but
# duplicate incoming one-way edges are present
msg = f"Unexpected simplify pattern handled near {successor}"
utils.log(msg, level=lg.WARN)
utils.log(msg, level=lg.WARNING)
return path
else: # pragma: no cover
# if successor has >1 successors, then successor must have
Expand Down
13 changes: 5 additions & 8 deletions osmnx/speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ def add_edge_speeds(G, hwy_speeds=None, fallback=None, precision=None, agg=np.me

# if user provided hwy_speeds, use them as default values, otherwise
# initialize an empty series to populate with values
if hwy_speeds is None:
hwy_speed_avg = pd.Series(dtype=float)
else:
hwy_speed_avg = pd.Series(hwy_speeds).dropna()
hwy_speed_avg = pd.Series(dtype=float) if hwy_speeds is None else pd.Series(hwy_speeds).dropna()

# for each highway type that caller did not provide in hwy_speeds, impute
# speed of type by taking the mean of the preexisting speed values of that
Expand All @@ -114,15 +111,15 @@ def add_edge_speeds(G, hwy_speeds=None, fallback=None, precision=None, agg=np.me

# all speeds will be null if edges had no preexisting maxspeed data and
# caller did not pass in hwy_speeds or fallback arguments
if pd.isnull(speed_kph).all():
if pd.isna(speed_kph).all():
msg = (
"this graph's edges have no preexisting `maxspeed` attribute "
"values so you must pass `hwy_speeds` or `fallback` arguments."
)
raise ValueError(msg)

# add speed kph attribute to graph edges
edges["speed_kph"] = speed_kph.round(precision).values
edges["speed_kph"] = speed_kph.round(precision).to_numpy()
nx.set_edge_attributes(G, values=edges["speed_kph"], name="speed_kph")

return G
Expand Down Expand Up @@ -165,7 +162,7 @@ def add_edge_travel_times(G, precision=None):
raise KeyError(msg)

# verify edge length and speed_kph attributes contain no nulls
if pd.isnull(edges["length"]).any() or pd.isnull(edges["speed_kph"]).any(): # pragma: no cover
if pd.isna(edges["length"]).any() or pd.isna(edges["speed_kph"]).any(): # pragma: no cover
msg = "edge `length` and `speed_kph` values must be non-null."
raise ValueError(msg)

Expand All @@ -177,7 +174,7 @@ def add_edge_travel_times(G, precision=None):
travel_time = distance_km / speed_km_sec

# add travel time attribute to graph edges
edges["travel_time"] = travel_time.round(precision).values
edges["travel_time"] = travel_time.round(precision).to_numpy()
nx.set_edge_attributes(G, values=edges["travel_time"], name="travel_time")

return G
Expand Down
2 changes: 1 addition & 1 deletion osmnx/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def streets_per_node(G):
"""
spn = dict(nx.get_node_attributes(G, "street_count"))
if set(spn) != set(G.nodes):
utils.log("Graph nodes changed since `street_count`s were calculated", level=lg.WARN)
utils.log("Graph nodes changed since `street_count`s were calculated", level=lg.WARNING)
return spn


Expand Down
Loading

0 comments on commit 15ba6fb

Please sign in to comment.