generated from aicoe-aiops/project-template
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrate hazard set selection; logging pytest. (#333)
* Demonstrate hazard set selection; logging pytest. Signed-off-by: Joe Moorhouse <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Joe Moorhouse <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1dae57f
commit 14d1a7c
Showing
3 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from typing import Optional | ||
import numpy as np | ||
|
||
from physrisk.data.hazard_data_provider import HazardDataHint | ||
from physrisk.data.inventory import EmbeddedInventory, Inventory | ||
from physrisk.data.pregenerated_hazard_model import ZarrHazardModel | ||
from physrisk.hazard_models.core_hazards import ( | ||
CoreFloodModels, | ||
CoreInventorySourcePaths, | ||
ResourceSubset, | ||
) | ||
from physrisk.kernel.hazard_model import HazardDataRequest, HazardEventDataResponse | ||
from physrisk.kernel.hazards import RiverineInundation | ||
|
||
from tests.data.hazard_model_store_test import ( | ||
TestData, | ||
ZarrStoreMocker, | ||
inundation_return_periods, | ||
) | ||
|
||
|
||
def test_tudelft_selection(): | ||
inventory: Inventory = EmbeddedInventory() | ||
source_paths = CoreInventorySourcePaths( | ||
inventory, flood_model=CoreFloodModels.TUDelft | ||
).source_paths() | ||
assert ( | ||
source_paths[RiverineInundation]( | ||
indicator_id="flood_depth", scenario="rcp8p5", year=2050 | ||
) | ||
== "inundation/river_tudelft/v2/flood_depth_unprot_rcp8p5_2050" | ||
) | ||
assert ( | ||
source_paths[RiverineInundation]( | ||
indicator_id="flood_depth", scenario="historical", year=-1 | ||
) | ||
== "inundation/river_tudelft/v2/flood_depth_unprot_historical_1985" | ||
) | ||
|
||
|
||
def test_customize_hazard_selection(): | ||
inventory: Inventory = EmbeddedInventory() | ||
|
||
source_path_selectors = CoreInventorySourcePaths(inventory) | ||
|
||
def select_riverine_inundation_tudelft( | ||
candidates: ResourceSubset, | ||
scenario: str, | ||
year: int, | ||
hint: Optional[HazardDataHint] = None, | ||
): | ||
return candidates.with_model_id("tudelft").first() | ||
|
||
# we can add selectors programmatically | ||
# test_tudelft_selection shows an example of using the options in CoreInventorySourcePaths | ||
source_path_selectors.add_selector( | ||
RiverineInundation, "flood_depth", select_riverine_inundation_tudelft | ||
) | ||
|
||
custom_source_paths = source_path_selectors.source_paths() | ||
|
||
def sp_riverine(scenario, year): | ||
return custom_source_paths[RiverineInundation]( | ||
indicator_id="flood_depth", scenario=scenario, year=year | ||
) | ||
|
||
mocker = ZarrStoreMocker() | ||
return_periods = inundation_return_periods() | ||
flood_histo_curve = np.array( | ||
[0.0596, 0.333, 0.505, 0.715, 0.864, 1.003, 1.149, 1.163, 1.163] | ||
) | ||
|
||
for path in [sp_riverine("historical", 1980)]: | ||
mocker.add_curves_global( | ||
path, | ||
TestData.longitudes, | ||
TestData.latitudes, | ||
return_periods, | ||
flood_histo_curve, | ||
) | ||
|
||
hazard_model = ZarrHazardModel(source_paths=custom_source_paths, store=mocker.store) | ||
|
||
req = HazardDataRequest( | ||
RiverineInundation, | ||
TestData.longitudes[0], | ||
TestData.latitudes[0], | ||
indicator_id="flood_depth", | ||
scenario="historical", | ||
year=-1, | ||
) | ||
responses = hazard_model.get_hazard_events([req]) | ||
resp = responses[req] | ||
assert isinstance(resp, HazardEventDataResponse) | ||
np.testing.assert_almost_equal(resp.intensities, flood_histo_curve) |