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

Fix format #2

Merged
merged 1 commit into from
Sep 24, 2023
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
4 changes: 3 additions & 1 deletion src/physrisk/kernel/impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def calculate_impacts( # noqa: C901
hazard_data = [responses[req] for req in get_iterable(requests)]
if isinstance(model, VulnerabilityModelAcuteBase):
impact, vul, event = model.get_impact_details(asset, hazard_data)
results[ImpactKey(asset, model.hazard_type)] = AssetImpactResult(impact, vulnerability=vul, event=event, hazard_data=hazard_data)
results[ImpactKey(asset, model.hazard_type)] = AssetImpactResult(
impact, vulnerability=vul, event=event, hazard_data=hazard_data
)
elif isinstance(model, VulnerabilityModelBase):
impact = model.get_impact(asset, hazard_data)
results[ImpactKey(asset, model.hazard_type)] = AssetImpactResult(impact, hazard_data=hazard_data)
Expand Down
62 changes: 46 additions & 16 deletions src/test/models/test_real_estate_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def test_real_estate_model_details(self):
np.testing.assert_almost_equal(hazard_bin_probs[1], 0.1)

# check that intensity bin edges for vulnerability matrix are same as for hazard
vulnerability_intensity_bin_edges = results[ImpactKey(assets[0], RiverineInundation)].vulnerability.intensity_bins
vulnerability_intensity_bin_edges = results[
ImpactKey(assets[0], RiverineInundation)
].vulnerability.intensity_bins
np.testing.assert_almost_equal(vulnerability_intensity_bin_edges, hazard_bin_edges)

# check the impact distribution the matrix is size [len(intensity_bins) - 1, len(impact_bins) - 1]
Expand All @@ -58,7 +60,9 @@ def test_real_estate_model_details(self):
np.testing.assert_allclose([mean, std], [0.09090909, 0.08184968], rtol=1e-6)

# probability that impact occurs between impact bin edge 1 and impact bin edge 2
prob_impact = np.dot(hazard_bin_probs, results[ImpactKey(assets[0], RiverineInundation)].vulnerability.prob_matrix[:, 1])
prob_impact = np.dot(
hazard_bin_probs, results[ImpactKey(assets[0], RiverineInundation)].vulnerability.prob_matrix[:, 1]
)
np.testing.assert_almost_equal(prob_impact, 0.19350789547968042)

# no check with pre-calculated values for others:
Expand Down Expand Up @@ -122,7 +126,9 @@ def test_coastal_real_estate_model(self):
)

def test_commercial_real_estate_model_details(self):
curve = np.array([2.8302893e-06, 0.09990284, 0.21215445, 0.531271, 0.7655724, 0.99438345, 1.2871761, 1.502281, 1.7134278])
curve = np.array(
[2.8302893e-06, 0.09990284, 0.21215445, 0.531271, 0.7655724, 0.99438345, 1.2871761, 1.502281, 1.7134278]
)
store = mock_hazard_model_store_inundation(TestData.longitudes, TestData.latitudes, curve)
hazard_model = ZarrHazardModel(source_paths=get_default_source_paths(), store=store)

Expand All @@ -136,7 +142,27 @@ def test_commercial_real_estate_model_details(self):
year = 2080

# impact bin edges are calibrated so that hazard_bin_probs == impact_bin_probs when the impact standard deviation is negligible:
vulnerability_models = {RealEstateAsset: [RealEstateRiverineInundationModel(impact_bin_edges=np.array([0, 0.030545039098059, 0.125953058445539, 0.322702019487674, 0.566880882840096, 0.731980974578735, 0.823993215529066, 0.884544511664047, 0.922115133960502, 0.969169745946688, 1.0]))]}
vulnerability_models = {
RealEstateAsset: [
RealEstateRiverineInundationModel(
impact_bin_edges=np.array(
[
0,
0.030545039098059,
0.125953058445539,
0.322702019487674,
0.566880882840096,
0.731980974578735,
0.823993215529066,
0.884544511664047,
0.922115133960502,
0.969169745946688,
1.0,
]
)
)
]
}

results = calculate_impacts(assets, hazard_model, vulnerability_models, scenario=scenario, year=year)

Expand All @@ -151,7 +177,9 @@ def test_commercial_real_estate_model_details(self):
np.testing.assert_almost_equal(hazard_bin_probs[2], 0.06)

# check that intensity bin edges for vulnerability matrix are same as for hazard
vulnerability_intensity_bin_edges = results[ImpactKey(assets[0], RiverineInundation)].vulnerability.intensity_bins
vulnerability_intensity_bin_edges = results[
ImpactKey(assets[0], RiverineInundation)
].vulnerability.intensity_bins
np.testing.assert_almost_equal(vulnerability_intensity_bin_edges, hazard_bin_edges)

# check the impact distribution the matrix is size [len(intensity_bins) - 1, len(impact_bins) - 1]
Expand All @@ -162,24 +190,26 @@ def test_commercial_real_estate_model_details(self):
np.testing.assert_allclose([mean, std], [0.1, 0.2884275164878624], rtol=1e-6)

# probability that impact occurs between impact bin edge 2 and impact bin edge 3
prob_impact = np.dot(hazard_bin_probs, results[ImpactKey(assets[0], RiverineInundation)].vulnerability.prob_matrix[:, 2])
prob_impact = np.dot(
hazard_bin_probs, results[ImpactKey(assets[0], RiverineInundation)].vulnerability.prob_matrix[:, 2]
)
np.testing.assert_almost_equal(prob_impact, 0.10040196672295522)

# no check with pre-calculated values for others:
np.testing.assert_allclose(
results[ImpactKey(assets[0], RiverineInundation)].impact.prob,
np.array(
[
2.009085E-07,
3.001528E-01,
1.004020E-01,
5.885136E-02,
1.760415E-02,
1.159864E-02,
6.130639E-03,
2.729225E-03,
1.446537E-03,
8.450993E-05
2.009085e-07,
3.001528e-01,
1.004020e-01,
5.885136e-02,
1.760415e-02,
1.159864e-02,
6.130639e-03,
2.729225e-03,
1.446537e-03,
8.450993e-05,
]
),
rtol=2e-6,
Expand Down