Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cases where a graphfunction has removed all potential pipe carrying edges #220

Merged
merged 6 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion swmmanywhere/graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,13 @@ def iterate_graphfcns(G: nx.Graph,
raise ValueError(f"Graphfcns are not registered:\n{', '.join(not_exists)}")
for function in graphfcn_list:
G = graphfcns[function](G, addresses = addresses, **params)
logger.info(f"graphfcn: {function} completed.")
if len(_filter_streets(G).edges) == 0:
logger.warning(f"""graphfcn: {function} removed all edges,
returning graph.""")
return G
else:
logger.info(f"graphfcn: {function} completed.")

if verbose():
save_graph(G, addresses.model / f"{function}_graph.json")
go.graph_to_geojson(graphfcns.fix_geometries(G),
Expand Down
6 changes: 6 additions & 0 deletions swmmanywhere/swmmanywhere.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ def swmmanywhere(config: dict) -> tuple[Path, dict | None]:
G.graph['crs']
)
save_graph(G, addresses.graph)

# Check any edges
if len(G.edges) == 0:
logger.warning("No edges in graph, returning graph file.")
return addresses.graph, None

# Write to .inp
synthetic_write(addresses)

Expand Down
41 changes: 39 additions & 2 deletions tests/test_graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,7 @@ def test_iterate_graphfcns():
project_name = None,
bbox_number = None,
model_number = None)
# Needed if VERBOSE is on.. maybe I should turn it off at the top of
# each test, not sure

addresses.model = temp_path
G = iterate_graphfcns(G,
['assign_id',
Expand All @@ -493,6 +492,44 @@ def test_iterate_graphfcns():
assert 'primary' not in get_edge_types(G)
assert len(set([d.get('bridge',None) for u,v,d in G.edges(data=True)])) == 1

def _remove_edges(G: nx.Graph,**kw):
"""Remove all edges from the graph.

Args:
G (nx.Graph): The graph to remove edges from.
kw: Additional keyword arguments. (which are ignored)

Returns:
nx.Graph: The graph with no edges.
"""
G.remove_edges_from(list(G.edges))
return G

def test_iterate_graphfcns_noedges():
"""Test the iterate_graphfcns function for a graph with no edges."""
G = load_graph(Path(__file__).parent / 'test_data' / 'graph_topo_derived.json')

with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
addresses = parameters.FilePaths(base_dir = None,
project_name = None,
bbox_number = None,
model_number = None)
os.environ['SWMMANYWHERE_VERBOSE'] = 'true'
addresses.model = temp_path
original_function = gu['remove_non_pipe_allowable_links']
gu['remove_non_pipe_allowable_links'] = _remove_edges
G = iterate_graphfcns(G,
['assign_id',
'remove_non_pipe_allowable_links'],
{},
addresses)
gu['remove_non_pipe_allowable_links'] = original_function
os.environ['SWMMANYWHERE_VERBOSE'] = 'false'
assert (addresses.model / 'assign_id_graph.json').exists()
assert not (addresses.model /\
'remove_non_pipe_allowable_links_graph.json').exists()

def test_fix_geometries():
"""Test the fix_geometries function."""
# Create a graph with edge geometry not matching node coordinates
Expand Down
Loading