Skip to content

Commit

Permalink
Add vulnerability functions.
Browse files Browse the repository at this point in the history
We add here the vulnerability funcions to use the data from the
ECB's stress test article from 2023.

Co-authored-by: Víctor de Luna <[email protected]>

Signed-off-by: Virginia Morales <[email protected]>
  • Loading branch information
VMarfima committed Aug 1, 2024
1 parent 580bf84 commit 08bbc20
Show file tree
Hide file tree
Showing 19 changed files with 1,776 additions and 176 deletions.
10 changes: 6 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ include = ["physrisk*"]

[tool.pdm.dev-dependencies]
test = [
"pdm[pytest]",
"pytest",
"pytest-cov",
"sphinx-pyproject"
"pdm[pytest]",
"pytest",
"pytest-cov",
"sphinx-pyproject",
"pandas>=2.0.3",
"dependency-injector>=4.41.0",
]
lint = [
"isort",
Expand Down
9 changes: 9 additions & 0 deletions src/physrisk/api/v1/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from typing import Dict, List, Optional, Union

import numpy as np
Expand All @@ -23,6 +24,14 @@ class Array(np.ndarray, metaclass=ArrayMeta):
pass


class ModelKind(Enum):
"""Enumeration to select the kind of vulnerability model and risk model for calculations."""

DEFAULT = 0
STRESS_TEST = 1
GENERIC = 2


class Asset(BaseModel, extra="allow"):
"""Defines an asset. An asset is identified first by its asset_class and then by its type within the class.
An asset's value may be impacted through damage or through disruption
Expand Down
9 changes: 6 additions & 3 deletions src/physrisk/api/v1/impact_req_resp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
VulnerabilityDistrib,
)
from physrisk.api.v1.hazard_data import Scenario
from physrisk.api.v1.common import ModelKind


class CalcSettings(BaseModel):
Expand All @@ -36,9 +37,9 @@ class AssetImpactRequest(BaseModel):
include_calc_details: bool = Field(
True, description="If true, include impact calculation details."
)
use_case_id: str = Field(
"",
description="Identifier for 'use case' used in the risk measures calculation.",
model_kind: ModelKind = Field(
ModelKind.DEFAULT,
description="Enumeration to select the type of vulnerability model and risk model for calculations. Options: Default, StressTest, Generic",
)
provider_max_requests: Dict[str, int] = Field(
{},
Expand Down Expand Up @@ -70,6 +71,8 @@ class Category(int, Enum):
HIGH = 3
REDFLAG = 4

NORISK = -1


class RiskMeasureDefinition(BaseModel):
measure_id: str = Field(None, description="Identifier for the risk measure.")
Expand Down
13 changes: 2 additions & 11 deletions src/physrisk/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

from dependency_injector import containers, providers

from physrisk.kernel import calculation as calc
from physrisk.data.hazard_data_provider import SourcePath
from physrisk.data.inventory import EmbeddedInventory
from physrisk.data.inventory_reader import InventoryReader
from physrisk.data.pregenerated_hazard_model import ZarrHazardModel
from physrisk.data.zarr_reader import ZarrReader
from physrisk.kernel import calculation as calc
from physrisk.kernel.hazard_model import HazardModelFactory
from physrisk.kernel.vulnerability_model import (
DictBasedVulnerabilityModels,
VulnerabilityModels,
VulnerabilityModelsFactory,
)
from physrisk.kernel.vulnerability_model import DictBasedVulnerabilityModelsFactory
from physrisk.requests import Requester, _create_inventory, create_source_paths


Expand Down Expand Up @@ -41,11 +37,6 @@ def hazard_model(
)


class DictBasedVulnerabilityModelsFactory(VulnerabilityModelsFactory):
def vulnerability_models(self) -> VulnerabilityModels:
return DictBasedVulnerabilityModels(calc.get_default_vulnerability_models())


class Container(containers.DeclarativeContainer):
config = providers.Configuration(default={"zarr_sources": ["embedded", "hazard"]})

Expand Down
6 changes: 2 additions & 4 deletions src/physrisk/data/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
import hashlib
import importlib.resources
import json
import logging
from collections import defaultdict
from typing import DefaultDict, Dict, Iterable, List, Tuple

from pydantic import TypeAdapter, parse_obj_as
from pydantic import TypeAdapter

import physrisk.data.colormap_provider as colormap_provider
import physrisk.data.static.hazard
from physrisk.api.v1.hazard_data import HazardResource, Period
from physrisk.data.inventory_reader import HazardModels

from ..api.v1.hazard_data import HazardResource, Period

# from physrisk.kernel.hazards import ChronicHeat


Expand Down
Loading

0 comments on commit 08bbc20

Please sign in to comment.