Skip to content

Commit

Permalink
Convert set_slope to comprehension
Browse files Browse the repository at this point in the history
  • Loading branch information
barneydobson committed Feb 6, 2024
1 parent 85d09a7 commit 83ec08c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions swmmanywhere/graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,13 @@ def __call__(self, G: nx.Graph,
G (nx.Graph): A graph
"""
G = G.copy()
for u,v,d in G.edges(data=True):
slope = (G.nodes[u]['elevation'] - G.nodes[v]['elevation']) / d['length']
d['surface_slope'] = slope
# Compute the slope for each edge
slope_dict = {(u, v, k): (G.nodes[u]['elevation'] - G.nodes[v]['elevation'])
/ d['length'] for u, v, k, d in G.edges(data=True,
keys=True)}

# Set the 'surface_slope' attribute for all edges
nx.set_edge_attributes(G, slope_dict, 'surface_slope')
return G

@register_graphfcn
Expand Down

0 comments on commit 83ec08c

Please sign in to comment.