From 3c18ef303302e9fa41eb8be2cb789fce5e239737 Mon Sep 17 00:00:00 2001 From: Dobson Date: Thu, 9 May 2024 11:02:45 +0100 Subject: [PATCH 1/6] Update geospatial_utilities.py move subbasin writing to model file --- swmmanywhere/geospatial_utilities.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/swmmanywhere/geospatial_utilities.py b/swmmanywhere/geospatial_utilities.py index ad287277..8962a51c 100644 --- a/swmmanywhere/geospatial_utilities.py +++ b/swmmanywhere/geospatial_utilities.py @@ -676,14 +676,10 @@ def derive_subbasins_streamorder(fid: Path, grid.crs, name="basin") - if ( - (streamorder != streamorder_ - 1) & - (os.getenv("SWMMANYWHERE_VERBOSE", "false").lower() == "true") - ): + if streamorder != streamorder_ - 1: logger.warning(f"""Stream order {streamorder_} resulted in no subbasins. Using {streamorder + 1} instead. You can manually inspect these at {fid.parent / 'subbasins.geojson'}.""") - gdf_bas.to_file(fid.parent / 'subbasins.geojson', driver='GeoJSON') return gdf_bas From a4184750b833ce4cff6ec976579acbe7bed5e49a Mon Sep 17 00:00:00 2001 From: Dobson Date: Thu, 9 May 2024 11:03:51 +0100 Subject: [PATCH 2/6] Update geospatial_utilities.py --- swmmanywhere/geospatial_utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swmmanywhere/geospatial_utilities.py b/swmmanywhere/geospatial_utilities.py index 8962a51c..81c1421f 100644 --- a/swmmanywhere/geospatial_utilities.py +++ b/swmmanywhere/geospatial_utilities.py @@ -679,7 +679,7 @@ def derive_subbasins_streamorder(fid: Path, if streamorder != streamorder_ - 1: logger.warning(f"""Stream order {streamorder_} resulted in no subbasins. Using {streamorder + 1} instead. You can manually inspect - these at {fid.parent / 'subbasins.geojson'}.""") + these in the model folder.""") return gdf_bas From 1673509f89b97a5a61a1546d3217ab4088f61722 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 9 May 2024 11:14:59 +0000 Subject: [PATCH 3/6] Bump typing-extensions from 4.9.0 to 4.11.0 Bumps [typing-extensions](https://github.com/python/typing_extensions) from 4.9.0 to 4.11.0. - [Release notes](https://github.com/python/typing_extensions/releases) - [Changelog](https://github.com/python/typing_extensions/blob/main/CHANGELOG.md) - [Commits](https://github.com/python/typing_extensions/compare/4.9.0...4.11.0) --- updated-dependencies: - dependency-name: typing-extensions dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- dev-requirements.txt | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 516f4344..8d4aad8e 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -318,7 +318,7 @@ tqdm==4.66.2 # via # cdsapi # swmmanywhere (pyproject.toml) -typing-extensions==4.9.0 +typing-extensions==4.11.0 # via # mypy # pydantic diff --git a/requirements.txt b/requirements.txt index b46234e2..a69d2809 100644 --- a/requirements.txt +++ b/requirements.txt @@ -260,7 +260,7 @@ tqdm==4.66.2 # via # cdsapi # swmmanywhere (pyproject.toml) -typing-extensions==4.9.0 +typing-extensions==4.11.0 # via # pydantic # pydantic-core From 24e797b8cf7bcc8317c99ec2f8b99503fa0c1a36 Mon Sep 17 00:00:00 2001 From: Dobson Date: Thu, 9 May 2024 14:11:58 +0100 Subject: [PATCH 4/6] remove automatic streamorder lowering --- swmmanywhere/geospatial_utilities.py | 24 ++++++------------------ tests/test_graph_utilities.py | 25 +++++++++---------------- 2 files changed, 15 insertions(+), 34 deletions(-) diff --git a/swmmanywhere/geospatial_utilities.py b/swmmanywhere/geospatial_utilities.py index 81c1421f..c1cbd242 100644 --- a/swmmanywhere/geospatial_utilities.py +++ b/swmmanywhere/geospatial_utilities.py @@ -31,8 +31,6 @@ from shapely.strtree import STRtree from tqdm import tqdm -from swmmanywhere.logging import logger - os.environ['NUMBA_NUM_THREADS'] = '1' import pyflwdir # noqa: E402 import pysheds # noqa: E402 @@ -658,17 +656,12 @@ def derive_subbasins_streamorder(fid: Path, transform = grid.affine, ) - # Iterate over stream orders to find the first one with subbasins - streamorder_ = streamorder - subbasins = np.zeros_like(flow_dir) - while np.unique(subbasins.reshape(-1)).shape[0] == 1: - if streamorder == 0: - raise ValueError("""No subbasins found in derive_subbasins_streamorder. - Fix your DEM""") - - - subbasins, _ = flw.subbasins_streamorder(min_sto=streamorder) - streamorder -= 1 + # Identify stream order + subbasins, _ = flw.subbasins_streamorder(min_sto=streamorder) + if np.unique(subbasins.reshape(-1)).shape[0] == 1: + raise ValueError("""No subbasins found in derive_subbasins_streamorder. + Use a lower `subcatchment_derivation.subbasin_streamorder and + probably check your DEM.""") gdf_bas = vectorize(subbasins.astype(np.int32), 0, @@ -676,11 +669,6 @@ def derive_subbasins_streamorder(fid: Path, grid.crs, name="basin") - if streamorder != streamorder_ - 1: - logger.warning(f"""Stream order {streamorder_} resulted in no subbasins. - Using {streamorder + 1} instead. You can manually inspect - these in the model folder.""") - return gdf_bas diff --git a/tests/test_graph_utilities.py b/tests/test_graph_utilities.py index 6369dd17..63b5b888 100644 --- a/tests/test_graph_utilities.py +++ b/tests/test_graph_utilities.py @@ -12,12 +12,12 @@ import geopandas as gpd import networkx as nx +import pytest from shapely import geometry as sgeom from swmmanywhere import parameters from swmmanywhere.graph_utilities import graphfcns as gu from swmmanywhere.graph_utilities import iterate_graphfcns, load_graph, save_graph -from swmmanywhere.logging import logger def load_street_network(): @@ -412,18 +412,11 @@ def test_clip_to_catchments(): assert len(G_.edges) == 28 # Check streamorder adjustment - os.environ['SWMMANYWHERE_VERBOSE'] = 'true' - logger.add(temp_path / 'test.log') - subcatchment_derivation = parameters.SubcatchmentDerivation( - subbasin_streamorder = 5, - subbasin_membership = 0.9 - ) - G_ = gu.clip_to_catchments(G, - addresses=addresses, - subcatchment_derivation=subcatchment_derivation) - with open(temp_path / 'test.log', 'r') as f: - log = f.read() - assert 'Stream order 5 resulted in no subbasins' in log - assert 'Using 4 instead' in log - logger.remove() - \ No newline at end of file + with pytest.raises(ValueError): + subcatchment_derivation = parameters.SubcatchmentDerivation( + subbasin_streamorder = 5, + subbasin_membership = 0.9 + ) + G_ = gu.clip_to_catchments(G, + addresses=addresses, + subcatchment_derivation=subcatchment_derivation) \ No newline at end of file From 1eb6947f2c4e3e54ee9f941b4b3d8218bf822d79 Mon Sep 17 00:00:00 2001 From: Dobson Date: Thu, 9 May 2024 14:12:10 +0100 Subject: [PATCH 5/6] Update geospatial_utilities.py --- swmmanywhere/geospatial_utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swmmanywhere/geospatial_utilities.py b/swmmanywhere/geospatial_utilities.py index c1cbd242..4c5a31ad 100644 --- a/swmmanywhere/geospatial_utilities.py +++ b/swmmanywhere/geospatial_utilities.py @@ -660,7 +660,7 @@ def derive_subbasins_streamorder(fid: Path, subbasins, _ = flw.subbasins_streamorder(min_sto=streamorder) if np.unique(subbasins.reshape(-1)).shape[0] == 1: raise ValueError("""No subbasins found in derive_subbasins_streamorder. - Use a lower `subcatchment_derivation.subbasin_streamorder and + Use a lower `subcatchment_derivation.subbasin_streamorder` and probably check your DEM.""") gdf_bas = vectorize(subbasins.astype(np.int32), From fb16392102490ab69dae901b35f0895d6f3e66a7 Mon Sep 17 00:00:00 2001 From: Dobson Date: Thu, 9 May 2024 14:23:12 +0100 Subject: [PATCH 6/6] Update test_swmmanywhere.py --- tests/test_swmmanywhere.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_swmmanywhere.py b/tests/test_swmmanywhere.py index 7dedd78c..8e686a05 100644 --- a/tests/test_swmmanywhere.py +++ b/tests/test_swmmanywhere.py @@ -59,7 +59,8 @@ def test_swmmanywhere(): 'building': str(test_data_dir / 'building.geoparquet'), 'precipitation': str(defs_dir / 'storm.dat') } - + config['parameter_overrides'] = {'subcatchment_derivation' : + {'subbasin_streamorder' : 5}} config['run_settings']['duration'] = 1000 api_keys = {'nasadem_key' : 'b206e65629ac0e53d599e43438560d28'} with open(base_dir / 'api_keys.yml', 'w') as f: