Skip to content

Commit

Permalink
directional bearing analysis: review revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
dhimmel committed Mar 4, 2024
1 parent 4264cf7 commit 32ed03f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
26 changes: 13 additions & 13 deletions osmnx/bearing.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def add_edge_bearings(G: nx.MultiDiGraph) -> nx.MultiDiGraph:


def orientation_entropy(
Gu: nx.MultiGraph | nx.MultiDiGraph,
G: nx.MultiGraph,
*,
num_bins: int = 36,
min_length: float = 0,
Expand All @@ -145,7 +145,7 @@ def orientation_entropy(
Parameters
----------
Gu
G
Unprojected graph with `bearing` attributes on each edge.
num_bins
Number of bins. For example, if `num_bins=36` is provided, then each
Expand All @@ -161,19 +161,19 @@ def orientation_entropy(
Returns
-------
entropy
The orientation entropy of `Gu`.
The orientation entropy of `G`.
"""
# check if we were able to import scipy
if scipy is None: # pragma: no cover
msg = "scipy must be installed as an optional dependency to calculate entropy."
raise ImportError(msg)
bin_counts, _ = _bearings_distribution(Gu, num_bins, min_length, weight)
bin_counts, _ = _bearings_distribution(G, num_bins, min_length, weight)
entropy: float = scipy.stats.entropy(bin_counts)
return entropy


def _extract_edge_bearings(
Gu: nx.MultiGraph | nx.MultiDiGraph,
G: nx.MultiGraph,
min_length: float,
weight: str | None,
) -> npt.NDArray[np.float64]:
Expand All @@ -187,7 +187,7 @@ def _extract_edge_bearings(
Parameters
----------
Gu
G
Unprojected graph with `bearing` attributes on each edge.
min_length
Ignore edges with `length` attributes less than `min_length`. Useful
Expand All @@ -203,11 +203,11 @@ def _extract_edge_bearings(
bearings
The edge bearings of `Gu`.
"""
if nx.is_directed(Gu) or projection.is_projected(Gu.graph["crs"]): # pragma: no cover
if projection.is_projected(G.graph["crs"]): # pragma: no cover
msg = "Graph must be unprojected to analyze edge bearings."
raise ValueError(msg)
bearings = []
for u, v, data in Gu.edges(data=True):
for u, v, data in G.edges(data=True):
# ignore self-loops and any edges below min_length
if u != v and data["length"] >= min_length:
if weight:
Expand All @@ -220,12 +220,12 @@ def _extract_edge_bearings(
# drop any nulls
bearings_array = np.array(bearings)
bearings_array = bearings_array[~np.isnan(bearings_array)]
if nx.is_directed(Gu):
if nx.is_directed(G):
# https://github.com/gboeing/osmnx/issues/1137
msg = (
"Extracting directional bearings (one bearing per edge) due to MultiDiGraph input. "
"To extract bidirectional bearings (two bearings per edge, including the reverse bearing), "
"supply an undirected graph instead via `Gu.to_undirected()`."
"supply an undirected graph instead via `osmnx.get_undirected(G)`."
)
warn(msg, category=UserWarning, stacklevel=2)
return bearings_array
Expand All @@ -235,7 +235,7 @@ def _extract_edge_bearings(


def _bearings_distribution(
Gu: nx.MultiGraph | nx.MultiDiGraph,
G: nx.MultiGraph,
num_bins: int,
min_length: float,
weight: str | None,
Expand All @@ -251,7 +251,7 @@ def _bearings_distribution(
Parameters
----------
Gu
G
Unprojected graph with `bearing` attributes on each edge.
num_bins
Number of bins for the bearing histogram.
Expand All @@ -272,7 +272,7 @@ def _bearings_distribution(
n = num_bins * 2
bins = np.arange(n + 1) * 360 / n

bearings = _extract_edge_bearings(Gu, min_length, weight)
bearings = _extract_edge_bearings(G, min_length, weight)
count, bin_edges = np.histogram(bearings, bins=bins)

# move last bin to front, so eg 0.01 degrees and 359.99 degrees will be
Expand Down
6 changes: 3 additions & 3 deletions osmnx/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ def plot_footprints( # noqa: PLR0913


def plot_orientation( # noqa: PLR0913
Gu: nx.MultiGraph | nx.MultiDiGraph,
G: nx.MultiGraph,
*,
num_bins: int = 36,
min_length: float = 0,
Expand Down Expand Up @@ -696,7 +696,7 @@ def plot_orientation( # noqa: PLR0913
Parameters
----------
Gu
G
Unprojected graph with `bearing` attributes on each edge.
num_bins
Number of bins. For example, if `num_bins=36` is provided, then each
Expand Down Expand Up @@ -750,7 +750,7 @@ def plot_orientation( # noqa: PLR0913

# get the bearings' distribution's bin counts and edges
bin_counts, bin_edges = bearing._bearings_distribution(
Gu,
G,
num_bins,
min_length=min_length,
weight=weight,
Expand Down
14 changes: 5 additions & 9 deletions tests/test_osmnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from typeguard import TypeCheckError

import osmnx as ox
from osmnx.bearing import _extract_edge_bearings

ox.settings.log_console = True
ox.settings.log_file = True
Expand Down Expand Up @@ -145,17 +144,14 @@ def test_stats() -> None:
def test_extract_edge_bearings_directionality() -> None:
"""Test support of edge bearings for directed and undirected graphs."""
G = nx.MultiDiGraph(crs="epsg:4326")
lon_0, lat_0 = 0.0, 0.0
lon_1, lat_1 = 0.0, 1.0
G.add_edge(
(lon_0, lat_0),
(lon_1, lat_1),
)
G.add_node("point_1", x=0.0, y=0.0)
G.add_node("point_2", x=0.0, y=1.0) # latitude increases northward
G.add_edge("point_1", "point_2")
G = ox.add_edge_bearings(G)
with pytest.warns(UserWarning, match="Extracting directional bearings"):
bearings = _extract_edge_bearings(G, min_length=0.0, weight=None)
bearings = ox.bearing._extract_edge_bearings(G, min_length=0.0, weight=None)
assert list(bearings) == [0.0] # north
bearings = _extract_edge_bearings(G.to_undirected(), min_length=0.0, weight=None)
bearings = ox.bearing._extract_edge_bearings(G.to_undirected(), min_length=0.0, weight=None)
assert list(bearings) == [0.0, 180.0] # north and south


Expand Down

0 comments on commit 32ed03f

Please sign in to comment.