From 0c67da97c82fbd11f3d9be6c0e0e74e35c4308f4 Mon Sep 17 00:00:00 2001 From: Geoff Boeing Date: Wed, 10 Jan 2024 21:32:01 -0800 Subject: [PATCH] add numpy linting and update deprecated numpy random number generation --- osmnx/utils_geo.py | 4 ++-- pyproject.toml | 2 ++ tests/test_osmnx.py | 12 ++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/osmnx/utils_geo.py b/osmnx/utils_geo.py index ef3c2d130..8fe59ca40 100644 --- a/osmnx/utils_geo.py +++ b/osmnx/utils_geo.py @@ -46,9 +46,9 @@ def sample_points(G, n): warn("graph should be undirected to avoid oversampling bidirectional edges", 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.choice(gdf_edges.index, size=n, p=weights) + idx = np.random.default_rng().choice(gdf_edges.index, size=n, p=weights) lines = gdf_edges.loc[idx, "geometry"] - return lines.interpolate(np.random.rand(n), normalized=True) + return lines.interpolate(np.random.default_rng().random(n), normalized=True) def interpolate_points(geom, dist): diff --git a/pyproject.toml b/pyproject.toml index aca366715..887d2cb9f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -80,11 +80,13 @@ extend-select = [ "EM", # check raw literals inside exception raising "ERA", # check commented-out code from python files a la eradicate "F", # check python source code for errors a la pyflakes + "FA", # check from __future__ import annotations "FIX", # check temporary developer notes a la flake8-fixme #"FURB", # check code improvements a la refurb (preview) "G", # check logging string formatting a la flake8-logging-format "I", # check isort imports #"LOG", # check logging module usage a la flake8-logging (preview) + "NPY", # check numpy usage "PD", # check pandas linting a la pandas-vet "PERF", # check performance anti-patterns a la perflint "PGH", # check pygrep hooks diff --git a/tests/test_osmnx.py b/tests/test_osmnx.py index cffac3970..dc1243387 100755 --- a/tests/test_osmnx.py +++ b/tests/test_osmnx.py @@ -322,8 +322,8 @@ def test_routing(): # test multiple origins-destinations n = 5 nodes = np.array(G.nodes) - origs = np.random.choice(nodes, size=n, replace=True) - dests = np.random.choice(nodes, size=n, replace=True) + origs = np.random.default_rng().choice(nodes, size=n, replace=True) + dests = np.random.default_rng().choice(nodes, size=n, replace=True) paths1 = ox.shortest_path(G, origs, dests, weight="length", cpus=1) paths2 = ox.shortest_path(G, origs, dests, weight="length", cpus=2) paths3 = ox.shortest_path(G, origs, dests, weight="length", cpus=None) @@ -500,16 +500,16 @@ def test_graph_save_load(): # create random boolean graph/node/edge attributes attr_name = "test_bool" G.graph[attr_name] = False - bools = np.random.randint(0, 2, len(G.nodes)) + bools = np.random.default_rng().integers(low=0, high=2, size=len(G.nodes)) node_attrs = {n: bool(b) for n, b in zip(G.nodes, bools)} nx.set_node_attributes(G, node_attrs, attr_name) - bools = np.random.randint(0, 2, len(G.edges)) + bools = np.random.default_rng().integers(low=0, high=2, size=len(G.edges)) edge_attrs = {n: bool(b) for n, b in zip(G.edges, bools)} nx.set_edge_attributes(G, edge_attrs, attr_name) # create list, set, and dict attributes for nodes and edges - rand_ints_nodes = np.random.randint(0, 10, len(G.nodes)) - rand_ints_edges = np.random.randint(0, 10, len(G.edges)) + rand_ints_nodes = np.random.default_rng().integers(low=0, high=10, size=len(G.nodes)) + rand_ints_edges = np.random.default_rng().integers(low=0, high=10, size=len(G.edges)) list_node_attrs = {n: [n, r] for n, r in zip(G.nodes, rand_ints_nodes)} nx.set_node_attributes(G, list_node_attrs, "test_list") list_edge_attrs = {e: [e, r] for e, r in zip(G.edges, rand_ints_edges)}