Skip to content

Commit

Permalink
Cooling model and chronic hazard enhancements (#163)
Browse files Browse the repository at this point in the history
* Cooling model; change to support multiple parameters for chronic hazards.

Signed-off-by: Joe Moorhouse <joe.moorhouse@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: Joe Moorhouse <joe.moorhouse@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
joemoorhouse and pre-commit-ci[bot] authored Nov 19, 2023
1 parent a860163 commit 51d6dba
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/physrisk/data/hazard_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,5 @@ def get_parameters(
"""

path = self._get_source_path(indicator_id=indicator_id, scenario=scenario, year=year, hint=hint)
parameters, _ = self._reader.get_curves(path, longitudes, latitudes, self._interpolation)
return parameters[:, 0]
parameters, defns = self._reader.get_curves(path, longitudes, latitudes, self._interpolation)
return parameters, defns
55 changes: 33 additions & 22 deletions src/physrisk/data/pregenerated_hazard_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from physrisk.kernel.hazards import Hazard, HazardKind

from ..kernel.hazard_model import (
HazardDataFailedResponse,
HazardDataRequest,
HazardDataResponse,
HazardEventDataResponse,
Expand Down Expand Up @@ -34,38 +35,48 @@ def __init__(
if Hazard.kind(k) == HazardKind.chronic
)

def get_hazard_events(self, requests: List[HazardDataRequest]) -> Mapping[HazardDataRequest, HazardDataResponse]:
def get_hazard_events( # noqa: C901
self, requests: List[HazardDataRequest]
) -> Mapping[HazardDataRequest, HazardDataResponse]:
batches = defaultdict(list)
for request in requests:
batches[request.group_key()].append(request)

responses: MutableMapping[HazardDataRequest, HazardDataResponse] = {}
for key in batches.keys():
batch: List[HazardDataRequest] = batches[key]
hazard_type, indicator_id, scenario, year, hint = (
batch[0].hazard_type,
batch[0].indicator_id,
batch[0].scenario,
batch[0].year,
batch[0].hint,
)
longitudes = [req.longitude for req in batch]
latitudes = [req.latitude for req in batch]
if hazard_type.kind == HazardKind.acute: # type: ignore
intensities, return_periods = self.acute_hazard_data_providers[hazard_type].get_intensity_curves(
longitudes, latitudes, indicator_id=indicator_id, scenario=scenario, year=year, hint=hint
try:
batch: List[HazardDataRequest] = batches[key]
hazard_type, indicator_id, scenario, year, hint = (
batch[0].hazard_type,
batch[0].indicator_id,
batch[0].scenario,
batch[0].year,
batch[0].hint,
)
longitudes = [req.longitude for req in batch]
latitudes = [req.latitude for req in batch]
if hazard_type.kind == HazardKind.acute: # type: ignore
intensities, return_periods = self.acute_hazard_data_providers[hazard_type].get_intensity_curves(
longitudes, latitudes, indicator_id=indicator_id, scenario=scenario, year=year, hint=hint
)

for i, req in enumerate(batch):
valid = ~np.isnan(intensities[i, :])
responses[req] = HazardEventDataResponse(return_periods[valid], intensities[i, :][valid])
elif hazard_type.kind == HazardKind.chronic: # type: ignore
parameters = self.chronic_hazard_data_providers[hazard_type].get_parameters(
longitudes, latitudes, indicator_id=indicator_id, scenario=scenario, year=year, hint=hint
)
for i, req in enumerate(batch):
valid = ~np.isnan(intensities[i, :])
valid_periods, valid_intensities = return_periods[valid], intensities[i, :][valid]
if len(valid_periods) == 0:
valid_periods, valid_intensities = np.array([100]), np.array([0])
responses[req] = HazardEventDataResponse(valid_periods, valid_intensities)
elif hazard_type.kind == HazardKind.chronic: # type: ignore
parameters, defns = self.chronic_hazard_data_providers[hazard_type].get_parameters(
longitudes, latitudes, indicator_id=indicator_id, scenario=scenario, year=year, hint=hint
)

for i, req in enumerate(batch):
responses[req] = HazardParameterDataResponse(parameters[i, :], defns)
except Exception as err:
# e.g. the requested data is unavailable
for i, req in enumerate(batch):
responses[req] = HazardParameterDataResponse(parameters[i])
responses[req] = HazardDataFailedResponse(err)
return responses


Expand Down
Loading

0 comments on commit 51d6dba

Please sign in to comment.