Skip to content

Commit

Permalink
improve warning messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gboeing committed Jan 17, 2024
1 parent 3e8df7a commit 845d32b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
12 changes: 6 additions & 6 deletions osmnx/_osm_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ def _opener(filepath: Path, encoding: str) -> TextIO:
with _opener(Path(filepath), encoding) as f:
root_attrs = ET.parse(f).getroot().attrib
if "generator" in root_attrs and "OSMnx" in root_attrs["generator"]:
warn(
msg = (

Check warning on line 99 in osmnx/_osm_xml.py

View check run for this annotation

Codecov / codecov/patch

osmnx/_osm_xml.py#L99

Added line #L99 was not covered by tests
"The XML file you are loading appears to have been generated "
"by OSMnx: this use case is not supported and may not behave "
"as expected. To save/load graphs to/from disk for later use "
"in OSMnx, use the `io.save_graphml` and `io.load_graphml` "
"functions instead. Refer to the documentation for details.",
stacklevel=2,
"functions instead. Refer to the documentation for details."
)
warn(msg, stacklevel=2)

Check warning on line 106 in osmnx/_osm_xml.py

View check run for this annotation

Codecov / codecov/patch

osmnx/_osm_xml.py#L106

Added line #L106 was not covered by tests

# parse the XML to Overpass-like JSON
with _opener(Path(filepath), encoding) as f:
Expand Down Expand Up @@ -175,11 +175,11 @@ def _save_graph_xml(
filepath.parent.mkdir(parents=True, exist_ok=True)

if not settings.all_oneway: # pragma: no cover
warn(
msg = (
"For the `save_graph_xml` function to behave properly, the graph "
"must have been created with `ox.settings.all_oneway=True`.",
stacklevel=2,
"must have been created with `ox.settings.all_oneway=True`."
)
warn(msg, stacklevel=2)

if isinstance(data, nx.MultiDiGraph):
gdf_nodes, gdf_edges = utils_graph.graph_to_gdfs(
Expand Down
12 changes: 6 additions & 6 deletions osmnx/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,25 +256,25 @@ def _verify_edge_attribute(G: nx.MultiDiGraph, attr: str) -> None:
"""
Verify attribute values are numeric and non-null across graph edges.
Raises a `ValueError` if attribute contains non-numeric values and raises
a warning if attribute is missing or null on any edges.
Raises a ValueError if this attribute contains non-numeric values, and
issues a UserWarning if this attribute is missing or null on any edges.
Parameters
----------
G : networkx.MultiDiGraph
input graph
attr : string
edge attribute to verify
name of the edge attribute to verify
Returns
-------
None
"""
try:
values = np.array(tuple(G.edges(data=attr)))[:, 2]
values_float = values.astype(float)
values_float = (np.array(tuple(G.edges(data=attr)))[:, 2]).astype(float)
if np.isnan(values_float).any():
warn(f"The attribute {attr!r} is missing or null on some edges.", stacklevel=2)
msg = f"The attribute {attr!r} is missing or null on some edges."
warn(msg, stacklevel=2)
except ValueError as e:
msg = f"The edge attribute {attr!r} contains non-numeric values."
raise ValueError(msg) from e
3 changes: 2 additions & 1 deletion osmnx/utils_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def sample_points(G: nx.MultiGraph, n: int) -> gpd.GeoSeries:
which each point was drawn
"""
if nx.is_directed(G): # pragma: no cover
warn("graph should be undirected to avoid oversampling bidirectional edges", stacklevel=2)
msg = "`G` should be undirected to avoid oversampling bidirectional edges."
warn(msg, stacklevel=2)
gdf_edges = utils_graph.graph_to_gdfs(G, nodes=False)[["geometry", "length"]]
weights = gdf_edges["length"] / gdf_edges["length"].sum()
idx = np.random.default_rng().choice(gdf_edges.index, size=n, p=weights)
Expand Down
8 changes: 4 additions & 4 deletions osmnx/utils_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ def graph_from_gdfs(
except (AssertionError, ValueError): # pragma: no cover
# AssertionError if x/y coords don't match geometry column
# ValueError if geometry column contains non-point geometry types
warn(
"discarding the gdf_nodes geometry column, though its "
"values differ from the coordinates in the x and y columns",
stacklevel=2,
msg = (
"Discarding the `gdf_nodes` geometry column, though its values "
"differ from the coordinates in the x and y columns."
)
warn(msg, stacklevel=2)
df_nodes = gdf_nodes.drop(columns=gdf_nodes.geometry.name)

# create graph and add graph-level attribute dict
Expand Down

0 comments on commit 845d32b

Please sign in to comment.