Skip to content

Commit

Permalink
add numpy linting and update deprecated numpy random number generation
Browse files Browse the repository at this point in the history
  • Loading branch information
gboeing committed Jan 11, 2024
1 parent bd35c3c commit 0c67da9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions osmnx/utils_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tests/test_osmnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)}
Expand Down

0 comments on commit 0c67da9

Please sign in to comment.